No sound - no compiling errors - help

Status
Not open for further replies.

dywen

Member
Hi,

I want to play 6 audio files simultaneously, raw format, from the sd card. When the button is pressed, the audio should play.

--> When I plug in the Teensy, I have complete silence. No pop, crackle or anything. Help. :confused:



Set-up:
- 6 working buttons
- Teensy 3.2 & audio board
- Check the raw files here:
https://cloud.constantvzw.org/s/TzpRFmioY7sxC5Q
As a zip file: https://cloud.constantvzw.org/remote.php/webdav/wvw/Teensy_raw_player/raw_files_sketch.zip
- the buttons, sd card worked perfectly with another sketch (to make the microphone record raw files)
- sketch was written, based on the example sample player for raw files from the teensy's internal memory (as I want my sounds to come from a microphone recording, this is not possible)

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


// Create the Audio components.  These should be created in the
// order data flows, inputs/sources -> processing -> outputs
//
AudioPlaySdRaw           sound0;     //xy=319,114
AudioPlaySdRaw           sound1;     //xy=320,160
AudioPlaySdRaw           sound2;     //xy=324,211
AudioPlaySdRaw           sound3;     //xy=331,255
AudioPlaySdRaw           sound4;     //xy=333,300
AudioPlaySdRaw           sound5;     //xy=341,350
AudioMixer4              mix1;       // two 4-channel mixers are needed in
AudioMixer4              mix2;       // tandem to combine 6 audio sources
AudioOutputI2S           headphones;     //xy=909,159
AudioOutputAnalog        dac;        // play to both I2S audio board and on-chip DAC
// Create Audio connections between the components
//
AudioConnection          patchCord1(sound0, 0, mix1, 0);
AudioConnection          patchCord2(sound1, 0, mix1, 1);
AudioConnection          patchCord3(sound2, 0, mix1, 2);
AudioConnection          patchCord4(sound3, 0, mix1, 3);
AudioConnection          patchCord5(sound4, 0, mix2, 1);
AudioConnection          patchCord6(sound5, 0, mix2, 2);
AudioConnection          patchCord7(mix1, 0, mix2, 0); // output of mix1 into 1st input on mix2
AudioConnection          patchCord8(mix2, 0, headphones, 0);
AudioConnection          patchCord9(mix2, 0, headphones, 1);
AudioConnection          patchCord10(mix2, dac);


// Create an object to control the audio shield.
// 
AudioControlSGTL5000     audioShield;    //xy=341,471

// Bounce objects to read six pushbuttons (pins 0-5)
//
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5);  // 5 ms debounce time
Bounce button2 = Bounce(2, 5);
Bounce button3 = Bounce(3, 5);
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(5, 5);


