Skip to content

Commit addfed4

Browse files
committed
Logger
1 parent 2e2844c commit addfed4

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

README.md

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

33
Some basic C++ classes that can be used for Audio Processing privided as Arduino Library
44

5-
- Additional Stream implementations: MemoryStream, UrlStream [ESP32 only]
5+
- Additional Stream implementations: MemoryStream - ESP32 only: UrlStream, I2SStream
66
- a simple I2S class (to read and write to the internal I2S) [ESP32 only]
77
- a simple ADC class (to read analog data with the help of I2S) [ESP32 only]
88
- Converters

src/AudioOutputWithCallback.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AudioOutputWithCallback : public AudioOutput
2424
delete buffer_ptr;
2525
}
2626

27+
/// Activates the output
2728
virtual bool begin() {
2829
active = true;
2930
return true;

src/AudioTools/AudioLogger.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ class AudioLogger {
3131
Error
3232
};
3333

34-
AudioLogger(){}
3534

3635
/// activate the logging
37-
void begin(Stream& out, LogLevel level=SOUND_LOG_LEVEL){
36+
void begin(Stream& out, LogLevel level=SOUND_LOG_LEVEL) {
3837
this->log_stream_ptr = &out;
3938
this->log_level = level;
4039
}
@@ -45,27 +44,27 @@ class AudioLogger {
4544
}
4645

4746
/// logs an error
48-
void error(const char *str, const char* str1=nullptr, const char* str2=nullptr){
47+
void error(const char *str, const char* str1=nullptr, const char* str2=nullptr) const {
4948
log(Error, str, str1, str2);
5049
}
5150

5251
/// logs an info message
53-
void info(const char *str, const char* str1=nullptr, const char* str2=nullptr){
52+
void info(const char *str, const char* str1=nullptr, const char* str2=nullptr) const {
5453
log(Info, str, str1, str2);
5554
}
5655

5756
/// logs an warning
58-
void warning(const char *str, const char* str1=nullptr, const char* str2=nullptr){
57+
void warning(const char *str, const char* str1=nullptr, const char* str2=nullptr) const {
5958
log(Warning, str, str1, str2);
6059
}
6160

6261
/// writes an debug message
63-
void debug(const char *str, const char* str1=nullptr, const char* str2=nullptr){
62+
void debug(const char *str, const char* str1=nullptr, const char* str2=nullptr) const {
6463
log(Debug, str, str1, str2);
6564
}
6665

6766
/// printf support
68-
int printf(LogLevel current_level, const char* fmt, ...) {
67+
int printf(LogLevel current_level, const char* fmt, ...) const {
6968
int len = 0;
7069
if (log_stream_ptr!=nullptr && current_level >= log_level){
7170
char serial_printf_buffer[PRINTF_BUFFER_SIZE] = {0};
@@ -80,7 +79,7 @@ class AudioLogger {
8079

8180

8281
/// write an message to the log
83-
void log(LogLevel current_level, const char *str, const char* str1=nullptr, const char* str2=nullptr){
82+
void log(LogLevel current_level, const char *str, const char* str1=nullptr, const char* str2=nullptr) const {
8483
if (log_stream_ptr!=nullptr){
8584
if (current_level >= log_level){
8685
log_stream_ptr->print((char*)str);
@@ -98,13 +97,25 @@ class AudioLogger {
9897
}
9998
}
10099

100+
/// provides the singleton instance
101+
static AudioLogger &instance(){
102+
static AudioLogger *ptr;
103+
if (ptr==nullptr){
104+
ptr = new AudioLogger;
105+
}
106+
return *ptr;
107+
}
108+
109+
101110
protected:
102111
Stream *log_stream_ptr;
103112
LogLevel log_level;
104113

114+
AudioLogger(){
105115

106-
};
116+
}
107117

108-
AudioLogger Logger;
118+
119+
};
109120

110121
}

src/AudioTools/Streams.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class UrlStream : public Stream {
221221
uint16_t read_buffer_size;
222222
uint16_t read_pos;
223223
uint16_t read_size;
224+
const AudioLogger &Logger = AudioLogger::instance();
224225

225226

226227
inline void fillBuffer() {

src/AudioWAV.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace audio_tools {
1010

11+
const AudioLogger& WAVLogger = AudioLogger::instance();
1112

1213
/**
1314
* @brief Sound information which is available in the WAV header
@@ -41,7 +42,7 @@ class WAVHeader {
4142
};
4243

4344
void begin(uint8_t* buffer, size_t len){
44-
Logger.printf(AudioLogger::Info,"WAVHeader len: ", len);
45+
WAVLogger.printf(AudioLogger::Info,"WAVHeader len: ", len);
4546

4647
this->buffer = buffer;
4748
this->len = len;
@@ -149,11 +150,11 @@ class WAVHeader {
149150
size_t sound_pos = 0;
150151

151152
void logInfo(){
152-
Logger.printf(AudioLogger::Info,"WAVHeader sound_pos: ", sound_pos);
153-
Logger.printf(AudioLogger::Info,"WAVHeader channels: ", headerInfo.channels);
154-
Logger.printf(AudioLogger::Info,"WAVHeader bits_per_sample: ", headerInfo.bits_per_sample);
155-
Logger.printf(AudioLogger::Info,"WAVHeader sample_rate: ", headerInfo.sample_rate);
156-
Logger.printf(AudioLogger::Info,"WAVHeader format: ", headerInfo.format);
153+
WAVLogger.printf(AudioLogger::Info,"WAVHeader sound_pos: ", sound_pos);
154+
WAVLogger.printf(AudioLogger::Info,"WAVHeader channels: ", headerInfo.channels);
155+
WAVLogger.printf(AudioLogger::Info,"WAVHeader bits_per_sample: ", headerInfo.bits_per_sample);
156+
WAVLogger.printf(AudioLogger::Info,"WAVHeader sample_rate: ", headerInfo.sample_rate);
157+
WAVLogger.printf(AudioLogger::Info,"WAVHeader format: ", headerInfo.format);
157158
}
158159

159160
uint32_t read_tag() {
@@ -245,16 +246,16 @@ class WAVDecoder {
245246
isFirst = false;
246247
isValid = header.audioInfo().is_valid;
247248

248-
Logger.printf(AudioLogger::Info,"WAV sample_rate: ", header.audioInfo().sample_rate);
249-
Logger.printf(AudioLogger::Info,"WAV data_length: ", header.audioInfo().data_length);
250-
Logger.printf(AudioLogger::Info,"WAV is_streamed: ", header.audioInfo().is_streamed);
251-
Logger.printf(AudioLogger::Info,"WAVis_valid: ", header.audioInfo().is_valid);
249+
WAVLogger.printf(AudioLogger::Info,"WAV sample_rate: ", header.audioInfo().sample_rate);
250+
WAVLogger.printf(AudioLogger::Info,"WAV data_length: ", header.audioInfo().data_length);
251+
WAVLogger.printf(AudioLogger::Info,"WAV is_streamed: ", header.audioInfo().is_streamed);
252+
WAVLogger.printf(AudioLogger::Info,"WAVis_valid: ", header.audioInfo().is_valid);
252253

253254
// check format
254255
int format = header.audioInfo().format;
255256
isValid = format == WAV_FORMAT_PCM;
256257
if (format != WAV_FORMAT_PCM){
257-
Logger.printf(AudioLogger::Error,"WAV format not supported: ", format);
258+
WAVLogger.printf(AudioLogger::Error,"WAV format not supported: ", format);
258259
isValid = false;
259260
} else {
260261
// update sampling rate if the target supports it
@@ -267,7 +268,7 @@ class WAVDecoder {
267268
}
268269

269270
// write prm data from first record
270-
Logger.printf(AudioLogger::Info,"WAVDecoder writing first sound data");
271+
WAVLogger.printf(AudioLogger::Info,"WAVDecoder writing first sound data");
271272
out->write(sound_ptr, len);
272273
}
273274
}

0 commit comments

Comments
 (0)