Skip to content

Commit b63db61

Browse files
committed
Unsigned support for NumberFormatConverterStreamT
1 parent 2d5d09a commit b63db61

File tree

13 files changed

+168
-24
lines changed

13 files changed

+168
-24
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "AudioTools.h"
2+
3+
using target_t = uint32_t; // uint8_t, int8_t, int16_t, uint16_t, int24_t, uint32_t, int32_t, FloatAudio
4+
SineWaveGenerator<int16_t> sineWave; // subclass of SoundGenerator with max amplitude of 32000
5+
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
6+
CsvOutput<target_t> out(Serial, sound.audioInfo().channels);
7+
NumberFormatConverterStreamT<int16_t, target_t> nfc(out);
8+
StreamCopy copier(nfc, sound); // copies sound into i2s
9+
10+
// Arduino Setup
11+
void setup(void) {
12+
// Open Serial
13+
Serial.begin(115200);
14+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
15+
16+
nfc.begin();
17+
out.begin();
18+
sineWave.begin();
19+
20+
// Setup sine wave
21+
sineWave.setFrequency(N_B4);
22+
Serial.println("started...");
23+
}
24+
25+
// Arduino loop - copy sound to out
26+
void loop() {
27+
copier.copy();
28+
}

src/AudioConfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,8 @@ using WiFiServerSecure = BearSSL::WiFiServerSecure;
818818
// select int24 implementation
819819
#include "AudioTools/CoreAudio/AudioBasic/Int24_3bytes_t.h"
820820
#include "AudioTools/CoreAudio/AudioBasic/Int24_4bytes_t.h"
821+
#include "AudioTools/CoreAudio/AudioBasic/FloatAudio.h"
822+
821823
namespace audio_tools {
822824
#ifdef USE_3BYTE_INT24
823825
using int24_t = audio_tools::int24_3bytes_t;

src/AudioTools/AudioLibs/FFTDisplay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class FFTDisplay {
5757
}
5858

5959
int getMagnitudeScaled(int x, int max) {
60-
int result = mapFloat(getMagnitude(x), 0, fft_max_magnitude, 0.0f,
60+
int result = mapT<float>(getMagnitude(x), 0, fft_max_magnitude, 0.0f,
6161
static_cast<float>(max));
6262
if (result > max){
6363
LOGD("fft_max_magnitude too small: current value is %f", getMagnitude(x))

src/AudioTools/AudioLibs/LEDOutput.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ void fftLEDOutput(LEDOutputConfig *cfg, LEDOutput *matrix) {
265265
/// Default update implementation which provides the fft result as "barchart"
266266
void volumeLEDOutput(LEDOutputConfig *cfg, LEDOutput *matrix) {
267267
float vol = matrix->getMaxMagnitude();
268-
int currY = mapFloat(vol, 0,
268+
int currY = mapT<float>(vol, 0,
269269
cfg->max_magnitude, 0.0f,
270270
static_cast<float>(cfg->y));
271271
matrix->addColumnBar(currY);

src/AudioTools/AudioLibs/LEDOutputUnoR4.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void fftLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix) {
183183
/// Default update implementation which provides the fft result as "barchart"
184184
void volumeLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix) {
185185
float vol = matrix->getMaxMagnitude();
186-
int currY = mapFloat(vol, 0.0,
186+
int currY = mapT<float>(vol, 0.0,
187187
cfg->max_magnitude, 0.0f,
188188
static_cast<float>(cfg->y));
189189
matrix->addColumnBar(currY);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
#include "AudioConfig.h"
3+
4+
namespace audio_tools {
5+
6+
/***
7+
* A simple float number (in the range of -1.0 to 1.0) the supports the conversion to
8+
* it's corresponding scaled int values.
9+
*/
10+
11+
class FloatAudio {
12+
public:
13+
FloatAudio() = default;
14+
FloatAudio(float in) { this->value = in; }
15+
16+
explicit inline operator int8_t() { return value * 127; }
17+
18+
explicit inline operator int16_t() { return value * 32767; }
19+
20+
inline operator float() { return value; }
21+
22+
// explicit inline operator int24_t() {
23+
// return value * 8388607;
24+
// }
25+
26+
explicit inline operator int32_t() { return value * 2147483647; }
27+
28+
protected:
29+
float value = 0.0f;
30+
};
31+
32+
} // namespace audio_tools
33+
34+
#ifdef USE_TYPETRAITS
35+
36+
namespace std {
37+
template <>
38+
class numeric_limits<audio_tools::FloatAudio> {
39+
public:
40+
static audio_tools::FloatAudio lowest() {
41+
return audio_tools::FloatAudio(-1.0f);
42+
};
43+
static audio_tools::FloatAudio min() {
44+
return audio_tools::FloatAudio(-1.0f);
45+
};
46+
static audio_tools::FloatAudio max() {
47+
return audio_tools::FloatAudio(1.0f);
48+
};
49+
};
50+
} // namespace std
51+
52+
#endif

src/AudioTools/CoreAudio/AudioBasic/Int24_3bytes_t.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,17 @@ class int24_3bytes_t {
129129
};
130130

131131

132-
} // namespace audio_tools
132+
} // namespace audio_tools
133+
134+
#ifdef USE_TYPETRAITS
135+
136+
namespace std {
137+
template<> class numeric_limits<audio_tools::int24_3bytes_t> {
138+
public:
139+
static audio_tools::int24_3bytes_t lowest() {return audio_tools::int24_3bytes_t(-0x7FFFFF);};
140+
static audio_tools::int24_3bytes_t min() {return audio_tools::int24_3bytes_t(-0x7FFFFF);};
141+
static audio_tools::int24_3bytes_t max() {return audio_tools::int24_3bytes_t(0x7FFFFF);};
142+
};
143+
}
144+
145+
#endif

src/AudioTools/CoreAudio/AudioBasic/Int24_4bytes_t.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,17 @@ class int24_4bytes_t {
134134
};
135135

136136

137-
} // namespace audio_tools
137+
} // namespace audio_tools
138+
139+
#ifdef USE_TYPETRAITS
140+
141+
namespace std {
142+
template<> class numeric_limits<audio_tools::int24_4bytes_t> {
143+
public:
144+
static audio_tools::int24_4bytes_t lowest() {return audio_tools::int24_4bytes_t(-0x7FFFFF);};
145+
static audio_tools::int24_4bytes_t min() {return audio_tools::int24_4bytes_t(-0x7FFFFF);};
146+
static audio_tools::int24_4bytes_t max() {return audio_tools::int24_4bytes_t(0x7FFFFF);};
147+
};
148+
}
149+
150+
#endif

src/AudioTools/CoreAudio/AudioOutput.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ template <typename T> class CsvOutput : public AudioOutput {
220220
for (size_t j = 0; j < frameCount; j++) {
221221
for (int ch = 0; ch < cfg.channels; ch++) {
222222
if (out_ptr != nullptr && data_ptr != nullptr) {
223-
int value = *data_ptr;
223+
T value = *data_ptr;
224224
out_ptr->print(value);
225225
}
226226
data_ptr++;

src/AudioTools/CoreAudio/AudioStreamsConverter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,13 @@ class NumberFormatConverterStreamT : public ReformatBaseStream {
362362
TRACED();
363363
if (p_print == nullptr) return 0;
364364
//addNotifyOnFirstWrite();
365+
366+
#ifdef USE_TYPETRAITS
367+
if (std::is_same<TFrom, TTo>::value) return p_print->write(data, len);
368+
#else
365369
if (sizeof(TFrom) == sizeof(TTo)) return p_print->write(data, len);
370+
#endif
371+
366372
size_t samples = len / sizeof(TFrom);
367373
size_t result_size = 0;
368374
TFrom *data_source = (TFrom *)data;

0 commit comments

Comments
 (0)