@@ -117,11 +117,11 @@ class DriverPWMBase {
117
117
// restart with prior definitions
118
118
bool begin (PWMConfig cfg) {
119
119
TRACED ();
120
+ decimation_factor = 0 ;
120
121
audio_config = cfg;
121
122
decimate.setChannels (cfg.channels );
122
123
decimate.setBits (cfg.bits_per_sample );
123
124
decimate.setFactor (decimation ());
124
- LOGI (" decimation: %d" , decimation ());
125
125
frame_size = audio_config.channels * (audio_config.bits_per_sample / 8 );
126
126
if (audio_config.channels > maxChannels ()) {
127
127
LOGE (" Only max %d channels are supported!" , maxChannels ());
@@ -158,9 +158,11 @@ class DriverPWMBase {
158
158
}
159
159
160
160
virtual int availableForWrite () {
161
- return is_blocking_write
162
- ? audio_config.buffer_size
163
- : buffer->availableForWrite () / frame_size * frame_size;
161
+ // return is_blocking_write
162
+ // ? audio_config.buffer_size
163
+ // : buffer->availableForWrite() / frame_size * frame_size;
164
+ // we must not write anything bigger then the buffer size
165
+ return buffer->size () / frame_size * frame_size;
164
166
}
165
167
166
168
// blocking write for an array: we expect a singed value and convert it into a
@@ -186,11 +188,17 @@ class DriverPWMBase {
186
188
187
189
size_t result = buffer->writeArray (data, size);
188
190
if (result != size) {
189
- LOGW (" Could not write all data: %u -> %d" , (unsigned int )size, result);
191
+ LOGW (" Could not write all data: %u -> %d" , (unsigned )size, result);
190
192
}
191
193
// activate the timer now - if not already done
192
194
if (!is_timer_started) startTimer ();
193
- return result * decimation ();
195
+
196
+ // adjust the result by the descimation
197
+ if (isDecimateActive ()) {
198
+ result = result * decimation ();
199
+ }
200
+ // LOGD("write %u -> %u", len, result);
201
+ return result;
194
202
}
195
203
196
204
// When the timer does not have enough data we increase the underflow_count;
@@ -237,6 +245,7 @@ class DriverPWMBase {
237
245
bool is_timer_started = false ;
238
246
bool is_blocking_write = true ;
239
247
Decimate decimate;
248
+ int decimation_factor = 0 ;
240
249
241
250
void deleteBuffer () {
242
251
// delete buffer if necessary
@@ -323,9 +332,23 @@ class DriverPWMBase {
323
332
}
324
333
325
334
// / Decimation factor to reduce the sample rate
326
- virtual int decimation () { return 1 ; }
335
+ virtual int decimation () {
336
+ if (decimation_factor == 0 ){
337
+ for (int j = 1 ; j < 20 ; j++){
338
+ if (audio_config.sample_rate / j <= ANALOG_MAX_SAMPLE_RATE){
339
+ decimation_factor = j;
340
+ LOGI (" Decimation factor: %d" ,j);
341
+ return j;
342
+ }
343
+ }
344
+ decimation_factor = 1 ;
345
+ LOGI (" Decimation factor: %d" , (int )decimation_factor);
346
+ }
347
+
348
+ return decimation_factor;
349
+ }
327
350
};
328
351
329
- } // namespace audio_tools
352
+ } // namespace audio_tool
330
353
331
354
#endif
0 commit comments