Ask suggestions in low latency/seamlessly loop project

charnjit

Well-known member
I am going to make an audio project inwhich I need to assess so many audio file(1-3 second long each) from SD card.
If it requires low latency to trigger for all audio files and some need to be playing seamlessly loop mode with playback speed control .

what type is needed on SD card ( .wav ?/ .raw ?) or any more??

I am new to Teensy audio project.
Finally I will create many Banks.
where Each bank will contain [ 24 mono Audio files]
[ 12 drum loop files(each 1-3 seconds long) one time only one file will be played. (Mono_mode) ]
[ 12 drum sounds to be play along with running drum loop file (poly_mode) ]

I am using Win7 32 bit/Arduino 1.8 .19
I have received Teensy 4.1 + audio shield Rev d .I tested it with SD Card. It is working play .wav and .raw file .
to play loop mode i use this code from built-in example
Code:
void loop() {
      button0.update();
       button1.update();

         if (button0.fallingEdge()) {
              playWav1.play("12.WAV");
              Serial.println("12 Wav running");     
             pad=1;
  }
  if (button1.fallingEdge()) {
           playWav1.play("13.WAV");
       Serial.println("13 wav running");//
      pad=2; 
  }
 

 
   if(!playWav1.isPlaying()&pad==1)      {  playWav1.play("12.WAV");   }
  if(!playWav1.isPlaying()&pad==2)      {  playWav1.play("13.WAV");   }
  

   if (Serial.available() > 0) {
    int incomingByte = Serial.read();
      if (incomingByte == 51) { Serial.println("stop");   playWav1.stop();   playWav2.stop();  pad=0; }
  }
}
after upload code, i got file is being played in repeat mode with a small gap (not seamlessly)

to get low latency i tried text replace in line54 from
Code:
C:\Program Files\Arduino\hardware\teensy\avr\cores\teensy4\AudioStream.cpp
with
C++:
 #define AUDIO_BLOCK_SAMPLES  16
after upload code, i got No sound from Audio board but getting (USB disconnected sound from PC) repeatly after few seconds.

Do i need special audio library to my project???

Do i need to do modification in .cpp / .h files for above functions???
Please suggest me with code example simple way .
like , how a sd wav file is trigger with minimum low latency, how to play audio file in seamlessly Loop mode.???
thank you....waiting hint examples... thank you
 
Am sorry if this is asked before.
I saw
My query is very near to this
Theard by yeahtuna. As I read post #26 , it solved. But I am not advance level Teensy user. I failed to find what code he used, what changes, editing is done in .cpp ,.h files in solving. Could you please Post code of solutions & other settings in . cpp/ .h etc .
.....thank you .....
 
The thread @joepasquariello linked is probably a good starting point for a library that can be used for seamless looping of audio samples.

There's also a discussion of low-latency triggering, based on "cueing" a sample by starting playback at a rate of 0.0; this loads audio from the filesystem but doesn't actually make a sound until the playback rate is set to a non-zero value. The open and load still takes appreciable time, but with your intended 24 samples per bank you can simply have them all cued and ready to trigger.

I would strongly recommend as a person who is "new to Teensy audio project" that you do, or at least thoroughly review, the Tutorial. You should also read and understand the various examples provided with the Audio library. If you then have a specific issue, you can post a small and complete example for us to comment on. This will get you much better results than a vague request to post code to solve a problem we know very little about.

Your code in post #1 is in code tags - well done! So many fail to do that and it can lead to all sorts of issues... However, it's not complete (I wouldn't be able just to copy and compile it), and the formatting is poor - the Arduino IDE has an auto-format facility, just press ctrl-T.

I wouldn't expect you to get far in your quest using the AudioPlaySdWav objects in the provided library. They just about work for a couple of files at once, but that's all. In addition, they don't immediately show isPlaying() as true, so your code looks as if it'd continually re-trigger the samples, rather than repeating only when they stop playing, as you might expect. An inspection of the WavFilePlay.ino sketch shows a delay() to allow the playback to start ... IMHO that should have been a red flag that the AudioPlaySdWav object needed a bit of a re-write, but there you go.

The other thing I spotted is that you've used a bitwise operator & where you want a boolean operator &&.

I would caution against editing the libraries / cores until you're closer to your goal, and have done as much as you believe you can within the existing constraints (like the 2.9ms update() cycle resulting from the 128-sample block size at 44.1kHz). Although the guidelines for writing audio objects counsel the use of AUDIO_BLOCK_SAMPLES for the block size, and AUDIO_SAMPLE_RATE for the sample rate, in practice a significant number of objects fail to work as intended if you make changes. One such is AudioPlaySdWav, which will fail if AUDIO_BLOCK_SAMPLES doesn't divide exactly into 256.
 
Thank you joepasquariello ,
Thank you h4yn0nnym0u5e
for response ,,,, i Am trying library you mention Teensy variable playback
i got much better than built-in Audio library
i modify example to code below
Code:
#include <Bounce.h>
#include <Arduino.h>
#include <Audio.h>
#include <SD.h>
#include <TeensyVariablePlayback.h>

