|
Author |
Topic: collision + sound causes delay (Read 478 times) |
|
peterl
|
collision + sound causes delay
« on: Dec 27th, 2003, 8:52pm » |
|
Just had a look at sonic features and made a movie that should give a sound each time a random moving object hits one of the movie's sides. It does so, but the sound comes with an enourmous delay (lag). Anybody knowing if I do something wrong or if this is just a problem of the program? movie is here: http://www.ctrlaltdel.org/p5/collision/
|
|
|
|
elout
|
Re: collision + sound causes delay
« Reply #1 on: Dec 28th, 2003, 4:35pm » |
|
Hi Peter, I found out you if you use jump, the freeze won`t happen. Code: // collision detection BSound pong; int size = 150; // Width of the shape float xpos, ypos; // Starting position of shape float xspeed = 2.8; // Speed of the shape float yspeed = 2.2; // Speed of the shape int xdirection = 1; // Left or Right int ydirection = 1; // Top to Bottom void setup() { size(400, 200); noStroke(); // framerate(30); // Set the starting position of the shape xpos = width/10; ypos = height/10; pong = loadSound("clong.wav"); repeat(pong); } void loop() { background(0); // Update the position of the shape xpos = xpos + ( xspeed * xdirection ); ypos = ypos + ( yspeed * ydirection ); // Test to see if the shape exceeds the boundaries of the screen // If it does, reverse its direction by multiplying by -1 if (xpos > width-size || xpos < 0) { xdirection *= -1; jump(pong, 0); // jumps to start of the sample volume(pong, 1.0); //set volume speed(pong, ypos/4.0f); //set sample speed } if (ypos > height-size || ypos < 0) { ydirection *= -1; jump(pong, 0); // jumps to start of the sample volume(pong, 1.0); //set volume speed(pong, xpos/4.0f); //set sample speed } // Draw the shape rect(xpos, ypos, size, size); } |
|
|
|
|
|
peterl
|
Re: collision + sound causes delay
« Reply #2 on: Dec 28th, 2003, 5:33pm » |
|
Hey Elout, Works! But now you got the sound in fact continously playing (repeat)... and got feeling that the sound is not in sync with collisions but this can be due to sample sample speed. But this for sure will make me look deeper into the program btw heard some people didn't have the problem of freezing or lag... maybe the problem(?) is processor related. bedankt
|
« Last Edit: Dec 28th, 2003, 6:09pm by peterl » |
|
|
|
|
elout
|
Re: collision + sound causes delay
« Reply #3 on: Dec 28th, 2003, 7:04pm » |
|
At; http://proce55ing.net/reference/sound/beginSound_.html Note: Due to current limitation in the way Java 1.1 processes audio, the full sound processing mode has a time lag which varies among different machines and configurations. This may improve in future versions of Processing. Anyway it can be interesting to write direcly into the soundbuffer Code: // snd / collision detection int size = 20; // Width of the shape float xpos, ypos; // Starting position of shape float xspeed = 2.8; // Speed of the shape float yspeed = 2.2; // Speed of the shape int xdirection = 1; // Left or Right int ydirection = 1; // Top to Bottom int soundstepper=0; int soundlength=256;//128;//256;//512;//1024;//4096; int [] soundbuffer=new int[soundlength]; int [] soundbuffer2=new int[soundlength]; void setup() { size(300, 160); noStroke(); framerate(30); // Set the starting position of the shape xpos = width/10; ypos = height/10; beginSound(soundlength); } void loop() { background(0); // Update the position of the shape xpos = xpos + ( xspeed * xdirection ); ypos = ypos + ( yspeed * ydirection ); soundbuffer[soundstepper]=(int) xpos;//%127;//(sin(xpos)*10.0f); soundbuffer2[soundstepper]=(int)ypos;//%127;//(cos(ypos)*10.0f); soundstepper++; if (soundstepper>=soundlength){soundstepper=0;} if (xpos > width-size || xpos < 0) { xdirection *= -1; } if (ypos > height-size || ypos < 0) { ydirection *= -1; } // Draw the shape rect(xpos, ypos, size, size); } void soundEvent() { for (int i=0; i < soundlength; i++) { // update your sample array if (soundbuffer[i]>127){soundbuffer[i]=127;} if (soundbuffer2[i]>127){soundbuffer2[i]=127;} samples[i] = (soundbuffer[i]-soundbuffer2[i]);//%127; //samples[i] = (soundbuffer[i]%127-soundbuffer2[i]%127);//%127; } } |
|
|
|
|
|
|