/
MidiEngine.java
213 lines (173 loc) · 4.03 KB
/
MidiEngine.java
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
/*
* Generates the actual MidiMusic, is independent of time
*/
import javax.sound.midi.*;
import javax.sound.midi.MidiDevice.*;
import java.io.*;
import java.util.*;
public class MidiEngine implements AudioEngine
{
private Synthesizer synth;
private MidiChannel[] midiChannels;
private Instrument[] instruments;
private int activeChannel = 0;
private int volume = 0; // volume
public static int velocityOn = 45;
public static int velocityOff = 127;
public static int highestNote = 45;
MidiEngine()
{
synth = null;
try
{
synth = MidiSystem.getSynthesizer();
synth.open();
}
catch (MidiUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
midiChannels = synth.getChannels();
instruments = synth.getAvailableInstruments();
System.out.println("Midi Sound Engine Started");
System.out.println("Channels: "+midiChannels.length);
System.out.println("Voices: "+synth.getMaxPolyphony());
}
/*
* Display list of instruments in Sound Bank
*/
public Vector showInstruments()
{
Vector insOut = new Vector();
for(int i=0; i < instruments.length; i++)
{
insOut.add(instruments[i].getName());
}
return insOut;
}
public static void setVelocityOn(int v)
{
velocityOn = v;
System.out.println("Velocity On: "+velocityOn);
}
public static void setVelocityOff(int v)
{
velocityOff = v;
System.out.println("Velocity Off: "+velocityOff);
}
public static void setHighestNote(int n)
{
highestNote = n;
System.out.println("Highest Note: "+highestNote);
}
public void setVolume(int v)
{
try
{
volume = v;
for(int i=0; i < midiChannels.length; i++)
{
midiChannels[i].controlChange(7, (int) (127*(((float)volume)/100)));
}
System.out.println("Volume set to: "+ volume);
}
catch(Exception e)
{
System.out.println(e);
}
}
public Instrument getInstrument(int i)
{
return instruments[i];
}
public int getVolume()
{
return volume;
}
public void muteEngine()
{
System.out.println("All Notes Off");
for(int i=0; i < midiChannels.length; i++)
{
midiChannels[i].allNotesOff();
}
}
/*
* Set the next free midi channel
*/
private int setActiveChannel()
{
activeChannel++;
//Channel 10 (9 when counting from 0) is reserved for percusion instruments
if(activeChannel == 9) activeChannel = 10;
if(activeChannel >= 16) activeChannel = 0;
return activeChannel;
}
public int getActiveChannel()
{
return activeChannel;
}
/*
* Assigns an instrument to the active channel
*/
private void setInstrument(int i)
{
Patch pat = getInstrument(i).getPatch();
midiChannels[activeChannel].programChange(pat.getProgram());
}
private void channelOff(MidiObject mo)
{
//midiChannels[mo.midiChannel].setPolyPressure(mo.note, velocityOff);
midiChannels[mo.midiChannel].noteOff(mo.note, velocityOff);
}
private void channelOn(MidiObject mo)
{
midiChannels[mo.midiChannel].noteOn(mo.note, velocityOn);
}
public void handleObject(AudibleObject ao)
{
setActiveChannel();
setInstrument(ao.getInstrument());
preUpdateAudibleObject(ao);
if(ao.getHits() > 0) playObject(ao);
ao.flushHits();
}
public void playObject(AudibleObject ao)
{
if((Auralizer.playModifiedOnly && ao.isModified() == true) || Auralizer.playModifiedOnly == false)
{
MidiObject mo = new MidiObject(ao, this);
channelOn(mo);
updateAudibleObject(ao, mo);
ao.setHasPlayed();
}
}
//may be buggy and need to add some more functionality
public void muteObject(AudibleObject ao)
{
setInstrument(ao.getInstrument());
preUpdateAudibleObject(ao);
ao.flushHits();
}
/*
* Handels turning off and updating past midi channel events
*/
private void preUpdateAudibleObject(AudibleObject ao)
{
if(ao.hasPlayed)
try
{
MidiObject mo = (MidiObject) ao.metaData.get("midiObject");
channelOff(mo);
ao.hasPlayed = false;
}
catch(NullPointerException npe)
{
}
}
private void updateAudibleObject(AudibleObject ao, MidiObject mo)
{
ao.metaData.put("midiObject",mo);
}
}