Mercurial > hg > jslab
comparison src/samer/midi/MidiSynth.java @ 2:74cc9e431818
Revert change to MidiSynth
author | samer |
---|---|
date | Fri, 05 Apr 2019 16:43:56 +0100 |
parents | 5df24c91468d |
children |
comparison
equal
deleted
inserted
replaced
1:5df24c91468d | 2:74cc9e431818 |
---|---|
34 pedal control whether or not note off events are generated | 34 pedal control whether or not note off events are generated |
35 */ | 35 */ |
36 | 36 |
37 public class MidiSynth extends Viewable implements Agent, Task, Saveable | 37 public class MidiSynth extends Viewable implements Agent, Task, Saveable |
38 { | 38 { |
39 VVector in; | |
40 Synthesizer synthesizer=null; | |
41 | |
39 protected int n; | 42 protected int n; |
40 protected double [] x; | 43 protected double [] x; |
41 protected int [] nnums; | 44 protected int [] nnums; |
42 protected int [] chans; | 45 protected int [] chans; |
43 protected MidiChannel [] cc; | 46 protected MidiChannel [] cc; |
44 | 47 |
45 protected VDouble factor; | 48 protected VDouble factor; |
46 protected VInteger offset; | 49 protected VInteger offset; |
47 protected VBoolean pedal; | 50 protected VBoolean pedal; |
48 | 51 |
49 public MidiSynth(VVector input, Synthesizer synth) | 52 public MidiSynth(VVector input) |
50 { | 53 { |
51 super("midisynth"); | 54 super("midisynth"); |
52 | 55 |
53 Shell.push(node); | 56 Shell.push(node); |
54 factor = new VDouble("factor",128,0); | 57 factor = new VDouble("factor",128,0); |
56 pedal = new VBoolean("pedal",false,0); | 59 pedal = new VBoolean("pedal",false,0); |
57 setAgent(this); | 60 setAgent(this); |
58 addAgent(new Saver(this)); | 61 addAgent(new Saver(this)); |
59 Shell.pop(); | 62 Shell.pop(); |
60 | 63 |
61 n = input.size(); | 64 in = input; |
62 x = input.array(); | 65 n = in.size(); |
63 cc = synth.getChannels(); | 66 x = in.array(); |
64 | 67 |
65 nnums = new int[n]; | 68 nnums = new int[n]; |
66 chans = new int[n]; | 69 chans = new int[n]; |
67 for (int i=0; i<n; i++) { nnums[i]=i; chans[i]=0; } | 70 for (int i=0; i<n; i++) { nnums[i]=i; chans[i]=0; } |
68 | 71 |
80 add(Shell.createButtonsFor(MidiSynth.this)); | 83 add(Shell.createButtonsFor(MidiSynth.this)); |
81 } | 84 } |
82 }; | 85 }; |
83 } | 86 } |
84 | 87 |
85 public void open() { } | 88 public void open() throws Exception { |
86 public void close() { } | 89 if (synthesizer!=null) { Shell.print("already open"); return; } |
90 if ((synthesizer = MidiSystem.getSynthesizer()) == null) { | |
91 throw new Exception("getSynthesizer() failed"); | |
92 } | |
93 Shell.print("opening synthesizer"); | |
94 synthesizer.open(); | |
95 | |
96 cc = synthesizer.getChannels(); | |
97 Shell.print("got synth and channel:"+synthesizer+cc); | |
98 } | |
99 | |
100 public void close() { | |
101 if (synthesizer != null) { | |
102 Shell.print("closing synthesiser"); | |
103 synthesizer.close(); | |
104 synthesizer = null; | |
105 } | |
106 } | |
107 | |
87 public void dispose() { | 108 public void dispose() { |
88 close(); | 109 close(); |
89 factor.dispose(); | 110 factor.dispose(); |
90 offset.dispose(); | 111 offset.dispose(); |
91 pedal.dispose(); | 112 pedal.dispose(); |
100 | 121 |
101 public void starting() {} | 122 public void starting() {} |
102 public void stopping() {} | 123 public void stopping() {} |
103 public void run() | 124 public void run() |
104 { | 125 { |
126 if (synthesizer==null) return; | |
127 | |
105 for (int i=0; i<n; i++) { | 128 for (int i=0; i<n; i++) { |
106 if (x[i]>0) { | 129 if (x[i]>0) { |
107 cc[chans[i]].noteOn(nnums[i]+offset.value,mapVelocity(x[i])); | 130 cc[chans[i]].noteOn(nnums[i]+offset.value,mapVelocity(x[i])); |
108 } else if (x[i]<0 && !pedal.value) { | 131 } else if (x[i]<0 && !pedal.value) { |
109 cc[chans[i]].noteOff(nnums[i]+offset.value, 0); | 132 cc[chans[i]].noteOff(nnums[i]+offset.value, 0); |