This repository has been archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harmonin.jsx
223 lines (199 loc) · 6.07 KB
/
harmonin.jsx
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
var Osiris = require('./channels/Osiris');
var Sampler = require('./channels/Sampler')
const ChannelUI = require('./ui/ChannelUI');
const React = require('react');
const ReactDOM = require('react-dom');
const WaveformVisualizer = require('./ui/WaveformVisualizer');
const MidiKeyboard = require('./ui/MidiKeyboard');
import {Tabs} from 'react-tabs';
Tabs.setUseDefaultStyles(false);
class Harmonin extends React.Component {
constructor() {
super();
this.state = {
masterVisualizerHeight: 0
};
this.midiKeyboard = new MidiKeyboard((channel, type, note, velocity) => this.onMidiEvent(channel, type, note, velocity));
this.midiKeyboard.attach();
this.channelUIs = {};
var audioContext = new AudioContext();
this.channels = [
new Osiris(audioContext, require('./presets/osiris/UltraNiceAnalogueStyleSaw')),
new Osiris(audioContext, require('./presets/osiris/Mellosynth')),
new Osiris(audioContext, require('./presets/osiris/WidePad')),
new Osiris(audioContext, require('./presets/osiris/SquarePluck')),
new Sampler(audioContext, {
volume: 0.6,
filename: 'data/kick.wav'
}),
new Sampler(audioContext, {
volume: 0.6,
filename: 'data/snare.wav'
}),
new Sampler(audioContext, {
reverb: 0.25,
volume: 0.1,
filename: 'data/hihat.wav'
}),
new Sampler(audioContext, {
reverb: .5,
volume: 0.5,
filename: 'data/crash.wav'
})
];
this.masterOutputNode = audioContext.createGain();
this.masterOutputNode.connect(audioContext.destination);
this.reverbNode = audioContext.createConvolver();
this.reverbNode.connect(this.masterOutputNode);
const that = this;
(function() {
var request = new XMLHttpRequest();
request.open('GET', 'data/irHall.ogg', true);
request.responseType = 'arraybuffer';
request.onload = function() {
audioContext.decodeAudioData(request.response, function(buffer) {
that.reverbNode.buffer = buffer;
},
function(e){console.log(e)});
}
request.send();
})();
var eventTimingLoopNode = audioContext.createScriptProcessor(256, 1, 1);
eventTimingLoopNode.connect(audioContext.destination);
var time = audioContext.currentTime;
var oldTime = time;
var deltaTime = 0;
eventTimingLoopNode.onaudioprocess = () => {
time = audioContext.currentTime;
deltaTime += time - oldTime;
oldTime = time;
var step = 256 / audioContext.sampleRate;
while(deltaTime >= step) {
deltaTime -= step;
tick(time, step);
}
};
var midiFile;
/*
var oReq = new XMLHttpRequest();
oReq.open("GET", "/harmonin2.mid");
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response;
if (arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer);
midiFile = new Midi(byteArray);
midiFile.add_callback(e => {
that.onMidiEvent(e.midi_channel, e.type, e.note_number, e.velocity);
});
}
};
oReq.send(null);
*/
function tick(time, stepSize) {
if(midiFile) {
midiFile.play_forward(stepSize * 1000);
}
for(var channel of that.channels) {
channel.tick(time);
}
}
navigator.requestMIDIAccess({}).then(midiAccess => {
var midi = midiAccess;
for(var input of midi.inputs.values()) {
input.addEventListener('midimessage', e => {
var channel = e.data[0] & 0xf;
var type = (e.data[0] & 0xf0) >> 4;
var note = e.data[1];
var velocity = e.data[2];
this.onMidiEvent(channel, type, note, velocity);
});
}
}, function(e) {
console.log(e);
});
}
onMidiEvent(channel, type, note, velocity) {
switch(type) {
case 0x9:
this.channels[channel].noteOn(note, velocity);
break;
case 0x8:
this.channels[channel].noteOff(note, velocity);
break;
case 0xB:
if(channel in this.channels) {
this.channels[channel].mod(note, velocity);
}
if(this.channelUIs) {
if(channel in this.channels && this.channels[channel].id in this.channelUIs) {
this.channelUIs[this.channels[channel].id].mod(note, velocity);
}
}
break;
}
}
resize() {
this.setState({
masterVisualizerHeight: window.innerHeight - 30
});
}
componentDidMount() {
window.addEventListener('resize', e => { this.resize(); });
this.resize();
}
solo(id) {
let didChange = false;
for(let channelId in this.channelUIs) {
if(id == channelId) {
if(this.channelUIs[channelId].state.isMuted) {
didChange = true;
}
this.channelUIs[channelId].unmute();
} else {
if(!this.channelUIs[channelId].state.isMuted) {
didChange = true;
}
this.channelUIs[channelId].mute();
}
}
if(!didChange) {
for(let channelId in this.channelUIs) {
this.channelUIs[channelId].unmute();
}
}
}
soloCollapse(id) {
let target = true;
for(let channelId in this.channelUIs) {
target = target && this.channelUIs[channelId].state.isCollapsed;
}
for(let channelId in this.channelUIs) {
this.channelUIs[channelId].setState({isCollapsed: !target});
}
}
render() {
return (
<div>
{this.channels.map(channel =>
<ChannelUI
channel={channel}
harmonin={this}
key={channel.id}
ref={channelUI => this.channelUIs[channel.id] = channelUI}
/>)};
<div
className="master-visualizer visualizer"
ref={ref => this.masterVisualizerWrapper = ref}
>
<WaveformVisualizer
audioNode={this.masterOutputNode}
width={100}
height={this.state.masterVisualizerHeight}
/>
</div>
</div>
);
}
}
ReactDOM.render(<Harmonin />, document.querySelector('#container'));