Conflict between some SD functions and touchRead?

LaurentD

New member
Hi,

Using a Teensy 3.6 board (with no shield), a SanDisk Ultra SD card and a pair of amplified PC speakers, I am trying to build a "sound board": short sounds are triggered by touch sensitive pins, up to 4 sounds can be played at the same time.

It _mostly_ works, but I face a strange issue: randomly, one of the AudioPlaySdWav objects sometimes streams out a kind of "pulsed white noise" until the end of the track (it generally succeeds to read the next file anyway).
I managed to reproduce the problem with the simplified sketch here:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     //xy=363,224
AudioPlaySdWav           playSdWav3;     //xy=365,373
AudioPlaySdWav           playSdWav4;     //xy=367,453
AudioPlaySdWav           playSdWav2;     //xy=368,297
AudioMixer4              mixer2;         //xy=700,374
AudioMixer4              mixer1;         //xy=701,278
AudioOutputAnalogStereo  dacs1;          //xy=880,326
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord2(playSdWav1, 1, mixer2, 0);
AudioConnection          patchCord3(playSdWav3, 0, mixer1, 2);
AudioConnection          patchCord4(playSdWav3, 1, mixer2, 2);
AudioConnection          patchCord5(playSdWav4, 0, mixer1, 3);
AudioConnection          patchCord6(playSdWav4, 1, mixer2, 3);
AudioConnection          patchCord7(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord8(playSdWav2, 1, mixer2, 1);
AudioConnection          patchCord9(mixer2, 0, dacs1, 1);
AudioConnection          patchCord10(mixer1, 0, dacs1, 0);
// GUItool: end automatically generated code

AudioPlaySdWav *playSdWav[4] = { &playSdWav1, &playSdWav2, &playSdWav3, &playSdWav4 };
const int touch_pins[11] = { 0, 1, 29, 30, 15, 16, 17, 18, 19, 22, 23 }; 


/*
 * When these both items are defined, I randomly get "pulsed white noise"
 * from AudioPlaySdWav objects. Otherwise, no problem!
 */
#define FILE_EXISTS
#define TOUCH_READ


void setup() {
  AudioMemory(8);
  SD.sdfs.begin(SdioConfig(DMA_SDIO));
  for (int i = 0; i < 4; ++i) {
    mixer1.gain(i, 0.1);
    mixer2.gain(i, 0.1);
  }
}

void play_track(int i) {
  char filename[7];
  strcpy(filename, "0.WAV");
  filename[0] = '0' + i + 1;
  #ifdef FILE_EXISTS
    if (SD.sdfs.exists(filename)) {
      playSdWav[i]->play(filename);
    }
  #else
    playSdWav[i]->play(filename);
  #endif
}

void loop() {
  for (int i = 0; i < 4; ++i) {
    #ifdef TOUCH_READ
      for (int j = 0; j < 11; ++j) {
        touchRead(touch_pins[j]);
      }
    #endif
    if (!playSdWav[i]->isPlaying()) {
      play_track(i);
      delay(100);
    }
  }
}
You also need to load a SD card in the slot, containing 4 short WAV files named 1.WAV to 4.WAV.

As you see, the FILE_EXISTS and TOUCH_READ items can be defined or not, enabling or not the use of SD.sdfs.exists() and touchRead() functions.
When both items are defined, one of the AudioPlaySdWav objects crash randomly about once per 30-50 played tracks, even with only 2 or 3 active AudioPlaySdWav objects (so less required bandwidth from the SD card and less CPU load).
If one of these two items (or both) are not defined, it seems everything works fine.


Actually, I really don't understand what could be the source of such a conflict between AudioPlaySdWav.play(), SD.sdfs.exists() and touchRead() functions. I didn't found anything on the web, but I may not search with the accurate words.
Any idea? :confused:
Thanks in advance
 
I was able to solve my issue by using the AudioNoInterrupts() and AudioInterrupts() functions:

AudioNoInterrupts();

... read and display text file

AudioInterrupts();

Give that a try.
 
Thank you @lerxstrulz, the same trick fixed my problem.

It's really a pity that nothing is written in the documentation about this use of AudioNoInterrupts():
https://www.pjrc.com/teensy/td_libs_AudioProcessorUsage.html

And I found that, in the Recorder official example, these functions shoud be used during SD read/write access:
https://github.com/PaulStoffregen/Audio/blob/master/examples/Recorder/Recorder.ino
If not, the Recorder freezes a few seconds sometimes.

Teensy is a great product, with a nice "Audio System Design Tool", but I don't find the documentation is up to the task... :-/
 
Back
Top