Sample.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/Format.h>
  9. #include <AK/Math.h>
  10. namespace Audio {
  11. using AK::Exponentials::exp;
  12. using AK::Exponentials::log;
  13. // Constants for logarithmic volume. See Sample::linear_to_log
  14. // Corresponds to 60dB
  15. constexpr float DYNAMIC_RANGE = 1000;
  16. constexpr float VOLUME_A = 1 / DYNAMIC_RANGE;
  17. float const VOLUME_B = log(DYNAMIC_RANGE);
  18. // A single sample in an audio buffer.
  19. // Values are floating point, and should range from -1.0 to +1.0
  20. struct Sample {
  21. constexpr Sample() = default;
  22. // For mono
  23. constexpr explicit Sample(float left)
  24. : left(left)
  25. , right(left)
  26. {
  27. }
  28. // For stereo
  29. constexpr Sample(float left, float right)
  30. : left(left)
  31. , right(right)
  32. {
  33. }
  34. // Returns the absolute maximum range (separate per channel) of the given sample buffer.
  35. // For example { 0.8, 0 } means that samples on the left channel occupy the range { -0.8, 0.8 },
  36. // while all samples on the right channel are 0.
  37. static Sample max_range(ReadonlySpan<Sample> span)
  38. {
  39. Sample result { NumericLimits<float>::min_normal(), NumericLimits<float>::min_normal() };
  40. for (Sample sample : span) {
  41. result.left = max(result.left, AK::fabs(sample.left));
  42. result.right = max(result.right, AK::fabs(sample.right));
  43. }
  44. return result;
  45. }
  46. void clip()
  47. {
  48. if (left > 1)
  49. left = 1;
  50. else if (left < -1)
  51. left = -1;
  52. if (right > 1)
  53. right = 1;
  54. else if (right < -1)
  55. right = -1;
  56. }
  57. // Logarithmic scaling, as audio should ALWAYS do.
  58. // Reference: https://www.dr-lex.be/info-stuff/volumecontrols.html
  59. // We use the curve `factor = a * exp(b * change)`,
  60. // where change is the input fraction we want to change by,
  61. // a = 1/1000, b = ln(1000) = 6.908 and factor is the multiplier used.
  62. // The value 1000 represents the dynamic range in sound pressure, which corresponds to 60 dB(A).
  63. // This is a good dynamic range because it can represent all loudness values from
  64. // 30 dB(A) (barely hearable with background noise)
  65. // to 90 dB(A) (almost too loud to hear and about the reasonable limit of actual sound equipment).
  66. //
  67. // Format ranges:
  68. // - Linear: 0.0 to 1.0
  69. // - Logarithmic: 0.0 to 1.0
  70. ALWAYS_INLINE float linear_to_log(float const change) const
  71. {
  72. // TODO: Add linear slope around 0
  73. return VOLUME_A * exp(VOLUME_B * change);
  74. }
  75. ALWAYS_INLINE float log_to_linear(float const val) const
  76. {
  77. // TODO: Add linear slope around 0
  78. return log(val / VOLUME_A) / VOLUME_B;
  79. }
  80. ALWAYS_INLINE Sample& log_multiply(float const change)
  81. {
  82. float factor = linear_to_log(change);
  83. left *= factor;
  84. right *= factor;
  85. return *this;
  86. }
  87. ALWAYS_INLINE Sample log_multiplied(float const volume_change) const
  88. {
  89. Sample new_frame { left, right };
  90. new_frame.log_multiply(volume_change);
  91. return new_frame;
  92. }
  93. // Constant power panning
  94. ALWAYS_INLINE Sample& pan(float const position)
  95. {
  96. float const pi_over_2 = AK::Pi<float> * 0.5f;
  97. float const root_over_2 = AK::sqrt<float>(2.0) * 0.5f;
  98. float const angle = position * pi_over_2 * 0.5f;
  99. float s, c;
  100. AK::sincos<float>(angle, s, c);
  101. left *= root_over_2 * (c - s);
  102. right *= root_over_2 * (c + s);
  103. return *this;
  104. }
  105. ALWAYS_INLINE Sample panned(float const position) const
  106. {
  107. Sample new_sample { left, right };
  108. new_sample.pan(position);
  109. return new_sample;
  110. }
  111. constexpr Sample& operator*=(float const mult)
  112. {
  113. left *= mult;
  114. right *= mult;
  115. return *this;
  116. }
  117. constexpr Sample operator*(float const mult) const
  118. {
  119. return { left * mult, right * mult };
  120. }
  121. constexpr Sample& operator+=(Sample const& other)
  122. {
  123. left += other.left;
  124. right += other.right;
  125. return *this;
  126. }
  127. constexpr Sample& operator+=(float other)
  128. {
  129. left += other;
  130. right += other;
  131. return *this;
  132. }
  133. constexpr Sample operator+(Sample const& other) const
  134. {
  135. return { left + other.left, right + other.right };
  136. }
  137. float left { 0 };
  138. float right { 0 };
  139. };
  140. }
  141. namespace AK {
  142. template<>
  143. struct Formatter<Audio::Sample> : Formatter<FormatString> {
  144. ErrorOr<void> format(FormatBuilder& builder, Audio::Sample const& value)
  145. {
  146. return Formatter<FormatString>::format(builder, "[{}, {}]"sv, value.left, value.right);
  147. }
  148. };
  149. }