1
6
7 package simworldobjects;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import javax.swing.*;
12 import javax.swing.border.*;
13 import javax.swing.table.*;
14 import javax.swing.event.*;
15 import javax.sound.midi.*;
16 import java.util.*;
17 import josx.platform.rcx.Sound.*;
18 import josx.util.*;
19
20
21
25 public class Sound extends Thread {
26
27 private static Synthesizer synthesizer;
28 private static Sequencer sequencer;
29 private static Sequence sequence;
30 private static Instrument instruments[];
31 private static ChannelData cc;
32
33
34 public static void playTune(int frequency, int duration){
35
36 try {
37
38 if (synthesizer == null) {
39 if ((synthesizer = MidiSystem.getSynthesizer()) == null) {
40 System.out.println("getSynthesizer() failed!");
41 return;
42 }
43 }
44
45 synthesizer.open();
46 sequencer = MidiSystem.getSequencer();
47 sequence = new Sequence(Sequence.PPQ, 2);
48
49 } catch (Exception ex) { ex.printStackTrace(); return; }
50
51 Soundbank sb = synthesizer.getDefaultSoundbank();
52 if (sb != null) {
53 instruments = synthesizer.getDefaultSoundbank().getInstruments();
54 synthesizer.loadInstrument(instruments[341]);
55
56 }
57
58 MidiChannel midiChannels[] = synthesizer.getChannels();
59 cc = new ChannelData(midiChannels[0], 0);
60
61 cc.channel.programChange(341);
62
63 cc.channel.allNotesOff();
64
65 cc.channel.noteOn(frequency, cc.velocity);
66
67 try{Thread.sleep(duration);}
68 catch(Exception e){}
69
70 cc.channel.allNotesOff();
71
72
73 }
74
75 public static LinkedList getInstrumentList(){
76 LinkedList instrumentList = new LinkedList();
77
78 for(int i=0; i<instruments.length;i++){
79 instrumentList.add(instruments[i].getName());
80 }
82
83 return instrumentList;
84
85 }
86
87 public static void close(){
88 if (synthesizer != null) {
89 synthesizer.close();
90 }
91 if (sequencer != null) {
92 sequencer.close();
93 }
94 sequencer = null;
95 synthesizer = null;
96 instruments = null;
97 cc = null;
98 }
99
100 static class ChannelData {
101
102 MidiChannel channel;
103 boolean solo, mono, mute, sustain;
104 int velocity, pressure, bend, reverb;
105 int row, col, num;
106
107 public ChannelData(MidiChannel channel, int num) {
108 this.channel = channel;
109 this.num = num;
110 velocity = pressure = bend = reverb = 100;
111 velocity = 127;
112 }
113 }
114
115
137
138 }
139