Skip to content

Commit 37315cb

Browse files
tsukasa-aumarkshannon
authored andcommitted
Fix ValueError exception that occurs when playing a second song.
We now explicitly stop the current song that is playing before attempting to start a new song.
1 parent c7e443b commit 37315cb

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

source/microbit/microbitmusic.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,6 @@ STATIC mp_obj_t microbit_music_play(mp_uint_t n_args, const mp_obj_t *pos_args,
296296
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
297297
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
298298

299-
// reset octave and duration so tunes always play the same
300-
music_state.last_octave = DEFAULT_OCTAVE;
301-
music_state.last_duration = DEFAULT_DURATION;
302-
303299
// get either a single note or a list of notes
304300
mp_uint_t len;
305301
mp_obj_t *items;
@@ -312,6 +308,18 @@ STATIC mp_obj_t microbit_music_play(mp_uint_t n_args, const mp_obj_t *pos_args,
312308

313309
// get the pin to play on
314310
const microbit_pin_obj_t *pin = microbit_obj_get_pin(args[1].u_obj);
311+
312+
if (async_music_state != ASYNC_MUSIC_STATE_IDLE) {
313+
// Stop the current music before starting new music.
314+
async_music_state = ASYNC_MUSIC_STATE_IDLE;
315+
pwm_set_duty_cycle(pin->name, 0);
316+
microbit_obj_pin_free(pin);
317+
}
318+
319+
// reset octave and duration so tunes always play the same
320+
music_state.last_octave = DEFAULT_OCTAVE;
321+
music_state.last_duration = DEFAULT_DURATION;
322+
315323
microbit_obj_pin_acquire(pin, MP_QSTR_music);
316324

317325
// start the tune running in the background

0 commit comments

Comments
 (0)