lancaster-university / codal-microbit-v2

CODAL target for the micro:bit v2.x series of devices
MIT License
41 stars 50 forks source link

Issues changing `splitterChannel` sample rate. #356

Closed microbit-carlos closed 4 months ago

microbit-carlos commented 11 months ago

From issue https://github.com/microsoft/pxt-microbit/issues/5312 as identified by @srietkerk:

@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
microbit-carlos commented 11 months ago

@srietkerk would you be able to provide C++ sample code to replicate? That will help significantly.

martinwork commented 11 months ago

Related: https://github.com/lancaster-university/codal-microbit-v2/issues/284

srietkerk commented 11 months ago

I can't supply what it looks like in C++ from micro:bit directly, as far as I know. However, I can point you to how the extension is calling the sample rate function. Hopefully that helps.

Setting the sample rate happens here: https://github.com/microsoft/pxt-microbit/blob/c6a9d193378b90753be0a9b3d21fd40c4f013b09/libs/audio-recording/recording.cpp#L174

and here: https://github.com/microsoft/pxt-microbit/blob/c6a9d193378b90753be0a9b3d21fd40c4f013b09/libs/audio-recording/recording.cpp#L207

Let me know if there is any other way that I can help.

JohnVidler commented 5 months ago

Right! This has been a persistent thorn in my side for a while, but I'm happy to say that I think I've got it fixed now.

SplitterChannel objects when on the bug/stream-resampling branch in codal-core and codal-microbit-v2 should now handle requests for rates lower than the upstream rate, and automatically resample down to the requested rate. It should also handle requests for rates higher than the current 11k we sample the microphone at, and it will forward on the requests to the hardware to bump up the sampling frequency.

I've not currently merged these changes into master while I test them against the existing MakeCode extension, but it was high time I actually gave an update on this issue (and the other.. err.. several others).

Thanks everyone for being patient on this one.

JohnVidler commented 4 months ago

This should now be finally fixed with the release of v0.2.65.