-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpcm.h
136 lines (113 loc) · 3.11 KB
/
pcm.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
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
#ifndef PCM_H
#define PCM_H
#include <node.h>
#include <node_buffer.h>
#include <alsa/asoundlib.h>
#include "macros.h"
using namespace v8;
using namespace node;
namespace alsa {
class Pcm;
class Pcm : public ObjectWrap {
public:
static void Init(Handle<Object> exports);
protected:
Pcm() : ObjectWrap(),
handle(NULL),
opened(false),
readable(false),
reading(false),
writable(false),
writing(false),
interleaved(false),
closing(false),
leftovers(NULL),
leftoversLength(0) {
}
~Pcm() {
handle = NULL;
opened = false;
readable = false;
reading = false;
writable = false;
writing = false;
interleaved = false;
closing = false;
leftovers = NULL;
leftoversLength = 0;
}
struct Baton {
uv_work_t request;
Pcm *pcm;
Persistent<Function> callback;
Baton(Pcm* pcm_, Handle<Function> cb_) :
pcm(pcm_) {
pcm->Ref();
request.data = this;
callback = Persistent<Function>::New(cb_);
}
virtual ~Baton() {
pcm->Unref();
callback.Dispose();
}
};
struct CloseBaton : Baton {
CloseBaton(Pcm* pcm_, Handle<Function> cb_) :
Baton(pcm_, cb_) {
}
};
struct IOBaton : Baton {
snd_pcm_uframes_t frames;
snd_pcm_sframes_t result;
char* buffer;
IOBaton(Pcm* pcm_, Handle<Function> cb_, snd_pcm_uframes_t frames_) :
Baton(pcm_, cb_), frames(frames_), result(0), buffer(NULL) {
buffer = (char*)malloc(snd_pcm_frames_to_bytes(pcm->handle, frames));
}
virtual ~IOBaton() {
free(buffer);
}
};
struct ReadBaton : IOBaton {
ReadBaton(Pcm* pcm_, Handle<Function> cb_, snd_pcm_uframes_t frames_) :
IOBaton(pcm_, cb_, frames_) {
}
};
struct WriteBaton : IOBaton {
char* chunk;
snd_pcm_uframes_t chunkFrames;
snd_pcm_uframes_t total;
WriteBaton(Pcm* pcm_, Handle<Function> cb_, snd_pcm_uframes_t frames_, char* chunk_, snd_pcm_uframes_t chunkFrames_) :
IOBaton(pcm_, cb_, frames_), chunk(chunk_), chunkFrames(chunkFrames_), total(0) {
}
};
static Handle<Value> New(const Arguments& args);
static Handle<Value> Open(const Arguments& args);
static Handle<Value> Close(const Arguments& args);
static Handle<Value> Read(const Arguments& args);
static Handle<Value> Write(const Arguments& args);
static Handle<Value> OpenedGetter(Local<String> str, const AccessorInfo& accessor);
static Handle<Value> ReadableGetter(Local<String> str, const AccessorInfo& accessor);
static Handle<Value> WritableGetter(Local<String> str, const AccessorInfo& accessor);
static void BeginRead(Baton* baton);
static void DoRead(uv_work_t* req);
static void AfterRead(uv_work_t* req);
static void BeginClose(Baton* baton);
static void DoClose(uv_work_t* req);
static void AfterClose(uv_work_t* req);
static void BeginWrite(Baton* baton);
static void DoWrite(uv_work_t* req);
static void AfterWrite(uv_work_t* req);
snd_pcm_t *handle;
bool opened;
bool readable;
bool reading;
bool writable;
bool writing;
bool interleaved;
bool closing;
char *leftovers;
int leftoversLength;
};
}
#endif