/
audio-system.js
132 lines (105 loc) · 3.66 KB
/
audio-system.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
/*
* Flocking Node.js Audio System
* http://github.com/colinbdclark/flocking
*
* Copyright 2013-2015, Colin Clark
* Dual licensed under the MIT and GPL Version 2 licenses.
*/
/*jslint white: true, vars: true, undef: true, newcap: true, regexp: true,
node: true, forin: true, continue: true, nomen: true,
bitwise: true, maxerr: 100, indent: 4 */
"use strict";
var fs = require("fs"),
url = require("url"),
fluid = fluid || require("infusion"),
flock = fluid.registerNamespace("flock");
fluid.defaults("flock.nodejs.audioSystem", {
gradeNames: "flock.audioSystem",
components: {
outputManager: {
type: "flock.nodejs.outputManager"
},
bufferWriter: {
type: "flock.nodejs.bufferWriter"
}
}
});
/********************************************************
* Monkeypatches for various parts of the buffer system *
********************************************************/
// TODO: Ditch all this by better componentizing Flocking's buffer system.
fluid.registerNamespace("flock.file");
flock.file.readFromPath = function (options) {
var path = options.src;
fs.exists(path, function (exists) {
if (!exists && options.error) {
options.error(path + " doesn't exist.");
return;
}
fs.stat(path, function (error, stats) {
fs.open(path, "r", function (error, fd) {
var buf = new Buffer(stats.size);
fs.read(fd, buf, 0, buf.length, null, function () {
var type = flock.file.parseFileExtension(path);
var arr = new Int8Array(buf);
options.success(arr.buffer, type);
});
});
});
});
};
fluid.registerNamespace("flock.net");
flock.net.readBufferFromUrl = function () {
throw new Error("Loading files from URLs is not currently supported in Node.js.");
};
fluid.registerNamespace("flock.audio.loadBuffer");
flock.audio.loadBuffer.readerForSource = function (src) {
if (typeof (src) !== "string") {
throw new Error("Flocking error: Can't load a buffer from an unknown type of source. " +
"Only paths and URLs are currently supported on Node.js.");
}
var parsed = url.parse(src);
return parsed.protocol === "data:" ? flock.file.readBufferFromDataUrl :
!parsed.protocol ? flock.file.readFromPath : flock.net.readBufferFromUrl;
};
fluid.registerNamespace("flock.audio.decode");
// TODO: Use a stream-style interface for decoding rather than just dumping the whole job on nextTick().
flock.audio.decode.node = function (options) {
process.nextTick(function () {
flock.audio.decode.sync(options);
});
};
flock.audio.registerDecoderStrategy({
"default": flock.audio.decode.node,
"aiff": flock.audio.decode.node
});
/**
* Distributes platform-specific grades for Node.js
*/
fluid.defaults("flock.nodejs.enviroContextDistributor", {
gradeNames: "fluid.component",
distributeOptions: [
{
target: "{/ flock.scheduler.once}.options.components.clock",
record: {
type: "flock.scheduler.scheduleClock"
}
},
{
target: "{/ flock.scheduler.repeat}.options.components.clock",
record: {
type: "flock.scheduler.intervalClock"
}
},
{
target: "{/ flock.enviro > audioSystem}.options",
record: {
gradeNames: "flock.nodejs.audioSystem"
}
}
]
});
fluid.constructSingle([], {
singleRootType: "flock.enviroContextDistributor",
type: "flock.nodejs.enviroContextDistributor"
});