Permalink
Browse files

now plays asynchronously

  • Loading branch information...
substack committed Jun 3, 2012
1 parent 7722e6e commit 31bc780633f86b8595fc2dbb83285a36ea89662d
Showing with 48 additions and 5 deletions.
  1. +44 −4 sox.cc
  2. +4 −1 test.js
View
48 sox.cc
@@ -7,18 +7,60 @@ using namespace v8;
#include <sox.h>
#include <assert.h>
+struct node_sox_request {
+ sox_format_t *in, *out;
+ sox_effects_chain_t *chain;
+ Persistent<Function> onend;
+};
+
+static void eio_flow (eio_req *ereq) {
+ node_sox_request *req = (node_sox_request *) ereq->data;
+
+ sox_flow_effects(req->chain, NULL, NULL);
+ sox_delete_effects_chain(req->chain);
+ sox_close(req->out);
+ sox_close(req->in);
+}
+
+static int eio_flow_after (eio_req *ereq) {
+ ev_unref(EV_DEFAULT_UC);
+ node_sox_request *req = (node_sox_request *) ereq->data;
+
+ HandleScope scope;
+ Local<Value> argv[0];
+
+ req->onend->Call(Context::GetCurrent()->Global(), 1, argv);
+
+ req->onend.Dispose();
+ free(req);
+ return 0;
+}
+
Handle<Value> hello(const Arguments& arguments) {
HandleScope scope;
+ node_sox_request *req;
+ req = (node_sox_request *) malloc(sizeof(node_sox_request));
+
+ Local<Object> opts = Local<Object>::Cast(arguments[0]);
+ req->onend = Persistent<Function>::New(
+ Local<Function>::Cast(
+ opts->Get(String::NewSymbol("onend"))
+ )
+ );
+
sox_format_t *in, *out;
in = sox_open_read("test.wav", NULL, NULL, NULL);
assert(in);
+ req->in = in;
out = sox_open_write("default", &in->signal, NULL, "alsa", NULL, NULL);
assert(out);
+ req->out = out;
sox_effects_chain_t *chain;
chain = sox_create_effects_chain(&in->encoding, &out->encoding);
+ req->chain = chain;
sox_effect_t *e;
char * args[10];
@@ -52,10 +94,8 @@ Handle<Value> hello(const Arguments& arguments) {
assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
free(e);
- sox_flow_effects(chain, NULL, NULL);
- sox_delete_effects_chain(chain);
- sox_close(out);
- sox_close(in);
+ eio_custom(eio_flow, EIO_PRI_DEFAULT, eio_flow_after, req);
+ ev_ref(EV_DEFAULT_UC);
return scope.Close(String::New("world"));
}
View
@@ -1,2 +1,5 @@
var sox = require('./build/Release/sox');
-sox.hello();
+var s = sox.hello({
+ onend : function () { console.log('__END__') },
+});
+console.log(s);

0 comments on commit 31bc780

Please sign in to comment.