Skip to content

call scaled microphone API instead of using our own scaling #6194

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

riknoll
Copy link
Member

@riknoll riknoll commented May 8, 2025

fixes #4381

see linked issue for the discussion here. swapping out our custom scaling for the scaling that's now built in to the ubit object

@microbit-carlos FYI

@riknoll riknoll requested a review from a team May 8, 2025 21:03
@microbit-carlos
Copy link
Collaborator

microbit-carlos commented May 9, 2025

Thanks @riknoll!

I just realised that to keep the current behaviour exactly the same we need to set these values in the codal.json file:

    "config":{
        "LEVEL_DETECTOR_SPL_8BIT_000_POINT": 52.0,
        "LEVEL_DETECTOR_SPL_8BIT_255_POINT": 120.0,
    }

This is because the CODAL default values are currently different, so setting these macros configures the sound level with the same range as before.

The underlying issue with #4381 was that the Sound Level detector never returns a value above ~100 dB (blowing directly on the microphone should saturate it and the max value is around that), so having the 8-bit max point at 120 dB means the sound level block output was never above ~191.

If we set 8-bit max point to 100 dB that will also change the scaling, so programmes that before outputted an 8-bit value of ~150 now might output something like ~180. Since this has been the behaviour the past few years, I'll check with the rest of the team if we would consider that a breaking change at this point, but in the meantime the PR can set the min and max to 52 and 120.

@riknoll
Copy link
Member Author

riknoll commented May 9, 2025

@microbit-carlos if we are just going to set the scaling back to the original value, is there a point to merging this at all?

@microbit-carlos
Copy link
Collaborator

One of the reasons to have this completely managed by CODAL is because we are doing an audio pipeline refactor and that affects the decibel calculations. As we haven't really exposed the dB sound level in the MakeCode nor the MicroPython API that won't affect most users, but we do need to tweak the 8-bit min and max values to match the current implementation.

Ideally we'd set MakeCode to use both these default values, and have it completely managed by CODAL, but it wasn't until now that I realised that fixing this bug might break some of the existing user projects.

Btw, I've just release codal v0.2.70 to ensure the micro:bit target uses the default min value as 52 and max value as 100 (I thought this was done in release 0.2.44, but looks like we didn't get around to it).
If the codal tag used in MakeCode is updated to v0.2.70 we can leave the default for LEVEL_DETECTOR_SPL_8BIT_000_POINT (before this tag the default value was set to 35) and only set LEVEL_DETECTOR_SPL_8BIT_255_POINT to 120 in codal.json.

So, it's not ideal to have the max value configured differently in MakeCode, but based initial testing of the audio refactor branch I think we might only need to tweak the min value, and so that would mean that future CODAL release would not need to be accompanied by a pxt code change.

@microbit-carlos
Copy link
Collaborator

microbit-carlos commented May 9, 2025

The v0.2.70 mostly includes minor bug fixes and CI updates, although it does also include a couple of patches to ensure floating point operations don't unnecessarily perform double operations and that saves about half a kilobyte of flash, and should also provide some minor performance improvements.

https://github.com/lancaster-university/codal-microbit-v2/blob/master/Changelog.md#v0270

@microbit-carlos
Copy link
Collaborator

@riknoll good news, after careful consideration the team is happy to include the bugfix as well, so we can let CODAL manage both the min and max values. It still needs the latest v0.2.70 tag, but there is no need to set up any additional config options.

@riknoll
Copy link
Member Author

riknoll commented May 14, 2025

@microbit-carlos that's great! i was actually struggling on figuring out where to inject those options in our toolchain anyhow

@microbit-carlos
Copy link
Collaborator

FYI, the codal version has been updated already via #6243, so this PR can be merged without having to change pxtarget.json.

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.

Microphone level maxes out at ~191 on hardware
4 participants