dac working, wave playback broken WIP

This commit is contained in:
Sebastian
2025-06-11 01:59:23 +02:00
parent ac9d4b5e6b
commit c1e204953f
2 changed files with 71 additions and 65 deletions

View File

@ -1,15 +1,17 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <ArduinoJson.hpp> #include <ArduinoJson.hpp>
#include <TCA9555.h> #include <TCA9555.h>
#include <AS5600.h> #include <AS5600.h>
#include <DAC8552.h>
#include <FastLED.h> #include <FastLED.h>
#include <PWMAudio.h> #include <PWMAudio.h>
#include <I2S.h> #include <I2S.h>
#include <SPI.h> #include <SPI.h>
#include <SD.h> #include <SD.h>
#include "codec.h" #include "codec.h"
#include "ringbuffer.h"
#include "wavestream.h" #include "wavestream.h"
#define HAPTIC 1 #define HAPTIC 1
@ -25,9 +27,7 @@
int16_t buffer[SIZE]; int16_t buffer[SIZE];
int16_t buffer2[ECHO]; int16_t buffer2[ECHO];
RingBuffer<int16_t> ringbuffer[NSTREAMS]; WaveStream stream[NSTREAMS];
WaveStream stream;
File streams[NSTREAMS]; File streams[NSTREAMS];
bool streams_loaded = false; bool streams_loaded = false;
@ -39,6 +39,8 @@ TLV320AIC3204 codec;
TCA9555 TCA(0x20, &Wire1); TCA9555 TCA(0x20, &Wire1);
AS5600 ENC(&Wire1); AS5600 ENC(&Wire1);
DAC8552 dac(9, &SPI1);
PWMAudio ui_snd(8); PWMAudio ui_snd(8);
enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3}; enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3};
@ -111,11 +113,9 @@ void codec_transmit() {
for(int i = 0; i < count; i++){ for(int i = 0; i < count; i++){
int16_t sample = 0; int16_t sample = 0;
// for(int j = 0; j < NSTREAMS; j++){ int j = active % NSTREAMS;
int j = active % NSTREAMS; sample = stream[j].get();
if(!ringbuffer[j].isEmpty()) sample = ringbuffer[j].pop(); buffer[i] += (sample);
buffer[i] += (sample);
// }
} }
i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t)); i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t));
@ -160,9 +160,11 @@ void setup() {
i2s.setFrequency(48000); i2s.setFrequency(48000);
for(int i = 0; i < NSTREAMS; i++){ SPI1.setSCK(10);
ringbuffer[i].setSize(RINGBUFFER); SPI1.setTX(11);
} SPI1.begin();
dac.begin();
FastLED.addLeds<NEOPIXEL, 4>(edge_leds, 11); FastLED.addLeds<NEOPIXEL, 4>(edge_leds, 11);
FastLED.addLeds<NEOPIXEL, 5>(ui_leds, 74); FastLED.addLeds<NEOPIXEL, 5>(ui_leds, 74);
@ -256,10 +258,6 @@ void setup() {
i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t)); i2s.setBuffers(6, SIZE * sizeof(int16_t) / sizeof(uint32_t));
for(int i = 0; i < NSTREAMS; i++){
ringbuffer[i].begin();
}
if(!i2s.begin(48000)){ if(!i2s.begin(48000)){
Serial.println("I2S error!"); Serial.println("I2S error!");
while(100); while(100);
@ -270,8 +268,9 @@ void setup() {
char filename[40]; char filename[40];
sprintf(filename, "/sound/%d.wav", i+1); sprintf(filename, "/sound/%d.wav", i+1);
if(SD.exists(filename)){ if(SD.exists(filename)){
streams[i] = SD.open(filename); stream[i].load(SD.open(filename));
streams[i].seek(44, SeekSet); stream[i].begin();
stream[i].play();
} else { } else {
for(int k = 0; k < 3; k++){ for(int k = 0; k < 3; k++){
digitalWrite(6, HIGH); digitalWrite(6, HIGH);
@ -284,16 +283,6 @@ void setup() {
streams_loaded = true; streams_loaded = true;
} }
// if(!ringbuffer.isFull()){
// int cnt = 0;
// while (!ringbuffer.isFull() && cnt < RINGBUFFER) {
// int16_t sample = stream1.read();
// ringbuffer.push(sample);
// if(!stream1.available()) stream1.seek(48000*6, SeekSet);
// cnt++;
// }
// }
digitalWrite(6, HIGH); digitalWrite(6, HIGH);
delay(25); delay(25);
digitalWrite(6, LOW); digitalWrite(6, LOW);
@ -304,6 +293,8 @@ void setup() {
} }
bool dactest = false;
void loop() { void loop() {
position = ENC.getCumulativePosition(); position = ENC.getCumulativePosition();
@ -386,11 +377,19 @@ void loop() {
speaker(speakerToggle); speaker(speakerToggle);
} }
if(buttons[DEBUG2]) {
dactest = !dactest;
dac.setValue(0, dactest ? 0 : 32768);
dac.setValue(1, !dactest ? 0 : 65535);
}
Serial.println(codec.getVolumeL()); Serial.println(codec.getVolumeL());
buttonChanged = false; buttonChanged = false;
} }
//ac.setValue(0, dactest ? 0 : sin((float)millis() / 100.0f) * 32768 + 32768);
// empty LED matrix // empty LED matrix
for (int i = 0; i < 13; i++) { for (int i = 0; i < 13; i++) {
ui_leds[lut_matrix[i]] = CRGB(0, 0, 0); ui_leds[lut_matrix[i]] = CRGB(0, 0, 0);
@ -399,14 +398,6 @@ void loop() {
// set active LED matrix LED // set active LED matrix LED
ui_leds[lut_matrix[active]] = CRGB(100, 100, 100); ui_leds[lut_matrix[active]] = CRGB(100, 100, 100);
if(active == 1) {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
} else {
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
if(position < 0) position += 4096; if(position < 0) position += 4096;
active_led_ring = (position / 32) % 48; active_led_ring = (position / 32) % 48;
@ -424,5 +415,9 @@ void setup1(){
} }
void loop1(){ void loop1(){
if(streams_loaded) {
for(int i = 0; i < NSTREAMS; i++){
stream[i].stream();
}
}
} }

View File

@ -64,15 +64,22 @@ struct WaveFile{
return true; return true;
} }
bool readblock(int16_t samplebuffer[]){ void readblock(){
uint8_t samplebyte[blockalign]; uint8_t samplebyte[blockalign];
file.read(samplebyte, blockalign); file.read(samplebyte, blockalign);
if(!file.available() && loop) file.seek(44, SeekSet); if(!file.available() && loop) file.seek(44, SeekSet);
int16_t sample = (samplebyte[1] << 8) + samplebyte[0]; int16_t sample = (samplebyte[1] << 8) + samplebyte[0];
ringbuffer[i].push(sample); buffer.push(sample);
}
int16_t get(){
return buffer.pop();
}
void stop(){
file.seek(44, SeekSet);
} }
bool loop = false; bool loop = false;
@ -88,39 +95,43 @@ struct WaveFile{
class WaveStream{ class WaveStream{
public: public:
WaveStream(){} WaveStream(){}
void begin(){} void begin(){
void update(){} wavefile.buffer.setSize(8192);
}
bool load(int stream, File wavefile){ bool load(File _wavefile){
if(stream < 0 || stream > 7) return false; if(!wavefile.load(_wavefile)) return false;
if(!wavefiles[stream].load(wavefile)) return false; return true;
return true; }
}
void start(int stream){} void play(){playing = true;}
void stop(int stream){} void stop(){
void pause(int stream){} playing = false;
wavefile.stop();
}
void pause(){playing = false;}
void stopAll(){} void stream(){
void startAll(){} if(!wavefile.buffer.isFull() && playing){
void pauseAll(){}
private:
WaveFile wavefiles[8];
void stream(){
for (int i = 0; i < NSTREAMS; i++) {
if(!wavefiles[i].buffer.isFull()){
int cnt = 0; int cnt = 0;
while (!wavefiles[i].buffer.isFull() && cnt < 10000) { while (!wavefile.buffer.isFull() && cnt < 10000) {
wavefiles[i].readblock(samplebyte); wavefile.readblock();
cnt++; cnt++;
} }
} }
} }
}
int16_t get(){
return playing ? wavefile.get() : 0;
}
private:
WaveFile wavefile;
bool playing = false;
}; };