Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

When triggering a sound file there is a delay or latency, can this be reduced?

$
0
0

Heres a simple example that demonstrates the 70mS delay between triggering a sound file and it actually creating the sound.

This was tested with processing 3 on a windows platform. A microphone signal showing the sound is compared with the serial data signal indicating the command to play.

Am I doing something wrong? Can the delay be reduced?

Thanks for any help.

cheers, Andy

// A quick demo to show the delay between .play() and the sound being generated
//
// every second, a serial character is sent - to trigger the oscilloscope on channel 1.
// then a sound file is played (edited .wav so there is no quiet start in the file)
// a microphone detects the sound which is displayed on the oscilloscope channel 2.
// the delay averages 70ms (varying between 65mS to 85mS)
//
// What is causing the delay and can it be reduced?



import processing.serial.*;
import processing.sound.*;
Serial myPort1;  // The serial port
SoundFile Sound1;
int now, last;


void setup()
{
  // List all the available serial ports
  printArray(Serial.list());
  // specify a specific port (to keep it simple)

  try {
        myPort1 = new Serial(this, Serial.list()[2], 57600);
        myPort1.clear();
      } catch (RuntimeException e)
      {
      }

  if (null != myPort1) print("Attached port1 to [2], ", Serial.list()[2], "\n");
  else print("port1 not connected\n");

  // open the .wav sound file which is a drum sound with no quiet time before the sample.
  Sound1 = new SoundFile(this, "click.wav");

  // set up timing to trigger sound every second
  last = millis();
}


void draw()
{
  int now = millis();
  // if 1000mS elapsed since last sound, trigger again
  if ( (now - last) >= 1000 )
  {
    last = now;  // record time of click
    DoClick();
  }
}


// make the sound
void DoClick()
{
  // trigger the sound before sending oscilloscope trigger
   Sound1.play();
  // send serial character as a simple timing trigger.
  // done after sound so we cant blame the serial port for the delay.
  if (null != myPort1)
  {
    myPort1.write(0x22);
  }
}

sound_latency


Viewing all articles
Browse latest Browse all 2896

Trending Articles