-
Notifications
You must be signed in to change notification settings - Fork 8
/
pcmdata.js
156 lines (144 loc) · 4.51 KB
/
pcmdata.js
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
// see https://raw.githubusercontent.com/jussi-kalliokoski/pcmdata.js/master/js/pcmdata.js
this.PCMData = (function(Binary, Stream){
function PCMData(data){
return (typeof data === 'string' ? PCMData.decode : PCMData.encode)(data);
}
PCMData.decodeFrame = function(frame, bitCount, result){
if (bitCount === 8){
var buffer = new (window.Uint8Array || Array)(result.length);
(new Stream(frame)).readBuffer(buffer, 8, 'Uint');
for (bitCount=0; bitCount<result.length; bitCount++){
result[bitCount] = (buffer[bitCount] - 127.5) * 127.5;
}
} else {
(new Stream(frame)).readBuffer(result, bitCount, 'Q');
}
return result;
};
PCMData.encodeFrame = function(frame, bitCount){
var properWriter = Binary[(bitCount === 8 ? 'fromUint' : 'fromQ') + bitCount],
l = frame.length,
r = '',
i;
if (bitCount === 8){
for (i=0; i<l; i++){
r += properWriter(frame[i] * 127.5 + 127.5);
}
} else {
for (i=0; i<l; i++){
r += properWriter(frame[i]);
}
}
return r;
};
PCMData.decode = function(data, asyncCallback){
var stream = new Stream(data),
sGroupID1 = stream.read(4),
dwFileLength = stream.readUint32();
stream = new Stream(stream.read(dwFileLength));
var sRiffType = stream.read(4),
sGroupID2 = stream.read(4),
dwChunkSize1 = stream.readUint32(),
formatChunk = new Stream(stream.read(dwChunkSize1)),
wFormatTag = formatChunk.readUint16(),
wChannels = formatChunk.readUint16(),
dwSamplesPerSec = formatChunk.readUint32(),
dwAvgBytesPerSec = formatChunk.readUint32(),
wBlockAlign = formatChunk.readUint16(),
sampleSize = wBlockAlign / wChannels,
dwBitsPerSample = /* dwChunkSize1 === 16 ? */ formatChunk.readUint16() /* : formatChunk.readUint32() */,
sGroupID,
dwChunkSize,
sampleCount,
chunkData,
samples,
dataTypeList,
i,
chunks = {},
output = {
channelCount: wChannels,
bytesPerSample: wBlockAlign / wChannels,
sampleRate: dwAvgBytesPerSec / wBlockAlign,
chunks: chunks,
data: samples
};
function readChunk(){
sGroupID = stream.read(4);
dwChunkSize = stream.readUint32();
chunkData = stream.read(dwChunkSize);
dataTypeList = chunks[sGroupID] = chunks[sGroupID] || [];
if (sGroupID === 'data'){
sampleCount = ~~(dwChunkSize / sampleSize);
samples = output.data = new (typeof Float32Array !== 'undefined' ? Float32Array : Array)(sampleCount);
PCMData.decodeFrame(chunkData, sampleSize * 8, samples);
} else {
dataTypeList.push(chunkData);
}
asyncCallback && (stream.data ? setTimeout(readChunk, 1) : asyncCallback(output));
}
if (asyncCallback){
stream.data ? readChunk() : asyncCallback(output);
} else {
while(stream.data){
readChunk();
}
}
return output;
}
PCMData.encode = function(data, asyncCallback){
var
dWord = Binary.fromUint32,
sWord = Binary.fromUint16,
samples = data.data,
sampleRate = data.sampleRate,
channelCount = data.channelCount || 1,
bytesPerSample = data.bytesPerSample || 1,
bitsPerSample = bytesPerSample * 8,
blockAlign = channelCount * bytesPerSample,
byteRate = sampleRate * blockAlign,
length = samples.length,
dLength = length * bytesPerSample,
padding = Math.pow(2, bitsPerSample - 1) - 1,
chunks = [],
chunk = '',
chunkType,
i, n, chunkData;
chunks.push(
'fmt ' + // sGroupID 4 bytes char[4]
dWord(16) + // dwChunkSize 4 bytes uint32 / dword
sWord(1) + // wFormatTag 2 bytes uint16 / ushort
sWord(channelCount) + // wChannels 2 bytes uint16 / ushort
dWord(sampleRate) + // dwSamplesPerSec 4 bytes uint32 / dword
dWord(byteRate) + // dwAvgBytesPerSec 4 bytes uint32 / dword
sWord(blockAlign) + // wBlockAlign 2 bytes uint16 / ushort
sWord(bitsPerSample) // dwBitsPerSample 2 or 4 bytes uint32 / dword OR uint16 / ushort
);
chunks.push(
'data' + // sGroupID 4 bytes char[4]
dWord(dLength) + // dwChunkSize 4 bytes uint32 / dword
PCMData.encodeFrame(samples, bitsPerSample)
);
chunkData = data.chunks;
if (chunkData){
for (i in chunkData){
if (chunkData.hasOwnProperty(i)){
chunkType = chunkData[i];
for (n=0; n<chunkType.length; n++){
chunk = chunkType[n];
chunks.push(i + dWord(chunk.length) + chunk);
}
}
}
}
chunks = chunks.join('');
chunks = 'RIFF' + // sGroupId 4 bytes char[4]
dWord(chunks.length) + // dwFileLength 4 bytes uint32 / dword
'WAVE' + // sRiffType 4 bytes char[4]
chunks;
asyncCallback && setTimeout(function(){
asyncCallback(chunks);
}, 1);
return chunks;
}
return PCMData;
}(this.Binary, this.Stream));