-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudionalSequencerWithMetaChannel
239 lines (208 loc) · 9.12 KB
/
AudionalSequencerWithMetaChannel
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html>
<head>
<title>Meta Sequence Player</title>
<style>
/* CSS styles go here */
</style>
</head>
<body>
<div id="drum-machine">
<h1>
<span class="button-label">Play</span>
<div class="button button-round">
<div class="button play-button" id="play"></div> <!-- Update the class and ID for play button -->
</div>
<span class="title">Audional Sequencer <span class="">₿</span>eta<span class="bright-orange">₿</span>itcoin<span class="">₿</span>eats</span>
<span class="button-label stop">Stop</span>
<div class="button button-round">
<div class="button stop-button" id="stop"></div> <!-- Update the class and ID for stop button -->
</div>
<div class="bpm-container">
BPM: <input type="range" min="60" max="180" value="125" step="1" id="bpm-slider">
<div class="bpm-display" id="bpm-display">125</div>
</div>
</h1>
<button id="load">Load Sequence</button>
<button id="play">Play</button>
<button id="stop">Stop</button>
<input type="file" id="fileInput">
<div class="subtext-container">
<p class="subtext centered-text">
Raw On-chain <span class="bright-orange">₿</span>itcoin Audionals: playing live, directly from the <span class="">₿</span>lockchain!
</p>
</div>
</span>
<main id="app" role="main">
<div class="channel" id="Channel-1">
<button class="load-sample-button" title="Load New Sequence">Load New Sequence</button>
<div>
<button class="control-button clear-button">C</button>
<button class="control-button mute-button">M</button>
</div>
<div class="channel-container">
<div class="steps-container"></div>
<div class="controls-container">
<div class="channel-controls">
<div class="button-container">
</div>
</div>
</div>
</div>
</div>
<script>
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var len = binaryString.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
let audioContext = new (window.AudioContext || window.webkitAudioContext)();
let sequence = null;
let currentStep = 0;
let isPlaying = false;
let bpm = 120;
let channels = [];
let stepDuration, currentStepTime, nextStepTime, timeoutId, stepsCount = 64;
function loadSequence(file) {
let reader = new FileReader();
reader.onload = function(e) {
sequence = JSON.parse(e.target.result);
bpm = sequence.bpm;
sequence.channels.forEach((channel, index) => {
channel.triggers = channel.triggers.map(Number);
channels[index] = channel;
if (channel.url) {
console.log(`Fetching audio for channel ${index}`);
console.log(`Channel ${index + 1} triggers: ${channel.triggers}`);
fetchAudio(channel.url, index);
}
});
};
reader.readAsText(file);
}
function checkChannels() {
console.log("Checking channels");
for (let i = 0; i < sequence.channels.length; i++) {
let channel = sequence.channels[i];
if (channel.mute) {
console.log("Channel " + i + " is muted");
continue;
}
if (channel.triggers.includes(currentStep + 1)) { // adjust for one-based indexing
console.log("Trigger for channel " + i); // Add this line
playAudio(channel.buffer, audioContext.currentTime);
} else {
console.log("Current step " + (currentStep + 1) + " not in triggers for channel " + i);
}
}
console.log("Advancing step");
advanceStep();
}
async function fetchAudio(url, channelId) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
const audioData = json.audioData;
// The audioData field is a base64 string, so we need to convert it to an ArrayBuffer
const base64Data = audioData.split(',')[1]; // remove the "data:audio/mpeg;base64," part
const raw = atob(base64Data);
const rawLength = raw.length;
const array = new Uint8Array(new ArrayBuffer(rawLength));
for(let i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
const audioBuffer = await context.decodeAudioData(array.buffer);
if (audioBuffers[channelId] !== undefined) {
audioBuffers[channelId].disconnect(); // disconnect the old buffer so we don't keep playing it
}
audioBuffers[channelId] = audioBuffer;
} catch (error) {
console.error('Error fetching audio:', error);
}
}
function playSequence() {
if (!sequence) return;
console.log('Starting scheduler');
startScheduler();
}
function stopSequence() {
console.log('Stopping scheduler');
stopScheduler();
}
function playAudio(audioBuffer) {
console.log('Playing audio');
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
source.start();
}
function startScheduler() {
stepDuration = 60.0 / bpm;
currentStepTime = audioContext.currentTime;
nextStepTime = currentStepTime;
isPlaying = true;
scheduler();
}
function scheduler() {
while (nextStepTime < audioContext.currentTime + 0.1) {
console.log('Checking channels'); // New log
channels.forEach((channel, index) => {
if (!channel.mute && channel.triggers.includes(currentStep + 1)) { // Added + 1 to currentStep
console.log(`Playing audio for channel ${index}`);
playAudio(channel.audioBuffer, nextStepTime); // Add nextStepTime to playAudio call
} else if (channel.mute) {
console.log(`Channel ${index} is muted`); // New log
} else if (!channel.triggers.includes(currentStep + 1)) { // Added + 1 to currentStep
console.log(`Current step ${currentStep + 1} not in triggers for channel ${index}`); // New log
}
});
console.log('Advancing step'); // New log
advanceStep();
}
console.log('Scheduling next check'); // New log
timeoutId = window.setTimeout(scheduler, 0);
}
function advanceStep() {
const secondsPerBeat = 60.0 / bpm;
nextStepTime += 0.25 * secondsPerBeat;
currentStep++;
if (currentStep >= stepsCount) {
console.log(`Reached end of sequence, starting from the beginning`); // New log
currentStep = 0;
} else {
console.log(`Advanced to step ${currentStep}`); // New log
}
}
function stopScheduler() {
window.clearTimeout(timeoutId);
isPlaying = false;
}
document.getElementById('load').addEventListener('click', () => {
let fileInput = document.getElementById('fileInput');
loadSequence(fileInput.files[0]);
});
document.getElementById('play').addEventListener('click', () => {
if (!isPlaying) {
console.log('Play button clicked');
playSequence();
isPlaying = true;
}
});
document.getElementById('stop').addEventListener('click', () => {
if (isPlaying) {
console.log('Stop button clicked');
stopSequence();
isPlaying = false;
currentStep = 0;
}
});
</script>
</body>
</html>