Skip to content

Commit 907b04e

Browse files
committed
new conversion wav 8 to 16 bit
1 parent 4607c5e commit 907b04e

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/Audio.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Created on: Oct 28.2018
55
*
6-
* Version 3.0.13p
7-
* Updated on: Nov 08.2024
6+
* Version 3.0.13q
7+
* Updated on: Nov 09.2024
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -2320,7 +2320,7 @@ void Audio::playChunk() {
23202320
size_t i2s_bytesConsumed = 0;
23212321
int16_t* sample[2] = {0};
23222322
int16_t* s2;
2323-
int sampleSize = (m_bitsPerSample / 8);
2323+
int sampleSize = 2; // 2 bytes per sample, int16_t
23242324
esp_err_t err = ESP_OK;
23252325
int i= 0;
23262326

@@ -2329,17 +2329,20 @@ void Audio::playChunk() {
23292329
if(m_bitsPerSample == 8){
23302330
if(getChannels() == 1){
23312331
for (int i = m_validSamples - 1; i >= 0; --i) { // uint8_t --> int16_t
2332-
m_outBuff[4 * i + 0] = m_outBuff[4 * i + 1] = (m_outBuff[i] & 0xFF00) - 0x8000;
2333-
m_outBuff[4 * i + 2] = m_outBuff[4 * i + 3] = ((m_outBuff[i] & 0x00FF) << 8) - 0x8000;
2332+
uint8_t sample1 = m_outBuff[i] & 0xFF;
2333+
uint8_t sample2 = (m_outBuff[i] >> 8) & 0xFF;
2334+
m_outBuff[4 * i + 0] = m_outBuff[4 * i + 1] = (int16_t)((sample1 - 128) * 256);
2335+
m_outBuff[4 * i + 2] = m_outBuff[4 * i + 3] = (int16_t)((sample2 - 128) * 256);
23342336
}
2335-
m_validSamples *= 4;
2337+
m_validSamples *= 2;
23362338
}
23372339
if(getChannels() == 2){
23382340
for (int i = m_validSamples - 1; i >= 0; --i) {
2339-
m_outBuff[2 * i] = (m_outBuff[i] & 0xFF00) - 0x8000;
2340-
m_outBuff[2 * i + 1] = ((m_outBuff[i] & 0x00FF) << 8) - 0x8000;
2341+
uint8_t sample1 = m_outBuff[i] & 0xFF;
2342+
uint8_t sample2 = (m_outBuff[i] >> 8) & 0xFF;
2343+
m_outBuff[2 * i] = (int16_t)((sample1 - 128) * 256);
2344+
m_outBuff[2 * i + 1] = (int16_t)((sample2 - 128) * 256);
23412345
}
2342-
m_validSamples *= 2;
23432346
}
23442347
}
23452348

@@ -2404,7 +2407,6 @@ void Audio::playChunk() {
24042407
if(m_validSamples < 0) { m_validSamples = 0; }
24052408
if(m_validSamples == 0) { count = 0; }
24062409

2407-
24082410
// ---- statistics, bytes written to I2S (every 10s)
24092411
// static int cnt = 0;
24102412
// static uint32_t t = millis();

src/Audio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* Created on: Oct 28,2018
66
*
7-
* Version 3.0.13o
8-
* Updated on: Nov 08.2024
7+
* Version 3.0.13q
8+
* Updated on: Nov 09.2024
99
* Author: Wolle (schreibfaul1)
1010
*/
1111

0 commit comments

Comments
 (0)