microsoft / pxt-microbit

A Blocks / JavaScript code editor for the micro:bit built on Microsoft MakeCode
https://makecode.microbit.org
Other
721 stars 594 forks source link

Sound recording sample rate only changes once on playback #5312

Closed microbit-giles closed 3 months ago

microbit-giles commented 1 year ago

Describe the bug If you set the sample rate, record a sound, change the sample rate and play it back, the change just before playback only happens once. On subsequent runs of the code, the playback sample rate defaults back to its previous value.

To Reproduce Steps to reproduce the behavior:

  1. Go to https://makecode.microbit.org/_VziHPeD7a3XJ
  2. Flash code to a micro:bit
  3. Press button A, speak, hear it played back speeded up (as expected) - both 'set sample rate' blocks have been acted upon.
  4. Press button A again and speak again. The new recording (and subsequent recordings) are not speeded up when played back (this is not what I expect). It appears that the first 'set sample rate to 11000' block is being acted upon, but not the second 'set sample rate to 18000' block after the first time it is run.

Expected behavior The play back sound should always be speeded up.

Screenshots microbit-screenshot

micro:bit version (please complete the following information): tested on V2.00

Additional context If you explicitly set recording and playback sample rates, the project works as expected, but I expect the default behaviour to be consistent, and in this case always play back speeded up audio.

srietkerk commented 1 year ago

@JohnVidler I think this is a CODAL issue that specifically has to do with splitterChannel->requestSampleRate(float samplerate).

It took me a bit to realize, but the problem emerges with the bottom sample rate call being a larger value than the top sample rate call. Once the sample rate gets set to 18000 for the upstream, the value of the input sample rate will never change due to this check here: https://github.com/lancaster-university/codal-core/blob/763a2d1a916d4db3fe3f3f2b1eaa9947cc8d0a7e/source/streams/StreamSplitter.cpp#L126. If we try to press A again, the upstream input sample rate is larger than what we are trying to change the sample rate to (11000), so we never get to request that sample rate to the upstream and thus the input sample rate for stays at 18000 while the playback rate changes back down to 11000. Thus, when we go to change both the sample rate and the playback to 18000, nothing happens to the input sample rate, but the output sample rate goes back to 18000, thus making the playback on the second go-round and beyond sound not sped up.

This can be seen by trying the following program Changing the sample rate in the same two places works as expected. The playback is slow every time:

image

This problem came to my attention when I noticed that just setting the playback sample rate in the second set sample rate call did not cause the same bug as noticed above.

image