esp32 soundboard project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.4 KiB

/**
8 years ago
* ESP32 Soundboard
*
8 years ago
* Hendriks Projekt
*
8 years ago
* https://dev.xd0.de/hendrik/soundboard
*/
#include "Arduino.h"
#include "hardware.h"
8 years ago
#include "keyboard.h"
8 years ago
#include "sdcard.h"
#include "sound.h"
#include "ringbuf.h"
8 years ago
#define VERSION "0.0"
#define WIFI_SSID "ssid"
#define WIFI_PASSWORD "password"
8 years ago
Keyboard keyboard;
8 years ago
SDCard sdcard;
Sound sound;
Ringbuf ringbuf = Ringbuf((size_t) BUF_LENGTH);
8 years ago
8 years ago
TaskHandle_t xTaskRead = NULL;
TaskHandle_t xTaskWrite = NULL;
bool playing = false;
static const char *soundFile[3][3] = {{"/T0.wav", "/T1.wav", "/T2.wav"},
{"/T3.wav", "/T4.wav", "/T5.wav"},
{"/T6.wav", "/T7.wav", "/T8.wav"}};
void i2s_write_task(void *pvParameter) {
sound.play();
for ( ;; ) {
//TickType_t delay = 10 / portTICK_PERIOD_MS; // max delay: 10ms instead of portMAX_DELAY
uint16_t *start_ptr = ringbuf.getRead();
size_t size_avail = ringbuf.getReadAvail();
int num_samples = 0;
for (int i=0; i<size_avail; i++) {
unsigned int sample = ((unsigned short) start_ptr[i] << 16 & 0xffff0000) | ((unsigned short) start_ptr[i]);
int num_pushed_bytes = i2s_push_sample(sound.i2s_num, (char *)&sample, 0);
num_samples += num_pushed_bytes/4;
}
ringbuf.setRead(num_samples);
// Serial.print(num_samples,DEC);
// Serial.println(" pushed samples to i2s");
if (!ringbuf.active && ringbuf.getReadAvail() == 0) break; // End
vTaskDelay(20 / portTICK_PERIOD_MS);
}
sound.stop();
vTaskDelete( NULL );
}
void sd_read_task(void *pvParameters) {
char *path = (char *) pvParameters;
Serial.println("sdcard: opening File");
sdcard.open(path);
ringbuf.active = true;
xTaskCreate(&i2s_write_task, "write_task", 1024, NULL, 5, &xTaskWrite);
for ( ;; ) {
uint16_t *start_ptr = ringbuf.getWrite();
size_t size_avail = ringbuf.getWriteAvail();
int num_samples = 0;
if (size_avail > 0) {
num_samples = sdcard.read(start_ptr, size_avail);
ringbuf.setWrite(num_samples);
}
Serial.print(num_samples,DEC);
Serial.println(" read samples from sdcard");
if (!sdcard.available()) break; // EOF
vTaskDelay(20 / portTICK_PERIOD_MS); // 20ms
}
8 years ago
ringbuf.active = false;
Serial.println("sdcard: EOF");
sdcard.close();
vTaskDelete( NULL );
}
8 years ago
8 years ago
void setup()
{
// initialize LED digital pin as an output.
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("-------------------------------------");
Serial.print("Soundboard v.");
Serial.println(VERSION);
Serial.println("https://dev.xd0.de/hendrik/soundboard");
Serial.println("-------------------------------------");
keyboard.init();
8 years ago
sdcard.mount();
sound.init();
8 years ago
static const char *bootSound = "/boot.wav";
8 years ago
xTaskCreate(&sd_read_task, "read_task", 2048, (void*)bootSound, 5, &xTaskRead);
}
void loop()
{
8 years ago
bool touched = false;
for(int i=0; i<=9; i++) {
if (keyboard.getTouchDetected(i) == true) {
8 years ago
touched = true;
8 years ago
Serial.println("touch");
if (sound.playing == false) {
Serial.println("new task");
xTaskCreate(&sd_read_task, "read_task", 2048, (void*)soundFile[i/3][i%3], 5, &xTaskRead);
}
8 years ago
}
8 years ago
}
8 years ago
if (touched == true) {
// turn the LED on (HIGH is the voltage level)
digitalWrite(LED_PIN, HIGH);
} else {
// turn the LED off by making the voltage LOW
digitalWrite(LED_PIN, LOW);
}
8 years ago
// wait for a second
8 years ago
delay(50);
}