void setup() {
  Serial.begin(115200);
  
  // Configure the pushbutton pins for pullups.
  // Each button should connect from the pin to GND.
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

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

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

  // by default the Teensy 3.1 DAC uses 3.3Vp-p output
  // if your 3.3V power has noise, switching to the
  // internal 1.2V reference can give you a clean signal
  // dac.analogReference(INTERNAL);

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

void loop() {
  // Update all the button objects
  button0.update();
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();

  // When the buttons are pressed, just start a sound playing.
  // The audio library will play each sound through the mixers
  // so any combination can play simultaneously.
  //
  if (button0.fallingEdge()) {
    Serial.println("Button 0 is pressed");
    sound0.play("sound0.RAW");
  }
  if (button1.fallingEdge()) {
    Serial.println("Button 1 is pressed");
    sound1.play("sound1.RAW");
  }
  if (button2.fallingEdge()) {
    Serial.println("Button 2 is pressed");
    sound2.play("sound2.RAW");
  }
  if (button3.fallingEdge()) {
    Serial.println("Button 3 is pressed");
    sound3.play("sound3.RAW");
  }
  if (button4.fallingEdge()) {
    Serial.println("Button 4 is pressed");
    sound4.play("sound4.RAW");
  }
  if (button5.fallingEdge()) {
    Serial.println("Button 5 is pressed");
    sound5.play("sound5.RAW");
  }

}


--> This thread has been reposted as the previous was written in a hurry, full of typo's.

Thanks - I'm still learning to code. This is for a community project with weaving..
Dywen
 
Last edited:
I don't see the pins you are using....


//Define the pins used

#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14





// Setup SPI in setup

SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
 
You haven't set the gain for the mixer1 input to mixer2(0). And the gains on the mixers may be too high.
Try this:
Code:
  mix1.gain(0, 0.25);
  mix1.gain(1, 0.25);
  mix1.gain(2, 0.25);
  mix1.gain(3, 0.25);
  mix2.gain(0, 0.3);
  mix2.gain(1, 0.3);
  mix2.gain(2, 0.3);
You may have to adjust the gains of inputs 1 and 2 into mixer2 to get them comparable to that of input 0

Pete
 
Yes, OK, el_supremo...I didnt spot that.....the code worked for me after setting the SPI pins...so didnt look any further...there is probably a default setting for the mixer gains that lets them work initially...????
 
I don't see the pins you are using....


//Define the pins used

#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14





// Setup SPI in setup

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

Of course!!
(mild slap on forehead)

Nowhere I tell the Teensy how to connect to the sd card.

I have been staring at this since Friday. Thanks!:D
 
Now I see that there are some errors in the conversion from wav to raw
--> some sounds play to slow
I used the default values in sox (commandline tool)
$ sox blabla.wav blabla.raw

Is this the detailed command?
$ sox msg0000.wav --bits 16 --encoding signed-integer --endian little msg0001.raw
 
Yes, you have to be careful about the sample rate....SD plays at 44.1k.....below is copied from web page and a link to it also...

Audio is rarely stored in RAW format. The SoX program can convert most common audio files to RAW format.
A typical command to convert from MP3 to RAW format would be:

sox file.mp3 --bits 16 --rate 44100 --channels 1 file.raw

WAV files encoded with 16 bit mono samples at 44.1 kHz can be played as RAW data. The WAV header, plus any extra non-audio data at the beginning or end of the file may play as a brief audio glitch or noise. Using sox to convert to only raw data is best.

Link to pjrc Audio Web pages

https://www.pjrc.com/teensy/td_libs_AudioPlaySdRaw.html
 
Aha,
I was looking on the www for the right command and it was right under my Teensy info nose

--> soxi gave me the answer as to why the files were slower as raw --> they were the only stereo wav files in converted in a quick and dirty way !

$ soxi blablabla.wav
Input File : 'blablabla.wav'
Channels : 2
Sample Rate : 44100
Precision : 16-bit
Duration : 00:00:01.50 = 66150 samples = 112.5 CDDA sectors
File Size : 265k
Bit Rate : 1.41M
Sample Encoding: 16-bit Signed Integer PCM

(soxi blabla.raw does not give the information - because of the missing headers on the file, I think)
 
Last edited:
---> the SD library must not be used from your sketch :-/

In the pjrc Audio Web pages it literally says:
While the audio library is playing, the SD library must not be used from your sketch. If you must access the SD card, playback should be stopped.
https://www.pjrc.com/teensy/td_libs_AudioPlaySdRaw.html

Why is that a problem? Damage to the sd card? Not enough memory?
because I am not listening to this warning in my sketch..

Example: push button 1 is pressed, while it is still playing, Pushbutton 2 is pressed and also plays a sound.
I want this multi-voiced sound.

My goal is to make a simplified sampler
--> record with the microphone and then play (with capacitive touch, on e-textile handwoven textile samplers)

///////////////////////////
Also from that webpage:
Resource Usage
Playing files from the SD card uses the SD library. While the audio library is playing, the SD library must not be used from your sketch. If you must access the SD card, playback should be stopped. An alternative is to use AudioNoInterrupts() to prevent the audio library from running while you acccess the card, but doing so it likely to cause the audio output to stutter if your code disables the audio library for too long.

TODO: CPU and memory usage estimate.

TODO: how many AudioPlaySdRaw and Mixer4 objects can realistically run simultaneously (eg, how many "voices" can really be played). This should be tested with good cards (eg, Sandisk Ultra) and some cheap no-name cards that lack good buffer management, even though they claim "class 10" speed.
 
Last edited:
Why is that a problem? Damage to the sd card? Not enough memory?

This advice comes from the lack of concurrency in the Ardiuno SD library. It's simply not designed to allow both main program and interrupts to access the card. If you try, usually it will work, but there is a small chance your code in the sketch may happen to try accessing the card at the exact same moment the audio library needs to read more data. If you get unlucky with the timing, the result can be something accessed from the wrong part of the card. If both are merely reading the card, the worst case is merely reading corrupted data. If you try writing, you could end up with a corrupted filesystem.

A couple years ago I started an overly ambitious attempt to make the SD library fully concurrent & reentrant. It only supports SPI access to the card, so you can't use it with the built in SD card on Teensy 3.5 & 3.6, and it only supports reading. Sometime I hope to get back to that project, since we really could use higher performance and fully reentrant and interrupt-safe SD card access! But for now it's quite limited. If you want to try, find SD_t3.h and uncomment the line to enable the special optimized version.
 
More questions about what's possible without damaging the sd memory

Ok, thanks. I get the gist of it.
As these sketches will be used in workshop contexts (where I'm not present, I'm the "designer" of the project), they need to be robust.
So I will add stop.playing.

In the meantime I have thought of several workarounds to have multiple "voices" and no damage. I want the idea of a choir, including human singing with Teensy recording & other sources.

Questions:

-- Can sd card Raw go together with raw of the internal memory?
So, the raw audio file on the sd card plays, and during that an internal memory raw is activated by a pushbutton

-- Can sd card Raw go together with teensy synthesis?
So, the raw audio file on the sd card plays, and during that sine wave (highly likely more complex synthesis) is activated by a pushbutton to play

-- If you play the sd card Raw file, you turn an effect on and off and you add synthesis (ok, I'm stretching it ;-))?

-- Can two (or more) Teensy's be connected - listen to each other to start together? Share their clock?
I have looked for this on the forum - but did not necessarily find it - or I don't know the right term



Super interesting to see how these flows of information software-wise implicate how the hardware handles it on this microlevel.


Greetz,

Dywen
 
Last edited:
-- Can sd card Raw go together with raw of the internal memory?

Yes.

-- Can sd card Raw go together with teensy synthesis?
So, the raw audio file on the sd card plays, and during that sine wave (highly likely more complex synthesis) is activated by a pushbutton to play

-- If you play the sd card Raw file, you turn an effect on and off and you add synthesis (ok, I'm stretching it ;-))?

Yes & yes.

You can create quite complex systems using the design tool. For example, the Monolith Synth we built last year for Bay Area Maker Faire used this system:

audiosystemonly_small.png


But you must stay under 100% CPU usage. Most synthesis objects and some of the effects use surprisingly little. Some use quite a lot. The same goes for memory. Effects like delay & reverb consume quite a lot of memory.

-- Can two (or more) Teensy's be connected - listen to each other to start together? Share their clock?
I have looked for this on the forum - but did not necessarily find it - or I don't know the right term

This answer really depends on the details. For "share their clock", probably not.

Certainly 2 boards can receive the same digital signal (eg, from a pushbutton) and be programmed to each do something upon the rising or falling edge. It's also quite possible to have them communicate with each other, using Serial1 for example.
 
Status
Not open for further replies.
Back
Top