Trouble integrating Audio playback

joebmz98

Member
Hiya,

I posted about a similar topic not too long ago. Since there wasn't any new information, I changed method of audio playback to converting Wav files to CPP arrays (With the wav2sketch app). To get started, I opened up the Sample Player from the Teensy Examples ( it's under File/Examples/Audio/SamplePlayer ) and got that working pretty well including a envelope. I'm happy with how it's sounding but want to port it over to my main project file that I've been working on. Most of this has been OLED display work for menus etc. as well as setting up parameters for the project. I've copied over the structure and necessary parts and have made adjustments such as button pin number changes, but the audio doesn't seem to play in this bigger project.

For reference, I'm using the Teensy 4.0 with Audio shield, a series of encoders and an SSD1306 OLED display.

At first I thought it was to do with the audio player memory, but on both files, the value returns 1 despite not being able to hear it in one code.

I've attached both source codes in separate Zip files so they can be compared. I'll also attach sections of the main project that are relevant to the audio processing

Here is the Button setup:

Code:
// SETUP BUTTONS PINS
#define SMPL_BTN 23 // SMPL
#define ADSR_BTN 22 // ADSR
#define FLTR_BTN 14 // FLTR
#define FX_BTN 20 // FX

#define PLAYPOS1_BTN 11 // Trigger from POS 1 - THIS IS BEING USED TO PLAY THE FILE

#define SHIFT_BTN 13 // MOVE SHIFT PIN LATER

// SETUP BUTTONS
// 10ms Delay to stop bouncing switches
Bounce smplBTN = Bounce(SMPL_BTN,10);
Bounce adsrBTN = Bounce(ADSR_BTN,10);
Bounce fltrBTN = Bounce(FLTR_BTN,10);
Bounce fxBTN = Bounce(FX_BTN,10);

Bounce playPos1BTN = Bounce(PLAYPOS1_BTN,30);

Bounce shiftBTN = Bounce(SHIFT_BTN,10);

Here is the Audio Obj setup:
Code:
// ----------------------------------- //
// SETUP AUDIO DEVICES AND CONNECTIONS
AudioPlayMemory           sound0;
AudioEffectEnvelope       envelope;      // ADSR L SETUP
AudioMixer4               mix1;    // two 4-channel mixers are needed in
AudioOutputI2S            headphones;
AudioOutputAnalog         dac;     // play to both I2S audio board and on-chip DAC

// Create Audio connections between the components
//
AudioConnection c1(sound0, 0, envelope, 0);
AudioConnection c2(envelope, 0, mix1, 0);   // output of mix1 into 1st input on mix2
AudioConnection c3(mix1, 0, headphones, 0);
AudioConnection c4(mix1, 0, headphones, 1);
AudioConnection c5(mix1, 0, dac, 0);

// AUDIO ANALYZER // FOR A MINI OSCILLISCOPE LATER ON

// AUDIO SHIELD CONTROL
AudioControlSGTL5000     audioShield;     //xy=172.85714285714283,354.2857142857143
// --------------------------------------- //

Here's the buttons initialisation:
Code:
// BUTTON INIT
  pinMode(SMPL_BTN, INPUT_PULLUP); // smpl
  pinMode(ADSR_BTN, INPUT_PULLUP); // adsr
  pinMode(FLTR_BTN, INPUT_PULLUP); // fltr
  pinMode(FX_BTN, INPUT_PULLUP); // fx

  pinMode(PLAYPOS1_BTN, INPUT_PULLUP); // PLAY POS 1
  //pinMode(PLAYPOS2_BTN, INPUT_PULLUP); // PLAY POS 2
 
  pinMode(SHIFT_BTN, INPUT_PULLUP); // shift

  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(25);

  // turn on the output
  audioShield.enable();
  audioShield.volume(0.5);

  // reduce the gain on mixer channels, so more than 1
  // sound can play simultaneously without clipping
  mix1.gain(0, 0.5);

Along with the ADSR Initialisation:
Code:
// ADSR PARAMS
  attack = 10;
  decay = 10;
  //sustain = 50; // Divide by 10 later to do gains from 0 to 1
  release = 200;

  // ADSR SETUP
    envelope.delay(0);
    envelope.attack(attack);
    envelope.hold(0);
    envelope.decay(decay);
    envelope.sustain(0.8); // divide by 100 to scale
    envelope.release(release);

And the button updates as well as the detection of whether the button is pressed:

Code:
// CHECKING IF BUTTONS UPDATE
  smplBTN.update();
  adsrBTN.update();
  fltrBTN.update();
  fxBTN.update();

  playPos1BTN.update();

  shiftBTN.update();
 
 
  if (playPos1BTN.fallingEdge()) {
    sound0.play(AudioSample_dmaj_piano_160bpm_monowa);
    envelope.noteOn();
      Serial.println("-----");
      Serial.println("playing Sample");
      Serial.println("-----");
      long audioMem = AudioMemoryUsage();
      Serial.println(audioMem);

  }

I apologise that there's a lot here, but any pointers to where I'm going wrong would be great :)

I also haven't figured out if I need to include an AudioInterrupts() in the setup()
 

Attachments

  • oled_withSampleFromMEM.zip
    131.6 KB · Views: 32
  • SamplePlayer_edits.zip
    365.1 KB · Views: 34
I think you're going to need to narrow your question down and provide a relatively small sketch that shows the problem you are trying to solve. Your original post is too big for anyone to answer without investing a lot of their own time and energy.
 
I tried to be as concise as possible. I've got a very large project file and so I dissected all of the relevant audio parts to make it readable. I also included the full code as a zip in case there was anything I've missed. Not sure how I can make it more concise than this tbh
 
Back
Top