Coverage Report

Created: 2026-02-16 07:47

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