Skip to content

Commit 82051ae

Browse files
committed
Resolve Complile Errors in non ESP32 systems
1 parent fa32fad commit 82051ae

File tree

9 files changed

+193
-41
lines changed

9 files changed

+193
-41
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ Dependent on the example you might need to install some of the following librari
147147
- [SD Library](https://www.arduino.cc/en/reference/SD) to read and write files.
148148
- [arduino-fdk-aac](https://github.com/pschatzmann/arduino-fdk-aac) to encode or decode AAC
149149
- [Mozzi](https://github.com/pschatzmann/Mozzi) A sound synthesis library for Arduino
150-
- [SAM](https://github.com/pschatzmann/SAM) A Text to Speach Engine
150+
- [SAM](https://github.com/pschatzmann/arduino-SAM) A Text to Speach Engine
151151
- [TTS](https://github.com/pschatzmann/TTS) A Text to Speach Engine
152-
- [flite](https://github.com/pschatzmann/flite) A Text to Speach Engine
152+
- [flite](https://github.com/pschatzmann/arduino-flite) A Text to Speach Engine
153153

154154
## Installation
155155

src/AudioConfig.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
#pragma once
88
#include <string.h>
99

10+
/**
11+
* -------------------------------------------------------------------------
12+
* @brief Logging
13+
* Logging Configuration in Arduino -> set USE_AUDIO_LOGGING to false if you want to deactivate Logging.
14+
* When using cmake you can set -DUSE_AUDIO_LOGGING=false
15+
* You can also change the LOG_LEVEL and LOG_STREAM here.
16+
* However it is recommended to do it in your Sketch e.g with AudioLogger::instance().begin(Serial,AudioLogger::Warning);
17+
*/
18+
19+
#define USE_AUDIO_LOGGING true
20+
#define PRINTF_BUFFER_SIZE 160
21+
#define LOG_LEVEL AudioLogger::Warning
22+
#define LOG_STREAM Serial
23+
24+
1025
/**
1126
* -------------------------------------------------------------------------
1227
* @brief Common Default Settings that can usually be changed in the API
@@ -73,16 +88,3 @@
7388
#define SOFT_MUTE_VALUE LOW
7489
#endif
7590

76-
/**
77-
* -------------------------------------------------------------------------
78-
* @brief Logging
79-
* Logging Configuration in Arduino -> set USE_AUDIO_LOGGING to false if you want to deactivate Logging.
80-
* When using cmake you can set -DUSE_AUDIO_LOGGING=false
81-
* You can also change the LOG_LEVEL and LOG_STREAM here.
82-
* However it is recommended to do it in your Sketch e.g with AudioLogger::instance().begin(Serial,AudioLogger::Warning);
83-
*/
84-
85-
#define USE_AUDIO_LOGGING true
86-
#define PRINTF_BUFFER_SIZE 160
87-
#define LOG_LEVEL AudioLogger::Warning
88-
#define LOG_STREAM Serial

src/AudioHttp/Str.h

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

33
#include <stdio.h>
44
#include <string.h>
5-
#include <iostream>
65
#include "AudioTools/AudioLogger.h"
76

87
namespace audio_tools {

src/AudioHttp/StrExt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class StrExt : public Str {
5555
// move assingment
5656
StrExt& operator = (StrExt &obj) {
5757
set(obj.c_str());
58+
return *this;
5859
};
5960

6061

src/AudioHttp/URLStreamArduino.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class URLStream : public Stream {
2727

2828
URLStream(const char* network, const char *password, int readBufferSize=DEFAULT_BUFFER_SIZE) {
2929
read_buffer = new uint8_t[readBufferSize];
30-
this->network = network;
31-
this->password = password;
30+
this->network = (char*)network;
31+
this->password = (char*)password;
3232
}
3333

3434
~URLStream(){
@@ -91,8 +91,8 @@ class URLStream : public Stream {
9191
uint16_t read_buffer_size;
9292
uint16_t read_pos;
9393
uint16_t read_size;
94-
const char* network;
95-
const char* password;
94+
char* network;
95+
char* password;
9696
WiFiClient client;
9797

9898
inline void fillBuffer() {

src/AudioTools/AnalogAudio.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
#pragma once
22

3-
#ifdef ESP32
4-
5-
#pragma once
63
#include "AudioConfig.h"
74
#if(defined ESP32)
85
#include "driver/i2s.h"
96
#include "esp_a2dp_api.h"
107
//#include "freertos/queue.h"
11-
#endif
12-
#if(defined ESP8266)
13-
#include "i2s.h"
14-
#define I2S_NUM_0 0
15-
#endif
168

179
namespace audio_tools {
1810

@@ -237,4 +229,6 @@ class AnalogAudio {
237229

238230
}
239231

240-
#endif
232+
#endif
233+
234+

src/AudioTools/I2S_ESP32.h

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace audio_tools {
99

1010
/**
11-
* @brief Basic I2S API - for the ESP32
11+
* @brief Basic I2S API - for the ESP32. If we receive 1 channel, we expand the result to 2 channels.
1212
*
1313
*/
1414
class I2SBase {
@@ -101,23 +101,91 @@ class I2SBase {
101101

102102
// update the cfg.i2s.channel_format based on the number of channels
103103
void setChannels(int channels){
104-
if (channels==2){
105-
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
106-
} else if (channels==1){
107-
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
108-
}
109104
cfg.channels = channels;
110105
}
111106

112107
/// writes the data to the I2S interface
113108
size_t writeBytes(const void *src, size_t size_bytes){
114-
size_t result = 0;
115-
if (i2s_write(i2s_num, src, size_bytes, &result, portMAX_DELAY)!=ESP_OK){
116-
LOGE("%s", __func__);
117-
}
109+
size_t result = 0;
110+
if (cfg.channels==2){
111+
if (i2s_write(i2s_num, src, size_bytes, &result, portMAX_DELAY)!=ESP_OK){
112+
LOGE("%s", __func__);
113+
}
114+
} else {
115+
result = writeExpandChannel(src, size_bytes);
116+
}
118117
return result;
119118
}
120119

120+
/// writes the data by making shure that we send 2 channels
121+
size_t writeExpandChannel(const void *src, size_t size_bytes){
122+
size_t result = 0;
123+
int j;
124+
switch(cfg.bits_per_sample){
125+
126+
case 8:
127+
for (j=0;j<size_bytes;j++){
128+
int8_t frame[2];
129+
int8_t *data = (int8_t *)src;
130+
frame[0]=data[j];
131+
frame[1]=data[j];
132+
size_t result_call = 0;
133+
if (i2s_write(i2s_num, frame, sizeof(int8_t)*2, &result_call, portMAX_DELAY)!=ESP_OK){
134+
LOGE("%s", __func__);
135+
} else {
136+
result += result_call;
137+
}
138+
}
139+
break;
140+
141+
case 16:
142+
for (j=0;j<size_bytes/2;j++){
143+
int16_t frame[2];
144+
int16_t *data = (int16_t*)src;
145+
frame[0]=data[j];
146+
frame[1]=data[j];
147+
size_t result_call = 0;
148+
if (i2s_write(i2s_num, frame, sizeof(int16_t)*2, &result_call, portMAX_DELAY)!=ESP_OK){
149+
LOGE("%s", __func__);
150+
} else {
151+
result += result_call;
152+
}
153+
}
154+
break;
155+
156+
case 24:
157+
for (j=0;j<size_bytes/4;j++){
158+
int24_t frame[2];
159+
int24_t *data = (int24_t*) src;
160+
frame[0]=data[j];
161+
frame[1]=data[j];
162+
size_t result_call = 0;
163+
if (i2s_write(i2s_num, frame, sizeof(int24_t)*2, &result_call, portMAX_DELAY)!=ESP_OK){
164+
LOGE("%s", __func__);
165+
} else {
166+
result += result_call;
167+
}
168+
}
169+
break;
170+
171+
case 32:
172+
for (j=0;j<size_bytes/4;j++){
173+
int32_t frame[2];
174+
int32_t *data = (int32_t*) src;
175+
frame[0]=data[j];
176+
frame[1]=data[j];
177+
size_t result_call = 0;
178+
if (i2s_write(i2s_num, frame, sizeof(int32_t)*2, &result_call, portMAX_DELAY)!=ESP_OK){
179+
LOGE("%s", __func__);
180+
} else {
181+
result += result_call;
182+
}
183+
}
184+
break;
185+
}
186+
return result;
187+
}
188+
121189
size_t readBytes(void *dest, size_t size_bytes){
122190
size_t result = 0;
123191
if (i2s_read(i2s_num, dest, size_bytes, &result, portMAX_DELAY)!=ESP_OK){

src/AudioTools/I2S_ESP8266.h

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,95 @@ class I2SBase {
4545

4646
/// writes the data to the I2S interface
4747
size_t writeBytes(const void *src, size_t size_bytes){
48+
size_t result = 0;
4849
size_t frame_size = cfg.channels * (cfg.bits_per_sample/8);
4950
uint16_t frame_count = size_bytes / frame_size;
50-
return i2s_write_buffer(( int16_t *)src, frame_count ) * frame_size;
51+
if (cfg.channels==2 && cfg.bits_per_sample==16){
52+
result = i2s_write_buffer(( int16_t *)src, frame_count ) * frame_size;
53+
} else {
54+
result = writeExt(src, size_bytes);
55+
}
56+
return result;
57+
}
58+
59+
/// writes the data by making shure that we send 2 channels 16 bit data
60+
size_t writeExt(const void *src, size_t size_bytes){
61+
size_t result = 0;
62+
int j;
63+
64+
switch(cfg.bits_per_sample){
65+
66+
case 8:
67+
for (j=0; j<size_bytes; j += cfg.channels){
68+
int8_t *data = (int8_t *)src;
69+
int16_t frame[2];
70+
frame[0]=data[j] * 256;
71+
if (cfg.channels==1){
72+
frame[1]=data[j] * 256;
73+
} else {
74+
frame[1]=data[j+1] * 256;
75+
}
76+
uint32_t* frame_ptr = (uint32_t*)frame;
77+
if(i2s_write_sample(*frame_ptr)){
78+
result+=2;
79+
}
80+
}
81+
break;
82+
83+
case 16:
84+
for (j=0;j<size_bytes/2; j += cfg.channels){
85+
int16_t *data = (int16_t *)src;
86+
int16_t frame[2];
87+
frame[0]=data[j];
88+
if (cfg.channels==1){
89+
frame[1]=data[j];
90+
} else {
91+
frame[1]=data[j+1];
92+
}
93+
uint32_t* frame_ptr = (uint32_t*)frame;
94+
if(i2s_write_sample(*frame_ptr)){
95+
result+=2;
96+
}
97+
}
98+
break;
99+
100+
case 24:
101+
for (j=0;j<size_bytes/3; j += cfg.channels){
102+
int24_t *data = (int24_t *)src;
103+
int24_t frame[2];
104+
int32_t value = data[j];
105+
frame[0]=value;
106+
if (cfg.channels==1){
107+
frame[1]=value;
108+
} else {
109+
value = data[j+1];
110+
frame[1]=value / 256;
111+
}
112+
uint32_t* frame_ptr = (uint32_t*)frame;
113+
if(i2s_write_sample(*frame_ptr)){
114+
result+=2;
115+
}
116+
}
117+
break;
118+
119+
case 32:
120+
for (j=0; j<size_bytes/4; j += cfg.channels){
121+
int32_t *data = (int32_t *)src;
122+
int32_t frame[2];
123+
frame[0]=data[j] / 65538;
124+
if (cfg.channels==1){
125+
frame[1]=data[j] / 65538;
126+
} else {
127+
frame[1]=data[j+1] / 65538;
128+
}
129+
uint32_t* frame_ptr = (uint32_t*)frame;
130+
if(i2s_write_sample(*frame_ptr)){
131+
result+=2;
132+
}
133+
}
134+
break;
135+
}
136+
return result;
51137
}
52138

53139
/// reads the data from the I2S interface

src/AudioTools/Streams.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class AudioOutputStream : public BufferedStream {
391391
};
392392

393393

394-
394+
#ifdef ESP32
395395

396396
/**
397397
* @brief We support the Stream interface for the ADC class
@@ -442,6 +442,8 @@ class AnalogAudioStream : public BufferedStream, public AudioBaseInfoDependent
442442

443443
};
444444

445+
#endif
446+
445447
/**
446448
* @brief AudioOutput class which stores the data in a temporary buffer.
447449
* The buffer can be consumed e.g. by a callback function by calling read();

0 commit comments

Comments
 (0)