-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.es6
58 lines (55 loc) · 2.02 KB
/
main2.es6
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
var lengthField = document.querySelector(".length");
var rateField = document.querySelector(".rate");
var adjustField = document.querySelector(".adjust");
var offlineCtx;
var ctx = new (window.AudioContext || window.webkitAudioContext)();
startOne();
document.querySelector("button").addEventListener('click', function() {
startOne();
});
function startOne() {
var random_score = []
current_note = ["c", 4]
for(var i = 0;i < 50; i++) {
var current_note = musicLearner.next_note(current_note);
var note = new Note(current_note[0].toUpperCase()+current_note[1].toString());
random_score.push(note);
}
var numChannels = 2, lengthInSamples = parseInt(lengthField.value, 10), sampleRate = 44100;
lengthInSamples = parseInt(lengthField.value, 10) || 15;
offlineCtx = new OfflineAudioContext(numChannels, sampleRate*lengthInSamples, sampleRate);
var node = new Waver(offlineCtx, 32.0, function(t) {
var i = new Instrument(t);
var note = random_score[Math.floor((t) % random_score.length)];
return (0
+ i.chord(note.freq)
+ i.bass((note.freq*(3/2))/2)
+ i.lowDrums(100)
)/3;
});
var source = offlineCtx.createBufferSource();
source.loop = true;
source.playbackRate.value = parseInt(rateField.value, 10) || 3;
source.buffer = node.sourceBuffer;
source.connect(offlineCtx.destination);
source.start();
offlineCtx.startRendering()
offlineCtx.oncomplete = function(e) {
console.log('Rendering completed successfully');
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var song = audioCtx.createBufferSource();
song.connect(audioCtx.destination);
song.buffer = e.renderedBuffer;
var ex = new Export(e.renderedBuffer);
var songBlob = ex.export();
var a = document.createElement("a");
document.body.appendChild(a);
var url = window.URL.createObjectURL(songBlob);
a.href = url;
a.download = "song.wav";
a.textContent = "Download";
var audio = document.querySelector("audio");
audio.src = url;
//song.start();
}
};