File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java Sound distorted/choppy/repeating  on playback Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Sound distorted/choppy/repeating  on playback" Watch "Java Sound distorted/choppy/repeating  on playback" New topic

Java Sound distorted/choppy/repeating on playback

john merritt

Joined: Aug 24, 2009
Posts: 14
As I am a little dyslexic I am attempting to write for myself a spelling tutor application. I can record and playback the words I need for a spelling list. The first two or three words record and playback ok, but after that the playback becomes very distorted and choppy. Can somebody please point me in the correct direction to resolve this issue? Thanks.

John, the recycled hippy, many times over.
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 208

Where is stopCapture set to true?

If stopCapture is never set to true, your while (!stopCapture) loop will never exit. Each time you captureAudio() you'll create a new thread. I'd expect that there's actually data coming in on targetDataLine even when you're not recording, it'll just be silence being recorded.

The first time you record, you have one recorder running.
The second time you hit record, you have two recorders running.
The third time you have three and so on.

As to the reason it's choppy, when you have only one recorder running that recorder gets all of the information. (sample rate 48000)
When you have two running they "fight" for the info. That is, each one quickly loops and grabs all the information (probably only a few bytes) off the audio line. So your sample rate is probably halved but you don't notice it much since it's still pretty good. (sample rate 24000)
As it continues though, there are more recorders fighting for the info, the sample rate is essentially split more ways. It's impossible to predict which recorder will get which bytes as it's completely dependent on what order your processor schedules the threads to run in. (16000, 12000, 9600, 8000, 6850... it's easy to see how quickly the sound will become choppy as the number of recorders increases.)

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
john merritt

Joined: Aug 24, 2009
Posts: 14
Thank you so much for the help. You were spot on.

The problem was dummy error and not engaging brain when reading the code.

I called the stop audio capture boolean
in the stop buttons event
listener and created it again in error after coming back to coding the next day and
called it
when using it in the while loop.
I agree. Here's the link:
subject: Java Sound distorted/choppy/repeating on playback
It's not a secret anymore!