diff --git a/examples/audio/audio.cpp b/examples/audio/audio.cpp index 5eb46d7..3645deb 100644 --- a/examples/audio/audio.cpp +++ b/examples/audio/audio.cpp @@ -103,7 +103,7 @@ void update(uint32_t tick) { // if the current sound has finished playing, or we changed a dial value // then restart the sound uint32_t full_duration = get_dial_value("duration") + get_dial_value("release") + get_dial_value("reverb"); - if(change || audio_position() > full_duration) { + if(change || position() > full_duration) { set_voice(); } } @@ -188,6 +188,6 @@ void draw(uint32_t tick) { // draw current playback marker pen(0, 15, 15); - uint32_t pos = (audio_position() * 230) / full_duration; + uint32_t pos = (position() * 230) / full_duration; frect(pos + 5, 5, 2, 50); } \ No newline at end of file diff --git a/libraries/audio.cpp b/libraries/audio.cpp index c0d4b1b..10dc8b7 100644 --- a/libraries/audio.cpp +++ b/libraries/audio.cpp @@ -9,6 +9,22 @@ namespace picosystem { uint32_t _duration; uint32_t _volume = 100; + voice_t voice(uint32_t attack, uint32_t decay, uint32_t sustain, + uint32_t release, int32_t bend, uint32_t bend_ms, + uint32_t reverb, uint32_t noise, uint32_t distort) { + return { + .bend = bend, + .bend_ms = bend_ms, + .attack = attack, + .decay = decay, + .sustain = sustain, + .release = release, + .reverb = reverb, + .noise = noise, + .distort = distort + }; + } + void play(voice_t voice, uint32_t frequency, uint32_t duration, uint32_t volume) { _frequency = frequency; @@ -19,13 +35,8 @@ namespace picosystem { _ms = 0; } - uint32_t audio_position() { - return _ms; - } - - // Convenience function to tell if the note is still playing - bool audio_playing() { - return !(_ms > _duration + _voice.release + _voice.reverb); + int32_t position() { + return (_ms > _duration + _voice.release + _voice.reverb) ? -1 : _ms; } uint8_t audio_sample(uint32_t ms) { diff --git a/libraries/picosystem.hpp b/libraries/picosystem.hpp index d26aaa1..efc09d2 100644 --- a/libraries/picosystem.hpp +++ b/libraries/picosystem.hpp @@ -170,12 +170,17 @@ namespace picosystem { color_t* ps, int32_t so, int32_t ss, color_t* pd, uint32_t c); // audio + voice_t voice( + uint32_t attack = 100, uint32_t decay = 50, + uint32_t sustain = 80, uint32_t release = 100, + int32_t bend = 0, uint32_t bend_ms = 0, uint32_t reverb = 0, + uint32_t noise = 0, uint32_t distort = 0); void play( voice_t voice, uint32_t frequency, uint32_t duration = 500, uint32_t volume = 100); uint8_t audio_sample(uint32_t ms); - uint32_t audio_position(); - bool audio_playing(); + int32_t position(); + bool playing(); // utility std::string str(float v, uint8_t precision = 2); @@ -185,11 +190,6 @@ namespace picosystem { color_t rgb(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 15); color_t hsv(float h, float s, float v, float a = 1.0f); buffer_t* buffer(uint32_t w, uint32_t h, void *data = nullptr); - voice_t voice( - uint32_t attack = 100, uint32_t decay = 50, - uint32_t sustain = 80, uint32_t release = 100, - int32_t bend = 0, uint32_t bend_ms = 0, uint32_t reverb = 0, - uint32_t noise = 0, uint32_t distort = 0); uint32_t time(); uint32_t time_us(); void sleep(uint32_t d); diff --git a/libraries/utility.cpp b/libraries/utility.cpp index 7093140..866b9e9 100644 --- a/libraries/utility.cpp +++ b/libraries/utility.cpp @@ -64,22 +64,6 @@ namespace picosystem { return b; } - voice_t voice(uint32_t attack, uint32_t decay, uint32_t sustain, - uint32_t release, int32_t bend, uint32_t bend_ms, - uint32_t reverb, uint32_t noise, uint32_t distort) { - return { - .bend = bend, - .bend_ms = bend_ms, - .attack = attack, - .decay = decay, - .sustain = sustain, - .release = release, - .reverb = reverb, - .noise = noise, - .distort = distort - }; - } - // returns true if the provided rectangles intersect bool intersects( int32_t x, int32_t y, int32_t w, int32_t h,