From ecbd6ab3ee87a81baf710e049724439ad4f275b6 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 15 Jun 2025 02:52:54 +0200 Subject: [PATCH] better audio with only a few glitches left --- soundcube-firmware/ringbuffer.h | 8 ++++--- soundcube-firmware/soundcube-firmware.ino | 28 ++++++++++++++++------- soundcube-firmware/wavestream.h | 4 ++-- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/soundcube-firmware/ringbuffer.h b/soundcube-firmware/ringbuffer.h index decd4b1..b80bfa5 100644 --- a/soundcube-firmware/ringbuffer.h +++ b/soundcube-firmware/ringbuffer.h @@ -17,7 +17,8 @@ class RingBuffer{ bool push(T data){ if(counter < bufferSize){ buffer[write] = data; - write = (write+1) % bufferSize; + write++; // % bufferSize; + if(write == bufferSize) write = 0; counter++; return true; } @@ -25,11 +26,12 @@ class RingBuffer{ } T pop(){ - T retval; + T retval = 0; if(counter > 0) { counter--; retval = buffer[read]; - read = (read+1) % bufferSize; + read++;// % bufferSize; + if(read == bufferSize) read = 0; } return retval; } diff --git a/soundcube-firmware/soundcube-firmware.ino b/soundcube-firmware/soundcube-firmware.ino index cf856ab..a7b1ee7 100644 --- a/soundcube-firmware/soundcube-firmware.ino +++ b/soundcube-firmware/soundcube-firmware.ino @@ -18,7 +18,7 @@ #define AURAL 1 #define UI_SAMPLERATE 22050 -#define BUFFERSIZE 1024 +#define BUFFERSIZE 64 #define NSTREAMS 8 int16_t buffer[BUFFERSIZE]; @@ -100,7 +100,7 @@ void codec_transmit() { int16_t sample = 0; int j = active % NSTREAMS; sample = stream[j].get(); - buffer[i] += sample; + buffer[i] = sample >> 2; } i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t)); @@ -236,12 +236,12 @@ void setup() { i2s.setDIN(14); i2s.setBCLK(16); // Note: LRCLK = BCLK + 1 i2s.setMCLK(18); - i2s.setMCLKmult(256); // 12.288.000Hz + i2s.setMCLKmult(512); // 256 = 12.288.000Hz 512 = 25Mhz i2s.swapClocks(); 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)){ Serial.println("I2S error!"); @@ -269,7 +269,8 @@ void setup() { Serial.print(stream[i].wavefile.blockalign); Serial.println(" bytes"); - //stream[i].play(); + stream[i].play(); + stream[i].wavefile.loop = true; } else { Serial.println("file loading error"); } @@ -423,13 +424,24 @@ void loop() { if(i2s.getOverflow()) Serial.println("overflow"); if(i2s.getUnderflow()) Serial.println("underflow"); - delay(20); // wait 1ms + //delay(20); // wait 1ms + } 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(){ - } \ No newline at end of file diff --git a/soundcube-firmware/wavestream.h b/soundcube-firmware/wavestream.h index ba32583..f42d01d 100644 --- a/soundcube-firmware/wavestream.h +++ b/soundcube-firmware/wavestream.h @@ -110,7 +110,7 @@ class WaveStream{ WaveStream(){} void begin(){ - wavefile.buffer.setSize(16384); + wavefile.buffer.setSize(24000); wavefile.buffer.begin(); } @@ -131,7 +131,7 @@ class WaveStream{ void stream(){ if(!wavefile.buffer.isFull() && playing){ int cnt = 0; - while (!wavefile.buffer.isFull() && cnt < 4096) { + while (!wavefile.buffer.isFull() && cnt < 6000) { wavefile.readblock(); cnt++; }