-
Notifications
You must be signed in to change notification settings - Fork 9
/
formatter.h
88 lines (70 loc) · 1.8 KB
/
formatter.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
#ifndef FORMATTER_H
#define FORMATTER_H
#include <node.h>
#include <node_buffer.h>
#include "macros.h"
#define FMT_BUFFER_SAMPLES 1024
using namespace v8;
using namespace node;
namespace pcmutils {
class Formatter;
class Formatter : public ObjectWrap {
public:
static void Init(Handle<Object> exports);
protected:
Formatter() : ObjectWrap(), inFormat(0), outFormat(0),
inAlignment(0), outAlignment(0), formatting(false), buffer(NULL) {
}
~Formatter() {
inFormat = 0;
outFormat = 0;
inAlignment = 0;
outAlignment = 0;
formatting = false;
if (buffer != NULL) free(buffer);
buffer = NULL;
}
struct Baton {
uv_work_t request;
Formatter* fmt;
Baton(Formatter* fmt_) : fmt(fmt_) {
fmt->Ref();
request.data = this;
}
virtual ~Baton() {
fmt->Unref();
}
};
struct FormatBaton : Baton {
Persistent<Function> callback;
Persistent<Object> chunk;
size_t chunkLength;
char* chunkData;
int totalSamples;
int formattedSamples;
FormatBaton(Formatter* fmt_, Handle<Function> cb_, Handle<Object> chunk_) : Baton(fmt_),
chunkLength(0), chunkData(NULL), totalSamples(0), formattedSamples(0) {
callback = Persistent<Function>::New(cb_);
chunk = Persistent<Object>::New(chunk_);
chunkData = Buffer::Data(chunk);
chunkLength = Buffer::Length(chunk);
}
virtual ~FormatBaton() {
callback.Dispose();
chunk.Dispose();
}
};
static Handle<Value> New(const Arguments& args);
static Handle<Value> Format(const Arguments& args);
static void BeginFormat(Baton* baton);
static void DoFormat(uv_work_t* req);
static void AfterFormat(uv_work_t* req);
int inFormat;
int outFormat;
int inAlignment;
int outAlignment;
bool formatting;
char* buffer;
};
}
#endif