Skip to content

audio refactor: SplitterChannel::pullInto(rawBuffer, length) doesn't pull data #495

@microbit-carlos

Description

@microbit-carlos

Related:

I wasn't looking at the data itself when debugging #494, but it turns out it doesn't really copy any data into the buffer either.

The example programme has been modified slightly to simply pull 256 bytes from the StreamNormaliser and print them to serial.

Click here to view source code

Built from codal-microbit-v2 commit 8256835, and the main branches from codal-core (a2f5051dc60f89276546517f1077e75659ae6741) and codal-nrf52 (1fbb7240290fe36a55c61378f5cdeb7640f3ec4a) at the time.

{
    "target": {
        "name": "codal-microbit-v2",
        "url": "https://github.com/lancaster-university/codal-microbit-v2",
        "branch": "master",
        "dev": true,
        "type": "git"
    },
    "config":{
        "MICROBIT_BLE_ENABLED" : 0,
        "MICROBIT_BLE_PAIRING_MODE": 0,
        "LEVEL_DETECTOR_SPL_8BIT_000_POINT": 52.0,
        "LEVEL_DETECTOR_SPL_8BIT_255_POINT": 100.0
    }
}
#include "MicroBit.h"

MicroBit uBit;

const size_t MIC_BUFFER_SIZE = 256;
uint8_t micBuffer[MIC_BUFFER_SIZE] = {0};


class DataSinkCapturer : public DataSink {
public:
    SplitterChannel *upstream;
    uint8_t *dest;
    int dest_pos_ptr;
    size_t dest_max;

    DataSinkCapturer(SplitterChannel *source) : upstream(source) { }
    virtual ~DataSinkCapturer() { }

    bool isCapturing() {
        return this->upstream->isConnected();
    }

    void captureAsync(uint8_t *buf, size_t max_len) {
        this->dest = buf;
        this->dest_max = max_len;
        this->dest_pos_ptr = 0;
        upstream->connect(*this);
    }

    virtual int pullRequest() {
        uint8_t *pull_buf = this->dest + this->dest_pos_ptr;
        int n = this->dest_max - this->dest_pos_ptr;

        if (n > 0) {
            n = this->upstream->pullInto(pull_buf, n) - pull_buf;
        }

        if (n <= 0) {
            this->upstream->disconnect();
        } else {
            this->dest_pos_ptr += n;
        }

        return DEVICE_OK;
    }
};


int main() {
    uBit.init();

    uBit.serial.printf("\nMicrophone data capture Test\n");
    DataSinkCapturer *micDataCapturer = new DataSinkCapturer(uBit.audio.splitter->createChannel());
    micDataCapturer->captureAsync(micBuffer, MIC_BUFFER_SIZE);
    while (micDataCapturer->isCapturing()) {
        uBit.sleep(10);
    }
    uBit.serial.printf("Captured %d samples, expected %d samples:", micDataCapturer->dest_pos_ptr, MIC_BUFFER_SIZE);

    for (size_t i = 0; i < MIC_BUFFER_SIZE; i++) {
        if (i % 32 == 0) uBit.serial.putc('\n');
        uBit.serial.printf("%d,", micBuffer[i]);
    }
    for(;;);
}

MICROBIT.hex.zip

Microphone data capture Test
Captured 256 samples, expected 256 samples:
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions