Skip to content

Commit

Permalink
codal_port: Don't call microbit_pin_audio_free when audio/music is done.
Browse files Browse the repository at this point in the history
Instead leave any connected pin (pin0 by default) in "music" mode.  This
simplifies the behaviour and can help to avoid clicks going from one sound
to another.

See related issue #50.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Jun 29, 2021
1 parent ab59d87 commit 6790bb5
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/codal_port/modaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ microbit_audio_frame_obj_t *microbit_audio_frame_make_new(void);
void microbit_audio_stop(void) {
audio_source_iter = NULL;
audio_running = false;
microbit_pin_audio_free();
}

STATIC void audio_buffer_ready(void) {
Expand Down
13 changes: 2 additions & 11 deletions src/codal_port/modmusic.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ void microbit_music_tick(void) {
music_data->async_notes_index = 0;
} else {
music_data->async_state = ASYNC_MUSIC_STATE_IDLE;
microbit_pin_audio_free();
return;
}
}
Expand Down Expand Up @@ -289,9 +288,8 @@ STATIC mp_obj_t microbit_music_stop(mp_uint_t n_args, const mp_obj_t *args) {
// Stop any ongoing background music
music_data->async_state = ASYNC_MUSIC_STATE_IDLE;

// Turn off the output and free the audio pin
// Turn off the output
music_output_amplitude(MUSIC_OUTPUT_AMPLITUDE_OFF);
microbit_pin_audio_free();

return mp_const_none;
}
Expand Down Expand Up @@ -326,9 +324,6 @@ STATIC mp_obj_t microbit_music_play(mp_uint_t n_args, const mp_obj_t *pos_args,
// Stop any ongoing background music
music_data->async_state = ASYNC_MUSIC_STATE_IDLE;

// Release the previous pin
microbit_pin_audio_free();

// get the pin to play on
microbit_pin_audio_select(args[1].u_obj);

Expand Down Expand Up @@ -376,15 +371,11 @@ STATIC mp_obj_t microbit_music_pitch(mp_uint_t n_args, const mp_obj_t *pos_args,
music_data->async_state = ASYNC_MUSIC_STATE_IDLE;

// Update pin modes
microbit_pin_audio_free();
microbit_pin_audio_select(args[2].u_obj);

bool wait = args[3].u_bool;
music_output_amplitude(MUSIC_OUTPUT_AMPLITUDE_ON);
if (frequency == 0) {
microbit_pin_audio_free();
} else if (music_output_period_us(1000000 / frequency) == -1) {
microbit_pin_audio_free();
if (frequency != 0 && music_output_period_us(1000000 / frequency) == -1) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid pitch"));
}
if (duration >= 0) {
Expand Down

0 comments on commit 6790bb5

Please sign in to comment.