File tree Expand file tree Collapse file tree 4 files changed +42
-8
lines changed
examples/tests/conversion/numberformat-converter Expand file tree Collapse file tree 4 files changed +42
-8
lines changed Original file line number Diff line number Diff line change
1
+ #include " AudioTools.h"
2
+ #include " AudioTools/AudioLibs/AudioBoardStream.h"
3
+
4
+ AudioInfo info (48000 , 2 , 16 );
5
+ AudioInfo info_to (48000 , 2 , 32 );
6
+ SineWaveGenerator<int16_t > sineWave (32000 ); // subclass of SoundGenerator with max amplitude of 32000
7
+ GeneratedSoundStream<int16_t > sound (sineWave); // Stream generated from sine wave
8
+ AudioBoardStream out (AudioKitEs8388V1);
9
+ NumberFormatConverterStream nfc (out);
10
+ StreamCopy copier (nfc, sound); // copies sound into i2s
11
+
12
+ // Arduino Setup
13
+ void setup (void ) {
14
+ // Open Serial
15
+ Serial.begin (115200 );
16
+ AudioToolsLogger.begin (Serial, AudioToolsLogLevel::Info);
17
+
18
+ nfc.begin (info.bits_per_sample , info_to.bits_per_sample );
19
+
20
+ // start I2S
21
+ Serial.println (" starting I2S..." );
22
+ auto config = out.defaultConfig (TX_MODE);
23
+ config.copyFrom (info_to);
24
+ out.begin (config);
25
+
26
+ // Setup sine wave
27
+ sineWave.begin (info, N_B4);
28
+ Serial.println (" started..." );
29
+ }
30
+
31
+ // Arduino loop - copy sound to out
32
+ void loop () {
33
+ copier.copy ();
34
+ }
Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ class DecoderL8 : public AudioDecoder {
93
93
if (!is_signed) {
94
94
tmp -= 129 ;
95
95
}
96
- return NumberConverter::clipT<int32_t , int16_t >(tmp * 258 );
96
+ return NumberConverter::clipT<int16_t >(tmp * 258 );
97
97
}
98
98
99
99
virtual operator bool () override { return p_print!=nullptr ; }
@@ -168,7 +168,7 @@ class EncoderL8 : public AudioEncoder {
168
168
operator bool () override { return is_open; }
169
169
170
170
int16_t convertSample (int16_t sample) {
171
- int16_t tmp = NumberConverter::clipT<int16_t , int8_t >(sample / 258 );
171
+ int16_t tmp = NumberConverter::clipT<int8_t >(sample / 258 );
172
172
if (!is_signed) {
173
173
tmp += 129 ;
174
174
// clip to range
Original file line number Diff line number Diff line change @@ -224,7 +224,7 @@ class Synthesizer : public SoundGenerator<int16_t> {
224
224
// prevent divide by zero
225
225
int result = 0 ;
226
226
if (count>0 ){
227
- result = NumberConverter::clipT<int , int16_t >(total / count);
227
+ result = NumberConverter::clipT<int16_t >(total / count);
228
228
}
229
229
return result;
230
230
}
Original file line number Diff line number Diff line change @@ -348,9 +348,9 @@ class NumberConverter {
348
348
}
349
349
350
350
// / Clips the value to avoid any over or underflows
351
- template <typename From, typename T>
352
- static T clipT (From value){
353
- From mv = maxValue ( sizeof (T)* 8 );
351
+ template <typename T>
352
+ static T clipT (float value){
353
+ float mv = maxValueT<T>( );
354
354
if (value > mv){
355
355
return mv;
356
356
} else if (value < -mv){
@@ -396,7 +396,7 @@ class NumberConverter {
396
396
template <typename FromT, typename ToT>
397
397
static ToT convert (FromT value){
398
398
int64_t value1 = value;
399
- return clipT<FromT, ToT>(value1 * maxValueT<ToT>() / maxValueT<FromT>());
399
+ return clipT<ToT>(value1 * maxValueT<ToT>() / maxValueT<FromT>());
400
400
}
401
401
402
402
// / Convert an array of int types
@@ -405,7 +405,7 @@ class NumberConverter {
405
405
float factor = static_cast <float >(maxValueT<ToT>()) / maxValueT<FromT>();
406
406
float vol_factor = factor * vol;
407
407
for (int j=0 ;j<samples;j++){
408
- to[j] = clipT<FromT, ToT>(vol_factor * from[j]);
408
+ to[j] = clipT<ToT>(vol_factor * from[j]);
409
409
}
410
410
}
411
411
You can’t perform that action at this time.
0 commit comments