Passing file names to audio play memory play() function

Status
Not open for further replies.

ZachD

Member
Hi there -

I'm working on a project with three FSRs as drum pads with the teensy audio shield. I also have a tactile button in my circuit trigger a change in the set of samples the pads control. I've been building off of the example sampler code and only now hit a snag when trying to pass a filename as a string to playMem. play(). The compiler error reads: "No matching function call to AudioPlayMemory:: play(String&)"

Looking into it a bit more, AudioPlayMemory:: play expects a const unsigned int. Any ideas on how to pass file names to this function to get around this error? My current code below.

Thanks!
Zach

Code:
// Advanced Microcontroller-based Audio Workshop
//
// http://www.pjrc.com/store/audio_tutorial_kit.html
// https://hackaday.io/project/8292-microcontroller-audio-workshop-had-supercon-2015
//
// Part 2-3: Playing Samples

// WAV files converted to code by wav2sketch
#include "AudioSampleSnare.h"        // http://www.freesound.org/people/KEVOY/sounds/82583/
#include "AudioSampleTomtom.h"       // http://www.freesound.org/people/zgump/sounds/86334/
#include "AudioSampleHihat.h"        // http://www.freesound.org/people/mhc/sounds/102790/
#include "AudioSampleKick.h"         // http://www.freesound.org/people/DWSD/sounds/171104/
#include "AudioSampleGong.h"         // http://www.freesound.org/people/juskiddink/sounds/86773/
#include "AudioSampleCashregister.h" // http://www.freesound.org/people/kiddpark/sounds/201159/
#include "AudioSampleD_steelpan.h"
#include "AudioSampleF_steelpan.h"
#include "AudioSampleA_steelpan.h"
#include "AudioSampleB_steelpan.h"
#include <Bounce.h>


///////////////////////////////////
// copy the Design Tool code here
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlayMemory          playMem2;       //xy=125,198
AudioPlayMemory          playMem1;       //xy=134,144
AudioPlayMemory          playMem3;       //xy=140,251
AudioPlayMemory          playMem4;       //xy=148,317
AudioMixer4              mixer1;         //xy=384,161
AudioOutputI2S           i2s1;           //xy=541,144
AudioConnection          patchCord1(playMem2, 0, mixer1, 1);
AudioConnection          patchCord2(playMem1, 0, mixer1, 0);
AudioConnection          patchCord3(playMem3, 0, mixer1, 2);
AudioConnection          patchCord4(playMem4, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=444,353
// GUItool: end automatically generated code

///////////////////////////////////


// Bounce objects to read pushbuttons
Bounce button0 = Bounce(15, 15);

String drumSamples[3][3] = {
  {"AudioSampleSnare", "AudioSampleTomtom", "AudioSampleKick"},
  {"AudioSampleD_steelpan", "AudioSampleF_steelpan", "AudioSampleA_steelpan"}
};

int button2 = A3;
int pad = 0;
int pad2 =0;
int pad3 =0;

int lastpadVal=0;
int lastpad2Val=0;
int lastpad3Val=0;

int led = 13;


//int inputPin = A0;

int buttonPushCounter = 0;

void setup() {
  pinMode(15, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  pinMode(17, INPUT);
  pinMode(20, INPUT);
  pinMode(21, INPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
//  Serial.print(drumSamples[0]);
  AudioMemory(10);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  mixer1.gain(0, 0.4);
  mixer1.gain(1, 0.4);
  mixer1.gain(2, 0.4);
  mixer1.gain(3, 0.4);
}

void loop() {
//check button state to see which set of samples we should trigger
  button0.update();
  if(button0.fallingEdge()){
    buttonPushCounter++;
  }

//read first drum pad and scale its volume based on the velocity     
  pad = analogRead(A3);
  float vol = pad / 1280.0;
  vol = map(vol,.1,.9,.3,1.0);

//function to trigger just on edge detection (of sorts)
  if(abs(pad-lastpadVal>30)){
    mixer1.gain(2, vol);
    String thisSample = sampleLookUp(0);
    Serial.println(thisSample); // this prints the correct file name 
    playMem3.play(thisSample);  // this line throws a comiler error  
  }
  lastpadVal = pad;

// commenting out the next to pads to isolate my compiler issues
/*  pad2 = analogRead(A6);
//
//  float vol2 = pad2 / 1280.0;
//  vol2 = map(vol2,.01,.5,.3,2.0);
//
//    //function to trigger just on edge detection (of sorts)
//  if(abs(pad2-lastpad2Val>30)){
//    mixer1.gain(1, vol2);
//    playMem2.play(AudioSampleKick);
//  }
//  lastpad2Val = pad2;
//
//  pad3 = analogRead(A7);
//
//  float vol3 = pad3 / 1280.0;
//  vol3 = map(vol3,.01,.5,.3,2.0);
//
//    //function to trigger just on edge detection (of sorts)
//  if(abs(pad3-lastpad3Val>30)){
//    mixer1.gain(3, vol3);
//    playMem4.play(AudioSampleTomtom);
//  }
//  lastpad3Val = pad3;
*/
}

String sampleLookUp(int padLocation){
    if(buttonPushCounter % 2 == 0){
      return drumSamples[1][padLocation];  
  }
    else return drumSamples[0][padLocation];
}
 
Thanks, Manitou.

I have been building this off of that example. I've successfully loaded my own samples using the wav2sketch script and when calling them explicitly with the play function as in: playMem3.play(AudioSampleD_steelpan) it works fine. My question is really, how do I pass the sample name (which is an unsigned int array name as you've said) to the play function without explicitly writing playMem3.play(AudioSampleD_steelpan) each time? I'm trying to keep my code from being a mile long here which is why I have that sampleLookUp function.
 
Correction. I'm working off a very similar but different example from the tutorial: examples->audio->tutorial->part02_03_samples. It's basically the same thing.
 
i had edited my post #2, -- have you tried an array like
unsigned const int * samples[] = {AudioSampleSnare, AudioSampleHihat};

you have to copy the sample files (.cpp and .h) into your sketch folder
 
Last edited:
Status
Not open for further replies.
Back
Top