Skip to content

Commit

Permalink
codal_app/microbithal_microphone: Make input streaming use pullInto.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Feb 26, 2024
1 parent e86145a commit 87f726c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/codal_app/microbithal_microphone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ static void level_detector_event_handler(Event evt) {
class MyStreamRecording : public DataSink
{
public:
DataSource &upStream;
SplitterChannel *upStream;

public:
uint8_t *dest;
size_t *dest_pos_ptr;
size_t dest_max;
bool request_stop;

MyStreamRecording(DataSource &source);
MyStreamRecording(SplitterChannel *source);
virtual ~MyStreamRecording();

virtual int pullRequest();
};

MyStreamRecording::MyStreamRecording( DataSource &source ) : upStream( source )
MyStreamRecording::MyStreamRecording(SplitterChannel *source) : upStream(source)
{
}

Expand All @@ -63,18 +63,20 @@ MyStreamRecording::~MyStreamRecording()

int MyStreamRecording::pullRequest()
{
ManagedBuffer data = this->upStream.pull();
uint8_t *pull_buf = this->dest + *this->dest_pos_ptr;
size_t n = this->dest_max - *this->dest_pos_ptr;

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

size_t n = MIN((size_t)data.length(), this->dest_max - *this->dest_pos_ptr);
if (n == 0 || this->request_stop) {
this->upStream.disconnect();
this->upStream->disconnect();
this->request_stop = false;
} else {
// Copy and convert signed 8-bit to unsigned 8-bit data.
const uint8_t *src = data.getBytes();
uint8_t *dest = this->dest + *this->dest_pos_ptr;
// Convert signed 8-bit to unsigned 8-bit data.
for (size_t i = 0; i < n; ++i) {
*dest++ = *src++ + 128;
pull_buf[i] += 128;
}
*this->dest_pos_ptr += n;
}
Expand Down Expand Up @@ -120,7 +122,7 @@ void microbit_hal_microphone_start_recording(uint8_t *buf, size_t max_len, size_
splitterChannel->requestSampleRate(rate);

if (recording == NULL) {
recording = new MyStreamRecording(*splitterChannel);
recording = new MyStreamRecording(splitterChannel);
} else {
if (microbit_hal_microphone_is_recording()) {
microbit_hal_microphone_stop_recording();
Expand Down

0 comments on commit 87f726c

Please sign in to comment.