Skip to content

Commit f6afee1

Browse files
committed
update demo [ci skip]
1 parent 43ae060 commit f6afee1

File tree

3 files changed

+106
-18
lines changed

3 files changed

+106
-18
lines changed

demo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out.wav

demo/index.js

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require("fs");
44
const optionator = require("optionator")(require("./package").cliOptions);
5+
const tickable = require("tickable-timer");
56
const wae = require("../src");
67

78
function requireSourceIfExists(filepath) {
@@ -15,7 +16,7 @@ function requireSourceIfExists(filepath) {
1516
function createContextOptions(opts) {
1617
return Object.assign({
1718
sampleRate: +opts.rate,
18-
numberOfChannels: opts.channels|0,
19+
numberOfChannels: Math.max(1, opts.channels|0),
1920
}, {
2021
u8: { bitDepth: 8 },
2122
s16: { bitDepth: 16 },
@@ -25,12 +26,27 @@ function createContextOptions(opts) {
2526
}
2627

2728
function createPrintFunc(opts) {
28-
if (opts.noShowProgress) {
29-
return () => {};
29+
if (opts.verbose) {
30+
return (msg) => {
31+
process.stderr.write(msg + "\n");
32+
};
3033
}
31-
return (msg) => {
32-
process.stderr.write(msg + "\n");
33-
};
34+
return () => {};
35+
}
36+
37+
function fetchAudioBuffer(context, filename) {
38+
if (Array.isArray(filename)) {
39+
return Promise.all(filename.map(filename => fetchAudioBuffer(context, filename)));
40+
}
41+
42+
return new Promise((resolve, reject) => {
43+
fs.readFile(`${ __dirname }/assets/sound/${ filename }`, (err, data) => {
44+
if (err) {
45+
return reject(err);
46+
}
47+
resolve(context.decodeAudioData(data));
48+
});
49+
});
3450
}
3551

3652
function showHelp() {
@@ -58,20 +74,78 @@ function main(opts) {
5874
return console.log(`demo: ${ name } is not found`);
5975
}
6076

61-
runWithStreamAudioContext(func, opts);
77+
if (opts.out) {
78+
runWithRenderingAudioContext(func, opts);
79+
} else {
80+
runWithStreamAudioContext(func, opts);
81+
}
82+
}
83+
84+
function runWithRenderingAudioContext(func, opts) {
85+
const AudioContext = wae.RenderingAudioContext;
86+
const context = new AudioContext(createContextOptions(opts));
87+
88+
let isEnded = false;
89+
90+
const util = {
91+
fetchAudioBuffer: fetchAudioBuffer.bind(null, context),
92+
timerAPI: tickable,
93+
print: createPrintFunc(opts),
94+
end: () => { isEnded = true }
95+
};
96+
97+
const promise = func(context, util) || Promise.resolve();
98+
99+
promise.then(() => {
100+
let currentTime = 0;
101+
let counter = 0;
102+
103+
const beginTime = Date.now();
104+
const duration = Math.max(0, opts.duration) || 10;
105+
const numberOfProcessing = Math.ceil((duration * context.sampleRate) / context.processingSizeInFrames);
106+
const processingTimeInFrames = context.processingSizeInFrames / context.sampleRate;
107+
108+
for (let i = 0; i < numberOfProcessing && !isEnded; i++) {
109+
tickable.tick(processingTimeInFrames * 1000);
110+
context.processTo(processingTimeInFrames * i);
111+
}
112+
113+
const endTime = Date.now();
114+
const outputFilename = opts.out || "out.wav";
115+
const audioData = context.exportAsAudioData();
116+
117+
context.encodeAudioData(audioData).then((arrayBuffer) => {
118+
fs.writeFile(outputFilename, new Buffer(arrayBuffer), (err) => {
119+
if (err) {
120+
console.error(err);
121+
} else {
122+
const renderingTime = (endTime - beginTime) / 1000;
123+
const duration = context.currentTime;
124+
125+
console.log(`rendering time: ${ renderingTime }sec; duration: ${ duration }sec`);
126+
}
127+
});
128+
});
129+
});
62130
}
63131

64132
function runWithStreamAudioContext(func, opts) {
65133
const AudioContext = wae.StreamAudioContext;
66134
const context = new AudioContext(createContextOptions(opts));
67-
68-
func(context, {
135+
const util = {
136+
fetchAudioBuffer: fetchAudioBuffer.bind(null, context),
69137
timerAPI: global,
70-
print: createPrintFunc(opts)
71-
});
138+
print: createPrintFunc(opts),
139+
end: () => { process.exit(0) }
140+
};
72141

73142
context.pipe(process.stdout);
74-
context.resume();
143+
144+
const promise = func(context, util) || Promise.resolve();
145+
146+
promise.then(() => {
147+
context.resume();
148+
});
75149
}
76150

77151
const opts = optionator.parse(process.argv);

demo/package.json

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,31 @@
3939
"default": "2"
4040
},
4141
{
42-
"option": "no-show-progress",
43-
"alias": "q",
42+
"option": "duration",
43+
"alias": "d",
44+
"type": "Number",
45+
"description": "Number of duration for rendering"
46+
},
47+
{
48+
"option": "out",
49+
"alias": "o",
50+
"type": "String",
51+
"description": "Write output to <file>"
52+
},
53+
{
54+
"option": "verbose",
55+
"alias": "V",
4456
"type": "Boolean",
45-
"description": "Run in quite mode",
57+
"description": "Run in verbose mode",
4658
"default": "false"
4759
}
4860
],
4961
"append": "AUDIO FILE FORMATS: s16 s32 u8 raw"
5062
},
51-
"dependencies": {},
52-
"devDependencies": {
53-
"optionator": "^0.8.1"
63+
"dependencies": {
64+
"optionator": "^0.8.1",
65+
"power-assert": "^1.3.1",
66+
"tickable-timer": "^1.0.0"
5467
},
5568
"license": "MIT",
5669
"main": "index.js",

0 commit comments

Comments
 (0)