-
Notifications
You must be signed in to change notification settings - Fork 103
/
d3.html
199 lines (175 loc) · 5.92 KB
/
d3.html
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>waveform-data.js Demo Page</title>
<style>
body {
font-family: 'Helvetica neue', Helvetica, Arial, sans-serif;
}
#titles, #waveform-container, #waveform-controls {
margin: 24px auto;
width: 1000px;
}
#demo-controls {
margin: 0 auto 24px auto;
width: 1000px;
display: flex;
align-items: center;
}
#demo-controls button {
background: #fff;
border: 1px solid #919191;
cursor: pointer;
}
#audio {
flex: 0 0 30%;
}
#controls {
flex: 1;
margin-left: 1em;
}
</style>
</head>
<body>
<div id="titles">
<h1>waveform-data.js</h1>
<p>
waveform-data.js is a JavaScript library that allows you to load and
manipulate audio waveform data files.
</p>
<p>
It was developed by <a href="https://www.bbc.co.uk/rd">BBC R&D</a>
to support <a href="https://github.com/bbc/peaks.js">Peaks.js</a>.
You can read more about the project
<a href="https://waveform.prototyping.bbc.co.uk/">here</a>.
</p>
<h2>Demo: Scalable Vector Graphics (SVG) using d3.js</h2>
<p>
This demo shows how to use <a href="https://d3js.org/">d3.js</a> to
draw a waveform image using <a href="https://developer.mozilla.org/en-US/docs/Web/SVG">SVG</a>.
</p>
</div>
<div id="waveform-container">
</div>
<div id="demo-controls">
<audio id="audio" controls="controls">
<source src="07023003.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<div id="controls">
<button data-action="load-dat">Load binary waveform data</button>
<button data-action="load-json">Load JSON waveform data</button>
<button data-action="generate">Generate using Web Audio API</button>
</div>
</div>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="waveform-data.js"></script>
<script>
const AudioContext = window.AudioContext || window.webkitAudioContext;
const drawWaveform = (waveform, color) => {
const channel = waveform.channel(0);
const container = d3.select('#waveform-container');
const x = d3.scaleLinear();
const y = d3.scaleLinear();
const offsetX = 100;
const min = channel.min_array();
const max = channel.max_array();
x.domain([0, waveform.length]).rangeRound([0, 1000]);
y.domain([d3.min(min), d3.max(max)]).rangeRound([offsetX, -offsetX]);
const area = d3.area()
.x((d, i) => x(i))
.y0((d, i) => y(min[i]))
.y1((d, i) => y(d));
container.select('svg').remove();
const graph = container.append('svg')
.style('width', '1000px')
.style('height', '200px')
.datum(max)
.append('path')
.attr('transform', () => `translate(0, ${offsetX})`)
.attr("d", area)
.attr('fill', color)
.attr('stroke', color);
};
document.querySelector('button[data-action="load-dat"]').addEventListener('click', function() {
fetch('07023003-2channel.dat')
.then(response => {
if (response.ok) {
return response.arrayBuffer();
}
else {
throw new Error(`${response.status} ${response.statusText}`);
}
})
.then(buffer => WaveformData.create(buffer))
.then(waveform => {
console.log(`Waveform has ${waveform.channels} channels`);
console.log(`Waveform has length ${waveform.length} points`);
drawWaveform(waveform, '#929982');
})
.catch(err => {
console.error(err.message);
});
});
document.querySelector('button[data-action="load-json"]').addEventListener('click', function() {
fetch('07023003-2channel.json')
.then(response => {
if (response.ok) {
return response.json();
}
else {
throw new Error(`${response.status} ${response.statusText}`);
}
})
.then(json => WaveformData.create(json))
.then(waveform => {
console.log(`Waveform has ${waveform.channels} channels`);
console.log(`Waveform has length ${waveform.length} points`);
drawWaveform(waveform, '#7a93ac');
})
.catch(err => {
console.error(err.message);
});
});
document.querySelector('button[data-action="generate"]').addEventListener('click', function() {
fetch('07023003.mp3')
.then(response => {
if (response.ok) {
return response.arrayBuffer();
}
else {
throw new Error(`${response.status} ${response.statusText}`);
}
})
.then(buffer => {
const audioContext = new AudioContext();
const options = {
audio_context: audioContext,
array_buffer: buffer,
scale: 128
};
return new Promise((resolve, reject) => {
WaveformData.createFromAudio(options, (err, waveform) => {
if (err) {
reject(err);
}
else {
resolve(waveform);
}
audioContext.close();
});
});
})
.then(waveform => {
console.log(`Waveform has ${waveform.channels} channels`);
console.log(`Waveform has length ${waveform.length} points`);
drawWaveform(waveform, '#b7999c');
})
.catch(err => {
console.error(err.message);
});
});
</script>
</body>
</html>