Skip to content

Commit 35c26b0

Browse files
committed
codal_app/microbithal_microphone: Store the current recording length.
Signed-off-by: Damien George <damien@micropython.org>
1 parent f4e5923 commit 35c26b0

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/codal_app/microbithal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ int microbit_hal_compass_get_heading(void);
156156
void microbit_hal_microphone_init(void);
157157
void microbit_hal_microphone_set_threshold(int kind, int value);
158158
int microbit_hal_microphone_get_level(void);
159-
void microbit_hal_microphone_start_recording(uint8_t *buf, size_t len, int rate);
159+
void microbit_hal_microphone_start_recording(uint8_t *buf, size_t max_len, size_t *cur_len, int rate);
160160
bool microbit_hal_microphone_is_recording(void);
161161
void microbit_hal_microphone_stop_recording(void);
162162

src/codal_app/microbithal_microphone.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MyStreamRecording : public DataSink
4343

4444
public:
4545
uint8_t *dest;
46-
size_t dest_pos;
46+
size_t *dest_pos_ptr;
4747
size_t dest_max;
4848
bool request_stop;
4949

@@ -65,18 +65,18 @@ int MyStreamRecording::pullRequest()
6565
{
6666
ManagedBuffer data = this->upStream.pull();
6767

68-
size_t n = MIN((size_t)data.length(), this->dest_max - this->dest_pos);
68+
size_t n = MIN((size_t)data.length(), this->dest_max - *this->dest_pos_ptr);
6969
if (n == 0 || this->request_stop) {
7070
this->upStream.disconnect();
7171
this->request_stop = false;
7272
} else {
7373
// Copy and convert signed 8-bit to unsigned 8-bit data.
7474
const uint8_t *src = data.getBytes();
75-
uint8_t *dest = this->dest + this->dest_pos;
75+
uint8_t *dest = this->dest + *this->dest_pos_ptr;
7676
for (size_t i = 0; i < n; ++i) {
7777
*dest++ = *src++ + 128;
7878
}
79-
this->dest_pos += n;
79+
*this->dest_pos_ptr += n;
8080
}
8181

8282
return DEVICE_OK;
@@ -110,7 +110,7 @@ int microbit_hal_microphone_get_level(void) {
110110
return value;
111111
}
112112

113-
void microbit_hal_microphone_start_recording(uint8_t *buf, size_t len, int rate) {
113+
void microbit_hal_microphone_start_recording(uint8_t *buf, size_t max_len, size_t *cur_len, int rate) {
114114
if (splitterChannel == NULL) {
115115
splitterChannel = uBit.audio.splitter->createChannel();
116116
splitterChannel->setFormat(DATASTREAM_FORMAT_8BIT_UNSIGNED);
@@ -131,8 +131,9 @@ void microbit_hal_microphone_start_recording(uint8_t *buf, size_t len, int rate)
131131
}
132132

133133
recording->dest = buf;
134-
recording->dest_pos = 0;
135-
recording->dest_max = len;
134+
recording->dest_pos_ptr = cur_len;
135+
*recording->dest_pos_ptr = 0;
136+
recording->dest_max = max_len;
136137
recording->request_stop = false;
137138

138139
splitterChannel->connect(*recording);

0 commit comments

Comments
 (0)