3
3
*
4
4
* Created on: Oct 26.2018
5
5
*
6
- * Version 3.0.7z
7
- * Updated on: Dec 16 .2023
6
+ * Version 3.0.8
7
+ * Updated on: Dec 22 .2023
8
8
* Author: Wolle (schreibfaul1)
9
9
*
10
10
*/
@@ -2130,30 +2130,33 @@ bool Audio::pauseResume() {
2130
2130
return retVal;
2131
2131
}
2132
2132
// ---------------------------------------------------------------------------------------------------------------------
2133
- bool Audio::playChunk () {
2134
- // If we've got data, try and pump it out..
2133
+ void Audio::playChunk () {
2134
+
2135
2135
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 ) {
2139
2150
uint8_t x = m_outBuff[m_curSample] & 0x00FF ;
2140
2151
uint8_t y = (m_outBuff[m_curSample] & 0xFF00 ) >> 8 ;
2141
2152
sample[RIGHTCHANNEL] = x;
2142
2153
sample[LEFTCHANNEL] = x;
2143
- while (1 ) {
2144
- if (playSample (sample)) break ;
2145
- } // Can't send?
2154
+ if (!pc (sample)){ break ;} // playSample in lambda
2146
2155
sample[RIGHTCHANNEL] = y;
2147
2156
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
2153
2158
}
2154
- }
2155
- if (getChannels () == 2 ) {
2156
- while (m_validSamples) {
2159
+ if (getChannels () == 2 ) {
2157
2160
uint8_t x = m_outBuff[m_curSample] & 0x00FF ;
2158
2161
uint8_t y = (m_outBuff[m_curSample] & 0xFF00 ) >> 8 ;
2159
2162
if (!m_f_forceMono) { // stereo mode
@@ -2165,54 +2168,29 @@ bool Audio::playChunk() {
2165
2168
sample[RIGHTCHANNEL] = xy;
2166
2169
sample[LEFTCHANNEL] = xy;
2167
2170
}
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
2174
2172
}
2175
2173
}
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 ) {
2182
2177
sample[RIGHTCHANNEL] = m_outBuff[m_curSample];
2183
2178
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++;
2190
2179
}
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
+ }
2207
2190
}
2208
2191
}
2209
- m_curSample = 0 ;
2210
- return true ;
2192
+ if (!pc (sample)){ break ;} // playSample in lambda
2211
2193
}
2212
- log_e (" BitsPer Sample must be 8 or 16!" );
2213
- m_validSamples = 0 ;
2214
- stopSong ();
2215
- return false ;
2216
2194
}
2217
2195
// ---------------------------------------------------------------------------------------------------------------------
2218
2196
void Audio::loop () {
@@ -2952,6 +2930,7 @@ void Audio::processLocalFile() {
2952
2930
if (f_fileDataComplete && InBuff.bufferFilled () < InBuff.getMaxBlockSize ()) {
2953
2931
if (InBuff.bufferFilled ()) {
2954
2932
if (!readID3V1Tag ()) {
2933
+ if (m_validSamples) {playChunk (); return ;} // play samples first
2955
2934
int bytesDecoded = sendBytes (InBuff.getReadPtr (), InBuff.bufferFilled ());
2956
2935
if (bytesDecoded <= InBuff.bufferFilled ()) { // avoid InBuff overrun (can be if file is corrupt)
2957
2936
if (m_f_playing) {
@@ -3001,17 +2980,7 @@ void Audio::processLocalFile() {
3001
2980
3002
2981
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3003
2982
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 ();
3015
2984
}
3016
2985
}
3017
2986
// ----------------------------------------------------------------------------------------------------------------------
@@ -3091,17 +3060,7 @@ void Audio::processWebStream() {
3091
3060
3092
3061
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3093
3062
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 ();
3105
3064
}
3106
3065
}
3107
3066
// ---------------------------------------------------------------------------------------------------------------------
@@ -3200,6 +3159,7 @@ void Audio::processWebFile() {
3200
3159
if (f_webFileDataComplete && InBuff.bufferFilled () < InBuff.getMaxBlockSize ()) {
3201
3160
if (InBuff.bufferFilled ()) {
3202
3161
if (!readID3V1Tag ()) {
3162
+ if (m_validSamples) {playChunk (); return ;} // play samples first
3203
3163
int bytesDecoded = sendBytes (InBuff.getReadPtr (), InBuff.bufferFilled ());
3204
3164
if (bytesDecoded > 2 ) {
3205
3165
InBuff.bytesWasRead (bytesDecoded);
@@ -3233,17 +3193,7 @@ void Audio::processWebFile() {
3233
3193
3234
3194
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3235
3195
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 ();
3247
3197
}
3248
3198
return ;
3249
3199
}
@@ -3356,12 +3306,7 @@ void Audio::processWebStreamTS() {
3356
3306
3357
3307
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3358
3308
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 ();
3365
3310
}
3366
3311
return ;
3367
3312
}
@@ -3479,17 +3424,14 @@ void Audio::processWebStreamHLS() {
3479
3424
3480
3425
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3481
3426
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 ();
3488
3428
}
3489
3429
return ;
3490
3430
}
3491
3431
// ---------------------------------------------------------------------------------------------------------------------
3492
3432
void Audio::playAudioData () {
3433
+ if (m_validSamples) {playChunk (); return ;} // play samples first
3434
+
3493
3435
if (InBuff.bufferFilled () < InBuff.getMaxBlockSize ()) return ; // guard
3494
3436
3495
3437
int bytesDecoded = sendBytes (InBuff.getReadPtr (), InBuff.getMaxBlockSize ());
@@ -3506,7 +3448,7 @@ void Audio::playAudioData() {
3506
3448
InBuff.bytesWasRead (bytesDecoded);
3507
3449
return ;
3508
3450
}
3509
- if (bytesDecoded == 0 ) return ; // syncword at pos0 found
3451
+ if (bytesDecoded == 0 ) return ; // syncword at pos0
3510
3452
}
3511
3453
3512
3454
return ;
@@ -4245,6 +4187,7 @@ void Audio::setDecoderItems() {
4245
4187
}
4246
4188
// ---------------------------------------------------------------------------------------------------------------------
4247
4189
int Audio::sendBytes (uint8_t * data, size_t len) {
4190
+
4248
4191
int bytesLeft;
4249
4192
static bool f_setDecodeParamsOnce = true ;
4250
4193
int nextSync = 0 ;
@@ -4365,7 +4308,8 @@ int Audio::sendBytes(uint8_t* data, size_t len) {
4365
4308
audio_process_extern (m_outBuff, m_validSamples, &continueI2S);
4366
4309
if (!continueI2S) { return bytesDecoded; }
4367
4310
}
4368
- while (m_validSamples) { playChunk (); }
4311
+ m_curSample = 0 ;
4312
+ playChunk ();
4369
4313
return bytesDecoded;
4370
4314
}
4371
4315
// ---------------------------------------------------------------------------------------------------------------------
@@ -4500,7 +4444,7 @@ void Audio::printDecodeError(int r) {
4500
4444
case ERR_OPUS_INVALID_SAMPLERATE: e = " SAMPLERATE IS NOT 48000Hz" ; break ;
4501
4445
case ERR_OPUS_EXTRA_CHANNELS_UNSUPPORTED: e = " EXTRA CHANNELS UNSUPPORTED" ; break ;
4502
4446
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 ;
4504
4448
case ERR_OPUS_CELT_BAD_ARG: e = " CELT_DECODER_BAD_ARG" ; break ;
4505
4449
case ERR_OPUS_CELT_INTERNAL_ERROR: e = " CELT DECODER INTERNAL ERROR" ; break ;
4506
4450
case ERR_OPUS_CELT_UNIMPLEMENTED: e = " CELT DECODER UNIMPLEMENTED ARG" ; break ;
@@ -4835,7 +4779,7 @@ bool Audio::playSample(int16_t sample[2]) {
4835
4779
sample = IIR_filterChain2 (sample);
4836
4780
// -------------------------------------------
4837
4781
4838
- uint32_t s32 = Gain (sample); // vosample2lume ;
4782
+ uint32_t s32 = Gain (sample); // sample2volume ;
4839
4783
4840
4784
if (audio_process_i2s) {
4841
4785
// process audio sample just before writing to i2s
@@ -4847,16 +4791,15 @@ bool Audio::playSample(int16_t sample[2]) {
4847
4791
if (m_f_internalDAC) { s32 += 0x80008000 ; }
4848
4792
m_i2s_bytesWritten = 0 ;
4849
4793
#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 );
4851
4795
#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
4853
4797
#endif
4854
4798
if (err != ESP_OK) {
4855
4799
log_e (" ESP32 Errorcode %i" , err);
4856
4800
return false ;
4857
4801
}
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
4860
4803
return false ;
4861
4804
}
4862
4805
return true ;
0 commit comments