Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio examples #644

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ add_example(strong_angular_quantities)
add_example(total_energy)
add_example(unmanned_aerial_vehicle example_utils)

add_subdirectory(audio)
add_subdirectory(glide_computer_lib)
add_subdirectory(kalman_filter)
52 changes: 52 additions & 0 deletions example/audio/0-wrapping_unsafe_apis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Roth Michaels
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <mp-units/ext/format.h>
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <iostream>
#endif
#ifdef MP_UNITS_MODULES
import mp_units;
#else
#include <mp-units/format.h>
#endif

#include "third_party_audio_api.h"
#include "wrapped_third_party_audio_api.h"

int main()
{
// Operating system or host applications will provide APIs with various musical context information such as tempo and
// sample rate, but these APIs are not type safe using float or double values; e.g.:
const auto unsafe_context = audio_third_party::get_musical_context();

std::cout << MP_UNITS_STD_FMT::format("Musical context:\n\tTempo: {}\n\tSample Rate: {}\n\n",
unsafe_context.current_tempo, unsafe_context.current_sample_rate);

// These unsafe APIs can be wrapped a new API returning type-safe quantities for tempo and sample rate; e.g.:
const auto safe_context = audio::get_musical_context();

std::cout << MP_UNITS_STD_FMT::format("Musical context:\n\tTempo: {}\n\tSample Rate: {}\n",
safe_context.current_tempo, safe_context.current_sample_rate);
}
150 changes: 150 additions & 0 deletions example/audio/1-oscillator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Roth Michaels
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <mp-units/ext/format.h>
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <algorithm>
#include <cassert>
#include <iostream>
#endif
#ifdef MP_UNITS_MODULES
import mp_units;
#else
#include <mp-units/format.h>
#include <mp-units/systems/angular.h>
#include <mp-units/systems/isq.h>
#include <mp-units/systems/si.h>
#endif

#include "audio.h"
#include "wrapped_third_party_audio_api.h"

