From a20b8a63da8f949c2b0bd42220fde66adfa8480b Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 10 Apr 2017 21:34:15 +0200 Subject: [PATCH] sound: first try --- README.md | 3 +++ src/main.cpp | 17 +++++++++++++++++ src/sdcard.cpp | 20 ++++++++++++++------ src/sdcard.h | 6 +++++- src/sound.cpp | 13 +++++++++---- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8921c84..2ccaf05 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,9 @@ pip install -U platformio platformio run -t upload && platformio device monitor -b 115200 ``` +* Convert mp3 files: `sox ~/Music/input.mp3 -c 1 -r 11025 output.wav trim 0 30` +* SD card is tested with one FAT16 partion + ## ToDo * Prototype * Keypad layout diff --git a/src/main.cpp b/src/main.cpp index c3986eb..d7ec437 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,8 @@ Keyboard keyboard; SDCard sdcard; Sound sound; +uint16_t buffer[2048]; + void setup() { // initialize LED digital pin as an output. @@ -32,6 +34,21 @@ void setup() keyboard.init(); sdcard.mount(); sound.init(); + sdcard.open("/sabaton.wav"); + int num_bytes_i = 0; + int num_bytes_o = 0; + while (sdcard.available()) { + num_bytes_i += sdcard.read(buffer, 2048); + num_bytes_o += sound.render_sample_block(buffer, buffer, 2048); + Serial.print("."); + } + sdcard.close(); + Serial.println(""); + Serial.print(num_bytes_i,DEC); + Serial.println(" bytes read from file"); + Serial.print(num_bytes_o,DEC); + Serial.println(" bytes written to i2s"); + delay(1000); } void loop() diff --git a/src/sdcard.cpp b/src/sdcard.cpp index 2bcb924..e4cda9c 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -11,6 +11,7 @@ #include "SPI.h" #include "sdcard.h" +#include "sound.h" using namespace std; @@ -55,15 +56,22 @@ void SDCard::umount() { SD.end(); } -void SDCard::readFile(const char *path){ - File file = fs.open(path); +void SDCard::open(const char *path) { + file = fs.open(path); if(!file){ Serial.println("Failed to open file for reading"); return; } +} - Serial.print("Read from file: "); - while(file.available()){ - Serial.write(file.read()); - } +void SDCard::close() { + file.close(); +} + +bool SDCard::available() { + return file.available(); +} + +size_t SDCard::read(uint16_t *buffer, size_t len){ + return file.read((uint8_t *)buffer, len); } diff --git a/src/sdcard.h b/src/sdcard.h index eb7d77d..8a80fda 100644 --- a/src/sdcard.h +++ b/src/sdcard.h @@ -11,11 +11,15 @@ class SDCard { SDCard(); void mount(); void umount(); - void readFile(const char *path); + void open(const char *path); + void close(); + size_t read(uint16_t *buffer, size_t len); + bool available(); private: fs::FS &fs = SD; uint8_t cardType; uint64_t cardSize; + File file; }; #endif /* _SDCARD_H */ diff --git a/src/sound.cpp b/src/sound.cpp index 38545e3..17b4148 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -17,7 +17,7 @@ #include -#define SAMPLE_RATE (36000) +#define SAMPLE_RATE (11025) #define WAVE_FREQ_HZ (100) //#define PI 3.14159265 @@ -88,12 +88,17 @@ void Sound::end() { /* callback(?) for new audio data */ int Sound::render_sample_block(uint16_t *sample_buf_left, uint16_t *sample_buf_right, int num_samples) { + TickType_t delay = 10 / portTICK_PERIOD_MS; // max delay: 10ms instead of portMAX_DELAY int num_pushed_samples = 0; for (int i=0; i < num_samples; i++) { unsigned int sample = ((unsigned short) sample_buf_left[i] << 16 & 0xffff0000) | ((unsigned short) sample_buf_right[i]); - int num_pushed_bytes = i2s_push_sample(i2s_num, (char *)&sample, portMAX_DELAY); - if (num_pushed_bytes > 0) num_pushed_samples += num_pushed_bytes; - else break; + int num_pushed_bytes = i2s_push_sample(i2s_num, (char *)&sample, delay); + if (num_pushed_bytes > 0) { + num_pushed_samples += num_pushed_bytes; + } else { + Serial.println("i2s buffer filled"); + break; + } } return num_pushed_samples; }