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 19 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions example/audio/1-oscillator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import std;
#else
#include <algorithm>
#include <cassert>
#include <iostream>
#include <ranges>
#include <vector>
#endif
#ifdef MP_UNITS_MODULES
import mp_units;
#else
#include <mp-units/format.h>
#include <mp-units/math.h>
#include <mp-units/systems/angular.h>
#include <mp-units/systems/isq.h>
#include <mp-units/systems/si.h>
Expand Down Expand Up @@ -64,7 +66,7 @@ class sine_wave_osc {

void set_period(QuantityOf<isq::time> auto period)
{
m_frequency = 1.f / value_cast<float>(period);
m_frequency = inverse<si::hertz>(period);
std::cout << MP_UNITS_STD_FMT::format("Setting period to {} (i.e. frequency to {})\n", period, m_frequency);
}

Expand Down Expand Up @@ -120,9 +122,10 @@ int main()
// 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;
const auto buffer_duration = value_cast<float>(beats) / context.current_tempo;
const auto buffer_size = (buffer_duration * context.current_sample_rate).in(audio::sample);
const quantity beats = 2 * audio::whole_note;
const quantity buffer_duration = value_cast<float>(beats) / context.current_tempo;
const quantity buffer_size =
quantity_cast<audio::sample_count>((buffer_duration * context.current_sample_rate).in(one));
Comment on lines +131 to +132
Copy link
Owner

Choose a reason for hiding this comment

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

Is a quantity_cast required here? I assume that the implicit conversion did not compile? Did you try an explicit conversion?

Suggested change
const quantity buffer_size =
quantity_cast<audio::sample_count>((buffer_duration * context.current_sample_rate).in(one));
const quantity buffer_size =
audio::sample_count((buffer_duration * context.current_sample_rate).in(one));

Copy link
Owner

Choose a reason for hiding this comment

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

Isn't audio::sample a good unit here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, audio::sample is a good unit here. This is what I had before the changes to dimensionless quantities:

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

Maybe I didn't know the best way to do this after the dimensionless changes.

Copy link
Owner

Choose a reason for hiding this comment

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

I've fixed the previous audio example in the paper (https://godbolt.org/z/z7sPqe66e) and improved inverse(). Maybe this will give you some ideas on how to refactor this.


std::cout << MP_UNITS_STD_FMT::format("\nCreating buffer with size:\n\t{}\n\t{}\n\t{}\n\n", beats, buffer_duration,
buffer_size);
Expand All @@ -134,10 +137,9 @@ int main()
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);
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]);
for (const auto sampleValue : std::ranges::subrange(begin(buffer_1) + 1, end(buffer_1))) {
std::cout << MP_UNITS_STD_FMT::format(", {}", sampleValue);
}
std::cout << "]\n\n";

Expand Down
Loading