@@ -200,41 +200,29 @@ class SAM {
200
200
}
201
201
}
202
202
203
- // / Defines the bits per sample (default 8)
204
- void setOutputBitsPerSample (int bits_per_sample){
205
- SAM_LOG (" setOutputBitsPerSample: %d" , bits_per_sample);
206
- if ((bits_per_sample==8 || bits_per_sample==16 ) &&arduino_output->bitsPerSample ()==-1 ){
207
- this ->bits_per_sample = bits_per_sample;
208
- arduino_output->setBitsPerSample (bits_per_sample);
209
- } else {
210
- SAM_LOG (" Bits Per Sample is not supported for this output type" );
211
- }
212
- }
213
-
214
- // / Enableds/disables the scaling to 16 bits - by default this is on
215
- void setTrue16Bits (bool active){
216
- true16Bits = active;
217
- }
218
203
219
204
// / Provides the sample rate (44100)
220
205
static int sampleRate () {
221
206
return SAMOutputBase::sampleRate ();
222
207
}
223
208
224
-
209
+ // / Provides the bits per sample
225
210
int bitsPerSample () {
226
211
return bits_per_sample;
227
212
}
228
213
214
+ // / Provides the number of channels
229
215
int channels () {
230
216
return channel_count;
231
217
}
232
218
233
219
protected:
234
220
SAMOutputBase *arduino_output=nullptr ;
235
- int bits_per_sample = 8 ;
221
+ int bits_per_sample = 16 ;
236
222
int channel_count = 1 ;
237
- bool true16Bits = true ;
223
+ uint8_t audio[2 ];
224
+ int16_t *p_audio = (int16_t *)&audio;
225
+ uint8_t audio_byte = 0 ;
238
226
239
227
// / Used to feed the audio result back to this class
240
228
static void outputByteCallback (void *cbdata, unsigned char b) {
@@ -248,51 +236,32 @@ class SAM {
248
236
SAM_LOG (" setOutput" );
249
237
arduino_output = out;
250
238
251
- // synchronize output definition with arduino_output
239
+ // synchronize channels definition with arduino_output: I2S must be 2 therefore we try to get it from arduino_output first
252
240
if (arduino_output->channels ()!=-1 ){
253
241
this ->channel_count = arduino_output->channels ();
254
242
} else {
255
243
arduino_output->setChannels (channel_count);
256
244
}
257
- if (arduino_output->bitsPerSample ()!=-1 ){
258
- this ->bits_per_sample = arduino_output->bitsPerSample ();
259
- } else {
260
- arduino_output->setBitsPerSample (bits_per_sample);
261
- }
262
245
SAM_LOG (" -> channel_count: %d" ,this ->channel_count );
263
246
SAM_LOG (" -> bits_per_sample: %d" ,this ->bits_per_sample );
264
247
}
265
248
266
- // / Writes the data to the output. The data is provided as unsinged byte, so the values are between 0 and 256
249
+ // / Writes the data to the output. The data is provided on 1 channel as unsinged byte of int16 values
267
250
void write (uint8_t in) {
268
251
SAM_LOG (" SAM::write bps:%d / channels:%d" ,bits_per_sample, channel_count);
269
- // filter out noise
270
- uint8_t b = filter (in);
252
+ audio[audio_byte++] = in;
271
253
272
- // convert data
273
- if (bits_per_sample==8 ){
274
- uint8_t sample[channel_count];
275
- for (int j=0 ;j<channel_count;j++){
276
- sample[j] = b;
277
- }
278
- SAM_LOG (" writing to %s" , arduino_output->name ());
279
- arduino_output->write ((byte*)sample, channel_count);
280
- } else if (bits_per_sample==16 ) {
281
- int16_t s16 = b;
282
- if (true16Bits) {
283
- // convert to signed
284
- s16 -= 128 ;
285
- // convert to int16
286
- s16 *= 128 ;
287
- }
254
+ if (audio_byte==2 ){
255
+ audio_byte = 0 ;
256
+ int16_t value = *p_audio;
257
+
258
+ // provide multiple channels if necessary
288
259
int16_t sample[channel_count];
289
- for (int j=0 ;j<channel_count; j++){
290
- sample[j] = s16 ;
260
+ for (int j=0 ;j<channel_count;j++){
261
+ sample[j] = value ;
291
262
}
292
263
SAM_LOG (" writing to %s" , arduino_output->name ());
293
264
arduino_output->write ((byte*)sample, channel_count*2 );
294
- } else {
295
- SAM_LOG (" Error - invalid bits_per_sample" );
296
265
}
297
266
}
298
267
@@ -339,19 +308,6 @@ class SAM {
339
308
340
309
return true ;
341
310
}
342
-
343
- // filter oscillating noise 80 112
344
- uint8_t filter (uint8_t b){
345
- static bool filter_active = false ;
346
- bool last_filter_active = filter_active;
347
-
348
- if (b==80 || b==112 ){
349
- filter_active = true ;
350
- } else {
351
- filter_active = false ;
352
- }
353
- return last_filter_active && filter_active ? 128 : b;
354
- }
355
311
};
356
312
357
313
void printLog (char * msg){
0 commit comments