-
Notifications
You must be signed in to change notification settings - Fork 68
/
ddnoise.js
138 lines (134 loc) · 4.12 KB
/
ddnoise.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
"use strict";
import * as utils from "./utils.js";
import _ from "underscore";
const IDLE = 0;
const SPIN_UP = 1;
const SPINNING = 2;
const VOLUME = 0.25;
export class DdNoise {
constructor(context) {
this.context = context;
this.sounds = {};
this.state = IDLE;
this.motor = null;
this.gain = context.createGain();
this.gain.gain.value = VOLUME;
this.gain.connect(context.destination);
// workaround for older safaris that GC sounds when they're playing...
this.playing = [];
}
async initialise() {
const sounds = await loadSounds(this.context, {
motorOn: "sounds/disc525/motoron.wav",
motorOff: "sounds/disc525/motoroff.wav",
motor: "sounds/disc525/motor.wav",
step: "sounds/disc525/step.wav",
seek: "sounds/disc525/seek.wav",
seek2: "sounds/disc525/seek2.wav",
seek3: "sounds/disc525/seek3.wav",
});
this.sounds = sounds;
}
oneShot(sound) {
const duration = sound.duration;
const context = this.context;
if (context.state !== "running") return duration;
const source = context.createBufferSource();
source.buffer = sound;
source.connect(this.gain);
source.start();
return duration;
}
play(sound, loop) {
if (this.context.state !== "running") return Promise.reject();
return new Promise((resolve) => {
const source = this.context.createBufferSource();
source.loop = !!loop;
source.buffer = sound;
source.connect(this.gain);
source.onended = () => {
this.playing = _.without(this.playing, source);
if (!source.loop) resolve();
};
source.start();
this.playing.push(source);
if (source.loop) {
resolve(source);
}
});
}
spinUp() {
if (this.state === SPINNING || this.state === SPIN_UP) return;
this.state = SPIN_UP;
this.play(this.sounds.motorOn).then(
() => {
// Handle race: we may have had spinDown() called on us before the
// spinUp() initial sound finished playing.
if (this.state === IDLE) {
return;
}
this.play(this.sounds.motor, true).then((source) => {
this.motor = source;
this.state = SPINNING;
});
},
() => {},
);
}
spinDown() {
if (this.state === IDLE) return;
this.state = IDLE;
if (this.motor) {
this.motor.stop();
this.motor = null;
this.oneShot(this.sounds.motorOff);
}
}
seek(diff) {
if (diff < 0) diff = -diff;
if (diff === 0) return 0;
else if (diff <= 2) return this.oneShot(this.sounds.step);
else if (diff <= 20) return this.oneShot(this.sounds.seek);
else if (diff <= 40) return this.oneShot(this.sounds.seek2);
else return this.oneShot(this.sounds.seek3);
}
mute() {
this.gain.gain.value = 0;
}
unmute() {
this.gain.gain.value = VOLUME;
}
}
async function loadSounds(context, sounds) {
const loaded = await Promise.all(
_.map(sounds, async (sound) => {
// Safari doesn't support the Promise stuff directly, so we create
// our own Promise here.
const data = await utils.loadData(sound);
return await new Promise((resolve) => {
context.decodeAudioData(data.buffer, (decodedData) => {
resolve(decodedData);
});
});
}),
);
const keys = _.keys(sounds);
const result = {};
for (let i = 0; i < keys.length; ++i) {
result[keys[i]] = loaded[i];
}
return result;
}
export class FakeDdNoise {
constructor() {}
seek() {
return 0;
}
initialise() {
return Promise.resolve();
}
spinUp() {}
spinDown() {}
mute() {}
unmute() {}
}