lancaster-university / codal-microbit-v2

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

New OOB recording and playback changed behaviour between CODAL v0.2.52 and v0.2.55 #344

Closed microbit-carlos closed 1 year ago

microbit-carlos commented 1 year ago

A PR in the samples repo has been opened with the OOB code and compiling it with the latest CODAL results different behaviour:

static void onButtonLogo(MicroBitEvent) {
    DMESG("Button Logo");

    int sampleRate = 11000;
    static SplitterChannel *splitterChannel = uBit.audio.splitter->createChannel();
    splitterChannel->requestSampleRate( sampleRate );

    // Uncomment these two lines and comment out the *recording declaration after them to insert a low-pass-filter.
    // static LowPassFilter *lowPassFilter = new LowPassFilter(*splitterChannel, 0.812313f, false);
    // static StreamRecording *recording = new StreamRecording(*lowPassFilter);
    static StreamRecording *recording = new StreamRecording(*splitterChannel);

    static MixerChannel *channel = uBit.audio.mixer.addChannel(*recording, sampleRate);

    // uBit.audio.processor->setGain(0.08f);  // Default gain
    // uBit.audio.processor->setGain(0.16f);  // Double

    MicroBitAudio::requestActivation();
    channel->setVolume(75.0);
    uBit.audio.mixer.setVolume(1023);

    uBit.display.clear();
    uBit.audio.levelSPL->setUnit(LEVEL_DETECTOR_SPL_8BIT);

    recording->record();
    while (uBit.logo.isPressed() && recording->isRecording()) {
        int audioLevel = (int)uBit.audio.levelSPL->getValue();
        DMESG("Audio level: %d", audioLevel);
        plotBarGraph(audioLevel, 255);
        uBit.sleep(5);
    }
    // At this point either the logo has been released or the recording is done
    recording->stop();
    // Note: The CODAL_STREAM_IDLE_TIMEOUT_MS config has been set in the
    // codal.json file to reduce the time it takes for the microphone LED
    // to turn off after the recording is done.
    // This area is still being tweaked in CODAL and the codal.json config
    // should be removed in the future to use the CODAL default.
    uBit.display.clear();

    // If the recording is done but the logo is still pressed we want to
    // hold back the playback until the logo has been released
    while (uBit.logo.isPressed());

    recording->play();
    while (recording->isPlaying()) {
        uBit.sleep(20);
    }
    recording->erase();
}

MICROBIT.hex.zip

When pressing the logo the recording takes place but the bar plot is not running and the recording will always run until the memory is full.

So it looks like maybe StreamRecording::record() is no longer async by default?

Something I'll need to look into, but opening an issue here as a future task in case I have to re-prioritise other things.

microbit-carlos commented 1 year ago

Okay, so record() has been made blocking and recordAsync() added in: https://github.com/lancaster-university/codal-core/commit/94cbd3933c61924677fc1f2255eaf61bd175a0d1