forked from xdissent/node-pcm-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zipper.cc
172 lines (132 loc) · 5.6 KB
/
zipper.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "zipper.h"
using namespace pcmutils;
void Zipper::Init(Handle<Object> exports) {
Isolate *isolate = exports->GetIsolate();
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(String::NewFromUtf8(isolate, "Zipper"));
NODE_SET_PROTOTYPE_METHOD(tpl, "write", Write);
NODE_SET_GETTER(isolate, tpl, "channelBuffers", ChannelBuffersGetter);
NODE_SET_GETTER(isolate, tpl, "channelsReady", ChannelsReadyGetter);
NODE_SET_GETTER(isolate, tpl, "samplesPerBuffer", SamplesPerBufferGetter);
NODE_SET_GETTER(isolate, tpl, "zipping", ZippingGetter);
// Persistent<Function> constructor = Persistent<Function>::New(tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "Zipper"), tpl->GetFunction());
}
void Zipper::New(const FunctionCallbackInfo<Value>& args) {
Isolate *isolate = args.GetIsolate();
if (!args.IsConstructCall()) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Use the new operator")));
return;
}
REQUIRE_ARGUMENTS(isolate, 3);
REQUIRE_ARGUMENT_FUNCTION(isolate, 2, callback);
Zipper* zip = new Zipper();
zip->Wrap(args.This());
zip->channels = args[0]->Int32Value();
zip->alignment = args[1]->Int32Value();
zip->frameAlignment = zip->alignment * zip->channels;
zip->callback.Reset(isolate, callback);
zip->zipping = false;
zip->channelBuffers.Reset(isolate, Array::New(isolate, zip->channels));
zip->channelsReady.Reset(isolate, Array::New(isolate, zip->channels));
for (int i = 0; i < zip->channels; i++) {
zip->channelsReady.Get(isolate)->Set(i, Boolean::New(isolate, false));
}
zip->buffer = (char*)malloc(ZIP_BUFFER_SAMPLES * zip->frameAlignment);
args.GetReturnValue().Set(args.This());
}
void Zipper::ChannelBuffersGetter(Local<String>, const PropertyCallbackInfo<Value>& args) {
Zipper* zip = ObjectWrap::Unwrap<Zipper>(args.This());
args.GetReturnValue().Set(zip->channelBuffers);
}
void Zipper::ChannelsReadyGetter(Local<String>, const PropertyCallbackInfo<Value>& args) {
Zipper* zip = ObjectWrap::Unwrap<Zipper>(args.This());
args.GetReturnValue().Set(zip->channelsReady);
}
void Zipper::SamplesPerBufferGetter(Local<String>, const PropertyCallbackInfo<Value>& args) {
Isolate *isolate = args.GetIsolate();
args.GetReturnValue().Set(Integer::New(isolate, ZIP_BUFFER_SAMPLES));
}
void Zipper::ZippingGetter(Local<String>, const PropertyCallbackInfo<Value>& args) {
Isolate *isolate = args.GetIsolate();
Zipper* zip = ObjectWrap::Unwrap<Zipper>(args.This());
args.GetReturnValue().Set(Boolean::New(isolate, zip->zipping));
}
void Zipper::Write(const FunctionCallbackInfo<Value>& args) {
Isolate *isolate = args.GetIsolate();
REQUIRE_ARGUMENTS(isolate, 2);
OPTIONAL_ARGUMENT_FUNCTION(isolate, 2, callback);
Zipper* zip = ObjectWrap::Unwrap<Zipper>(args.Holder());
COND_ERR_CALL(isolate, zip->zipping, callback, "Still zipping");
int channel = args[0]->Int32Value();
COND_ERR_CALL(isolate, zip->channelsReady.Get(isolate)->Get(channel)->BooleanValue(), callback, "Already Ready");
zip->channelBuffers.Get(isolate)->Set(channel, args[1]->ToObject());
zip->channelsReady.Get(isolate)->Set(channel, Boolean::New(isolate, true));
WriteBaton* baton = new WriteBaton(isolate, zip, callback, channel);
BeginWrite(baton);
}
void Zipper::BeginWrite(Baton* baton) {
uv_queue_work(uv_default_loop(), &baton->request, DoWrite, (uv_after_work_cb)AfterWrite);
}
void Zipper::DoWrite(uv_work_t* req) {
// Just a hop thru the evq
}
void Zipper::AfterWrite(uv_work_t* req) {
Isolate *isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
WriteBaton* baton = static_cast<WriteBaton*>(req->data);
Zipper* zip = baton->zip;
if (!baton->callback.IsEmpty()) {
Local<Value> argv[0] = { };
TRY_CATCH_CALL(isolate, zip->handle(), baton->callback, 0, argv);
}
if (!zip->zipping) {
bool ready = true;
for (int i = 0; i < zip->channels; i++) {
if (!zip->channelsReady.Get(isolate)->Get(i)->BooleanValue()) ready = false;
}
if (ready) {
ZipBaton* zipBaton = new ZipBaton(isolate, zip);
zip->zipping = true;
BeginZip(zipBaton);
}
}
delete baton;
}
void Zipper::BeginZip(Baton* baton) {
uv_queue_work(uv_default_loop(), &baton->request, DoZip, (uv_after_work_cb)AfterZip);
}
void Zipper::DoZip(uv_work_t* req) {
ZipBaton* baton = static_cast<ZipBaton*>(req->data);
Zipper* zip = baton->zip;
for (int sample = 0; sample < ZIP_BUFFER_SAMPLES; sample++) {
for (int channel = 0; channel < zip->channels; channel++) {
memcpy(
zip->buffer + (sample * zip->frameAlignment) + (channel * zip->alignment),
baton->channelData[channel] + (sample * zip->alignment),
zip->alignment
);
}
}
}
void Zipper::AfterZip(uv_work_t* req) {
Isolate *isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
ZipBaton* baton = static_cast<ZipBaton*>(req->data);
Zipper* zip = baton->zip;
size_t blen = ZIP_BUFFER_SAMPLES * zip->frameAlignment;
MaybeLocal<Object> buffer = Buffer::New(isolate, zip->buffer, blen);
for (int i = 0; i < zip->channels; i++) {
zip->channelsReady.Get(isolate)->Set(i, Boolean::New(isolate, false));
}
for (int i = 0; i < zip->channels; i++) {
zip->channelBuffers.Get(isolate)->Set(i, Local<Value>::New(isolate, Null(isolate)));
}
zip->zipping = false;
delete baton;
if (!zip->callback.IsEmpty()) {
Local<Value> argv[2] = { Local<Value>::New(isolate, Null(isolate)), Local<Value>::New(isolate, buffer.ToLocalChecked()) };
TRY_CATCH_CALL(isolate, zip->handle(), zip->callback, 2, argv);
}
}