Skip to content

Commit 154d7e6

Browse files
committed
codal_app/microbithal_microphone: Make input streaming use pullInto.
Signed-off-by: Damien George <[email protected]>
1 parent 61a3fd2 commit 154d7e6

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/codal_app/microbithal_microphone.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ static void level_detector_event_handler(Event evt) {
3939
class MyStreamRecording : public DataSink
4040
{
4141
public:
42-
DataSource &upStream;
42+
SplitterChannel *upStream;
4343

4444
public:
4545
uint8_t *dest;
4646
size_t *dest_pos_ptr;
4747
size_t dest_max;
4848
bool request_stop;
4949

50-
MyStreamRecording(DataSource &source);
50+
MyStreamRecording(SplitterChannel *source);
5151
virtual ~MyStreamRecording();
5252

5353
virtual int pullRequest();
5454
};
5555

56-
MyStreamRecording::MyStreamRecording( DataSource &source ) : upStream( source )
56+
MyStreamRecording::MyStreamRecording(SplitterChannel *source) : upStream(source)
5757
{
5858
}
5959

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

6464
int MyStreamRecording::pullRequest()
6565
{
66-
ManagedBuffer data = this->upStream.pull();
66+
uint8_t *pull_buf = this->dest + *this->dest_pos_ptr;
67+
size_t n = this->dest_max - *this->dest_pos_ptr;
68+
69+
if (n > 0) {
70+
n = this->upStream->pullInto(pull_buf, n) - pull_buf;
71+
}
6772

68-
size_t n = MIN((size_t)data.length(), this->dest_max - *this->dest_pos_ptr);
6973
if (n == 0 || this->request_stop) {
70-
this->upStream.disconnect();
74+
this->upStream->disconnect();
7175
this->request_stop = false;
7276
} else {
73-
// Copy and convert signed 8-bit to unsigned 8-bit data.
74-
const uint8_t *src = data.getBytes();
75-
uint8_t *dest = this->dest + *this->dest_pos_ptr;
77+
// Convert signed 8-bit to unsigned 8-bit data.
7678
for (size_t i = 0; i < n; ++i) {
77-
*dest++ = *src++ + 128;
79+
pull_buf[i] += 128;
7880
}
7981
*this->dest_pos_ptr += n;
8082
}
@@ -120,7 +122,7 @@ void microbit_hal_microphone_start_recording(uint8_t *buf, size_t max_len, size_
120122
splitterChannel->requestSampleRate(rate);
121123

122124
if (recording == NULL) {
123-
recording = new MyStreamRecording(*splitterChannel);
125+
recording = new MyStreamRecording(splitterChannel);
124126
} else {
125127
if (microbit_hal_microphone_is_recording()) {
126128
microbit_hal_microphone_stop_recording();

0 commit comments

Comments
 (0)