Update audio example
This commit is contained in:
parent
6c213f9c45
commit
21b2028122
1 changed files with 22 additions and 12 deletions
|
|
@ -5,9 +5,13 @@
|
|||
#include <psemek/audio/wave/square.hpp>
|
||||
#include <psemek/audio/wave/triangle.hpp>
|
||||
#include <psemek/audio/effect/volume.hpp>
|
||||
#include <psemek/audio/duplicate.hpp>
|
||||
#include <psemek/audio/stereo.hpp>
|
||||
#include <psemek/audio/mixer.hpp>
|
||||
#include <psemek/app/app.hpp>
|
||||
#include <psemek/app/main.hpp>
|
||||
#include <psemek/util/clock.hpp>
|
||||
#include <psemek/geom/constants.hpp>
|
||||
|
||||
#include <map>
|
||||
|
||||
|
|
@ -35,18 +39,12 @@ struct audio_app
|
|||
: app::app("Audio example")
|
||||
{
|
||||
mixer_ = audio::make_mixer();
|
||||
engine_.output(mixer_);
|
||||
|
||||
auto volume = audio::volume(audio::sine_wave(440.f), 1.f, 0.01f);
|
||||
auto channel = mixer_->add(volume);
|
||||
std::this_thread::sleep_for(std::chrono::seconds{1});
|
||||
volume->gain(0.5f);
|
||||
std::this_thread::sleep_for(std::chrono::seconds{1});
|
||||
volume->gain(0.25f);
|
||||
std::this_thread::sleep_for(std::chrono::seconds{1});
|
||||
volume->gain(0.125f);
|
||||
std::this_thread::sleep_for(std::chrono::seconds{1});
|
||||
channel->stop();
|
||||
auto [ dup1, dup2 ] = audio::duplicate(mixer_);
|
||||
left_volume_ = audio::volume(dup1, 0.f, 0.1f);
|
||||
right_volume_ = audio::volume(dup2, 0.f, 0.1f);
|
||||
auto result = audio::stereo(left_volume_, right_volume_);
|
||||
engine_.output(result);
|
||||
}
|
||||
|
||||
void on_key_down(SDL_Keycode key) override
|
||||
|
|
@ -57,7 +55,7 @@ struct audio_app
|
|||
{
|
||||
int midi = key_to_midi.at(key);
|
||||
auto tone = audio::sine_wave(440.f * std::pow(2.f, (midi - 69) / 12.f));
|
||||
channels_[key] = mixer_->add(audio::volume(tone, 0.5f));
|
||||
channels_[key] = mixer_->add(tone);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -72,10 +70,22 @@ struct audio_app
|
|||
}
|
||||
}
|
||||
|
||||
void update() override
|
||||
{
|
||||
float const time = clock_.count();
|
||||
|
||||
float volume = std::sin(time);
|
||||
left_volume_->gain(0.5f + 0.5f * volume);
|
||||
right_volume_->gain(0.5f - 0.5f * volume);
|
||||
}
|
||||
|
||||
private:
|
||||
audio::engine engine_;
|
||||
audio::mixer_ptr mixer_;
|
||||
std::shared_ptr<audio::volume_control> left_volume_, right_volume_;
|
||||
std::map<SDL_Keycode, audio::mixer::channel_ptr> channels_;
|
||||
|
||||
util::clock<> clock_;
|
||||
};
|
||||
|
||||
int main()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue