Skip to content
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

A StreamRecording with low splitter sampling rate fails to stop recording #427

Open
microbit-carlos opened this issue May 1, 2024 · 1 comment
Assignees
Milestone

Comments

@microbit-carlos
Copy link
Collaborator

microbit-carlos commented May 1, 2024

With this example code, setting the recording rate to 320 samples per second works, but setting it to 319 hangs when recording and never "stops".

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

MicroBit uBit;

const int SAMPLE_RATE = 319;
const int BUFFER_LEN = SAMPLE_RATE * 5;

int main() {
    uBit.init();

    SplitterChannel *splitterChannel = uBit.audio.splitter->createChannel();
    splitterChannel->requestSampleRate( SAMPLE_RATE );
    StreamRecording *recording = new StreamRecording(*splitterChannel, BUFFER_LEN);
    // Setting the mixer channel to a different sampling rate doesn't make a difference
    MixerChannel *recordingChannel = uBit.audio.mixer.addChannel(*recording, SAMPLE_RATE);
    recordingChannel->setVolume(75.0);
    MicroBitAudio::requestActivation();

    while (true) {
        if (uBit.buttonA.isPressed()) {
            recording->erase();
            recording->recordAsync();
            while (recording->isRecording()) {
                uBit.display.print("R");
                uBit.sleep(5);
            }
            uBit.display.clear();
        } else if (uBit.buttonB.isPressed()) {
            recording->playAsync();
            while (recording->isPlaying()) {
                uBit.display.print("P");
                uBit.sleep(20);
            }
            uBit.display.clear();
        }
        uBit.sleep(200);
    }
}

To replicate:

  • Compile this example or download 319.hex & flash it to the micro:bit
  • Press button A to start recording
    • Recording should only last 5 seconds
    • The letter R should show on the display for as long as the device is recording
  • No matter how long you wait, the recording never stop
    • There isn't a 020 panic (memory error) thrown either, so if it is indeed recording it must be doing it within the allocated buffer
@martinwork
Copy link
Collaborator

Single byte buffers are quietly rejected, though the format has one byte per sample
https://github.com/lancaster-university/codal-core/blob/df05db9e15499bd8906618192a4d482e3836c62f/source/streams/StreamRecording.cpp#L96

data == ManagedBuffer() doesn't seem to add anything.

To reject buffers without a whole sample, the test could be
if( data.length() < DATASTREAM_FORMAT_BYTES_PER_SAMPLE( this->upStream.getFormat()) {

With this, the sample rate borderline for "records forever" moves to 142/143, as in #428

With low sample rates, I think StreamRecording will build a list of tiny buffers, with a 12+ byte overhead for each node
https://github.com/lancaster-university/codal-core/blob/df05db9e15499bd8906618192a4d482e3836c62f/inc/streams/StreamRecording.h#L19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants