Storing a sound as an object  
Author Message
Robert Dodier





PostPosted: 2008-2-2 4:57:00 Top

java-programmer, Storing a sound as an object Hi,

I'd like to load a sound file, store the sound as an object, and then
play it back at some later time. What are the classes appropriate
for that scheme? I have a lot of experience programming Java, aside
from audio, so really all I need is a pointer to the right approach,
and I think I can figure out the rest.

Thanks for your help. I found a lot of stuff about Java audio
programming,
but I didn't see (or couldn't recognize) a solution to this problem.

Robert Dodier
 
Eric Sosman





PostPosted: 2008-2-2 5:01:00 Top

java-programmer >> Storing a sound as an object Robert Dodier wrote:
> Hi,
>
> I'd like to load a sound file, store the sound as an object, and then
> play it back at some later time. What are the classes appropriate
> for that scheme? I have a lot of experience programming Java, aside
> from audio, so really all I need is a pointer to the right approach,
> and I think I can figure out the rest.
>
> Thanks for your help. I found a lot of stuff about Java audio
> programming,
> but I didn't see (or couldn't recognize) a solution to this problem.

The javax.sound.sampled package looks like a promising start.

--
email***@***.com
 
Mark Space





PostPosted: 2008-2-2 5:10:00 Top

java-programmer >> Storing a sound as an object Robert Dodier wrote:
> Hi,
>
> I'd like to load a sound file, store the sound as an object, and then
> play it back at some later time. What are the classes appropriate
> for that scheme? I have a lot of experience programming Java, aside
> from audio, so really all I need is a pointer to the right approach,
> and I think I can figure out the rest.
>
> Thanks for your help. I found a lot of stuff about Java audio
> programming,
> but I didn't see (or couldn't recognize) a solution to this problem.
>
> Robert Dodier

I did some poking at the Java audio and media packages a long while
back. One obvious answer would be to store the sound a a file with a
know format (.wav, etc.) on disk then use
javax.sound.sampled.spi.AudioFileReader object to open and play back the
sound.

Sun makes a sample app that's pretty good for demonstrating their audio
stuff. I got it to read input from the microphone on my laptop (just me
speaking) and then play it back. Try doing a search for JavaSoundDemo.jar.



 
 
Peter Duniho





PostPosted: 2008-2-2 6:02:00 Top

java-programmer >> Storing a sound as an object On Fri, 01 Feb 2008 12:56:48 -0800, Robert Dodier
<email***@***.com> wrote:

> I'd like to load a sound file, store the sound as an object, and then
> play it back at some later time. What are the classes appropriate
> for that scheme? I have a lot of experience programming Java, aside
> from audio, so really all I need is a pointer to the right approach,
> and I think I can figure out the rest.

I just did this. You don't need the SPI packages...those are for
implementing audio services ("service provider interface"), not needed for
using them. You do need the javax.sound.sampled package.

The basic unit you need is the Clip class. You get one from the
AudioSystem class, and initialize it with an AudioInputStream, also
obtainable from the AudioSystem class using an existing InputStream (e.g..
created from a file, or from a resource in your .jar file).

Here's some sample code from my effort, followed by some notes regarding
"gotchas" I ran into:

// some variable used to store the reference to the
// Clip...put it wherever is appropriate for your needs
Clip clip;

// A stream that will read your data. In my case, I get this using
// Class.getResourceAsStream, but you can just as easily open a file
Stream streamSource;

// Then, where you want to initialize the Clip:
InputStream stream = new BufferedInputStream(streamSource);
AudioInputStream ais = AudioSystem.getAudioInputStream(stream);
clip = AudioSystem.getClip();
clip.open(ais);
ais.close();

clip.addLineListener(new LineListener()
{
public void update(LineEvent event)
{
if (event.getType() == LineEvent.Type.STOP)
{
clip.stop();
clip.setFramePosition(0);
}
}
});

// Finally, wherever you want to play the Clip:
if (clip != null && !clip.isActive())
{
clip.start();
}

Now, for the gotchas:

1) On at least one Java implementation (Mac OS), you must stop the
clip before you can start it again, even if the audio has actually reached
the end
2) On at least one Java implementation (Windows), you must also reset
the clip's position back to the beginning before playing it again (on the
Mac, the clip resets automatically if you stop it when it's reached the
end)
3) You need to use a BufferedInputStream to read the data, at least
when reading it as a resource. Without buffering, something goes wrong
and the clip can't be correctly initialized (an exception is thrown,
though I don't recall off the top of my head what exception it was).

I don't know Java well enough to know for sure whether closing the
AudioInputStream is sufficient for also closing the wrapped
BufferedInputStream and the original Stream (from the resource in my
case). I'm assuming it is, but someone might correct me on that point. :)

As you can see, the basic idea is actually very simple and not hard to
implement at all. Good luck and have fun!

Pete
 
 
Roedy Green





PostPosted: 2008-2-2 9:51:00 Top

java-programmer >> Storing a sound as an object On Fri, 1 Feb 2008 12:56:48 -0800 (PST), Robert Dodier
<email***@***.com> wrote, quoted or indirectly quoted someone
who said :

>Thanks for your help. I found a lot of stuff about Java audio
>programming,
>but I didn't see (or couldn't recognize) a solution to this problem.

You could read the file and store it as GZipped ByteArrayStream
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com