lancaster-university / codal-microbit-v2

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

Halving the recording sampling rate doesn't seem to double the max length #322

Closed microbit-carlos closed 1 year ago

microbit-carlos commented 1 year ago

Both these hex files record sound until the memory is full, one configured at 5.5K and the other at 11K, but the both only record less than 5 seconds of audio. 5500.hex.zip 11000.hex.zip

It's possible I'm not configuring the pipeline correctly, is this code correct?

#include "MicroBit.h"
#include "StreamRecording.h"

MicroBit uBit;

static const int SAMPLE_RATE = 11000;

static void onButtonLogo(MicroBitEvent) {
    static SplitterChannel *splitterChannel = uBit.audio.splitter->createChannel();
    splitterChannel->requestSampleRate(SAMPLE_RATE);
    static StreamRecording *recording = new StreamRecording(*splitterChannel);
    static MixerChannel *channel = uBit.audio.mixer.addChannel(*recording, SAMPLE_RATE);

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

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

    recording->record();
    bool showR = true;
    while (uBit.logo.isPressed() && recording->isRecording()) {
        if (showR)
            uBit.display.print("R");
        else
            uBit.display.clear();
        uBit.sleep(150);
    }
    recording->stop();

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

int main() {
    uBit.init();

    uBit.messageBus.listen(MICROBIT_ID_LOGO, MICROBIT_BUTTON_EVT_DOWN, onButtonLogo);

    while (true) {
        uBit.sleep(100);
    }
}
JohnVidler commented 1 year ago

The code here is correct, but the current implementation of StreamRecording simply collects upto REC_MAX_BUFFERS in an array, rather than dynamically tracking blocks until it hits a specific memory limit.

I'll be updating this shortly so that rather than a buffer limit, we use a soft memory limit.

JohnVidler commented 1 year ago

As a note to myself:

microbit-carlos commented 1 year ago

Ah, thanks John! I mistakenly thought it allocated buffers until there wasn't more memory available.

JohnVidler commented 1 year ago

Master now includes the changes to use a linked list of buffers, rather than a static array of buffers, so should be able to adjust to different packet sizes for different sample rates, fixing this issue.

Pending further testing, this should now be fixed.

microbit-carlos commented 1 year ago

I've tested this with the latest commit 02a85fa86a00ac204cbd2a4caacd5a964e1cf26e, and reducing the sampling rate in the example doesn't increase the recording time. Going from 11_000 to 1_100 decreases the sound quality but still has around 5 seconds of audio.

microbit-carlos commented 1 year ago

I wasn't using "dev": true in the codal.json file, so I wasn't using the latest commits from codal-core. With the latest I can confirm this now works 🎉