84 lines
1.8 KiB
C++
84 lines
1.8 KiB
C++
#include <psemek/audio/engine.hpp>
|
|
#include <psemek/audio/wave/silence.hpp>
|
|
#include <psemek/audio/wave/sine.hpp>
|
|
#include <psemek/audio/wave/sawtooth.hpp>
|
|
#include <psemek/audio/wave/square.hpp>
|
|
#include <psemek/audio/wave/triangle.hpp>
|
|
#include <psemek/audio/effect/volume.hpp>
|
|
#include <psemek/audio/mixer.hpp>
|
|
#include <psemek/app/app.hpp>
|
|
#include <psemek/app/main.hpp>
|
|
|
|
#include <map>
|
|
|
|
using namespace psemek;
|
|
|
|
static std::map<SDL_Keycode, int> const key_to_midi
|
|
{
|
|
{SDLK_a, 69},
|
|
{SDLK_s, 70},
|
|
{SDLK_d, 71},
|
|
{SDLK_f, 72},
|
|
{SDLK_g, 73},
|
|
{SDLK_h, 74},
|
|
{SDLK_j, 75},
|
|
{SDLK_k, 76},
|
|
{SDLK_l, 77},
|
|
{SDLK_SEMICOLON, 78},
|
|
{SDLK_QUOTE, 79},
|
|
};
|
|
|
|
struct audio_app
|
|
: app::app
|
|
{
|
|
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();
|
|
}
|
|
|
|
void on_key_down(SDL_Keycode key) override
|
|
{
|
|
app::app::on_key_down(key);
|
|
|
|
if (key_to_midi.contains(key) && !channels_.contains(key))
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
|
|
void on_key_up(SDL_Keycode key) override
|
|
{
|
|
app::app::on_key_up(key);
|
|
|
|
if (channels_.contains(key))
|
|
{
|
|
channels_[key]->stop();
|
|
channels_.erase(key);
|
|
}
|
|
}
|
|
|
|
private:
|
|
audio::engine engine_;
|
|
audio::mixer_ptr mixer_;
|
|
std::map<SDL_Keycode, audio::mixer::channel_ptr> channels_;
|
|
};
|
|
|
|
int main()
|
|
{
|
|
return app::main<audio_app>();
|
|
}
|