本文整理汇总了Java中javax.sound.midi.MidiSystem类的典型用法代码示例。如果您正苦于以下问题:Java MidiSystem类的具体用法?Java MidiSystem怎么用?Java MidiSystem使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MidiSystem类属于javax.sound.midi包,在下文中一共展示了MidiSystem类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMidiInput
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
/**
* Capture midi input events, dispatching them to given Receiver.
* The MidiDevice returned is the device providing the input, and
* should be closed when input events are no longer needed.
* Note that this method returns the first MidiDevice which
* has at least one transmitter.
*
* @param receiver the Receiver to which midi input events should be dispatched
* @return the MidiDevice providing the input events
* @throws MidiUnavailableException if midi input can't be found
*/
public static MidiDevice getMidiInput(Receiver receiver) throws MidiUnavailableException {
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (MidiDevice.Info info : infos) {
MidiDevice device;
device = MidiSystem.getMidiDevice(info);
if (DEBUG) {
System.out.println("Found: " + device);
}
int maxTransmitters = device.getMaxTransmitters();
if (DEBUG) {
System.out.println(" Max transmitters: " + maxTransmitters);
}
if (maxTransmitters == -1 || maxTransmitters > 0) {
Transmitter transmitter = device.getTransmitter();
transmitter.setReceiver(receiver);
device.open();
return device;
}
}
throw new MidiUnavailableException("Could not find any midi input sources");
}
示例2: playWarningSound
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
private static void playWarningSound() {
// if (2 > 1) {
// return;
// }
try {
// int velocity = 127; // max volume
int velocity = 90; // max volume
int sound = 65;
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
MidiChannel channel = synthesizer.getChannels()[9]; // drums channel.
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
channel.noteOn(sound + i, velocity);
Thread.sleep(100);
channel.noteOff(sound + i);
}
} catch (MidiUnavailableException | InterruptedException e1) {
e1.printStackTrace();
}
}
示例3: play
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
public void play() {
if (isPlaying) { // 如果已经在播放,返回
return;
}
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY );
sequencer.addMetaEventListener(this);
} catch (InvalidMidiDataException ex) {
} catch (MidiUnavailableException e) {
}
thread = new Thread(this);
thread.start();
}
示例4: open
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
private void open () throws MidiUnavailableException{
synth = MidiSystem.getSynthesizer();
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
MidiDevice.Info msInfo = null;
StringBuilder sb = new StringBuilder();
sb.append("Available MidiDevice are\n");
for ( MidiDevice.Info i:infos ){
if ( i.toString().contains("Microsoft GS Wavetable Synth") ){
msInfo = i;
sb.append(" *****");
}
sb.append("\t" + i.toString() + ": " + i.getDescription() + '\n');
}
// MidiDevice msDevice = MidiSystem.getMidiDevice(msInfo);
synth.open();
sb.append("synth=" + synth.getDeviceInfo().toString() + " with default soundbank " + synth.getDefaultSoundbank().getDescription() + '\n');
sb.append("max synthesizer latency =" + synth.getLatency() + " us\n");
log.info(sb.toString());
channels = synth.getChannels();
channel = channels[PERCUSSION_CHANNEL];
}
示例5: main
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
boolean allOk = true;
MidiDevice.Info[] infos;
out("\nTesting MidiDevices retrieved via MidiSystem");
infos = MidiSystem.getMidiDeviceInfo();
allOk &= testDevices(infos, null);
out("\nTesting MidiDevices retrieved from MidiDeviceProviders");
List providers = JDK13Services.getProviders(MidiDeviceProvider.class);
for (int i = 0; i < providers.size(); i++) {
MidiDeviceProvider provider = (MidiDeviceProvider)providers.get(i);
infos = provider.getDeviceInfo();
allOk &= testDevices(infos, provider.getClass().getName());
}
if (!allOk) {
throw new Exception("Test failed");
} else {
out("Test passed");
}
}
示例6: main
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
boolean foundDuplicates = false;
int[] aTypes = MidiSystem.getMidiFileTypes();
for (int i = 0; i < aTypes.length; i++)
{
for (int j = 0; j < aTypes.length; j++)
{
if (aTypes[i] == aTypes[j] && i != j) {
foundDuplicates = true;
}
}
}
if (foundDuplicates) {
throw new Exception("Test failed");
} else {
System.out.println("Test passed");
}
}
示例7: main
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
public static void main(final String[] args) {
final MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (final MidiDeviceProvider mdp : load(MidiDeviceProvider.class)) {
for (final MidiDevice.Info info : infos) {
if (mdp.isDeviceSupported(info)) {
if (mdp.getDevice(info) == null) {
throw new RuntimeException("MidiDevice is null");
}
} else {
try {
mdp.getDevice(info);
throw new RuntimeException(
"IllegalArgumentException expected");
} catch (final IllegalArgumentException ignored) {
// expected
}
}
}
}
}
示例8: main
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
public static void main(String argv[]) {
Sequencer seq = null;
try {
seq = MidiSystem.getSequencer();
seq.open();
} catch (final MidiUnavailableException ignored) {
// the test is not applicable
return;
}
try {
seq.startRecording();
System.out.println("Test passed.");
} catch (NullPointerException npe) {
System.out.println("Caught NPE: "+npe);
npe.printStackTrace();
throw new RuntimeException("Test FAILED!");
} catch (Exception e) {
System.out.println("Unexpected Exception: "+e);
e.printStackTrace();
System.out.println("Test NOT failed.");
} finally {
seq.close();
}
}
示例9: isMidiInstalled
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
/**
* Returns true if at least one MIDI (port) device is correctly installed on
* the system.
*/
public static boolean isMidiInstalled() {
boolean result = false;
MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < devices.length; i++) {
try {
MidiDevice device = MidiSystem.getMidiDevice(devices[i]);
result = ! (device instanceof Sequencer) && ! (device instanceof Synthesizer);
} catch (Exception e1) {
System.err.println(e1);
}
if (result)
break;
}
return result;
}
示例10: testReceiverSend
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
/**
* Execute Receiver.send() and expect that there is no exception.
*/
private static boolean testReceiverSend() {
boolean result = true;
Receiver receiver;
ShortMessage shMsg = new ShortMessage();
try {
receiver = MidiSystem.getReceiver();
shMsg.setMessage(ShortMessage.NOTE_ON, 0,60, 93);
try {
receiver.send( shMsg, -1 );
} catch(IllegalStateException ilEx) {
ilEx.printStackTrace(System.out);
out("IllegalStateException was thrown incorrectly!");
result = false;
}
receiver.close();
} catch(MidiUnavailableException e) {
out("Midi unavailable, cannot test.");
} catch(InvalidMidiDataException ine) {
out("InvalidMidiDataException, cannot test.");
}
return result;
}
示例11: isMidiInstalled
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
/**
* Returns true if at least one MIDI (port) device is correctly installed on
* the system.
*/
private static boolean isMidiInstalled() {
boolean result = false;
MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < devices.length; i++) {
try {
MidiDevice device = MidiSystem.getMidiDevice(devices[i]);
result = !(device instanceof Sequencer)
&& !(device instanceof Synthesizer);
} catch (Exception e1) {
System.err.println(e1);
}
if (result)
break;
}
return result;
}
示例12: doAllTests
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
private static void doAllTests() {
boolean problemOccured = false;
boolean succeeded = true;
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < infos.length; i++) {
MidiDevice device = null;
try {
device = MidiSystem.getMidiDevice(infos[i]);
succeeded &= doTest(device);
} catch (MidiUnavailableException e) {
out("exception occured; cannot test");
problemOccured = true;
}
}
if (infos.length == 0) {
out("Soundcard does not exist or sound drivers not installed!");
out("This test requires sound drivers for execution.");
}
isTestExecuted = !problemOccured;
isTestPassed = succeeded;
}
示例13: doAll
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
private static void doAll() throws Exception {
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (int i=0; i < infos.length; i++) {
MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
if ((! (device instanceof Sequencer)) &&
(! (device instanceof Synthesizer)) &&
(device.getMaxReceivers() > 0 || device.getMaxReceivers() == -1)) {
System.out.println("--------------");
System.out.println("Testing MIDI device: " + infos[i]);
testDevice(device);
}
if (infos.length==0) {
System.out.println("No MIDI devices available!");
}
}
}
示例14: isMidiInstalled
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
/**
* Returns true if at least one MIDI (port) device is correctly installed on
* the system.
*/
public static boolean isMidiInstalled() {
boolean result = false;
MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < devices.length; i++) {
try {
MidiDevice device = MidiSystem.getMidiDevice(devices[i]);
result = ! (device instanceof Sequencer) && ! (device instanceof Synthesizer);
} catch (Exception e1) {
System.err.println(e1);
}
if (result)
break;
}
if (!result) {
System.err.println("Soundcard does not exist or sound drivers not installed!");
System.err.println("This test requires sound drivers for execution.");
}
return result;
}
示例15: main
import javax.sound.midi.MidiSystem; //导入依赖的package包/类
public static void main(String args[]) throws Exception {
boolean failed = false;
try {
String filename = "GetSoundBankIOException.java";
System.out.println("Opening "+filename+" as soundbank...");
File midiFile = new File(System.getProperty("test.src", "."), filename);
MidiSystem.getSoundbank(midiFile);
//Soundbank sBank = MidiSystem.getSoundbank(new NonMarkableIS());
System.err.println("InvalidMidiDataException was not thrown!");
failed = true;
} catch (InvalidMidiDataException invMidiEx) {
System.err.println("InvalidMidiDataException was thrown. OK.");
} catch (IOException ioEx) {
System.err.println("Unexpected IOException was caught!");
System.err.println(ioEx.getMessage());
ioEx.printStackTrace();
failed = true;
}
if (failed) throw new Exception("Test FAILED!");
System.out.println("Test passed.");
}