better audio with only a few glitches left

This commit is contained in:
Sebastian
2025-06-15 02:52:54 +02:00
parent 76f2e0ec28
commit ecbd6ab3ee
3 changed files with 27 additions and 13 deletions

View File

@ -17,7 +17,8 @@ class RingBuffer{
bool push(T data){ bool push(T data){
if(counter < bufferSize){ if(counter < bufferSize){
buffer[write] = data; buffer[write] = data;
write = (write+1) % bufferSize; write++; // % bufferSize;
if(write == bufferSize) write = 0;
counter++; counter++;
return true; return true;
} }
@ -25,11 +26,12 @@ class RingBuffer{
} }
T pop(){ T pop(){
T retval; T retval = 0;
if(counter > 0) { if(counter > 0) {
counter--; counter--;
retval = buffer[read]; retval = buffer[read];
read = (read+1) % bufferSize; read++;// % bufferSize;
if(read == bufferSize) read = 0;
} }
return retval; return retval;
} }

View File

@ -18,7 +18,7 @@
#define AURAL 1 #define AURAL 1
#define UI_SAMPLERATE 22050 #define UI_SAMPLERATE 22050
#define BUFFERSIZE 1024 #define BUFFERSIZE 64
#define NSTREAMS 8 #define NSTREAMS 8
int16_t buffer[BUFFERSIZE]; int16_t buffer[BUFFERSIZE];
@ -100,7 +100,7 @@ void codec_transmit() {
int16_t sample = 0; int16_t sample = 0;
int j = active % NSTREAMS; int j = active % NSTREAMS;
sample = stream[j].get(); sample = stream[j].get();
buffer[i] += sample; buffer[i] = sample >> 2;
} }
i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t)); i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t));
@ -236,12 +236,12 @@ void setup() {
i2s.setDIN(14); i2s.setDIN(14);
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1 i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
i2s.setMCLK(18); i2s.setMCLK(18);
i2s.setMCLKmult(256); // 12.288.000Hz i2s.setMCLKmult(512); // 256 = 12.288.000Hz 512 = 25Mhz
i2s.swapClocks(); i2s.swapClocks();
i2s.setBitsPerSample(16); i2s.setBitsPerSample(16);
i2s.setBuffers(4, BUFFERSIZE * sizeof(int16_t) / sizeof(uint32_t)); i2s.setBuffers(6, BUFFERSIZE * sizeof(int16_t) / sizeof(uint32_t));
if(!i2s.begin(48000)){ if(!i2s.begin(48000)){
Serial.println("I2S error!"); Serial.println("I2S error!");
@ -269,7 +269,8 @@ void setup() {
Serial.print(stream[i].wavefile.blockalign); Serial.print(stream[i].wavefile.blockalign);
Serial.println(" bytes"); Serial.println(" bytes");
//stream[i].play(); stream[i].play();
stream[i].wavefile.loop = true;
} else { } else {
Serial.println("file loading error"); Serial.println("file loading error");
} }
@ -423,13 +424,24 @@ void loop() {
if(i2s.getOverflow()) Serial.println("overflow"); if(i2s.getOverflow()) Serial.println("overflow");
if(i2s.getUnderflow()) Serial.println("underflow"); if(i2s.getUnderflow()) Serial.println("underflow");
delay(20); // wait 1ms //delay(20); // wait 1ms
} }
void setup1(){ void setup1(){
// while(1){
// if(streams_loaded){
// int16_t l, r;
// i2s.read16(&l, &r);
// int j = active % NSTREAMS;
// l = stream[j].get() >> 2;
// r = stream[j].get() >> 2;
// i2s.write16(l, r);
// }
// }
} }
void loop1(){ void loop1(){
} }

View File

@ -110,7 +110,7 @@ class WaveStream{
WaveStream(){} WaveStream(){}
void begin(){ void begin(){
wavefile.buffer.setSize(16384); wavefile.buffer.setSize(24000);
wavefile.buffer.begin(); wavefile.buffer.begin();
} }
@ -131,7 +131,7 @@ class WaveStream{
void stream(){ void stream(){
if(!wavefile.buffer.isFull() && playing){ if(!wavefile.buffer.isFull() && playing){
int cnt = 0; int cnt = 0;
while (!wavefile.buffer.isFull() && cnt < 4096) { while (!wavefile.buffer.isFull() && cnt < 6000) {
wavefile.readblock(); wavefile.readblock();
cnt++; cnt++;
} }