-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
110 lines (87 loc) · 2.54 KB
/
index.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
/** @module audio-play, play audio buffer in node */
'use strict';
const AudioSource = require('audio-source/direct');
const AudioSpeaker = require('audio-speaker/direct');
const isAudioBuffer = require('is-audio-buffer');
const AudioBuffer = require('audio-buffer');
const AudioBufferUtils = require('audio-buffer-utils');
const idx = require('negative-index');
module.exports = function (buffer, how, cb) {
if (!isAudioBuffer(buffer)) throw Error('Argument should be an audio buffer');
if (how instanceof Function) {
cb = how;
}
how = how || {};
cb = cb || (() => {});
if (how.currentTime == null) how.currentTime = 0;
if (how.start == null) how.start = 0;
if (how.end == null) how.end = buffer.duration;
if (how.volume == null) how.volume = 1;
how.start = idx(how.start, buffer.duration);
how.end = idx(how.end, buffer.duration);
//prepare buffer - slice to duration
if (how.start != 0 || how.end != buffer.duration) {
let start = Math.floor(how.start * buffer.sampleRate);
let end = Math.floor(how.end * buffer.sampleRate);
let slicedBuffer = new AudioBuffer(null, {
numberOfChannels: buffer.numberOfChannels,
length: end - start,
sampleRate: buffer.sampleRate
});
for (let channel = 0; channel < buffer.numberOfChannels; channel++) {
slicedBuffer.getChannelData(channel).set(
buffer.getChannelData(channel).subarray(start, end)
);
}
buffer = slicedBuffer;
}
// apply volume
buffer = AudioBufferUtils.fill(buffer, v => v * how.volume)
//TODO: somewhere here goes rate mapping and detune
let read = AudioSource(buffer, {
loop: how.loop
}, () => {
//node-speaker fix: we send 2s of silence
write(new AudioBuffer(null, {
length: buffer.sampleRate * 2,
numberOfChannels: buffer.numberOfChannels,
sampleRate: buffer.sampleRate
}));
});
let write = AudioSpeaker({
channels: buffer.numberOfChannels,
sampleRate: buffer.sampleRate,
device: how.device
});
//provide API
play.play = pause.play = play;
play.pause = pause.pause = pause;
let isPlaying = false;
return how.autoplay != false ? play() : play;
function play () {
if (isPlaying) return;
isPlaying = true;
(function loop (err, buf) {
if (err) {
return cb(err);
}
if (!isPlaying) return;
buf = read(buf);
if (!buf) {
pause()
return
}
//track current time
how.currentTime += buf.duration
play.currentTime = pause.currentTime = how.currentTime
write(buf, loop);
}());
return pause;
}
function pause () {
if (!isPlaying) return;
isPlaying = false;
cb()
return play;
}
}