2021-09-23 22:16:03 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-13 14:07:00 +03:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2021-09-23 22:16:03 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-04-27 13:41:37 +03:00
|
|
|
#include <AK/Format.h>
|
2021-09-23 22:16:03 +03:00
|
|
|
#include <AK/Math.h>
|
|
|
|
|
|
|
|
namespace Audio {
|
2021-11-16 00:27:28 +03:00
|
|
|
using AK::Exponentials::exp;
|
|
|
|
using AK::Exponentials::log;
|
2021-09-23 22:16:03 +03:00
|
|
|
// Constants for logarithmic volume. See Sample::linear_to_log
|
|
|
|
// Corresponds to 60dB
|
2022-05-06 23:14:16 +03:00
|
|
|
constexpr float DYNAMIC_RANGE = 1000;
|
|
|
|
constexpr float VOLUME_A = 1 / DYNAMIC_RANGE;
|
|
|
|
float const VOLUME_B = log(DYNAMIC_RANGE);
|
2021-09-23 22:16:03 +03:00
|
|
|
|
|
|
|
// A single sample in an audio buffer.
|
|
|
|
// Values are floating point, and should range from -1.0 to +1.0
|
|
|
|
struct Sample {
|
|
|
|
constexpr Sample() = default;
|
|
|
|
|
|
|
|
// For mono
|
2022-05-06 23:14:16 +03:00
|
|
|
constexpr explicit Sample(float left)
|
2021-09-23 22:16:03 +03:00
|
|
|
: left(left)
|
|
|
|
, right(left)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// For stereo
|
2022-05-06 23:14:16 +03:00
|
|
|
constexpr Sample(float left, float right)
|
2021-09-23 22:16:03 +03:00
|
|
|
: left(left)
|
|
|
|
, right(right)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:01:51 +03:00
|
|
|
// Returns the absolute maximum range (separate per channel) of the given sample buffer.
|
|
|
|
// For example { 0.8, 0 } means that samples on the left channel occupy the range { -0.8, 0.8 },
|
|
|
|
// while all samples on the right channel are 0.
|
|
|
|
static Sample max_range(ReadonlySpan<Sample> span)
|
|
|
|
{
|
|
|
|
Sample result { NumericLimits<float>::min_normal(), NumericLimits<float>::min_normal() };
|
|
|
|
for (Sample sample : span) {
|
|
|
|
result.left = max(result.left, AK::fabs(sample.left));
|
|
|
|
result.right = max(result.right, AK::fabs(sample.right));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-23 22:16:03 +03:00
|
|
|
void clip()
|
|
|
|
{
|
|
|
|
if (left > 1)
|
|
|
|
left = 1;
|
|
|
|
else if (left < -1)
|
|
|
|
left = -1;
|
|
|
|
|
|
|
|
if (right > 1)
|
|
|
|
right = 1;
|
|
|
|
else if (right < -1)
|
|
|
|
right = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logarithmic scaling, as audio should ALWAYS do.
|
|
|
|
// Reference: https://www.dr-lex.be/info-stuff/volumecontrols.html
|
|
|
|
// We use the curve `factor = a * exp(b * change)`,
|
|
|
|
// where change is the input fraction we want to change by,
|
|
|
|
// a = 1/1000, b = ln(1000) = 6.908 and factor is the multiplier used.
|
|
|
|
// The value 1000 represents the dynamic range in sound pressure, which corresponds to 60 dB(A).
|
|
|
|
// This is a good dynamic range because it can represent all loudness values from
|
|
|
|
// 30 dB(A) (barely hearable with background noise)
|
|
|
|
// to 90 dB(A) (almost too loud to hear and about the reasonable limit of actual sound equipment).
|
|
|
|
//
|
|
|
|
// Format ranges:
|
|
|
|
// - Linear: 0.0 to 1.0
|
|
|
|
// - Logarithmic: 0.0 to 1.0
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
ALWAYS_INLINE float linear_to_log(float const change) const
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
|
|
|
// TODO: Add linear slope around 0
|
|
|
|
return VOLUME_A * exp(VOLUME_B * change);
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
ALWAYS_INLINE float log_to_linear(float const val) const
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
|
|
|
// TODO: Add linear slope around 0
|
|
|
|
return log(val / VOLUME_A) / VOLUME_B;
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
ALWAYS_INLINE Sample& log_multiply(float const change)
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
2022-05-06 23:14:16 +03:00
|
|
|
float factor = linear_to_log(change);
|
2021-09-23 22:16:03 +03:00
|
|
|
left *= factor;
|
|
|
|
right *= factor;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
ALWAYS_INLINE Sample log_multiplied(float const volume_change) const
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
|
|
|
Sample new_frame { left, right };
|
|
|
|
new_frame.log_multiply(volume_change);
|
|
|
|
return new_frame;
|
|
|
|
}
|
|
|
|
|
2021-10-31 14:31:58 +03:00
|
|
|
// Constant power panning
|
2022-05-06 23:14:16 +03:00
|
|
|
ALWAYS_INLINE Sample& pan(float const position)
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
2022-05-06 23:14:16 +03:00
|
|
|
float const pi_over_2 = AK::Pi<float> * 0.5f;
|
|
|
|
float const root_over_2 = AK::sqrt<float>(2.0) * 0.5f;
|
|
|
|
float const angle = position * pi_over_2 * 0.5f;
|
|
|
|
float s, c;
|
|
|
|
AK::sincos<float>(angle, s, c);
|
2022-02-03 14:48:17 +03:00
|
|
|
left *= root_over_2 * (c - s);
|
|
|
|
right *= root_over_2 * (c + s);
|
2021-09-23 22:16:03 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
ALWAYS_INLINE Sample panned(float const position) const
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
2021-10-31 14:31:58 +03:00
|
|
|
Sample new_sample { left, right };
|
|
|
|
new_sample.pan(position);
|
|
|
|
return new_sample;
|
2021-09-23 22:16:03 +03:00
|
|
|
}
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
constexpr Sample& operator*=(float const mult)
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
|
|
|
left *= mult;
|
|
|
|
right *= mult;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
constexpr Sample operator*(float const mult) const
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
|
|
|
return { left * mult, right * mult };
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Sample& operator+=(Sample const& other)
|
|
|
|
{
|
|
|
|
left += other.left;
|
|
|
|
right += other.right;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-05-06 23:14:16 +03:00
|
|
|
constexpr Sample& operator+=(float other)
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
|
|
|
left += other;
|
|
|
|
right += other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-02-03 14:48:17 +03:00
|
|
|
constexpr Sample operator+(Sample const& other) const
|
2021-09-23 22:16:03 +03:00
|
|
|
{
|
|
|
|
return { left + other.left, right + other.right };
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
float left { 0 };
|
|
|
|
float right { 0 };
|
2021-09-23 22:16:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2022-04-27 13:41:37 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Formatter<Audio::Sample> : Formatter<FormatString> {
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Audio::Sample const& value)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
return Formatter<FormatString>::format(builder, "[{}, {}]"sv, value.left, value.right);
|
2022-04-27 13:41:37 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|