Using Teensy Audio in a custom Library

Status
Not open for further replies.

sennert

Member
Hi Folks,

I'm trying to write my own Arduino Library for a project that uses a Teensy + Audio Board. I don't get any error messages but there's no sound coming out of the the audio board, only some kind of digital noise.

Basically i tried to put the playSDWav Example into a library. The Example itself and my Wav files work on its own. I guess the problem comes with the copied lines from the GUI System Designer Tool. Will i have to put them in the main code or is it possible to do this in the library files?

Can somebody spot the mistake and help me get this to work? Would be great. ;)

Thanks in advance,
steffen

NvE_AudioControl.h
Code:
#ifndef NvE_AudioControl_h
#define NvE_AudioControl_h

#include "Arduino.h"

// for Audioshield
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>


class NvE_AudioControl
{
  public:
    NvE_AudioControl(int);
    void init();
    void update();
    void play(int);
    void volume(float);
    void stop();
    int tracktime();

  private:
    int _tracks;

    // GUItool: begin automatically generated code
    AudioPlaySdWav           _playWav1;       
    AudioOutputI2S           _i2s1;          
    //AudioConnection          _patchCord1(_playWav1, 0, _i2s1, 0);
    //AudioConnection          _patchCord2(_playWav1, 1, _i2s1, 1);
    AudioControlSGTL5000     _sgtl5000_1; 
    // GUItool: end automatically generated code


};

#endif

NvE_AudioControl.cpp
Code:
#include "Arduino.h"
#include "NvE_AudioControl.h"


// for Audioshield
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>



/////// AUDIO CONTROL ///////////////////
/////////////////////////////////////


NvE_AudioControl::NvE_AudioControl(int tracks)
{
  _tracks = tracks;

  // GUItool: begin automatically generated code
  //AudioPlaySdWav           _playWav1;       
  //AudioOutputI2S           _i2s1;          
  AudioConnection          _patchCord1(_playWav1, 0, _i2s1, 0);
  AudioConnection          _patchCord2(_playWav1, 1, _i2s1, 1);
  //AudioControlSGTL5000     _sgtl5000_1;    
  // GUItool: end automatically generated code*/
}

void NvE_AudioControl::init()
{
   // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(60);

  _sgtl5000_1.enable();
  _sgtl5000_1.volume(0.8);

  SPI.setMOSI(7);
  SPI.setSCK(14);
  if (!(SD.begin(10))) {
    // stop here, but print a message repetitively
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
}

void NvE_AudioControl::update()
{
  /*
    // uncomment these lines if you audio shield
    // has the optional volume pot soldered
    float vol = analogRead(15);
    vol = vol / 1024;
    _sgtl5000_1.volume(vol);
    */
}

void NvE_AudioControl::play(int track_num)
{
    switch(track_num) {
      case 1:
          _playWav1.play("TRACK02.WAV");
          delay(20);
          Serial.println("playing Track 1");
          break;
      case 2:
          _playWav1.play("TRACK02.WAV");
          delay(20);
          Serial.println("playing Track 2");
          break;
      case 3:
          _playWav1.play("TRACK03.WAV");
          break;
      case 4:
          _playWav1.play("TRACK04.WAV");
          break;
      case 5:
          _playWav1.play("TRACK05.WAV");
          break;
      case 6:
          _playWav1.play("TRACK06.WAV");
          break;
      case 7:
          _playWav1.play("TRACK07.WAV");
          break;
      case 8:
          _playWav1.play("TRACK08.WAV");
          break;
      case 9:
          _playWav1.play("TRACK09.WAV");
          break;
      case 10:
          _playWav1.play("TRACK10.WAV");
          break;
      default:
          //playWav1.play("TRACK00.WAV");     //ignore unknown cases
          break;
    } 
}

void NvE_AudioControl::volume(float vol)    // Max: 1.0   Min: 0.0
{
  _sgtl5000_1.volume(vol);
}

void NvE_AudioControl::stop()
{
  if(_playWav1.isPlaying()) {
      _playWav1.stop();
  }}

int NvE_AudioControl::tracktime()
{ 
  if(_playWav1.isPlaying()) {
       return (_playWav1.positionMillis());
  }
}
 
Status
Not open for further replies.
Back
Top