Skip to content

Add simulator support for sample rate #5757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2024
Merged

Conversation

riknoll
Copy link
Member

@riknoll riknoll commented Jul 29, 2024

Fixes #5286

This adds simulator support for the sample rate blocks in the audio recording extension.

The new behavior includes:

  • Worse audio quality at lower sample rates (and better at higher)
  • Longer recording times at lower sample rates (and shorter at higher)
  • The ability to mess with playback speed by changing the output sample rate

The recording times vary from 3 seconds at the highest sample rate to 20 seconds at the lowest. I can adjust this range if need be, but those were the values @srietkerk got when testing on hardware.

Also, the browser has limits on how fast/slow you can play audio, so I capped it within a reasonable range; I've tested recording and playback in Chrome + Firefox on Windows and Safari on macOS.

Here's a test URL:

https://makecode.microbit.org/app/8ac1af74a11ed81b3d41f1a0d0eee068d2158fdf-3acc60290d

@riknoll riknoll requested a review from a team July 29, 2024 22:54
} catch (e) {
state.recording.volume = volume;

const minPlaybackRate = 0.15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason that these aren't defined with the other default variables at the top of the file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they're only used in this one spot

const minPlaybackRate = 0.15

let maxPlaybackRate = 15;
if (isFirefox()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that you said that you were experiencing weirdness with Firefox. Is there a reason why setting this to 8 works for Firefox or was it with testing that this was found to be the value that works on Firefox?

If there is a tangible reason, could we maybe add a comment here for a quick explanation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll add a comment, but yes i found it by experimenting. 15 was the max playback rate in chrome and 8 was the max in firefox. i didn't try to figure out the max in safari, but 15 worked and that's higher than anyone will be able to detect anyhow.

const playbackRate = Math.max(minPlaybackRate,
Math.min(
maxPlaybackRate,
bitRateToSampleRate(state.outputBitRate) / bitRateToSampleRate(state.audioURLBitRate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question might be best asked as what is the relationship between bit rate and sample rate, but I'm going to put here what I'm thinking through.

I know you say below that the browser APIs don't allow us to adjust the sample rate directly and can map in between sample rate and bit rate. If that's the case, why do we need the playback to be the sample rate? Can we just have playback rate be the bit rate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bit rate is the sample rate times the bit depth. I couldn't immediately find what the bit depth of the browser's media recorder is, but in any case the sample rate should have a linear relationship with the bit rate. personally, i don't think it's super important that we accurately match the sample rate of the hardware since your computer has a different mic and is going to sound better no matter what; all that matters is that lower sample rate sounds worse and higher sounds better.

the reason i convert back and forth is because we still want the playback rate to be based on the sample rate that the user set. for example, if they set the recording rate to be 11000 and the playback rate to be 22000, the playback rate should be 2.0, but because the bit rate scales differently it would end up being much higher if we used the bit rate instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, that makes sense. Thanks!

Copy link
Contributor

@srietkerk srietkerk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@riknoll riknoll merged commit eb1498e into master Jul 30, 2024
6 checks passed
@riknoll riknoll deleted the dev/riknoll/microphone-sim branch July 30, 2024 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recording extension: Set Sample Rate block not implemented in simulator
3 participants