Buffer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/Error.h>
  10. #include <AK/FixedArray.h>
  11. #include <AK/MemoryStream.h>
  12. #include <AK/NonnullRefPtr.h>
  13. #include <AK/RefPtr.h>
  14. #include <AK/String.h>
  15. #include <AK/Types.h>
  16. #include <AK/Vector.h>
  17. #include <AK/kmalloc.h>
  18. #include <LibAudio/Sample.h>
  19. #include <LibCore/AnonymousBuffer.h>
  20. #include <string.h>
  21. namespace Audio {
  22. using namespace AK::Exponentials;
  23. // Supported PCM sample formats.
  24. enum PcmSampleFormat : u8 {
  25. Uint8,
  26. Int16,
  27. Int24,
  28. Int32,
  29. Float32,
  30. Float64,
  31. };
  32. // Most of the read code only cares about how many bits to read or write
  33. u16 pcm_bits_per_sample(PcmSampleFormat format);
  34. String sample_format_name(PcmSampleFormat format);
  35. // Small helper to resample from one playback rate to another
  36. // This isn't really "smart", in that we just insert (or drop) samples.
  37. // Should do better...
  38. template<typename SampleType>
  39. class ResampleHelper {
  40. public:
  41. ResampleHelper(u32 source, u32 target);
  42. // To be used as follows:
  43. // while the resampler doesn't need a new sample, read_sample(current) and store the resulting samples.
  44. // as long as the resampler needs a new sample, process_sample(current)
  45. // Stores a new sample
  46. void process_sample(SampleType sample_l, SampleType sample_r);
  47. // Assigns the given sample to its correct value and returns false if there is a new sample required
  48. bool read_sample(SampleType& next_l, SampleType& next_r);
  49. Vector<SampleType> resample(Vector<SampleType> to_resample);
  50. void reset();
  51. u32 source() const { return m_source; }
  52. u32 target() const { return m_target; }
  53. private:
  54. const u32 m_source;
  55. const u32 m_target;
  56. u32 m_current_ratio { 0 };
  57. SampleType m_last_sample_l;
  58. SampleType m_last_sample_r;
  59. };
  60. // A buffer of audio samples.
  61. class Buffer : public RefCounted<Buffer> {
  62. public:
  63. static ErrorOr<NonnullRefPtr<Buffer>> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
  64. static ErrorOr<NonnullRefPtr<Buffer>> from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples);
  65. static ErrorOr<NonnullRefPtr<Buffer>> create_with_samples(Vector<Sample>&& samples)
  66. {
  67. return adopt_nonnull_ref_or_enomem(new (nothrow) Buffer(move(samples)));
  68. }
  69. static ErrorOr<NonnullRefPtr<Buffer>> create_with_samples(FixedArray<Sample>&& samples)
  70. {
  71. return adopt_nonnull_ref_or_enomem(new (nothrow) Buffer(move(samples)));
  72. }
  73. static ErrorOr<NonnullRefPtr<Buffer>> create_with_anonymous_buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
  74. {
  75. return adopt_nonnull_ref_or_enomem(new (nothrow) Buffer(move(buffer), buffer_id, sample_count));
  76. }
  77. static NonnullRefPtr<Buffer> create_empty()
  78. {
  79. // If we can't allocate an empty buffer, things are in a very bad state.
  80. return MUST(adopt_nonnull_ref_or_enomem(new (nothrow) Buffer(FixedArray<Sample> {})));
  81. }
  82. Sample const* samples() const { return (const Sample*)data(); }
  83. int sample_count() const { return m_sample_count; }
  84. void const* data() const { return m_buffer.data<void>(); }
  85. int size_in_bytes() const { return m_sample_count * (int)sizeof(Sample); }
  86. int id() const { return m_id; }
  87. Core::AnonymousBuffer const& anonymous_buffer() const { return m_buffer; }
  88. private:
  89. explicit Buffer(Vector<Sample>&& samples)
  90. // FIXME: AnonymousBuffers can't be empty, so even for empty buffers we create a buffer of size 1 here,
  91. // although the sample count is set to 0 to mark this.
  92. : m_buffer(Core::AnonymousBuffer::create_with_size(max(samples.size(), 1) * sizeof(Sample)).release_value())
  93. , m_id(allocate_id())
  94. , m_sample_count(samples.size())
  95. {
  96. memcpy(m_buffer.data<void>(), samples.data(), samples.size() * sizeof(Sample));
  97. }
  98. explicit Buffer(FixedArray<Sample>&& samples)
  99. // FIXME: AnonymousBuffers can't be empty, so even for empty buffers we create a buffer of size 1 here,
  100. // although the sample count is set to 0 to mark this.
  101. : m_buffer(Core::AnonymousBuffer::create_with_size(max(samples.size(), 1) * sizeof(Sample)).release_value())
  102. , m_id(allocate_id())
  103. , m_sample_count(samples.size())
  104. {
  105. memcpy(m_buffer.data<void>(), samples.data(), samples.size() * sizeof(Sample));
  106. }
  107. explicit Buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
  108. : m_buffer(move(buffer))
  109. , m_id(buffer_id)
  110. , m_sample_count(sample_count)
  111. {
  112. }
  113. static i32 allocate_id();
  114. Core::AnonymousBuffer m_buffer;
  115. const i32 m_id;
  116. const int m_sample_count;
  117. };
  118. // This only works for double resamplers, and therefore cannot be part of the class
  119. ErrorOr<NonnullRefPtr<Buffer>> resample_buffer(ResampleHelper<double>& resampler, Buffer const& to_resample);
  120. }