@@ -39,21 +39,21 @@ static void level_detector_event_handler(Event evt) {
39
39
class MyStreamRecording : public DataSink
40
40
{
41
41
public:
42
- DataSource & upStream;
42
+ SplitterChannel * upStream;
43
43
44
44
public:
45
45
uint8_t *dest;
46
46
size_t *dest_pos_ptr;
47
47
size_t dest_max;
48
48
bool request_stop;
49
49
50
- MyStreamRecording (DataSource & source);
50
+ MyStreamRecording (SplitterChannel * source);
51
51
virtual ~MyStreamRecording ();
52
52
53
53
virtual int pullRequest ();
54
54
};
55
55
56
- MyStreamRecording::MyStreamRecording ( DataSource & source ) : upStream( source )
56
+ MyStreamRecording::MyStreamRecording (SplitterChannel * source) : upStream(source)
57
57
{
58
58
}
59
59
@@ -63,18 +63,20 @@ MyStreamRecording::~MyStreamRecording()
63
63
64
64
int MyStreamRecording::pullRequest ()
65
65
{
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
+ }
67
72
68
- size_t n = MIN ((size_t )data.length (), this ->dest_max - *this ->dest_pos_ptr );
69
73
if (n == 0 || this ->request_stop ) {
70
- this ->upStream . disconnect ();
74
+ this ->upStream -> disconnect ();
71
75
this ->request_stop = false ;
72
76
} 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.
76
78
for (size_t i = 0 ; i < n; ++i) {
77
- *dest++ = *src++ + 128 ;
79
+ pull_buf[i] += 128 ;
78
80
}
79
81
*this ->dest_pos_ptr += n;
80
82
}
@@ -120,7 +122,7 @@ void microbit_hal_microphone_start_recording(uint8_t *buf, size_t max_len, size_
120
122
splitterChannel->requestSampleRate (rate);
121
123
122
124
if (recording == NULL ) {
123
- recording = new MyStreamRecording (* splitterChannel);
125
+ recording = new MyStreamRecording (splitterChannel);
124
126
} else {
125
127
if (microbit_hal_microphone_is_recording ()) {
126
128
microbit_hal_microphone_stop_recording ();
0 commit comments