Skip to content

Commit

Permalink
Fix reuse of sample_rate name for analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
rothmichaels committed Dec 20, 2024
1 parent 50a323b commit b0a6ac9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions example/audio/0-wrapping_unsafe_apis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ int main()
// 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.tempo,
unsafe_context.sample_rate);
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.tempo,
safe_context.sample_rate);
std::cout << MP_UNITS_STD_FMT::format("Musical context:\n\tTempo: {}\n\tSample Rate: {}\n",
safe_context.current_tempo, safe_context.current_sample_rate);
}
10 changes: 5 additions & 5 deletions example/audio/1-lfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class sine_wave_osc {
{
std::cout << MP_UNITS_STD_FMT::format(
"Created oscillator with starting frequency {} ({}) for sample rate {} at tempo {}\n", freq, m_frequency,
context.sample_rate, context.tempo);
context.current_sample_rate, context.current_tempo);
}

quantity<si::hertz, float> get_frequency() const { return m_frequency; }
Expand All @@ -71,7 +71,7 @@ class sine_wave_osc {
void set_period(QuantityOf<audio::beat_count> auto period)
{
std::cout << MP_UNITS_STD_FMT::format("Setting period to {} -- ", period);
set_period(period / m_context.tempo);
set_period(period / m_context.current_tempo);
}

quantity<audio::sample_value, float> operator()()
Expand All @@ -86,7 +86,7 @@ class sine_wave_osc {
private:
using phase_t = quantity_point<angular::radian, mp_units::default_point_origin(angular::radian), float>;

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

audio::musical_context m_context;
quantity<si::hertz, float> m_frequency;
Expand Down Expand Up @@ -121,8 +121,8 @@ int main()
// 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.tempo;
const auto buffer_size = (buffer_duration * context.sample_rate).in(audio::sample);
const auto buffer_duration = value_cast<float>(beats) / context.current_tempo;
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);
Expand Down
6 changes: 3 additions & 3 deletions example/audio/third_party_audio_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ namespace audio_third_party {

//! State of a playback engine for music host application.
struct musical_context {
float sample_rate; //!< samples per second
float tempo; //!< beats per minute (quarter note == one beat)
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()
{
// Example data, this would be variable in a real-world context
return musical_context{.sample_rate = 8000.f, .tempo = 130.f};
return musical_context{.current_sample_rate = 8000.f, .current_tempo = 130.f};
}
} // namespace audio_third_party
8 changes: 4 additions & 4 deletions example/audio/wrapped_third_party_audio_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ namespace audio {

//! Typesafe version of music application playback engine state
struct musical_context {
mp_units::quantity<sample_rate[mp_units::si::hertz], float> sample_rate;
mp_units::quantity<beats_per_minute, float> tempo;
mp_units::quantity<sample_rate[mp_units::si::hertz], float> current_sample_rate;
mp_units::quantity<beats_per_minute, float> current_tempo;
};

//! Typesafe wrapper around API for host application musical context
musical_context get_musical_context()
{
auto context = audio_third_party::get_musical_context();
return musical_context{.sample_rate = context.sample_rate * mp_units::si::hertz,
.tempo = context.tempo * beats_per_minute};
return musical_context{.current_sample_rate = context.current_sample_rate * mp_units::si::hertz,
.current_tempo = context.current_tempo * beats_per_minute};
}

} // namespace audio

0 comments on commit b0a6ac9

Please sign in to comment.