Buffer.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/Math.h>
  10. #include <AK/MemoryStream.h>
  11. #include <AK/String.h>
  12. #include <AK/Types.h>
  13. #include <AK/Vector.h>
  14. #include <LibCore/AnonymousBuffer.h>
  15. #include <string.h>
  16. namespace Audio {
  17. using namespace AK::Exponentials;
  18. // Constants for logarithmic volume. See Frame::operator*
  19. // Corresponds to 60dB
  20. constexpr double DYNAMIC_RANGE = 1000;
  21. constexpr double VOLUME_A = 1 / DYNAMIC_RANGE;
  22. double const VOLUME_B = log(DYNAMIC_RANGE);
  23. // A single sample in an audio buffer.
  24. // Values are floating point, and should range from -1.0 to +1.0
  25. struct Frame {
  26. constexpr Frame()
  27. : left(0)
  28. , right(0)
  29. {
  30. }
  31. // For mono
  32. constexpr Frame(double left)
  33. : left(left)
  34. , right(left)
  35. {
  36. }
  37. // For stereo
  38. constexpr Frame(double left, double right)
  39. : left(left)
  40. , right(right)
  41. {
  42. }
  43. void clip()
  44. {
  45. if (left > 1)
  46. left = 1;
  47. else if (left < -1)
  48. left = -1;
  49. if (right > 1)
  50. right = 1;
  51. else if (right < -1)
  52. right = -1;
  53. }
  54. // Logarithmic scaling, as audio should ALWAYS do.
  55. // Reference: https://www.dr-lex.be/info-stuff/volumecontrols.html
  56. // We use the curve `factor = a * exp(b * change)`,
  57. // where change is the input fraction we want to change by,
  58. // a = 1/1000, b = ln(1000) = 6.908 and factor is the multiplier used.
  59. // The value 1000 represents the dynamic range in sound pressure, which corresponds to 60 dB(A).
  60. // This is a good dynamic range because it can represent all loudness values from
  61. // 30 dB(A) (barely hearable with background noise)
  62. // to 90 dB(A) (almost too loud to hear and about the reasonable limit of actual sound equipment).
  63. ALWAYS_INLINE Frame& log_multiply(double const change)
  64. {
  65. double factor = VOLUME_A * exp(VOLUME_B * change);
  66. left *= factor;
  67. right *= factor;
  68. return *this;
  69. }
  70. ALWAYS_INLINE Frame log_multiplied(double const volume_change) const
  71. {
  72. Frame new_frame { left, right };
  73. new_frame.log_multiply(volume_change);
  74. return new_frame;
  75. }
  76. constexpr Frame& operator*=(double const mult)
  77. {
  78. left *= mult;
  79. right *= mult;
  80. return *this;
  81. }
  82. constexpr Frame operator*(double const mult)
  83. {
  84. return { left * mult, right * mult };
  85. }
  86. constexpr Frame& operator+=(Frame const& other)
  87. {
  88. left += other.left;
  89. right += other.right;
  90. return *this;
  91. }
  92. constexpr Frame operator+(Frame const& other)
  93. {
  94. return { left + other.left, right + other.right };
  95. }
  96. double left;
  97. double right;
  98. };
  99. // Supported PCM sample formats.
  100. enum PcmSampleFormat : u8 {
  101. Uint8,
  102. Int16,
  103. Int24,
  104. Int32,
  105. Float32,
  106. Float64,
  107. };
  108. // Most of the read code only cares about how many bits to read or write
  109. u16 pcm_bits_per_sample(PcmSampleFormat format);
  110. String sample_format_name(PcmSampleFormat format);
  111. // Small helper to resample from one playback rate to another
  112. // This isn't really "smart", in that we just insert (or drop) samples.
  113. // Should do better...
  114. template<typename SampleType>
  115. class ResampleHelper {
  116. public:
  117. ResampleHelper(u32 source, u32 target);
  118. // To be used as follows:
  119. // while the resampler doesn't need a new sample, read_sample(current) and store the resulting samples.
  120. // as long as the resampler needs a new sample, process_sample(current)
  121. // Stores a new sample
  122. void process_sample(SampleType sample_l, SampleType sample_r);
  123. // Assigns the given sample to its correct value and returns false if there is a new sample required
  124. bool read_sample(SampleType& next_l, SampleType& next_r);
  125. Vector<SampleType> resample(Vector<SampleType> to_resample);
  126. void reset();
  127. u32 source() const { return m_source; }
  128. u32 target() const { return m_target; }
  129. private:
  130. const u32 m_source;
  131. const u32 m_target;
  132. u32 m_current_ratio { 0 };
  133. SampleType m_last_sample_l;
  134. SampleType m_last_sample_r;
  135. };
  136. // A buffer of audio samples.
  137. class Buffer : public RefCounted<Buffer> {
  138. public:
  139. static RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
  140. static RefPtr<Buffer> from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples);
  141. static NonnullRefPtr<Buffer> create_with_samples(Vector<Frame>&& samples)
  142. {
  143. return adopt_ref(*new Buffer(move(samples)));
  144. }
  145. static NonnullRefPtr<Buffer> create_with_anonymous_buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
  146. {
  147. return adopt_ref(*new Buffer(move(buffer), buffer_id, sample_count));
  148. }
  149. const Frame* samples() const { return (const Frame*)data(); }
  150. int sample_count() const { return m_sample_count; }
  151. const void* data() const { return m_buffer.data<void>(); }
  152. int size_in_bytes() const { return m_sample_count * (int)sizeof(Frame); }
  153. int id() const { return m_id; }
  154. const Core::AnonymousBuffer& anonymous_buffer() const { return m_buffer; }
  155. private:
  156. explicit Buffer(const Vector<Frame> samples)
  157. : m_buffer(Core::AnonymousBuffer::create_with_size(samples.size() * sizeof(Frame)))
  158. , m_id(allocate_id())
  159. , m_sample_count(samples.size())
  160. {
  161. memcpy(m_buffer.data<void>(), samples.data(), samples.size() * sizeof(Frame));
  162. }
  163. explicit Buffer(Core::AnonymousBuffer buffer, i32 buffer_id, int sample_count)
  164. : m_buffer(move(buffer))
  165. , m_id(buffer_id)
  166. , m_sample_count(sample_count)
  167. {
  168. }
  169. static i32 allocate_id();
  170. Core::AnonymousBuffer m_buffer;
  171. const i32 m_id;
  172. const int m_sample_count;
  173. };
  174. // This only works for double resamplers, and therefore cannot be part of the class
  175. NonnullRefPtr<Buffer> resample_buffer(ResampleHelper<double>& resampler, Buffer const& to_resample);
  176. }