java制作简易电子琴
时间: 2023-09-12 14:06:04 浏览: 117
制作简易电子琴可以使用Java语言中的javax.sound.midi包和java.awt包。以下是一个简单的Java代码示例,演示如何使用这些包来制作简易电子琴:
```java
import javax.sound.midi.*;
import java.awt.*;
import java.awt.event.*;
public class SimplePiano extends Frame implements ActionListener, Receiver {
private static final int DEFAULT_CHANNEL = 0;
private static final int DEFAULT_VELOCITY = 64;
private static final int DEFAULT_DURATION = 100;
private MidiDevice device;
private Receiver receiver;
private int channel;
private int velocity;
private int duration;
public SimplePiano() throws MidiUnavailableException {
setTitle("Simple Piano");
setSize(300, 200);
setResizable(false);
setLayout(new GridLayout(0, 5));
add(createButton("C"));
add(createButton("D"));
add(createButton("E"));
add(createButton("F"));
add(createButton("G"));
add(createButton("A"));
add(createButton("B"));
add(createButton("C#"));
add(createButton("D#"));
add(createButton("F#"));
add(createButton("G#"));
add(createButton("A#"));
device = MidiSystem.getDefaultMidiDevice();
device.open();
receiver = device.getReceiver();
channel = DEFAULT_CHANNEL;
velocity = DEFAULT_VELOCITY;
duration = DEFAULT_DURATION;
}
private Button createButton(String label) {
Button button = new Button(label);
button.addActionListener(this);
return button;
}
public void actionPerformed(ActionEvent e) {
String noteName = e.getActionCommand();
int noteNumber = getNoteNumber(noteName);
if (noteNumber != -1) {
sendNoteOn(noteNumber);
try {
Thread.sleep(duration);
} catch (InterruptedException ex) {
}
sendNoteOff(noteNumber);
}
}
public void send(MidiMessage message, long timeStamp) {
ShortMessage shortMessage = (ShortMessage) message;
int command = shortMessage.getCommand();
int data1 = shortMessage.getData1();
int data2 = shortMessage.getData2();
if (command == ShortMessage.NOTE_ON) {
System.out.println("Note on: " + data1);
} else if (command == ShortMessage.NOTE_OFF) {
System.out.println("Note off: " + data1);
}
receiver.send(message, timeStamp);
}
public void close() {
device.close();
}
private int getNoteNumber(String noteName) {
String[] noteNames = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
int[] noteNumbers = { 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71 };
for (int i = 0; i < noteNames.length; i++) {
if (noteName.equals(noteNames[i])) {
return noteNumbers[i];
}
}
return -1;
}
private void sendNoteOn(int noteNumber) {
ShortMessage message = new ShortMessage();
try {
message.setMessage(ShortMessage.NOTE_ON, channel, noteNumber, velocity);
} catch (InvalidMidiDataException e) {
}
receiver.send(message, -1);
}
private void sendNoteOff(int noteNumber) {
ShortMessage message = new ShortMessage();
try {
message.setMessage(ShortMessage.NOTE_OFF, channel, noteNumber, velocity);
} catch (InvalidMidiDataException e) {
}
receiver.send(message, -1);
}
public static void main(String[] args) {
try {
SimplePiano piano = new SimplePiano();
piano.setVisible(true);
piano.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
piano.close();
System.exit(0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
这个程序创建了一个简单的GUI窗口,其中包含12个按钮,每个按钮代表一个音符。当用户按下一个按钮时,程序会向MIDI设备发送一个NOTE_ON消息,然后等待一段时间(即音符的持续时间),然后发送一个NOTE_OFF消息。这样就可以实现一个简单的电子琴。
阅读全文