Skip to content
Browse files

fix .play() from cb()s

  • Loading branch information...
1 parent bd53c45 commit 2b70afa9a591f4e76cab45dac48189f05bbd930f @xk committed
Showing with 94 additions and 71 deletions.
  1. +3 −0 AUTHORS
  2. +31 −0 LICENSE
  3. 0 README.md
  4. BIN build/.wafpickle-7
  5. +1 −1 build/config.log
  6. BIN build/default/sound.node
  7. BIN build/default/sound_1.o
  8. +40 −26 sound.cc
  9. +0 −5 test.js
  10. +3 −11 test10.js
  11. +6 −7 test11.js
  12. +2 −2 test2.js
  13. +1 −8 test3.js
  14. +2 −2 test4.js
  15. +2 −1 test5.js
  16. +1 −1 test7.js
  17. +1 −4 test8.js
  18. +1 −3 test9.js
View
3 AUTHORS
@@ -0,0 +1,3 @@
+# Authors ordered by first contribution.
+
+Jorge Chamorro Bieling <Jorge@jorgechamorro.com>
View
31 LICENSE
@@ -0,0 +1,31 @@
+Node-sound's license follows:
+
+====
+
+Copyright Jorge Chamorro Bieling, Proyectos Equis Ka, S.L. and other
+Node-sound contributors (see AUTHORS).
+
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+
+====
+
+This license applies to all parts of Node-sound that are not externally
+maintained libraries.
View
0 README.md
No changes.
View
BIN build/.wafpickle-7
Binary file not shown.
View
2 build/config.log
@@ -1,4 +1,4 @@
-# project noname (1.0) configured on Sat May 14 21:28:21 2011 by
+# project noname (1.0) configured on Sun May 15 14:28:42 2011 by
# waf 1.5.16 (abi 7, python 20601f0 on darwin)
# using /Users/jorge/JAVASCRIPT/bin/node-waf configure
#
View
BIN build/default/sound.node
Binary file not shown.
View
BIN build/default/sound_1.o
Binary file not shown.
View
66 sound.cc
@@ -45,6 +45,7 @@
int hasCallback;
int callbackIsPending;
+ v8::Persistent<v8::Object> pendingJSCallback;
v8::Persistent<v8::Object> JSObject;
v8::Persistent<v8::Object> JSCallback;
@@ -103,6 +104,8 @@ static long int playingNow= 0;
void tracker (int i) {
playingNow+= i;
+ return;
+
if (playingNow > 0) {
if (fondoSnd->playing) {
fondoSnd->playing= 0;
@@ -138,11 +141,11 @@ playerStruct* newPlayer () {
player->id= createdCtr++;
player->format= gFormato;
player->loop= 0;
- player->hasCallback= 0;
player->paused= 0;
player->playing= 0;
- player->callbackIsPending= 0;
player->destroying= 0;
+ player->hasCallback= 0;
+ player->callbackIsPending= 0;
return player;
}
@@ -251,7 +254,7 @@ v8::Handle<Value> Play (const Arguments &args) {
#if defined (__APPLE__)
- OSStatus err0, err1, err2;
+ OSStatus err;
playerStruct* player;
player= (playerStruct*) (v8::External::Unwrap(args.This()->GetHiddenValue(player_symbol)));
@@ -280,9 +283,9 @@ v8::Handle<Value> Play (const Arguments &args) {
player->playing= 1;
tracker(+1);
- err0= AudioQueueStart(player->AQ, NULL);
- if (err0) {
- fprintf(stderr, " ERROR:AudioQueueStart:[%d]", err0);
+ err= AudioQueueStart(player->AQ, NULL);
+ if (err) {
+ fprintf(stderr, " ERROR:AudioQueueStart:[%d]", err);
}
goto end;
@@ -290,24 +293,27 @@ v8::Handle<Value> Play (const Arguments &args) {
if (player->playing) goto end;
+ //fprintf(stderr, "\n[%d] PLAY", player->id);
+ //fflush(stderr);
+
+ player->playing= 1;
+ tracker(+1);
+
player->AQBuffer1->mAudioDataByteSize= player->AQBuffer1Length;
- err0= AudioQueueEnqueueBuffer(player->AQ, player->AQBuffer1, 0, NULL);
- if (err0) {
- fprintf(stderr, " ERROR:AudioQueueEnqueueBuffer:[%d]", err0);
+ err= AudioQueueEnqueueBuffer(player->AQ, player->AQBuffer1, 0, NULL);
+ if (err) {
+ fprintf(stderr, " ERROR:AudioQueueEnqueueBuffer:[%d]", err);
}
player->AQBuffer2->mAudioDataByteSize= player->AQBuffer2Length;
- err1= AudioQueueEnqueueBuffer(player->AQ, player->AQBuffer2, 0, NULL);
- if (err1) {
- fprintf(stderr, " ERROR:AudioQueueEnqueueBuffer:[%d]", err1);
+ err= AudioQueueEnqueueBuffer(player->AQ, player->AQBuffer2, 0, NULL);
+ if (err) {
+ fprintf(stderr, " ERROR:AudioQueueEnqueueBuffer:[%d]", err);
}
- player->playing= 1;
- tracker(+1);
-
- err2= AudioQueueStart(player->AQ, NULL);
- if (err2) {
- fprintf(stderr, " ERROR:AudioQueueStart:[%d]", err2);
+ err= AudioQueueStart(player->AQ, NULL);
+ if (err) {
+ fprintf(stderr, " ERROR:AudioQueueStart:[%d]", err);
}
#else
@@ -340,7 +346,7 @@ void AQBufferCallback (void* priv, AudioQueueRef AQ, AudioQueueBufferRef AQBuffe
OSStatus err;
playerStruct* player= (playerStruct*) priv;
- //fprintf(stderr, "\n*** AQBufferCallback() [%ld]", player->id);
+ //fprintf(stderr, "\n[%ld] AQBufferCallback() [%d]", player->id, 1+(AQBuffer == player->AQBuffer2));
if (player->destroying) {
@@ -372,7 +378,7 @@ void AQBufferCallback (void* priv, AudioQueueRef AQ, AudioQueueBufferRef AQBuffe
AQBuffer->mAudioDataByteSize= (AQBuffer == player->AQBuffer1) ? player->AQBuffer1Length : player->AQBuffer2Length;
- err= AudioQueueEnqueueBuffer(AQ, AQBuffer, 0, NULL);
+ err= AudioQueueEnqueueBuffer(player->AQ, AQBuffer, 0, NULL);
if (err) {
fprintf(stderr, " ERROR:AQBufferCallback AudioQueueEnqueueBuffer:[%d] ", err);
}
@@ -382,10 +388,17 @@ void AQBufferCallback (void* priv, AudioQueueRef AQ, AudioQueueBufferRef AQBuffe
//fprintf(stderr, "\n[%ld] CLEANUP", player->id);
+ err= AudioQueueStop(player->AQ, true);
+ if (err) {
+ fprintf(stderr, " ERROR:AudioQueueStop:[%d]", err);
+ }
+
player->playing= 0;
if (player->hasCallback) {
player->callbackIsPending= 1;
+ player->pendingJSCallback= player->JSCallback;
+ player->hasCallback= 0;
queueStruct* theItem= (queueStruct*) calloc(1, sizeof(queueStruct));
theItem->player= player;
@@ -404,14 +417,14 @@ void AQBufferCallback (void* priv, AudioQueueRef AQ, AudioQueueBufferRef AQBuffe
tracker(-1);
- err= AudioQueueStop(AQ, false);
+ err= AudioQueueStop(player->AQ, false);
if (err) {
fprintf(stderr, " ERROR:AudioQueueStop:[%d]", err);
}
}
end:
- //fflush(stderr);
+ fflush(stderr);
return;
}
@@ -696,6 +709,8 @@ v8::Handle<Value> Bufferify (const Arguments &args) {
+
+
// ===================================
// = **** Callback into node.js **** =
// ===================================
@@ -718,19 +733,18 @@ static void Callback (EV_P_ ev_async *watcher, int revents) {
callbacksQueue= NULL;
pthread_mutex_unlock(&callbacksQueue_mutex);
-
/*
TODO
- ver que hay que hacer exactamente cuando cb() throws.
*/
+
queueStruct* next;
//TryCatch try_catch;
while (item != NULL) {
- if (item->player->hasCallback) {
- Persistent<Object> cb= item->player->JSCallback;
- item->player->hasCallback= 0;
+ if (item->player->callbackIsPending) {
+ Persistent<Object> cb= item->player->pendingJSCallback;
Persistent<Function>::Cast(cb)->Call(item->player->JSObject, 0, NULL);
item->player->callbackIsPending= 0;
cb.Dispose();
View
5 test.js
@@ -11,11 +11,6 @@ function noise (v,i,o) {
o[i]= Math.floor(Math.random()*256);
}
-var buffer= new Buffer(44100*4*2);
-var i= buffer.length;
-while (i--) buffer[i]= 0;
-Sound.create(buffer).loop(1e9).play();
-
var i= 99; // Create this many identical sounds.
while (i--) sounds[i]= Sound.create(bufferShort);
View
14 test10.js
@@ -2,7 +2,7 @@
var Sound= require('./build/default/sound');
-var quit= setTimeout(function nop(){}, 1e9); // don't quit
+setTimeout(function nop(){}, 1e9); // don't quit
var howMany= 999;
@@ -11,23 +11,15 @@ function cb () {
}
-var buffer= new Buffer(44100*4);
-var i= buffer.length;
-while (i--) buffer[i]= 0;
-Sound.create(buffer).loop(1e9).play();
-
-
var buffer= new Buffer(2048);
var i= buffer.length;
-while (i--) buffer[i]= Math.floor(256*Math.random());
+while (i--) buffer[i]= Math.floor(256*Math.random()); // noise
var i= howMany;
function next () {
- var snd= Sound.create(buffer);
+ var snd= Sound.create(buffer).play(cb);
process.stdout.write('\n['+ snd.id+ '].play(cb)');
- snd.play(cb);
- snd= null;
if (--i) setTimeout(next, Math.floor(10*Math.random()));
};
View
13 test11.js
@@ -1,4 +1,4 @@
-// exercise recursive .play() from the callback; It's a bad idea, you'll get big glitches, should use .loop() instead !
+// exercise recursive .play() from the callback; It's a bad idea, looping via the cb() produces glitches, should really use .loop() instead !
var Sound= require('./build/default/sound');
@@ -7,20 +7,20 @@ var quit= setTimeout(function nop(){}, 1e9); // don't quit
var howMany= 999;
function cb () {
- if (this.again) {
+ if (this.again === 10) {
process.stdout.write('\n['+ this.id+ '].callback: NO_MORE');
}
else {
- this.again= 1;
+ this.again= (this.again || 0) + 1;
this.play(cb);
process.stdout.write('\n['+ this.id+ '].callback: PLAY_IT_AGAIN'); // Sam.
}
}
-var buffer= new Buffer(2048);
+var buffer= new Buffer(8192);
var i= buffer.length;
-while (i--) buffer[i]= Math.floor(256*Math.random());
+while (i--) buffer[i]= i%256;
var i= howMany;
@@ -28,8 +28,7 @@ function next () {
var snd= Sound.create(buffer);
process.stdout.write('\n['+ snd.id+ '].play(cb)');
snd.play(cb);
- snd= null;
- if (--i) setTimeout(next, Math.floor(10*Math.random()));
+ if (--i) setTimeout(next, 3333);
};
next();
View
4 test2.js
@@ -1,7 +1,7 @@
// tests proper destruction/absence of leaks when there's unreferenced sound objects that are still play()ing.
function noise (v,i,o) {
- o[i]= Math.floor(Math.random()*256);
+ o[i]= Math.floor(256*Math.random());
}
function saw (v,i,o) {
@@ -23,7 +23,7 @@ var Sound= require('./build/default/sound');
(function loop () {
process.nextTick(loop);
- Sound.create(buffers[Math.floor(buffers.length*Math.random())]).play();
+ Sound.create(buffers[Math.floor(buffers.length*Math.random())]).loop(5).play();
process.stdout.write('.');
})();
View
9 test3.js
@@ -4,13 +4,6 @@
// = OJO DA SEGMENTATION FAULT DE VEZ EN CUANDO NO SE SABE POR QUE =
// ===================================================================
-function noise (v,i,o) {
- o[i]= Math.floor(Math.random()*256);
-}
-
-function sin (v,i,o) {
- o[i]= Math.floor(Math.sin(i/5)*256);
-}
var Sound= require('./build/default/sound');
var snds= [];
@@ -36,7 +29,7 @@ var bufs= [];
(function loop () {
process.stdout.write('.');
- snds[Math.floor(snds.length*Math.random())].play();
+ snds[Math.floor(snds.length*Math.random())].loop(3).play();
process.nextTick(loop);
})();
View
4 test4.js
@@ -5,10 +5,10 @@ var Sound= require('./build/default/sound');
var buf= new Buffer(256*256);
var i= buf.length;
while (i--) buf[i]= i%256;
-//[].forEach.call(b, function (v,i,o) { o[i]= Math.floor(Math.random()*256) });
+//while (i--) buf[i]= Math.floor(Math.random()*256);
var snd= Sound.create(buf);
-snd.loop(1e9).play();
+snd.loop(1e9);
var flipflop= 0;
function loop () {
View
3 test5.js
@@ -5,8 +5,9 @@ var len= buffer.length;
//while (len--) buffer[len]= Math.floor(Math.random()*256); // noise
while (len--) buffer[len]= len%256; // saw
-var sound= require('./build/default/sound').create(buffer);
+var Sound= require('./build/default/sound');
+var sound= Sound.create(buffer);
sound.volume(0).loop(1e9).play();
var i= 0;
View
2 test7.js
@@ -1,6 +1,6 @@
// test loop() to see if it makes any glitches when playing in a loop.
-var buffer= new Buffer(256*256); // 1 seconds.
+var buffer= new Buffer(256*256); // enough
var len= buffer.length;
while (len--) buffer[len]= len%256; // saw;
View
5 test8.js
@@ -28,9 +28,6 @@ while (len--) {
}
-var snd= require('./build/default/sound');
-
-snd.create(buffer).loop(1e6).volume(1).play(); // loop
-
+require('./build/default/sound').create(buffer).loop(1e6).volume(1).play(); // loop
setTimeout(Date.now, 1e9); // forever
View
4 test9.js
@@ -28,9 +28,7 @@ do {
} while (len);
-var snd= require('./build/default/sound');
-
-snd.create(buffer).loop(1e6).volume(1).play(); // loop
+require('./build/default/sound').create(buffer).loop(1e6).volume(1).play(); // loop
setTimeout(Date.now, 1e9); // forever

0 comments on commit 2b70afa

Please sign in to comment.
Something went wrong with that request. Please try again.