Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2008
    Thanked 0 Times in 0 Posts

    Java and sound (completely lost)

    Hello everyone.

    I'm trying to create some sounds in java and I wanted a really simplistic way of doing so. I tried looking over the APIs on Sun, but they seem really overly complicated. Either that or I don't understand much about how sounds work so I might as well be reading a foreign language.

    Basically I have created a separate program that creates "Notes" for a piano. What i want to do in a separate file is to be able to play the sounds, but alas I am at a complete loss as to what I should do.

    My notes have 3 things, the number of the Note (which basically tells you whether the key is A, A#, B etc), and the length of the note (Whole, Half, Quarter etc), and the frequency of the note in Hertz.

    So my question is:

    1: What class do I import that will create sounds?
    2: How do I go about creating/playing the sounds with this? (Some simple to read examples would be nice, the ones over at the Sun Java site does not provide easy to read/understand material). I'm really looking for a rough example to be able to create sound in Java.
    An example of a simple example:

    import example.java* //this contains the class library so you can make sounds
    //skip some setup code
    sound blah = new sound (2, 34, 3000); //explain what 2, 34, 3000 are
    blah.play(); //plays the sound

  • #2
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Ontario, Canada
    Thanked 20 Times in 20 Posts
    I found this post interesting because it reminded me of a project I did many years ago in a different language. Unfortunately, I have not done anything like this in Java, so I was curious myself.

    After some google searching I didn't find a "simple example" as you requested, but I did find some links that should prove helpful if you take the time to read and understand them.

    First I'll point you back to Suns website because the information is there in the Midi Audio section. Yes this section is long but it's all useful information.

    Here's a few lines of code I found partially buried in the above overview that might prove helpful. http://java.sun.com/docs/books/tutor...-messages.html
    ShortMessage myMsg = new ShortMessage();
      // Start playing the note Middle C (60), 
      // moderately loud (velocity = 93).
      myMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, 93);
      long timeStamp = -1;
      Receiver	 rcvr = MidiSystem.getReceiver();
      rcvr.send(myMsg, timeStamp);
    Here's a couple more links to code that at least includes the information you require. The programs in these links may be a little more complex then you're after, but the information appears to be in there.

    Playing a Note on a Midi Device (3 links):

    Example of making a player piano:

    Example of playing drum sounds:

    In regards to the last two links based on a quick read of the code my guess is that you'll want to look more closely at the "drum" example as it just plays single notes rather than make a track (player piano). You should then just be able to combine this example with the information in "player piano" to setup a piano sound and play various notes and durations.
    Last edited by Gox; 01-13-2009 at 07:51 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