Skip to content

Commit 420ecb9

Browse files
committed
RAWDecoder and RAWEncoder
1 parent a9c5f31 commit 420ecb9

File tree

9 files changed

+188
-20
lines changed

9 files changed

+188
-20
lines changed

examples/examples-stream/streams-i2s-filter-i2s/streams-i2s-filter-i2s.ino

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ I2SStream in;
1313
I2SStream out;
1414

1515
// copy filtered values
16-
ConverterNChannels<int16_t> converter(channels); // Defiles the filter as BaseConverter
17-
FilteredStream<int16_t, float> inFiltered(in); //
18-
StreamCopy copier(out, inFiltered); // copies sound into i2s
16+
FilteredStream<int16_t, float> filtered(in, channels); // Defiles the filter as BaseConverter
17+
StreamCopy copier(out, filtered); // copies sound into i2s
1918

2019
// define FIR filter parameters
2120
float coef[] = { 0.021, 0.096, 0.146, 0.096, 0.021};
@@ -29,8 +28,8 @@ void setup(void) {
2928
AudioLogger::instance().begin(Serial, AudioLogger::Info);
3029

3130
// setup filters for all available channels
32-
inFiltered.setFilter(0, new FIR<float>(coef));
33-
inFiltered.setFilter(1, new FIR<float>(coef));
31+
filtered.setFilter(0, new FIR<float>(coef));
32+
filtered.setFilter(1, new FIR<float>(coef));
3433

3534
// start I2S in
3635
Serial.println("starting I2S...");

src/AudioCodecs/AudioCodecs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "AudioCodecs/CodecWAV.h"
44
#include "AudioCodecs/CodecNOP.h"
5+
#include "AudioCodecs/CodecRAW.h"
56

67
#if defined(USE_HELIX) || defined(USE_DECODERS)
78
#include "AudioCodecs/CodecHelix.h"

src/AudioCodecs/CodecMP3MAD.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MP3DecoderMAD : public AudioDecoder {
4747
}
4848

4949
/// Defines the callback which receives the decoded data
50-
void setDataCallback(libmad::MP3DataCallback cb){
50+
void setAudioDataCallback(libmad::MP3DataCallback cb){
5151
LOGD(LOG_METHOD);
5252
mad->setDataCallback(cb);
5353
}

src/AudioCodecs/CodecRAW.h

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#pragma once
2+
3+
#include "AudioTools/AudioTypes.h"
4+
5+
namespace audio_tools {
6+
7+
const char* raw_mime = "audio/pcm";
8+
9+
/**
10+
* @brief RAWDecoder - Actually this class does no encoding or decoding at all. It just passes on the
11+
* data. The reason that this class exists is that we can use the same processing chain for different
12+
* file types and just replace the decoder.
13+
*
14+
* @author Phil Schatzmann
15+
* @copyright GPLv3
16+
*/
17+
class RAWDecoder : public AudioDecoder {
18+
public:
19+
/**
20+
* @brief Construct a new RAWDecoder object
21+
*/
22+
23+
RAWDecoder(){
24+
LOGD(LOG_METHOD);
25+
}
26+
27+
/**
28+
* @brief Construct a new RAWDecoder object
29+
*
30+
* @param out_stream Output Stream to which we write the decoded result
31+
*/
32+
RAWDecoder(Print &out_stream, bool active=true){
33+
LOGD(LOG_METHOD);
34+
p_print = &out_stream;
35+
this->active = active;
36+
}
37+
38+
/**
39+
* @brief Construct a new RAWDecoder object
40+
*
41+
* @param out_stream Output Stream to which we write the decoded result
42+
* @param bi Object that will be notified about the Audio Formt (Changes)
43+
*/
44+
45+
RAWDecoder(Print &out_stream, AudioBaseInfoDependent &bi){
46+
LOGD(LOG_METHOD);
47+
p_print = &out_stream;
48+
}
49+
50+
/// Defines the output Stream
51+
void setOutputStream(Print &out_stream) override {
52+
p_print = &out_stream;
53+
}
54+
55+
void setNotifyAudioChange(AudioBaseInfoDependent &bi) override {
56+
this->bid = &bi;
57+
}
58+
59+
AudioBaseInfo audioInfo() override {
60+
return cfg;
61+
}
62+
63+
void begin(AudioBaseInfo info) {
64+
LOGD(LOG_METHOD);
65+
cfg = info;
66+
if (bid!=nullptr){
67+
bid->setAudioInfo(cfg);
68+
}
69+
active = true;
70+
}
71+
72+
void begin() override {
73+
LOGD(LOG_METHOD);
74+
active = true;
75+
}
76+
77+
void end() override {
78+
LOGD(LOG_METHOD);
79+
active = false;
80+
}
81+
82+
virtual size_t write(const void *in_ptr, size_t in_size) override {
83+
if (p_print!=nullptr) return 0;
84+
return p_print->write((uint8_t*)in_ptr, in_size);
85+
}
86+
87+
virtual operator boolean() override {
88+
return active;
89+
}
90+
91+
protected:
92+
Print *p_print=nullptr;
93+
AudioBaseInfoDependent *bid=nullptr;
94+
AudioBaseInfo cfg;
95+
bool active;
96+
97+
};
98+
99+
/**
100+
* @brief RAWDecoder - Actually this class does no encoding or decoding at all. It just passes on the
101+
* data.
102+
* @author Phil Schatzmann
103+
* @copyright GPLv3
104+
*/
105+
class RAWEncoder : public AudioEncoder {
106+
public:
107+
// Empty Constructor - the output stream must be provided with begin()
108+
RAWEncoder(){
109+
}
110+
111+
// Constructor providing the output stream
112+
RAWEncoder(Print &out){
113+
p_print = &out;
114+
}
115+
116+
/// Defines the output Stream
117+
void setOutputStream(Print &out_stream) override {
118+
p_print = &out_stream;
119+
}
120+
121+
/// Provides "audio/pcm"
122+
const char* mime() override{
123+
return raw_mime;
124+
}
125+
126+
/// We actually do nothing with this
127+
virtual void setAudioInfo(AudioBaseInfo from) override {
128+
}
129+
130+
/// starts the processing using the actual RAWAudioInfo
131+
virtual void begin() override{
132+
is_open = true;
133+
}
134+
135+
/// starts the processing
136+
void begin(Print &out) {
137+
p_print = &out;
138+
begin();
139+
}
140+
141+
/// stops the processing
142+
void end() override {
143+
is_open = false;
144+
}
145+
146+
/// Writes PCM data to be encoded as RAW
147+
virtual size_t write(const void *in_ptr, size_t in_size) override {
148+
if (p_print!=nullptr) return 0;
149+
return p_print->write((uint8_t*)in_ptr, in_size);
150+
}
151+
152+
operator boolean() override {
153+
return is_open;
154+
}
155+
156+
bool isOpen(){
157+
return is_open;
158+
}
159+
160+
protected:
161+
Print* p_print=nullptr;;
162+
volatile bool is_open;
163+
164+
};
165+
166+
}

src/AudioMetaData/MetaDataICY.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class MetaDataICY : public AbstractMetaData {
4141
callback = fn;
4242
}
4343

44-
/// Defines the metadata callback function
45-
virtual void setDataCallback(void (*fn)(const uint8_t* str, int len), int bufferLen=1024) {
44+
/// Defines the audio callback function
45+
virtual void setAudioDataCallback(void (*fn)(const uint8_t* str, int len), int bufferLen=1024) {
4646
dataBuffer = new uint8_t[bufferLen];
4747
dataCallback = fn;
4848
dataLen = 0;

src/AudioTools/AudioStreams.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ class AudioStream : public Stream, public AudioBaseInfoDependent {
5252
*/
5353
class AudioStreamX : public AudioStream {
5454
public:
55-
virtual size_t readBytes(uint8_t *buffer, size_t length) { return not_supported(0); }
56-
virtual size_t write(const uint8_t *buffer, size_t size) { return not_supported(0); }
57-
virtual size_t write(uint8_t) { return not_supported(0); }
58-
virtual int available() { return not_supported(0); };
55+
virtual size_t readBytes(uint8_t *buffer, size_t length) override { return not_supported(0); }
56+
virtual size_t write(const uint8_t *buffer, size_t size) override{ return not_supported(0); }
57+
virtual size_t write(uint8_t) override { return not_supported(0); }
58+
virtual int available() override { return not_supported(0); };
5959
virtual int availableForWrite() override { return DEFAULT_BUFFER_SIZE; }
6060

61-
virtual int read() { return not_supported(-1); }
62-
virtual int peek() { return not_supported(-1); }
63-
virtual void flush() {}
64-
virtual void setAudioInfo(audio_tools::AudioBaseInfo){}
61+
virtual int read() override { return not_supported(-1); }
62+
virtual int peek() override { return not_supported(-1); }
63+
virtual void flush() override {}
64+
virtual void setAudioInfo(audio_tools::AudioBaseInfo) override {}
6565
};
6666

6767
/**

src/AudioTools/AudioTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AudioBaseInfoSource {
6969
*/
7070
class AudioWriter {
7171
public:
72-
virtual size_t write(const void *in_ptr, size_t in_size) = 0;
72+
virtual size_t write(const void *in_ptr, size_t in_size) = 0;
7373
virtual operator boolean() = 0;
7474
};
7575

tests/build-examples-log.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
../examples/examples-basic-api/base-adc-a2dp -> rc=0
32
../examples/examples-basic-api/base-adc-serial -> rc=0
43
../examples/examples-basic-api/base-file_mp3-a2dp -> rc=0
@@ -54,6 +53,7 @@
5453
../examples/examples-audiokit/player-url_icy-audiokit -> rc=0
5554
../examples/examples-audiokit/streams-audiokit-audiokit -> rc=0
5655
../examples/examples-audiokit/streams-audiokit-csv -> rc=0
56+
../examples/examples-audiokit/streams-audiokit-filter-audiokit -> rc=0
5757
../examples/examples-audiokit/streams-audiokit-webserver_aac -> rc=0
5858
../examples/examples-audiokit/streams-audiokit-webserver_wav -> rc=0
5959
../examples/examples-audiokit/streams-flite-audiokit -> rc=0

tests/mp3-mad/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ endif()
2222
# build sketch as executable
2323
add_executable (mp3-mad mp3-mad.cpp ../main.cpp)
2424
# set preprocessor defines
25-
target_compile_definitions(mp3-mad PUBLIC -DARDUINO -DEXIT_ON_STOP -DUSE_MAD -DUSE_PORTAUDIO -DIS_DESKTOP)
25+
target_compile_definitions(mp3-mad PUBLIC -DARDUINO -DEXIT_ON_STOP -DUSE_MAD -DUSE_PORTAUDIO -DIS_DESKTOP )
2626

2727
# specify libraries
28-
target_link_libraries(mp3-mad portaudio arduino_emulator arduino_libmad arduino-audio-tools)
28+
target_link_libraries(mp3-mad portaudio arduino_emulator arduino_libmad arduino-audio-tools )
29+
# ESP32: CONFIG_ARDUINO_LOOP_STACK_SIZE 8192 -> so we test it with this setting "-Wl,-z,stack-size=8192"
30+
add_link_options("-z,stack-size=8192")
2931

0 commit comments

Comments
 (0)