Sample.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. void clip()
  35. {
  36. if (left > 1)
  37. left = 1;
  38. else if (left < -1)
  39. left = -1;
  40. if (right > 1)
  41. right = 1;
  42. else if (right < -1)
  43. right = -1;
  44. }
  45. // Logarithmic scaling, as audio should ALWAYS do.
  46. // Reference: https://www.dr-lex.be/info-stuff/volumecontrols.html
  47. // We use the curve `factor = a * exp(b * change)`,
  48. // where change is the input fraction we want to change by,
  49. // a = 1/1000, b = ln(1000) = 6.908 and factor is the multiplier used.
  50. // The value 1000 represents the dynamic range in sound pressure, which corresponds to 60 dB(A).
  51. // This is a good dynamic range because it can represent all loudness values from
  52. // 30 dB(A) (barely hearable with background noise)
  53. // to 90 dB(A) (almost too loud to hear and about the reasonable limit of actual sound equipment).
  54. //
  55. // Format ranges:
  56. // - Linear: 0.0 to 1.0
  57. // - Logarithmic: 0.0 to 1.0
  58. ALWAYS_INLINE float linear_to_log(float const change) const
  59. {
  60. // TODO: Add linear slope around 0
  61. return VOLUME_A * exp(VOLUME_B * change);
  62. }
  63. ALWAYS_INLINE float log_to_linear(float const val) const
  64. {
  65. // TODO: Add linear slope around 0
  66. return log(val / VOLUME_A) / VOLUME_B;
  67. }
  68. ALWAYS_INLINE Sample& log_multiply(float const change)
  69. {
  70. float factor = linear_to_log(change);
  71. left *= factor;
  72. right *= factor;
  73. return *this;
  74. }
  75. ALWAYS_INLINE Sample log_multiplied(float const volume_change) const
  76. {
  77. Sample new_frame { left, right };
  78. new_frame.log_multiply(volume_change);
  79. return new_frame;
  80. }
  81. // Constant power panning
  82. ALWAYS_INLINE Sample& pan(float const position)
  83. {
  84. float const pi_over_2 = AK::Pi<float> * 0.5f;
  85. float const root_over_2 = AK::sqrt<float>(2.0) * 0.5f;
  86. float const angle = position * pi_over_2 * 0.5f;
  87. float s, c;
  88. AK::sincos<float>(angle, s, c);
  89. left *= root_over_2 * (c - s);
  90. right *= root_over_2 * (c + s);
  91. return *this;
  92. }
  93. ALWAYS_INLINE Sample panned(float const position) const
  94. {
  95. Sample new_sample { left, right };
  96. new_sample.pan(position);
  97. return new_sample;
  98. }
  99. constexpr Sample& operator*=(float const mult)
  100. {
  101. left *= mult;
  102. right *= mult;
  103. return *this;
  104. }
  105. constexpr Sample operator*(float const mult) const
  106. {
  107. return { left * mult, right * mult };
  108. }
  109. constexpr Sample& operator+=(Sample const& other)
  110. {
  111. left += other.left;
  112. right += other.right;
  113. return *this;
  114. }
  115. constexpr Sample& operator+=(float other)
  116. {
  117. left += other;
  118. right += other;
  119. return *this;
  120. }
  121. constexpr Sample operator+(Sample const& other) const
  122. {
  123. return { left + other.left, right + other.right };
  124. }
  125. float left { 0 };
  126. float right { 0 };
  127. };
  128. }
  129. namespace AK {
  130. template<>
  131. struct Formatter<Audio::Sample> : Formatter<FormatString> {
  132. ErrorOr<void> format(FormatBuilder& builder, Audio::Sample const& value)
  133. {
  134. return Formatter<FormatString>::format(builder, "[{}, {}]"sv, value.left, value.right);
  135. }
  136. };
  137. }