Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Que
    Que is offline
    New Coder
    Join Date
    Apr 2008
    Posts
    11
    Thanks
    2
    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:

    Code:
    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
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    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.
    http://java.sun.com/docs/books/tutor...view-MIDI.html

    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
    Code:
    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):
    http://www.jsresources.org/examples/MidiNote.html
    http://www.jsresources.org/examples/MidiNote.java.html
    http://www.jsresources.org/examples/...mmon.java.html

    Example of making a player piano:
    http://www.onjava.com/pub/a/onjava/e...17/index1.html

    Example of playing drum sounds:
    http://www.onjava.com/pub/a/onjava/e...x1.html?page=2

    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
    •