namespace {
using namespace mp_units;

//! A DSP generator class that generates sample values for a sine wave oscillator.
class sine_wave_osc {
public:
sine_wave_osc(const audio::musical_context& context, QuantityOf<isq::frequency> auto freq) :
m_context{context}, m_frequency{freq}
{
std::cout << MP_UNITS_STD_FMT::format(
"Created oscillator with starting frequency {} ({}) for sample rate {} at tempo {}\n", freq, m_frequency,
context.current_sample_rate, context.current_tempo);
}

quantity<si::hertz, float> get_frequency() const { return m_frequency; }

void set_frequency(QuantityOf<isq::frequency> auto freq)
{
m_frequency = freq;
std::cout << MP_UNITS_STD_FMT::format("Setting frequency to {} ({})\n", freq, m_frequency);
}

void set_period(QuantityOf<isq::time> auto period)
{
m_frequency = 1.f / value_cast<float>(period);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use inverse() from math.h that provides additional safety (https://aurora-opensource.github.io/au/main/reference/math/#inverse_as-inverse_in). However, it is not that mandatory as you are using floating-point representations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, I like this.

std::cout << MP_UNITS_STD_FMT::format("Setting period to {} (i.e. frequency to {})\n", period, m_frequency);
}

void set_period(QuantityOf<audio::beat_count> auto period)
{
std::cout << MP_UNITS_STD_FMT::format("Setting period to {} -- ", period);
set_period(value_cast<float>(period) / m_context.current_tempo);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value_cast might not be needed here as current_tempo is already a floating point.

}

quantity<audio::sample_value, float> operator()()
{
auto out = angular::sin(m_phase.quantity_from_zero());
m_phase += m_step;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_step is uninitialized here?

return out;
}

void reset() { m_phase = phase_t{0.f * angular::radian}; }

private:
using phase_t = quantity_point<angular::radian, mp_units::default_point_origin(angular::radian), float>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is unfortunate that a point origin must be provided when we want to specify a specific representation type. I have no ideas on how to improve here. I believe that for points, most people will use double, and the main interest will be to work against different origins. This is why I provided arguments in such order. But maybe I am wrong, or is there another way to improve here?

@JohelEGP and @chiphogg, do you have any ideas here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the origin is an expression, {}.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you mean by that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant something like this, but it turns out that it doesn't work with auto NTTPs, whereas it does with function parameters (https://cpp1.godbolt.org/z/hPY9KeYfa):

#include <initializer_list>
#include <type_traits>

template<int Origin = 1, class Rep = double>
struct point;

static_assert(std::is_same_v<point<{}, int>, point<0, int>>);


void update_step() { m_step = (m_frequency / m_context.current_sample_rate) * angular::revolution; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this one is never called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch thanks


audio::musical_context m_context;
quantity<si::hertz, float> m_frequency;
phase_t m_phase{0.f * angular::radian};
quantity<angular::radian, float> m_step;
};
} // namespace

int main()
{
using namespace mp_units::si::unit_symbols;

const auto context = audio::get_musical_context();

// Sine oscillators are sometimes used as a "low-frequency oscillator"
// (LFO) that runs at a frequency below the range of human hearing and
// is used a source of modulation for other paramters in an audio
// algorithm.
auto sin_gen = sine_wave_osc{context, 1 * Hz};

// Depending on the use-case sometimes an LFO will be set with a frequency in Hz
sin_gen.set_frequency(13 * Hz);

// for some use-cases it is more convient for a user to set the period
sin_gen.set_period(42 * s);

// and in some other use-cases setting the period in musical note duration is more intuitive
sin_gen.set_period(1 * audio::half_note);
Comment on lines +110 to +117
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API is fine, but you could also consider one overloaded name for all of those (e.g., set()). This will properly handle whatever a user will provide. I am not sure if that will provide a more readable code, though 😉


// Our oscillator can be used to generate sample values for a buffer
// of audio samples. In this example we will create a buffer with
// duration equal to 2 measures of 4/4 music (i.e. 2 whole notes at
// the current tempo):
const auto beats = 2 * audio::whole_note;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is up to you, and this is purely a matter of personal preference. 😉 AAA (Almost Always Auto) is a nice style, and I like it in many cases (especially not to repeat myself).

However, with time, I have learned to appreciate CTAD as it makes the code easier to reason about. Using quantity and quantity_point CTAD placeholders instead of auto probably makes the code easier for a new user to understand or maintain.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const Quantity auto beats is also an option, but it is more to type and does not have any benefits here over just const quantity beats.

const auto buffer_duration = value_cast<float>(beats) / context.current_tempo;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value_cast is probably not needed here?

const auto buffer_size = (buffer_duration * context.current_sample_rate).in(audio::sample);

std::cout << MP_UNITS_STD_FMT::format("\nCreating buffer with size:\n\t{}\n\t{}\n\t{}\n\n", beats, buffer_duration,
buffer_size);

using buffer_t = std::vector<quantity<audio::sample_value, float>>;

auto buffer_1 = buffer_t(std::size_t(buffer_size.numerical_value_in(audio::sample)));

std::cout << MP_UNITS_STD_FMT::format("Filling buffer with values from LFO @ {}", sin_gen.get_frequency());
std::generate(begin(buffer_1), end(buffer_1), sin_gen);

assert(buffer_1.size() > 0u);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what this assert does as you set the vector's size explicitly in line 132. Did you mean reserve() and then using back_inserter in the algorithm?

std::cout << MP_UNITS_STD_FMT::format("\nLFO Values:\n[{}", buffer_1[0u]);
for (std::size_t i = 1u; i < buffer_1.size(); ++i) {
std::cout << MP_UNITS_STD_FMT::format(", {}", buffer_1[i]);
}
Comment on lines +139 to +141
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it is up to you but I would use a range-for here 😉

std::cout << "]\n\n";

// generated values should be the same after resetting oscillator
sin_gen.reset();
auto buffer_2 = buffer_t(buffer_1.size());
std::generate(begin(buffer_2), end(buffer_2), sin_gen);

return buffer_1 == buffer_2;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error in the CI is strange, but #include <vector> is missing, so it might be the reason why the comparison operator is not found on one of the configurations.

}
24 changes: 24 additions & 0 deletions example/audio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The MIT License (MIT)
#
# Copyright (c) 2024 Roth Michaels
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

add_example(0-wrapping_unsafe_apis)
add_example(1-oscillator)
94 changes: 94 additions & 0 deletions example/audio/audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Roth Michaels
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#ifdef MP_UNITS_MODULES
import mp_units;
#else
#include <mp-units/framework/quantity_spec.h>
#include <mp-units/framework/unit.h>
#include <mp-units/systems/isq.h>
#include <mp-units/systems/si.h>
#endif

namespace audio {

QUANTITY_SPEC(sample_count, mp_units::dimensionless, mp_units::is_kind);
QUANTITY_SPEC(sample_duration, mp_units::isq::time);
QUANTITY_SPEC(sample_rate, mp_units::isq::frequency, sample_count / mp_units::isq::time);

inline constexpr struct sample final : mp_units::named_unit<"Smpl", mp_units::one, mp_units::kind_of<sample_count>> {
} sample;

QUANTITY_SPEC(unit_sample_amount, mp_units::dimensionless, mp_units::is_kind);

inline constexpr struct sample_value final :
mp_units::named_unit<"PCM", mp_units::one, mp_units::kind_of<unit_sample_amount>> {
} sample_value;

QUANTITY_SPEC(beat_count, mp_units::dimensionless, mp_units::is_kind);
QUANTITY_SPEC(beat_duration, mp_units::isq::time);
QUANTITY_SPEC(tempo, mp_units::isq::frequency, beat_count / mp_units::isq::time);

inline constexpr struct quarter_note final : mp_units::named_unit<"q", mp_units::one, mp_units::kind_of<beat_count>> {
} quarter_note;

inline constexpr struct whole_note final : mp_units::named_unit<"w", mp_units::mag<4> * quarter_note> {
} whole_note;
inline constexpr struct half_note final : mp_units::named_unit<"h", mp_units::mag<2> * quarter_note> {
} half_note;
inline constexpr struct dotted_half_note final : mp_units::named_unit<"h.", mp_units::mag<3> * quarter_note> {
} dotted_half_note;
inline constexpr struct eigth_note final : mp_units::named_unit<"8th", mp_units::mag_ratio<1, 2> * quarter_note> {
} eigth_note;
inline constexpr struct dotted_quarter_note final : mp_units::named_unit<"q.", mp_units::mag<3> * eigth_note> {
} dotted_quarter_note;
inline constexpr struct quarter_note_triplet final : mp_units::named_unit<"qt", mp_units::mag_ratio<1, 3> * half_note> {
} quarter_note_triplet;
inline constexpr struct sixteenth_note final : mp_units::named_unit<"16th", mp_units::mag_ratio<1, 2> * eigth_note> {
} sixteenth_note;
inline constexpr struct dotted_eigth_note final : mp_units::named_unit<"8th.", mp_units::mag<3> * sixteenth_note> {
} dotted_eigth_note;

inline constexpr struct beats_per_minute final : mp_units::named_unit<"bpm", quarter_note / mp_units::non_si::minute> {
} beats_per_minute;

namespace unit_symbols {
inline constexpr auto smpl = sample;

inline constexpr auto pcm = sample_value;

inline constexpr auto n_wd = 3 * half_note;
inline constexpr auto n_w = whole_note;
inline constexpr auto n_hd = dotted_half_note;
inline constexpr auto n_h = half_note;
inline constexpr auto n_qd = dotted_quarter_note;
inline constexpr auto n_q = quarter_note;
inline constexpr auto n_qt = quarter_note_triplet;
inline constexpr auto n_8thd = dotted_eigth_note;
inline constexpr auto n_8th = eigth_note;
inline constexpr auto n_16th = sixteenth_note;

inline constexpr auto bpm = beats_per_minute;
rothmichaels marked this conversation as resolved.
Show resolved Hide resolved
} // namespace unit_symbols
} // namespace audio
40 changes: 40 additions & 0 deletions example/audio/third_party_audio_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Roth Michaels
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

namespace audio_third_party {

//! State of a playback engine for music host application.
struct musical_context {
float current_sample_rate; //!< samples per second
float current_tempo; //!< beats per minute (quarter note == one beat)
};

//! API provided by music host application to provide global info
//! about the playback engine.
musical_context get_musical_context()
rothmichaels marked this conversation as resolved.
Show resolved Hide resolved
{
// Example data, this would be variable in a real-world context
return musical_context{.current_sample_rate = 8000.f, .current_tempo = 130.f};
}
} // namespace audio_third_party
Loading
Loading