Buffer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include "AK/TypedTransfer.h"
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/Error.h>
  11. #include <AK/FixedArray.h>
  12. #include <AK/MemoryStream.h>
  13. #include <AK/NonnullRefPtr.h>
  14. #include <AK/RefPtr.h>
  15. #include <AK/String.h>
  16. #include <AK/Types.h>
  17. #include <AK/Vector.h>
  18. #include <AK/kmalloc.h>
  19. #include <LibAudio/Resampler.h>
  20. #include <LibAudio/Sample.h>
  21. #include <LibAudio/SampleFormats.h>
  22. #include <LibCore/AnonymousBuffer.h>
  23. #include <LibCore/SharedCircularQueue.h>
  24. #include <string.h>
  25. namespace Audio {
  26. static constexpr size_t AUDIO_BUFFERS_COUNT = 128;
  27. // The audio buffer size is specifically chosen to be about 1/1000th of a second (1ms).
  28. // This has the biggest impact on latency and performance.
  29. // The currently chosen value was not put here with much thought and a better choice is surely possible.
  30. static constexpr size_t AUDIO_BUFFER_SIZE = 50;
  31. using AudioQueue = Core::SharedSingleProducerCircularQueue<Array<Sample, AUDIO_BUFFER_SIZE>, AUDIO_BUFFERS_COUNT>;
  32. using namespace AK::Exponentials;
  33. // A buffer of audio samples.
  34. class LegacyBuffer : public RefCounted<LegacyBuffer> {
  35. public:
  36. static ErrorOr<NonnullRefPtr<LegacyBuffer>> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
  37. static ErrorOr<NonnullRefPtr<LegacyBuffer>> from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples);
  38. template<ArrayLike<Sample> ArrayT>
  39. static ErrorOr<NonnullRefPtr<LegacyBuffer>> create_with_samples(ArrayT&& samples)
  40. {
  41. return adopt_nonnull_ref_or_enomem(new (nothrow) LegacyBuffer(move(samples)));
  42. }
  43. static ErrorOr<NonnullRefPtr<LegacyBuffer>> create_with_anonymous_buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
  44. {
  45. return adopt_nonnull_ref_or_enomem(new (nothrow) LegacyBuffer(move(buffer), buffer_id, sample_count));
  46. }
  47. static NonnullRefPtr<LegacyBuffer> create_empty()
  48. {
  49. // If we can't allocate an empty buffer, things are in a very bad state.
  50. return MUST(adopt_nonnull_ref_or_enomem(new (nothrow) LegacyBuffer));
  51. }
  52. Sample const* samples() const { return (Sample const*)data(); }
  53. ErrorOr<FixedArray<Sample>> to_sample_array() const
  54. {
  55. FixedArray<Sample> samples = TRY(FixedArray<Sample>::try_create(m_sample_count));
  56. AK::TypedTransfer<Sample>::copy(samples.data(), this->samples(), m_sample_count);
  57. return samples;
  58. }
  59. int sample_count() const { return m_sample_count; }
  60. void const* data() const { return m_buffer.data<void>(); }
  61. int size_in_bytes() const { return m_sample_count * (int)sizeof(Sample); }
  62. int id() const { return m_id; }
  63. Core::AnonymousBuffer const& anonymous_buffer() const { return m_buffer; }
  64. private:
  65. template<ArrayLike<Sample> ArrayT>
  66. explicit LegacyBuffer(ArrayT&& samples)
  67. : m_buffer(Core::AnonymousBuffer::create_with_size(samples.size() * sizeof(Sample)).release_value())
  68. , m_id(allocate_id())
  69. , m_sample_count(samples.size())
  70. {
  71. memcpy(m_buffer.data<void>(), samples.data(), samples.size() * sizeof(Sample));
  72. }
  73. explicit LegacyBuffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
  74. : m_buffer(move(buffer))
  75. , m_id(buffer_id)
  76. , m_sample_count(sample_count)
  77. {
  78. }
  79. // Empty Buffer representation, to avoid tiny anonymous buffers in EOF states
  80. LegacyBuffer() = default;
  81. static i32 allocate_id();
  82. Core::AnonymousBuffer m_buffer;
  83. const i32 m_id { -1 };
  84. int const m_sample_count { 0 };
  85. };
  86. // This only works for double resamplers, and therefore cannot be part of the class
  87. ErrorOr<NonnullRefPtr<LegacyBuffer>> resample_buffer(ResampleHelper<double>& resampler, LegacyBuffer const& to_resample);
  88. }