Sample.h 3.8 KB

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