Browse Source

sound: first try

main
Hendrik Langer 8 years ago
parent
commit
a20b8a63da
  1. 3
      README.md
  2. 17
      src/main.cpp
  3. 18
      src/sdcard.cpp
  4. 6
      src/sdcard.h
  5. 13
      src/sound.cpp

3
README.md

@ -54,6 +54,9 @@ pip install -U platformio
platformio run -t upload && platformio device monitor -b 115200 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 ## ToDo
* Prototype * Prototype
* Keypad layout * Keypad layout

17
src/main.cpp

@ -18,6 +18,8 @@ Keyboard keyboard;
SDCard sdcard; SDCard sdcard;
Sound sound; Sound sound;
uint16_t buffer[2048];
void setup() void setup()
{ {
// initialize LED digital pin as an output. // initialize LED digital pin as an output.
@ -32,6 +34,21 @@ void setup()
keyboard.init(); keyboard.init();
sdcard.mount(); sdcard.mount();
sound.init(); 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() void loop()

18
src/sdcard.cpp

@ -11,6 +11,7 @@
#include "SPI.h" #include "SPI.h"
#include "sdcard.h" #include "sdcard.h"
#include "sound.h"
using namespace std; using namespace std;
@ -55,15 +56,22 @@ void SDCard::umount() {
SD.end(); SD.end();
} }
void SDCard::readFile(const char *path){ void SDCard::open(const char *path) {
File file = fs.open(path); file = fs.open(path);
if(!file){ if(!file){
Serial.println("Failed to open file for reading"); Serial.println("Failed to open file for reading");
return; return;
} }
}
void SDCard::close() {
file.close();
}
Serial.print("Read from file: "); bool SDCard::available() {
while(file.available()){ return file.available();
Serial.write(file.read());
} }
size_t SDCard::read(uint16_t *buffer, size_t len){
return file.read((uint8_t *)buffer, len);
} }

6
src/sdcard.h

@ -11,11 +11,15 @@ class SDCard {
SDCard(); SDCard();
void mount(); void mount();
void umount(); 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: private:
fs::FS &fs = SD; fs::FS &fs = SD;
uint8_t cardType; uint8_t cardType;
uint64_t cardSize; uint64_t cardSize;
File file;
}; };
#endif /* _SDCARD_H */ #endif /* _SDCARD_H */

13
src/sound.cpp

@ -17,7 +17,7 @@
#include <math.h> #include <math.h>
#define SAMPLE_RATE (36000) #define SAMPLE_RATE (11025)
#define WAVE_FREQ_HZ (100) #define WAVE_FREQ_HZ (100)
//#define PI 3.14159265 //#define PI 3.14159265
@ -88,12 +88,17 @@ void Sound::end() {
/* callback(?) for new audio data */ /* callback(?) for new audio data */
int Sound::render_sample_block(uint16_t *sample_buf_left, uint16_t *sample_buf_right, int num_samples) { 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; int num_pushed_samples = 0;
for (int i=0; i < num_samples; i++) { 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]); 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); int num_pushed_bytes = i2s_push_sample(i2s_num, (char *)&sample, delay);
if (num_pushed_bytes > 0) num_pushed_samples += num_pushed_bytes; if (num_pushed_bytes > 0) {
else break; num_pushed_samples += num_pushed_bytes;
} else {
Serial.println("i2s buffer filled");
break;
}
} }
return num_pushed_samples; return num_pushed_samples;
} }

Loading…
Cancel
Save