redfrik
YaBB Newbies
Offline
Posts: 8
ESS hickups while loading soundfiles
Feb 23rd , 2006, 3:07am
hi, i'm trying to load a soundfile while playing another and i get dropouts. even tried in separate threads (see below). anyone know if this is possible. i've used various soundfile lengths but always get the same hickups. (need to use ess library as the files are mp3.) //make sure you have 4 mono mp3 files called testsnd0.mp3, testsnd1.mp3, testsnd2.mp3, testsnd3.mp3 in data directory //these files must be longer than dly seconds. i tried with 5 second files and dly=2 import krister.Ess.*; int dly= 2; //delay for loading next mp3 in seconds int numMp3= 4; Mp3[] mp3s; int mp3index= 0; //currently playing mp3 int counter; //for delaying loading next mp3 until after dly seconds void setup() { size(400, 400, JAVA2D); framerate(12); counter= int(dly*framerate); noSmooth(); Ess.start(this); mp3s= new Mp3[numMp3]; for(int i= 0; i<numMp3; i++) { mp3s[i]= new Mp3(); } loadNextMp3(0, true); //load and autostart first mp3 } void draw() { background(100); if(counter==0) { background(0); //black flash when start loading thread loadNextMp3((mp3index+1)%numMp3, false); //cycle counter--; } else if(counter>0) { counter--; } } void loadNextMp3(int index, boolean autostart) { new Mp3LoadThread("testsnd"+index+".mp3", index, autostart).start(); } public class Mp3LoadThread extends Thread { //thread for loading mp3s String mp3path; int index; boolean autostart; public Mp3LoadThread(String _mp3path, int _index, boolean _autostart) { super("mp3"+_index); mp3path= _mp3path; index= _index; autostart= _autostart; } public void run() { Mp3 mp3= mp3s[index]; mp3.load(mp3path); mp3.isLoaded= true; println("loaded index "+index+" with "+mp3path); if(autostart) { mp3.play(); } } } class Mp3 { //class for loading mp3s, storing channels and current states Channel channel; boolean isPlaying; boolean isLoaded; Mp3() { channel= new Channel(); isPlaying= false; isLoaded= false; } void load(String mp3path) { channel.loadSound(mp3path); channel.loadSamples(); //for level meters gui later isLoaded= true; } void play() { isPlaying= true; channel.play(); } void free() { isPlaying= false; isLoaded= false; } } void channelDone(Channel ch) { int index= findChannel(ch); println("done playing channel index "+index); mp3s[index].free(); mp3index= (index+1)%numMp3; //cycle Mp3 newMp3= mp3s[mp3index]; if(newMp3.isLoaded) { newMp3.play(); } else { println("failed to keep up"); } counter= int(dly*framerate); } int findChannel(Channel ch) { for(int i= 0; i<numMp3; i++) { if(mp3s[i].channel==ch) {return i;} } return -1; } public void stop() { Ess.stop(); super.stop(); }