Hello, I'm using the Teensy 4.1 with the PT8211 audio kit. I'm trying to figure out what I'm doing wrong here: I'm trying to make a simple program where when I press a key on my midi keyboard the teensy will play a pre-defined sample, however when I do this I can only hear a very faint click in my headphones. From the serial output it is clear that the MIDI is working fine but something is happening when the wav file is playing. Here is my code:
Sample output:
File exists.
Playing from SD card...
Done playing.
Note On, ch=1, note=62, velocity=63
Playing from SD card...
Done playing.
Note On, ch=1, note=53, velocity=121
Playing from SD card...
Done playing.
Code:
#include <USBHost_t36.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1; //xy=272,942
AudioOutputPT8211_2 pt8211_2_1; //xy=557,951
AudioConnection patchCord1(playSdWav1, 0, pt8211_2_1, 0);
AudioConnection patchCord2(playSdWav1, 1, pt8211_2_1, 1);
// GUItool: end automatically generated code
// USB
USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
MIDIDevice midi1(myusb);
const char* wavFile = "1B.WAV";
void play() {
if (!playSdWav1.isPlaying()) {
Serial.println("Playing from SD card...");
if (!playSdWav1.play(wavFile)) {
Serial.println("Could not play from SD card!");
return;
}
delay(5);
while (playSdWav1.isPlaying()) {
Serial.println("Playing...");
delay(100);
}
Serial.println("Done playing.");
}
}
void myNoteOn(byte channel, byte note, byte velocity) {
Serial.print("Note On, ch=");
Serial.print(channel, DEC);
Serial.print(", note=");
Serial.print(note, DEC);
Serial.print(", velocity=");
Serial.println(velocity, DEC);
play();
}
void setup() {
Serial.begin(115200);
myusb.begin();
midi1.setHandleNoteOn(myNoteOn);
if (!(SD.begin(BUILTIN_SDCARD))) {
Serial.println("Unable to access the SD card");
}
if (SD.exists(wavFile)) {
Serial.println("File exists.");
play();
}
else {
Serial.println("File doesn't exist.");
}
}
void loop() {
myusb.Task();
midi1.read();
}
Sample output:
File exists.
Playing from SD card...
Done playing.
Note On, ch=1, note=62, velocity=63
Playing from SD card...
Done playing.
Note On, ch=1, note=53, velocity=121
Playing from SD card...
Done playing.