New to Teensy and Audio Please Help

nikolasdove

New member
Hi, super new here. I'm making a recording device for a school project with a Teensy 4.1 + Audio Module Rev D and I have a few questions:

First, do I need to use the Line In pins on the Audio Module and use an additional jack like a 3.5mm trrs breakout board to those pins or can I program the existing jack that's already on the board as a recording input? I'm trying to go from headphone out of a computer to an input on the Teensy and record .wav straight to the microSD card on the board.

Second, I have seen the Recorder code that's posted on GitHub but noticed that it's for one of the v3 boards. Can I copy and paste that code for my 4.1 board and will that communicate with the microSD slot already embedded on the board?

The first bit of code I tried lets me use one button as a toggle switch to Start / Stop recording and when I tried it creates a "recording.wav" file but it is corrupt and can not be opened. I have 3.5mm aux cord from computer headphones out to trrs breakout board line in on the audio module and microSD in the Teensy 4.1. To be fair most of this code is ChatGPT slightly modified adding a sample toggle switch code in the void loop I found on an Arduino forum.

Like I said I am super new to this so don't roast me too hard lol.

Code:
#include <Audio.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>

// Audio objects
AudioInputI2S audioInput;
AudioRecordQueue queue1;
AudioOutputI2S audioOutput;

// Record queue buffer
AudioConnection patchCord1(audioInput, 0, queue1, 0);

// SD card parameters
const int chipSelect = BUILTIN_SDCARD;

// WAV header structure
typedef struct {
  char riff[4];
  uint32_t overall_size;
  char wave[4];
  char fmt_chunk_marker[4];
  uint32_t length_of_fmt;
  uint16_t format_type;
  uint16_t channels;
  uint32_t sample_rate;
  uint32_t byterate;
  uint16_t block_align;
  uint16_t bits_per_sample;
  char data_chunk_header[4];
  uint32_t data_size;
} wav_header;

// Button pin
const int buttonPin = 31;  // Change this to your desired pin
int val = 0;
int old_val = 0;
int state = 0;

// LED pin
int recLED = 33;

// Recording state
bool isRecording = false;

// Audio file variable
File audioFile;

void setup() {
  Serial.begin(9600);
  pinMode(recLED, OUTPUT);
  pinMode(buttonPin, INPUT);  // Set button pin as input with pull-down resistor
  AudioMemory(30);
  SD.begin(chipSelect);
  stopRecording();
}

void loop() {
  val = digitalRead(buttonPin);
  if ((val == HIGH) && (old_val == LOW)) {
    state = 1 - state;
  }

  old_val = val;
   if (state == 1) {
    digitalWrite(recLED, HIGH);
    startRecording();
      Serial.println("Recording . . .");
  } else {
    digitalWrite(recLED, LOW);
    stopRecording();
      Serial.println("Recording stopped");
  }
}

void startRecording() {
  audioFile = SD.open("recording.wav", FILE_WRITE);
  if (audioFile) {
    // Write WAV header (same as before)
    wav_header wavHeader;
    // Writing WAV header code...

    audioFile.write((byte*)&wavHeader, sizeof(wavHeader));

    // Set recording state to true
    isRecording = true;
  } else {
    Serial.println("Error opening file for recording");
  }
}

void stopRecording() {
  isRecording = false;
  // Close the audio file
  audioFile.close();
}
 
Back
Top