Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Java Midi Synthesizer

Thread view: 
bryan.crompton@gmail.com - 31 May 2007 22:33 GMT
Hello,

I've been having a little trouble working with the Java midi
synthesizer included with the API.

I found a peice of code on a forum something like what follows:

import java.util.*;
import javax.sound.midi.*;

class SoundTest
{
  public static void main(String[] args) {
      try {
          Synthesizer synth = MidiSystem.getSynthesizer();
          synth.open();

          final MidiChannel[] mc    = synth.getChannels();
          Instrument[]        instr =
synth.getAvailableInstruments();
          synth.loadInstrument(instr[55]);
          for (int i = 20; i < 80; ++i) {
              try {
                  Thread.sleep(200);
              } catch (InterruptedException e) {}
              mc[4].noteOn(i,100);
          }
      } catch (MidiUnavailableException e) {}
  }
}

Though I'm not very familier with the sound API (I've just come
accross it today), I'm not confused with the code. The problem is, I
would expect, when modifying the line
"synth.loadInstrument(instr[55]);" to get a different instrument, but
I haven't.

Can anyone help me out?

-Bryan
Andrew Thompson - 01 Jun 2007 03:55 GMT
...
>Can anyone help me out?

Try this..
<sscce>
import java.util.*;
import javax.sound.midi.*;

class SoundTest
{
 public static void main(String[] args) {
   try {
     Synthesizer synth = MidiSystem.getSynthesizer();
     synth.open();

     final MidiChannel[] mc= synth.getChannels();
     Instrument[]instr =
       synth.getAvailableInstruments();
     for (int ii= 0; ii<20; ii++) {
       System.out.println( instr[ii].getName() );
       for (int i = 50; i < 60; ++i) {
         // instrument loading is irrelevant, it is
         // the prgram change that matters..
         mc[4].programChange(ii);
         mc[4].noteOn(i,300);
         try {
           Thread.sleep(200);
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
       }
     }
   } catch (MidiUnavailableException e) {
     e.printStackTrace();
   }
 }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.athompson.info/andrew/

bryan.crompton@gmail.com - 01 Jun 2007 19:47 GMT
Thank you, that helps.

-Bryan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.