Cross-platform sound reliability issues -- anyone have experience with this?

C

cadar6

Hi, I'm hoping that someone out there can share their experience and
perhaps let me know why I'm having problems trying to play several 1-
second sound clips in a Java 1.5 applet. In particular, I'm having
different kinds of problems on the Mac vs. the PC. By the way, I'm
writing this application for an academic lab that would like to
conduct language learning experiments over the web using a Java
applet.

I'm just going to list the performance problems that I've had (and not
yet solved), and if anyone can give me either a solution to any of
them or just let me know that there *is* no known solution, I would
appreciate it!

I'm doing my development on the PC, and here are the problems I've had
on that platform:

1. If I open a SourceDataLine and leave it open for multiple (that is,
hundreds) of write() calls, with calls to drain() interspersed
periodically (as required by the design of my application), then I'll
eventually get a fraction of a second of a sound repeating every 5
seconds or so, independent of the continued calls to write() (which
succeed and cause new sounds to be played).

2. To deal with this, I tried creating a SourceDataLine for each
individual sound clip I need to play, call write(), then drain(), then
close(). But this introduces another problem: every once in a while,
there is an unexplained second or so of silence between sound clips.
The gap is too long to be explained by inopportune garbage collection.

On the Mac, the situation is even worse.

3. (Mac only.) If I create a new SourceDataLine for each clip, as in
problem #2 above, then I get a new thread which never goes away
(close() seems to have no effect). If I play 20 cilps, I have 20 new
threads. This is obviously unacceptable if I'm going to play hundreds
of clips.

4. (Mac only.) The sound clips come from little MP3s, which I decode
using JLayer 1.0. If I decode them on the fly, I get loud, annoying
clicking sounds at the end of the 4th or 5th clip, and every clip
thereafter. Further testing shows that the computer is amply able to
decode fast enough to keep the sound buffer full. I temporarily solved
this problem by decoding all the clips into memory first (before
playing any of them), and then playing the clips directly from memory,
but this will not be practical for us in the long run. I think this
problem has to do with competition for CPU time among the threads.

Again, any information would be most helpful, thanks!

Samuel
 
A

Andrew Thompson

Hi, I'm hoping that someone out there can share their experience and
perhaps let me know why I'm having problems trying to play several 1-
second sound clips in a Java 1.5 applet.

You can expect no end of problems deploying
applets in browsers. Launch your applet using
web start and take 1 complication from the
problem description. E.G.
<http://www.physci.org/jws/#jtest>

Andrew T.
 
K

Knute Johnson

Hi, I'm hoping that someone out there can share their experience and
perhaps let me know why I'm having problems trying to play several 1-
second sound clips in a Java 1.5 applet. In particular, I'm having
different kinds of problems on the Mac vs. the PC. By the way, I'm
writing this application for an academic lab that would like to
conduct language learning experiments over the web using a Java
applet.

I'm just going to list the performance problems that I've had (and not
yet solved), and if anyone can give me either a solution to any of
them or just let me know that there *is* no known solution, I would
appreciate it!

I'm doing my development on the PC, and here are the problems I've had
on that platform:

1. If I open a SourceDataLine and leave it open for multiple (that is,
hundreds) of write() calls, with calls to drain() interspersed
periodically (as required by the design of my application), then I'll
eventually get a fraction of a second of a sound repeating every 5
seconds or so, independent of the continued calls to write() (which
succeed and cause new sounds to be played).

2. To deal with this, I tried creating a SourceDataLine for each
individual sound clip I need to play, call write(), then drain(), then
close(). But this introduces another problem: every once in a while,
there is an unexplained second or so of silence between sound clips.
The gap is too long to be explained by inopportune garbage collection.

I can maybe help with these two. They are both related. If you open a
SourceDataLine, you need to keep sending it data to keep it's buffer
full or you will have these problems. You might consider using the Clip
interface instead. You don't have to keep feeding it.
 
Joined
Sep 27, 2007
Messages
1
Reaction score
0
Another thing that might cause this problem is if you aren't clearing your byte[] between calls to sourceDataLine.write(), or if you always use the byte[] length as the length parameter.

For example, if you call something like:

SourceDataLine sink = loadSink();
sink.open(audioFormat());
int frame = sink.getBufferSize() ;

byte[] read = new byte[frame];
int amountRead = stream.read(read);
if (amountRead != -1) {
sink.write(read,0,frame);
}

You will have extra information in 'read' on the last call. Instead, try:

SourceDataLine sink = loadSink();
sink.open(audioFormat());
int frame = sink.getBufferSize() ;

byte[] read = new byte[frame];
int amountRead = stream.read(read);
if (amountRead != -1) {
sink.write(read,0,amountRead);
}


Oh, and don't forget to clear your byte[] between writes!!!
Hope this helped!
-greg frohring
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top