Results 1 to 3 of 3

Thread: Java Sound

  1. #1
    cyberia is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default Java Sound

    hey
    so I'm trying to make a program that can play sounds with different notes and instruments and stuff using javax.sound.midi. long story.

    anyway, have set this up in eclipse but no matter what I change the instrument loaded into the synth to, it only plays the same piano.

    changing channels changes the instrument but there's only 16 in that and the instruments array says its length is 411... where have the others gone/how do i access them?

    help would be much appreciated. not total java noob but have never used sound and there is little to go on online.

    ta

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    import javax.sound.midi.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     * @author Terry
     *	
     */
    public class Note {
    
    	MidiChannel[] mc = null;
    
    	/**
    	 * @return 
    	 */
    	public Note(){
    
    		try {
    			
    			Synthesizer synth = MidiSystem.getSynthesizer();
    			synth.open(); 
    			
    			//synth.loadAllInstruments(MidiSystem.getSoundbank(new File("C:\\Program Files\\Java\\jre6\\lib\\audio\\" +
    			//"soundbank.gm")));
    			
    			//System.out.println(syn.isSoundbankSupported(MidiSystem.getSoundbank(new File("C:\\Program Files\\Java\\jre7\\lib\\audio\\" +
    			//"soundbank.gm"))));
    			
    			mc = synth.getChannels();
    			
    			//changing the soundbank gives different instruments?/my one is shit
    			Instrument[] instr = synth.getDefaultSoundbank().getInstruments();
    			
    			//System.out.println("\n\nSOUNDBANK\n"+syn.getDefaultSoundbank().getDescription());
    			//System.out.println("\n\nINSTRUMENTS\n"+instr.length);
    			//for(int i=0;i<instr.length;i++)
    				//System.out.println(i+"  "+instr[i].getName());
    			
    			//supposed to change instrument, but doesnt
    			synth.loadInstrument(instr[85]);
    			System.out.println(instr[85].getName());
    			
    			JFrame frame = new JFrame("Sound1");                
    			JPanel pane = new JPanel();                         
    			JButton button1 = new JButton("Click me!");            
    			frame.getContentPane().add(pane);                   
    			pane.add(button1);                                     
    			frame.pack();                                       
    			frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    			frame.show();
    
    			button1.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					
    					//changing index changes the instrument version
    					
    					//changing first is note pitch, second is how hard it is hit 
    					mc[5].noteOn(60,600);
    					
    					//change the wait to alter note duration
    					try {Thread.sleep(1000);} catch (InterruptedException e1) {e1.printStackTrace();}
    					
    					//pull off to stop note
    					mc[5].noteOff(60,600);
    				}
    			}); 
    
    		} catch (Exception e) {e.printStackTrace();}
    
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		new Note();   
    	}
    
    }

  2. #2
    cyberia is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    fixed

    Java Code:
    mc[5].programChange(instr[200].getPatch().getProgram());

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default

    //changing the soundbank gives different instruments?/my one is shit
    May be useful or at least relevant:
    https://forums.oracle.com/forums/thr...89625&#5389625

    db

Similar Threads

  1. Sound In java
    By chetoos in forum New To Java
    Replies: 2
    Last Post: 06-28-2011, 05:59 PM
  2. Problems with sound in java and USB sound card
    By marblecatdog in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 12:02 PM
  3. java sound
    By nikkka in forum New To Java
    Replies: 11
    Last Post: 04-10-2011, 02:54 PM
  4. Calibration of Sound in Java
    By satishrah@gmail.com in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 01:44 PM
  5. Sound in applet Java
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 05:52 AM

Posting Permissions

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