Results 1 to 9 of 9
  1. #1
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default playing sounds with the javax.sound package

    Hey guys,

    I've been reading the javax.sound package tutorial from sun, but i still don't understand. In the tutorial they almost only speak about getting audio from somewhere, but not playing a sound for example.
    Can someone help me with this??

    Hannes

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hannes View Post
    Hey guys,

    I've been reading the javax.sound package tutorial from sun, but i still don't understand. In the tutorial they almost only speak about getting audio from somewhere, but not playing a sound for example.
    Can someone help me with this??
    I've been fiddling with it a bit myself lately; the following test thingy plays one major scale. Do with it what you want:

    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();
    	}
    
    }
    kind regards,

    Jos

  3. #3
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    This is what i need, thanks. Do you know how to play a sound of applauding people?

    Hannes

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hannes View Post
    This is what i need, thanks. Do you know how to play a sound of applauding people?
    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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Java 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();
        }
      }
    }
    db

  7. #7
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    I downloaded a applause soundeffect and saved it in my documents. Now, how can i play it?

    Hannes

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hannes View Post
    I downloaded a applause soundeffect and saved it in my documents. Now, how can i play it?

    Hannes
    Did you try Darryl's approach? It works.

    kind regards,

    Jos

  9. #9
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    I really have to play a sound i've downloaded, i also have to play other sounds so

Similar Threads

  1. javax.sound.sampled
    By carderne in forum New To Java
    Replies: 5
    Last Post: 02-28-2009, 11:12 PM
  2. Re-playing sound
    By JSK in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-06-2008, 12:34 PM
  3. Replies: 2
    Last Post: 10-31-2007, 04:55 PM
  4. Playing sound in applet??????
    By Bagesh in forum Java Applets
    Replies: 2
    Last Post: 07-13-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •