Skip to content

Commit 8ba3208

Browse files
authored
Merge pull request #636 from schreibfaul1/dev
Arduino V3
2 parents 961b320 + 7caef30 commit 8ba3208

File tree

3 files changed

+143
-134
lines changed

3 files changed

+143
-134
lines changed

src/Audio.cpp

+54-111
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Created on: Oct 26.2018
55
*
6-
* Version 3.0.7z
7-
* Updated on: Dec 16.2023
6+
* Version 3.0.8
7+
* Updated on: Dec 22.2023
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -2130,30 +2130,33 @@ bool Audio::pauseResume() {
21302130
return retVal;
21312131
}
21322132
//---------------------------------------------------------------------------------------------------------------------
2133-
bool Audio::playChunk() {
2134-
// If we've got data, try and pump it out..
2133+
void Audio::playChunk() {
2134+
21352135
int16_t sample[2];
2136-
if(getBitsPerSample() == 8) {
2137-
if(getChannels() == 1) {
2138-
while(m_validSamples) {
2136+
2137+
auto pc = [&](int16_t *s16) { // lambda, inner function
2138+
if(playSample(s16)){
2139+
m_validSamples--;
2140+
m_curSample++;
2141+
return true;
2142+
}
2143+
return false;
2144+
};
2145+
2146+
// If we've got data, try and pump it out..
2147+
while(m_validSamples){
2148+
if(getBitsPerSample() == 8) {
2149+
if(getChannels() == 1) {
21392150
uint8_t x = m_outBuff[m_curSample] & 0x00FF;
21402151
uint8_t y = (m_outBuff[m_curSample] & 0xFF00) >> 8;
21412152
sample[RIGHTCHANNEL] = x;
21422153
sample[LEFTCHANNEL] = x;
2143-
while(1) {
2144-
if(playSample(sample)) break;
2145-
} // Can't send?
2154+
if(!pc(sample)){ break;} // playSample in lambda
21462155
sample[RIGHTCHANNEL] = y;
21472156
sample[LEFTCHANNEL] = y;
2148-
while(1) {
2149-
if(playSample(sample)) break;
2150-
} // Can't send?
2151-
m_validSamples--;
2152-
m_curSample++;
2157+
if(!pc(sample)){ break;} // playSample in lambda
21532158
}
2154-
}
2155-
if(getChannels() == 2) {
2156-
while(m_validSamples) {
2159+
if(getChannels() == 2) {
21572160
uint8_t x = m_outBuff[m_curSample] & 0x00FF;
21582161
uint8_t y = (m_outBuff[m_curSample] & 0xFF00) >> 8;
21592162
if(!m_f_forceMono) { // stereo mode
@@ -2165,54 +2168,29 @@ bool Audio::playChunk() {
21652168
sample[RIGHTCHANNEL] = xy;
21662169
sample[LEFTCHANNEL] = xy;
21672170
}
2168-
2169-
while(1) {
2170-
if(playSample(sample)) break;
2171-
} // Can't send?
2172-
m_validSamples--;
2173-
m_curSample++;
2171+
if(!pc(sample)){ break;} // playSample in lambda
21742172
}
21752173
}
2176-
m_curSample = 0;
2177-
return true;
2178-
}
2179-
if(getBitsPerSample() == 16) {
2180-
if(getChannels() == 1) {
2181-
while(m_validSamples) {
2174+
2175+
if(getBitsPerSample() == 16) {
2176+
if(getChannels() == 1) {
21822177
sample[RIGHTCHANNEL] = m_outBuff[m_curSample];
21832178
sample[LEFTCHANNEL] = m_outBuff[m_curSample];
2184-
if(!playSample(sample)) {
2185-
log_e("can't send");
2186-
return false;
2187-
} // Can't send
2188-
m_validSamples--;
2189-
m_curSample++;
21902179
}
2191-
}
2192-
if(getChannels() == 2) {
2193-
m_curSample = 0;
2194-
while(m_validSamples) {
2195-
if(!m_f_forceMono) { // stereo mode
2196-
sample[RIGHTCHANNEL] = m_outBuff[m_curSample * 2];
2197-
sample[LEFTCHANNEL] = m_outBuff[m_curSample * 2 + 1];
2198-
}
2199-
else { // mono mode, #100
2200-
int16_t xy = (m_outBuff[m_curSample * 2] + m_outBuff[m_curSample * 2 + 1]) / 2;
2201-
sample[RIGHTCHANNEL] = xy;
2202-
sample[LEFTCHANNEL] = xy;
2203-
}
2204-
playSample(sample);
2205-
m_validSamples--;
2206-
m_curSample++;
2180+
if(getChannels() == 2) {
2181+
if(!m_f_forceMono) { // stereo mode
2182+
sample[RIGHTCHANNEL] = m_outBuff[m_curSample * 2];
2183+
sample[LEFTCHANNEL] = m_outBuff[m_curSample * 2 + 1];
2184+
}
2185+
else { // mono mode, #100
2186+
int16_t xy = (m_outBuff[m_curSample * 2] + m_outBuff[m_curSample * 2 + 1]) / 2;
2187+
sample[RIGHTCHANNEL] = xy;
2188+
sample[LEFTCHANNEL] = xy;
2189+
}
22072190
}
22082191
}
2209-
m_curSample = 0;
2210-
return true;
2192+
if(!pc(sample)){ break;} // playSample in lambda
22112193
}
2212-
log_e("BitsPer Sample must be 8 or 16!");
2213-
m_validSamples = 0;
2214-
stopSong();
2215-
return false;
22162194
}
22172195
//---------------------------------------------------------------------------------------------------------------------
22182196
void Audio::loop() {
@@ -2952,6 +2930,7 @@ void Audio::processLocalFile() {
29522930
if(f_fileDataComplete && InBuff.bufferFilled() < InBuff.getMaxBlockSize()) {
29532931
if(InBuff.bufferFilled()) {
29542932
if(!readID3V1Tag()) {
2933+
if(m_validSamples) {playChunk(); return;} // play samples first
29552934
int bytesDecoded = sendBytes(InBuff.getReadPtr(), InBuff.bufferFilled());
29562935
if(bytesDecoded <= InBuff.bufferFilled()) { // avoid InBuff overrun (can be if file is corrupt)
29572936
if(m_f_playing) {
@@ -3001,17 +2980,7 @@ void Audio::processLocalFile() {
30012980

30022981
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
30032982
if(f_stream) {
3004-
static uint8_t cnt = 0;
3005-
uint8_t compression;
3006-
if(m_codec == CODEC_WAV) compression = 1;
3007-
if(m_codec == CODEC_FLAC) compression = 2;
3008-
else
3009-
compression = 3;
3010-
cnt++;
3011-
if(cnt == compression) {
3012-
playAudioData();
3013-
cnt = 0;
3014-
}
2983+
playAudioData();
30152984
}
30162985
}
30172986
//----------------------------------------------------------------------------------------------------------------------
@@ -3091,17 +3060,7 @@ void Audio::processWebStream() {
30913060

30923061
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
30933062
if(f_stream) {
3094-
static uint8_t cnt = 0;
3095-
uint8_t compression;
3096-
if(m_codec == CODEC_WAV) compression = 1;
3097-
if(m_codec == CODEC_FLAC) compression = 2;
3098-
else
3099-
compression = 3;
3100-
cnt++;
3101-
if(cnt == compression) {
3102-
playAudioData();
3103-
cnt = 0;
3104-
}
3063+
playAudioData();
31053064
}
31063065
}
31073066
//---------------------------------------------------------------------------------------------------------------------
@@ -3200,6 +3159,7 @@ void Audio::processWebFile() {
32003159
if(f_webFileDataComplete && InBuff.bufferFilled() < InBuff.getMaxBlockSize()) {
32013160
if(InBuff.bufferFilled()) {
32023161
if(!readID3V1Tag()) {
3162+
if(m_validSamples) {playChunk(); return;} // play samples first
32033163
int bytesDecoded = sendBytes(InBuff.getReadPtr(), InBuff.bufferFilled());
32043164
if(bytesDecoded > 2) {
32053165
InBuff.bytesWasRead(bytesDecoded);
@@ -3233,17 +3193,7 @@ void Audio::processWebFile() {
32333193

32343194
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32353195
if(f_stream) {
3236-
static uint8_t cnt = 0;
3237-
uint8_t compression;
3238-
if(m_codec == CODEC_WAV) compression = 1;
3239-
if(m_codec == CODEC_FLAC) compression = 2;
3240-
else
3241-
compression = 3;
3242-
cnt++;
3243-
if(cnt == compression) {
3244-
playAudioData();
3245-
cnt = 0;
3246-
}
3196+
playAudioData();
32473197
}
32483198
return;
32493199
}
@@ -3356,12 +3306,7 @@ void Audio::processWebStreamTS() {
33563306

33573307
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33583308
if(f_stream) {
3359-
static uint8_t cnt = 0;
3360-
cnt++;
3361-
if(cnt == 3) {
3362-
playAudioData();
3363-
cnt = 0;
3364-
} // aac only
3309+
playAudioData();
33653310
}
33663311
return;
33673312
}
@@ -3479,17 +3424,14 @@ void Audio::processWebStreamHLS() {
34793424

34803425
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34813426
if(f_stream) {
3482-
static uint8_t cnt = 0;
3483-
cnt++;
3484-
if(cnt == 2) {
3485-
playAudioData();
3486-
cnt = 0;
3487-
} // aac only
3427+
playAudioData();
34883428
}
34893429
return;
34903430
}
34913431
//---------------------------------------------------------------------------------------------------------------------
34923432
void Audio::playAudioData() {
3433+
if(m_validSamples) {playChunk(); return;} // play samples first
3434+
34933435
if(InBuff.bufferFilled() < InBuff.getMaxBlockSize()) return; // guard
34943436

34953437
int bytesDecoded = sendBytes(InBuff.getReadPtr(), InBuff.getMaxBlockSize());
@@ -3506,7 +3448,7 @@ void Audio::playAudioData() {
35063448
InBuff.bytesWasRead(bytesDecoded);
35073449
return;
35083450
}
3509-
if(bytesDecoded == 0) return; // syncword at pos0 found
3451+
if(bytesDecoded == 0) return; // syncword at pos0
35103452
}
35113453

35123454
return;
@@ -4245,6 +4187,7 @@ void Audio::setDecoderItems() {
42454187
}
42464188
//---------------------------------------------------------------------------------------------------------------------
42474189
int Audio::sendBytes(uint8_t* data, size_t len) {
4190+
42484191
int bytesLeft;
42494192
static bool f_setDecodeParamsOnce = true;
42504193
int nextSync = 0;
@@ -4365,7 +4308,8 @@ int Audio::sendBytes(uint8_t* data, size_t len) {
43654308
audio_process_extern(m_outBuff, m_validSamples, &continueI2S);
43664309
if(!continueI2S) { return bytesDecoded; }
43674310
}
4368-
while(m_validSamples) { playChunk(); }
4311+
m_curSample = 0;
4312+
playChunk();
43694313
return bytesDecoded;
43704314
}
43714315
//---------------------------------------------------------------------------------------------------------------------
@@ -4500,7 +4444,7 @@ void Audio::printDecodeError(int r) {
45004444
case ERR_OPUS_INVALID_SAMPLERATE: e = "SAMPLERATE IS NOT 48000Hz"; break;
45014445
case ERR_OPUS_EXTRA_CHANNELS_UNSUPPORTED: e = "EXTRA CHANNELS UNSUPPORTED"; break;
45024446
case ERR_OPUS_SILK_MODE_UNSUPPORTED: e = "SILK MODE UNSUPPORTED"; break;
4503-
case ERR_OPUS_HYBRID_MODE_UNSUPPORTED: e = "HYBRID MODE UMSUPPORTED"; break;
4447+
case ERR_OPUS_HYBRID_MODE_UNSUPPORTED: e = "HYBRID MODE UNSUPPORTED"; break;
45044448
case ERR_OPUS_CELT_BAD_ARG: e = "CELT_DECODER_BAD_ARG"; break;
45054449
case ERR_OPUS_CELT_INTERNAL_ERROR: e = "CELT DECODER INTERNAL ERROR"; break;
45064450
case ERR_OPUS_CELT_UNIMPLEMENTED: e = "CELT DECODER UNIMPLEMENTED ARG"; break;
@@ -4835,7 +4779,7 @@ bool Audio::playSample(int16_t sample[2]) {
48354779
sample = IIR_filterChain2(sample);
48364780
//-------------------------------------------
48374781

4838-
uint32_t s32 = Gain(sample); // vosample2lume;
4782+
uint32_t s32 = Gain(sample); // sample2volume;
48394783

48404784
if(audio_process_i2s) {
48414785
// process audio sample just before writing to i2s
@@ -4847,16 +4791,15 @@ bool Audio::playSample(int16_t sample[2]) {
48474791
if(m_f_internalDAC) { s32 += 0x80008000; }
48484792
m_i2s_bytesWritten = 0;
48494793
#if(ESP_IDF_VERSION_MAJOR == 5)
4850-
esp_err_t err = i2s_channel_write(m_i2s_tx_handle, (const char*)&s32, sizeof(uint32_t), &m_i2s_bytesWritten, 100);
4794+
esp_err_t err = i2s_channel_write(m_i2s_tx_handle, (const char*)&s32, sizeof(uint32_t), &m_i2s_bytesWritten, 0);
48514795
#else
4852-
esp_err_t err = i2s_write((i2s_port_t)m_i2s_num, (const char*)&s32, sizeof(uint32_t), &m_i2s_bytesWritten, 100);
4796+
esp_err_t err = i2s_write((i2s_port_t)m_i2s_num, (const char*)&s32, sizeof(uint32_t), &m_i2s_bytesWritten, 0); // no wait
48534797
#endif
48544798
if(err != ESP_OK) {
48554799
log_e("ESP32 Errorcode %i", err);
48564800
return false;
48574801
}
4858-
if(m_i2s_bytesWritten < 4) {
4859-
log_e("Can't stuff any more in I2S..."); // increase waitingtime or outputbuffer
4802+
if(m_i2s_bytesWritten < 4) { // no more space in dma buffer --> break and try it later
48604803
return false;
48614804
}
48624805
return true;

src/Audio.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Created on: Oct 28,2018
55
*
6-
* Version 3.0.7z
7-
* Updated on: Dec 16.2023
6+
* Version 3.0.8
7+
* Updated on: Dec 22.2023
88
* Author: Wolle (schreibfaul1)
99
*/
1010

@@ -220,7 +220,7 @@ class Audio : private AudioBuffer{
220220
bool setBitsPerSample(int bits);
221221
bool setChannels(int channels);
222222
bool setBitrate(int br);
223-
bool playChunk();
223+
void playChunk();
224224
bool playSample(int16_t sample[2]);
225225
void computeVUlevel(int16_t sample[2]);
226226
void computeLimit();

0 commit comments

Comments
 (0)