forked from xdissent/node-pcm-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer.h
103 lines (83 loc) · 2.37 KB
/
mixer.h
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
#ifndef MIXER_H
#define MIXER_H
#include <node.h>
#include <node_buffer.h>
#include "macros.h"
#define MIX_BUFFER_SAMPLES 1024
using namespace v8;
using namespace node;
namespace pcmutils {
class Mixer;
class Mixer : public ObjectWrap {
public:
static void Init(Handle<Object> exports);
protected:
Mixer() : ObjectWrap(), channels(0), alignment(0), format(0), mixing(false) {
channelBuffers.Clear();
channelsReady.Clear();
callback.Clear();
}
~Mixer() {
channels = 0;
alignment = 0;
format = 0;
mixing = false;
channelBuffers.Dispose();
channelsReady.Dispose();
callback.Dispose();
}
struct Baton {
uv_work_t request;
Mixer* mix;
Baton(Mixer* mix_) : mix(mix_) {
mix->Ref();
request.data = this;
}
virtual ~Baton() {
mix->Unref();
}
};
struct WriteBaton : Baton {
Persistent<Function> callback;
int channel;
WriteBaton(Mixer* mix_, Handle<Function> cb_, int channel_) : Baton(mix_), channel(channel_) {
callback = Persistent<Function>::New(cb_);
}
virtual ~WriteBaton() {
callback.Dispose();
}
};
struct MixBaton : Baton {
char** channelData;
MixBaton(Mixer* mix_) : Baton(mix_), channelData(NULL) {
channelData = (char**)malloc(mix->channels * sizeof(char*));
for (int i = 0; i < mix->channels; i++) {
channelData[i] = Buffer::Data(mix->channelBuffers->Get(i)->ToObject());
}
}
virtual ~MixBaton() {
free(channelData);
}
};
static Handle<Value> New(const Arguments& args);
static Handle<Value> Write(const Arguments& args);
static Handle<Value> ChannelBuffersGetter(Local<String> str, const AccessorInfo& accessor);
static Handle<Value> ChannelsReadyGetter(Local<String> str, const AccessorInfo& accessor);
static Handle<Value> SamplesPerBufferGetter(Local<String> str, const AccessorInfo& accessor);
static Handle<Value> MixingGetter(Local<String> str, const AccessorInfo& accessor);
static void BeginWrite(Baton* baton);
static void DoWrite(uv_work_t* req);
static void AfterWrite(uv_work_t* req);
static void BeginMix(Baton* baton);
static void DoMix(uv_work_t* req);
static void AfterMix(uv_work_t* req);
Persistent<Array> channelBuffers;
Persistent<Array> channelsReady;
Persistent<Function> callback;
int channels;
int alignment;
int format;
bool mixing;
};
}
#endif