-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundIO.js
185 lines (176 loc) · 5.08 KB
/
soundIO.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use strict";
class SoundEncoder extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: "baudRate",
defaultValue: 1200,
minValue: 0,
automationRate: "k-rate",
},
];
}
static COMMAND_INVALID = -1;
static COMMAND_NOP = 0;
static COMMAND_HEADER = 1;
static COMMAND_BLANK = 2;
static COMMAND_BYTES = 3;
constructor(...args) {
super(...args);
this.commandQueue = [];
this.commandSequence = null;
this.seqBaudRate = 1200;
this.seqPos = 0;
this.seqKind = SoundEncoder.COMMAND_NOP;
this.seqData = null;
this.bitStartFrame = 0;
this.seqStatus1 = 0;
this.seqStatus2 = 0;
const self = this;
this.port.onmessage = function(event) {
const data = event.data;
if (data.type === "send") {
self.commandQueue.push(data);
}
};
}
startProcessingCommand(frame) {
if (this.commandSequence === null) return;
if (this.seqPos >= this.commandSequence.length) {
this.commandSequence = null;
this.seqKind = SoundEncoder.COMMAND_NOP;
this.port.postMessage({
"type": "done",
"id": this.commandSequenceId,
});
return;
}
const command = this.commandSequence[this.seqPos];
if (!command) {
this.seqKind = SoundEncoder.COMMAND_INVALID;
return;
}
// よく使うものを先に判定する
if (command.type === "bytes") {
this.seqKind = SoundEncoder.COMMAND_BYTES;
this.seqData = command.data;
this.seqStatus1 = -1; // 何ビット目か (-1:スタート 0~7:データ 8,9:ストップ)
this.seqStatus2 = 0; // 何バイト目か
} else if (command.type === "short_header") {
this.seqKind = SoundEncoder.COMMAND_HEADER;
this.seqData = null;
this.seqStatus1 = 0; // 何ビット送信したか
this.seqStatus2 = Math.floor(2000 * this.seqBaudRate / 1200); // 何ビット送信するか
} else if (command.type === "long_header") {
this.seqKind = SoundEncoder.COMMAND_HEADER;
this.seqData = null;
this.seqStatus1 = 0; // 何ビット送信したか
this.seqStatus2 = Math.floor(8000 * this.seqBaudRate / 1200); // 何ビット送信するか
} else if (command.type === "blank") {
this.seqKind = SoundEncoder.COMMAND_BLANK;
this.seqData = null;
this.seqStatus1 = Math.round(sampleRate * command.length_sec); // 何フレーム送信するか
this.seqStatus2 = frame + this.seqStatus1 - 1; // 送信を終了するフレーム
} else {
this.seqKind = SoundEncoder.COMMAND_INVALID;
this.seqData = null;
this.seqStatus1 = 0; // なし
this.seqStatus2 = 0; // なし
}
}
process(inputs, outputs, parameters) {
if (this.commandSequence === null && this.commandQueue.length > 0){
const commandData = this.commandQueue.shift();
this.commandSequence = commandData.commands;
this.commandSequenceId = commandData.id;
this.seqBaudRate = parameters.baudRate[0];
this.seqPos = 0;
this.startProcessingCommand(currentFrame);
this.bitStartFrame = currentFrame;
}
const framePerBit = Math.round(sampleRate / parameters.baudRate[0]);
const output = outputs[0][0];
for (let i = 0; i < output.length; i++) {
const frame = currentFrame + i;
const frameInBit = frame - this.bitStartFrame;
let bit = -1;
let requestNextBit = false;
switch (this.seqKind) {
case SoundEncoder.COMMAND_INVALID:
default:
this.seqPos++;
this.startProcessingCommand(frame);
break;
case SoundEncoder.COMMAND_NOP:
// 何もしない
break;
case SoundEncoder.COMMAND_HEADER:
bit = 1;
if (frameInBit >= framePerBit - 1) {
this.seqStatus1++;
if (this.seqStatus1 >= this.seqStatus2) {
this.seqPos++;
this.startProcessingCommand(frame);
}
requestNextBit = true;
}
break;
case SoundEncoder.COMMAND_BLANK:
if (frame >= this.seqStatus2) {
this.seqPos++;
this.startProcessingCommand(frame);
requestNextBit = true;
}
break;
case SoundEncoder.COMMAND_BYTES:
if (this.seqStatus1 < 0) bit = 0;
else if (this.seqStatus1 > 7) bit = 1;
else bit = (this.seqData[this.seqStatus2] >> this.seqStatus1) & 1;
if (frameInBit >= framePerBit - 1) {
this.seqStatus1++;
if (this.seqStatus1 > 9) {
this.seqStatus2++;
this.seqStatus1 = -1;
}
if (this.seqStatus2 >= this.seqData.length) {
this.seqPos++;
this.startProcessingCommand(frame);
}
requestNextBit = true;
}
break;
}
if (bit === 0) {
output[i] = frameInBit >= (framePerBit >> 1) ? 1 : -1;
} else if (bit === 1) {
const phase = (frameInBit * 4 / framePerBit) >>> 0;
output[i] = phase & 1 ? 1 : -1;
} else {
output[i] = 0;
}
if (requestNextBit) {
this.bitStartFrame = frame + 1;
}
}
return true;
}
}
class SoundDecoder extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: "baudRate",
defaultValue: 1200,
minValue: 0,
automationRate: "k-rate",
},
];
}
constructor(...args) {
super(...args);
}
process(inputs, outputs, parameters) {
}
}
registerProcessor("sound-encoder", SoundEncoder);
registerProcessor("sound-decoder", SoundDecoder);