AudioPlaySdResmp         playSdWav1;     //xy=324,457
AudioOutputI2S           i2s2;           //xy=840.8571472167969,445.5714416503906
AudioConnection          patchCord1(playSdWav1, 0, i2s2, 0);
AudioConnection          patchCord2(playSdWav1, 0, i2s2, 1);
AudioControlSGTL5000     audioShield;


#define A14 10

char* _filename = "16.WAV";
const int analogInPin = A14;
unsigned long lastSamplePlayed = 0;

double getPlaybackRate(int16_t analog) { 
    return  (analog - 512.0) / 512.0;
}

 Bounce button0 = Bounce(4, 5);
Bounce button1 = Bounce(5, 5);  
  int pad = 0;
 int newsensorValue =  1023;  
 
void setup() {
    analogReference(0);
    pinMode(analogInPin, INPUT_DISABLE);   // i.e. Analog

    Serial.begin(57600);
      pinMode(4, INPUT_PULLUP);
      pinMode(5, INPUT_PULLUP);

    if (!(SD.begin(BUILTIN_SDCARD))) {
        // stop here if no SD card, but print a message
        while (1) {
            Serial.println("Unable to access the SD card");
            delay(500);
        }
    }

    AudioMemory(24);

    audioShield.enable();
    audioShield.volume(0.5);

    playSdWav1.enableInterpolation(true);
    playSdWav1.setPlaybackRate(getPlaybackRate(newsensorValue));

    Serial.println("playing...");
}

void loop() {
       button0.update();
       button1.update();
    if (button0.fallingEdge()) {
            playSdWav1.playWav("13.WAV");         
             pad=1;   Serial.print("13 WAV");
  }
  if (button1.fallingEdge()) {
           playSdWav1.playWav("14.WAV"); 
                 pad=2;    Serial.print("14 wav");
  }

        if ((!playSdWav1.isPlaying()) && (pad==1)) {  playSdWav1.playWav("13.WAV");  }
          if ((!playSdWav1.isPlaying()) && (pad==2)) {  playSdWav1.playWav("14.WAV");  }

    playSdWav1.setPlaybackRate(getPlaybackRate(newsensorValue));

       // -------------------------------------------------------
   if (Serial.available() > 0) {
    int incomingByte = Serial.read();
    if (incomingByte == 49) {   newsensorValue=newsensorValue+20;   Serial.println("+++");  Serial.println( newsensorValue);}  // key1
     if (incomingByte == 50) {    newsensorValue=newsensorValue-20;   Serial.println("---"); Serial.println( newsensorValue);}    //key2
      if (incomingByte == 51) { Serial.println("stop");    playSdWav1.stop(); pad = 0; }   //key3
       if (incomingByte == 52) {       }   // key4
     if (incomingByte == 53) {         }    // key5
  }
   // ------------------------------------------------------
}


namespace std {
    void __throw_bad_function_call() {}
    void __throw_length_error(char const*) {}
}

to repeat audio i used
Code:
  if ((!playSdWav1.isPlaying()) && (pad==1)) {  playSdWav1.playWav("13.WAV");  }
1...you added seamlessly loop funtion to library but i could not find seamless loop functions from example . i am getting a little gap in repeating file
please tell me how loop function is written in code//////

2.. second thing i notice , playback speed also affect pitch of audio. can speed be change without affect pitch????

3.. wav files are not being played with its original quality, are playing with a little chrrrrrr chrrrrrr sound ?????

4.. can i call 2000 audio files(each 1-3second long) to play in my skecth ????
 
  1. Use something like playWav1.setLoopType(looptype::looptype_repeat);: playback should then loop seamlessly until you call stop(). Other options are ::looptype_none and ::looptype_pingpong
  2. Correct - this is expected. There is no pitch-preserving variable playback rate library that I'm aware of for Teensy
  3. Assuming this occurs with the WAV files used in your code, please attach copies of them to a post - you'll need to zip them as WAV can't be used as a forum attachment
  4. A 3 second long stereo 44.1kHz audio file is 3*2*2*44100=529,200 bytes, plus the header. 2000 of those would be about 1Gb, which comfortably fits on an SD card. So, yes. You can't play 2000 files at the same time, though.
 
playWav1.setLoopType(looptype::looptype_repeat);
::looptype_pingpong
::looptype_none
Do you mean if i want play 14.wav in loop mode, should i use.??
Code:
playSdWav1.playWav("14.WAV");
playWav1.setLoopType(repeat);
and call
Code:
playWav1.stop();
to stop.
also take care ,
i am writing in my code
Code:
playSdWav1
and you given
Code:
playWav1
please write proper line,,,,,,
as you will provide ,i will test it tonight

i do not want all 2000 files play at same time , i want them one at one time according to button input in skecth.
my wav s are attached,,,
thank you......
 
thank you h4yn0nnym0u5e
code
Code:
playSdWav1.playWav("14.WAV");
playSdWav1.setLoopType(looptype_repeat);
working for proper looping without cut.
now i will wait for variable playback rate library without affect pitch.
 
Back
Top