Go Down

Topic: Sample playback time critical issue (Read 896 times) previous topic - next topic

FrankStark

Jun 13, 2019, 03:30 pm Last Edit: Jun 13, 2019, 03:45 pm by FrankStark
Hey there!

My goal is to play sample(s) after a button push, but the playback should start as fast as possible because I'd like to use it in a live music environment and if I push it and it has a little delay that would be pointless.

I'm using an UNO, and a yx5300 based serial mp3 player so far. ( class 10 16gb SD card formatted to fat32, 6 samples with short names, no folder; mp3 128 bps). The goal would be at least 16 bit 44.1khz sample playback. ( But it's maybe compromiseable if that's too much to ask ). Right now there are 2 digital.read inputs for the buttons.

So far the program works kind of, but if I try to go with the music it has a slight 4-5ms delay ( don't know exactly how much it is, but I can feel it ). I don't know what's causing the slight lag and what would be the solution. I'm open for other applications.

I'm concerned about the yx5300 as it works at 9600 baud, it might be too slow.
Was thinking about serial feeding a PC and controlling an mp3 player there somehow. Would it work?

So please share your insights! How would it be possible to build such a system

About the code.: I was fiddling about, tried to serial feed the yx5300 without a for loop, the code at the bottom is just a simple debounce thingy, I thought it out, so it might not be the best.

Have a great day!

Code: [Select]
#include <SoftwareSerial.h>

#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module

SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX


static int8_t Send_buf[8] = {0} ;//The MP3 player undestands orders in a 8 int string
                                //0X7E FF 06 command 00 00 00 EF;(if command =01 next song order) 
#define NEXT_SONG 0X01 
#define PREV_SONG 0X02 
#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)
#define VOLUME_UP_ONE 0X04
#define VOLUME_DOWN_ONE 0X05
#define CMD_SET_VOLUME 0X1E //DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X22 //data is needed  0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)
#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
#define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED
#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B
#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED
#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3
#define STOP_PLAY 0X16
#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)
#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close
#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output

const int buttonPin = 2;
const int buttonPin2 = 3;
int buttonState;
int buttonState2;
//int lastButtonState = LOW;
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
unsigned long startMillis2;  //some global variables available anywhere in the program
unsigned long currentMillis2;
const unsigned long period = 4;  //the value is a number of milliseconds


void sendCommand(int8_t command, int16_t dat)
{
//delay(20);
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0xff; //version
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)//
{
  mySerial.write(Send_buf[i]) ;
}
}

void setup() {

mySerial.begin(9600);//Start our Serial coms for our serial monitor!
delay(500);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card   
delay(200);//wait for 200ms  // put your setup code here, to run once:
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(buttonPin2, INPUT_PULLUP);
   startMillis = millis();  //initial start time
   startMillis2 = millis();
}

void loop() {

int reading = digitalRead(buttonPin);
int reading2 = digitalRead(buttonPin2);


if ( reading == LOW ) {
  startMillis = millis();

  if (buttonState == 0) {
    mySerial.write(0x7E);
    mySerial.write(0xFF);
    mySerial.write(0x06);
    mySerial.write(0x01);
    mySerial.write((byte)0x00);
    mySerial.write((byte)0x00);
    mySerial.write((byte)0x00);
    mySerial.write(0xEF);
    //sendCommand(CMD_PLAY_W_INDEX, 002);
    //0X7E FF 06 command 00 00 00 EF
    buttonState = 1;
  }
}

else {
  currentMillis = millis();
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    buttonState = 0;
  }
}

if ( reading2 == LOW ) {
  startMillis2 = millis();

  if (buttonState2 == 0) {
    mySerial.write(0x7E);
    mySerial.write(0xFF);
    mySerial.write(0x06);
    mySerial.write(0x01);
    mySerial.write((byte)0x00);
    mySerial.write((byte)0x00);
    mySerial.write((byte)0x00);
    mySerial.write(0xEF);
    //sendCommand(CMD_PLAY_W_INDEX, 003);
    buttonState2 = 1;
  }
}

else {
  currentMillis2 = millis();
  if (currentMillis2 - startMillis2 >= period)  //test whether the period has elapsed
  {
    buttonState2 = 0;
  }
}

}

DVDdoug

Quote
So far the program works kind of, but if I try to go with the music it has a slight 4-5ms delay ( don't know exactly how much it is, but I can feel it ).
I don't know what's causing it, but I'm sure the delay is longer than that.   Sound travels at about 1 foot per millisecond so a speaker 5-feet away from your ear is 5mS and that's usually not a problem.   You might start to hear/feel a delay at more than 25ms.

MP3 does add a short bit of silence to the beginning and end of the file.   That might be long enough to hear or there might be some additional "processing time" added to that delaty/silence.

FrankStark

#2
Jun 13, 2019, 09:32 pm Last Edit: Jun 13, 2019, 09:33 pm by FrankStark
I don't know what's causing it, but I'm sure the delay is longer than that.   Sound travels at about 1 foot per millisecond so a speaker 5-feet away from your ear is 5mS and that's usually not a problem.   You might start to hear/feel a delay at more than 25ms.

MP3 does add a short bit of silence to the beginning and end of the file.   That might be long enough to hear or there might be some additional "processing time" added to that delaty/silence.
TLDR: Wow, that mp3 to wav change made it so much better, the "latency" went down to a tolerable level it seems, I can test fully in "live" for a longer period! Thank you very much for the tip!

Go Up