Skip to content

Commit

Permalink
Merge pull request #64 from pimoroni/tidy_api
Browse files Browse the repository at this point in the history
Tidy api
  • Loading branch information
lowfatcode authored Nov 2, 2021
2 parents 5322efe + b4c1308 commit 8748a48
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
4 changes: 2 additions & 2 deletions examples/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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);
}
25 changes: 18 additions & 7 deletions libraries/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions libraries/picosystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
16 changes: 0 additions & 16 deletions libraries/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8748a48

Please sign in to comment.