Results 1 to 9 of 9
- 05-23-2010, 03:16 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
- 05-23-2010, 04:32 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
I've been fiddling with it a bit myself lately; the following test thingy plays one major scale. Do with it what you want:
kind regards,Java Code:import javax.sound.midi.Instrument; import javax.sound.midi.MidiChannel; import javax.sound.midi.MidiDevice; import javax.sound.midi.MidiSystem; import javax.sound.midi.MidiUnavailableException; import javax.sound.midi.Patch; import javax.sound.midi.Soundbank; import javax.sound.midi.Synthesizer; public class SynthNote { public static void main(String[] args) { int nNoteNumber = 61; // MIDI key number int nVelocity = 127; Synthesizer synth = null; try { MidiDevice.Info[] info= MidiSystem.getMidiDeviceInfo(); for (int i= 0; i < info.length; i++) { System.out.println(i+": "+info[i].getName()+" ("+info[i].getDescription()+")"); } synth = MidiSystem.getSynthesizer(); synth.open(); } catch (MidiUnavailableException e) { e.printStackTrace(); System.exit(1); } int nChannelNumber = 0; MidiChannel[] channels = synth.getChannels(); MidiChannel channel = channels[nChannelNumber]; int nDuration = 500; Soundbank bank = synth.getDefaultSoundbank(); System.out.println("bank: " + bank); System.out.println("name: " + bank.getName()); System.out.println("desc: " + bank.getDescription()); Instrument[] instrument = bank.getInstruments(); for (int i = 0; i < instrument.length; i++) { System.out.println("instr #" + i + ": " + instrument[i].getName()); } synth.loadAllInstruments(bank); Patch patch = instrument[3].getPatch(); int b = patch.getBank(); int p = patch.getProgram(); int fade= 0; channel.programChange(24); int[] inc= { 0, 2, 4, 5, 7, 9, 11, 12 }; for (int i= 0; i < inc.length; i++) { channel.noteOn(nNoteNumber + inc[i], nVelocity); try { Thread.sleep(nDuration); } catch (InterruptedException e) { } /* * Turn the note off. */ channel.noteOff(nNoteNumber + inc[i], fade); } try { Thread.sleep(2000); } catch (InterruptedException e) { } synth.close(); } }
Jos
- 05-24-2010, 08:57 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
This is what i need, thanks. Do you know how to play a sound of applauding people?
Hannes
- 05-24-2010, 09:24 AM #4
You might find the code I posted at response #5 in this thread on another forum useful.
Java Sound - Java soundbank - certain patches won't play or have wrong sound???
db
- 05-24-2010, 10:06 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
I don't think you can't do this with MIDI; to me that entire MIDI engine is like the old mellotron, i.e. if you don't have an "applaus tape" you can't select it nor play it; I must confess that I know next to nothing about MIDI (just fiddling with it) so I could be totally wrong here ...
kind regards,
Jos
- 05-24-2010, 06:30 PM #6
dbJava Code:import javax.sound.midi.*; public class Applause implements Runnable { private Synthesizer synth; private MidiChannel channel; public static void main(String[] args) { new Applause().applaud(); } private void applaud() { if (!setUpMidi()) { System.out.println("Unable to play MIDI"); return; } new Thread(this).start(); } private boolean setUpMidi() { try { synth = MidiSystem.getSynthesizer(); synth.open(); channel = synth.getChannels()[0]; channel.programChange(0, 126); return true; } catch (MidiUnavailableException ex) { ex.printStackTrace(); } return false; } @Override public void run() { try { for (int i = 0; i < 3; i++) { channel.noteOn(40, 64); Thread.sleep(100); channel.noteOn(47, 64); Thread.sleep(100); channel.noteOn(52, 64); Thread.sleep(100); channel.noteOn(56, 64); Thread.sleep(1000); } channel.noteOff(56, 64); Thread.sleep(1000); channel.noteOff(52, 64); Thread.sleep(1000); channel.noteOff(47, 64); Thread.sleep(1000); channel.noteOff(40, 64); } catch (InterruptedException ex) { ex.printStackTrace(); } } }
- 05-26-2010, 05:36 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
I downloaded a applause soundeffect and saved it in my documents. Now, how can i play it?
Hannes
- 05-26-2010, 06:13 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 05-27-2010, 06:45 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
Similar Threads
-
javax.sound.sampled
By carderne in forum New To JavaReplies: 5Last Post: 02-28-2009, 10:12 PM -
Re-playing sound
By JSK in forum CLDC and MIDPReplies: 0Last Post: 02-06-2008, 11:34 AM -
Nokia 5300 freezes when playing sound after receiving a call
By presto in forum CLDC and MIDPReplies: 2Last Post: 10-31-2007, 03:55 PM -
Playing sound in applet??????
By Bagesh in forum Java AppletsReplies: 2Last Post: 07-13-2007, 04:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks