Skip to content

Commit c1542ba

Browse files
committed
NumberFormatConverter wrong clipping
1 parent 1464fc0 commit c1542ba

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

src/AudioTools/AudioCodecs/CodecL8.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class DecoderL8 : public AudioDecoder {
9393
if (!is_signed) {
9494
tmp -= 129;
9595
}
96-
return NumberConverter::clipT<int32_t, int16_t>(tmp * 258);
96+
return NumberConverter::clipT<int16_t>(tmp * 258);
9797
}
9898

9999
virtual operator bool() override { return p_print!=nullptr; }
@@ -168,7 +168,7 @@ class EncoderL8 : public AudioEncoder {
168168
operator bool() override { return is_open; }
169169

170170
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);
172172
if (!is_signed) {
173173
tmp += 129;
174174
// clip to range

src/AudioTools/CoreAudio/AudioEffects/Synthesizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class Synthesizer : public SoundGenerator<int16_t> {
224224
// prevent divide by zero
225225
int result = 0;
226226
if (count>0){
227-
result = NumberConverter::clipT<int, int16_t>(total / count);
227+
result = NumberConverter::clipT<int16_t>(total / count);
228228
}
229229
return result;
230230
}

src/AudioTools/CoreAudio/AudioTypes.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ class NumberConverter {
348348
}
349349

350350
/// 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>();
354354
if (value > mv){
355355
return mv;
356356
} else if (value < -mv){
@@ -396,7 +396,7 @@ class NumberConverter {
396396
template <typename FromT, typename ToT>
397397
static ToT convert(FromT value){
398398
int64_t value1 = value;
399-
return clipT<FromT,ToT>(value1 * maxValueT<ToT>() / maxValueT<FromT>());
399+
return clipT<ToT>(value1 * maxValueT<ToT>() / maxValueT<FromT>());
400400
}
401401

402402
/// Convert an array of int types
@@ -405,7 +405,7 @@ class NumberConverter {
405405
float factor = static_cast<float>(maxValueT<ToT>()) / maxValueT<FromT>();
406406
float vol_factor = factor * vol;
407407
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]);
409409
}
410410
}
411411

0 commit comments

Comments
 (0)