Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Java AudioSystem .wav Scratchy/Bad Quality

Hey, I'm working on a sound system for a game I'm working on, and I'm using java's Audiosystem to load and play the sound files. The sounds are fine near max or loud volume, but if I lower it, to let's say, around -27 decibels, it's scratchy, bad, and distorted. Any clues on what I can do?

Code
public void run()
    {
        try {
            if (isSong)
                stream = AudioSystem.getAudioInputStream(new File(location));
             else
                stream = AudioSystem.getAudioInputStream(soundStream);
            info = new DataLine.Info(Clip.class, stream.getFormat());
            sound = (Clip) AudioSystem.getLine(info);
            sound.open(stream);
            volumeControl = (FloatControl) sound.getControl(FloatControl.Type.MASTER_GAIN);
            volumeControl.setValue(getDecibels(Configuration.volume));
            if (delay > 0) {
                Thread.sleep(delay);
            }
            sound.start();
            playing = true;
            while (sound.isRunning()) {
                Thread.sleep(250);
            }
            if (!isSong) {
                Thread.sleep(10000);
                playing = false;
                sound.close();
                stream.close();
                player.interrupt();
            }
        } catch (Exception e) {
            player.interrupt();
            e.printStackTrace();
        }
    }

public static float getDecibels(int level)
    {
        System.err.println("GetDecibels: Volume:" + level + ", lvl:" + (-35.0 + ((level * 4) / 10)));
        return (float) -35.0 + ((level * 4) / 10);
    }

I'm also using the same thing to play MIDI files, and they play perfectly fine no matter the volume. It's only the .wav files that get distorted and scratchy.
I've tried changing the audio format, but defaultly, playing like this, Java is already playing it at its maximum quality.
Can you pm me a link for your wav? Specifically I want to know the length and how many bits it is. What device are you running your code on that is showing this issue or is it present on multiple devices? What version of the JDK/JRE are you using?

If you can get me that stuff I can take a look at your problem and try to get you sorted out. May take me a day or two to get back to you, but just send me a reminder by PM if I don't acknowledge your answer in a timely manner.
I don't know how to Java.
Code
try {
    File yourFile = new File("a.wav");
    AudioInputStream stream = AudioSystem.getAudioInputStream(yourFile);
    AudioFormat format = stream.getFormat();
    DataLine.Info info = new DataLine.Info(Clip.class, format);
    Clip clip = (Clip) AudioSystem.getLine(info);

    clip.open(stream);

    FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
    gainControl.setValue(-10.0f);
    
    clip.start();
}
catch (Exception e) {
    System.out.println("Error?");
}

This works for me and doesn't sound grainy. I'm almost sure this is what you have too but I can't see all your code. Is it possible it is your headphones causing the grain?
BTC - 15Cn2u7onAuPaMFGmT4qbEhWgzsKSvMEn6
ETH - 0x5198A50b164a14Df7eAB562da7b440197cb9808B
[Image: folks.gif]



Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by KodaKingMay 13, 2018 05:55 AM 3 137 May 13, 2018 05:55 AM
Last Post: int
[Shell] by Nakamoto.Mar 14, 2016 11:23 AM 1 174 Mar 14, 2016 11:23 AM
Last Post: Advertisement
[Java] by President HolyFeb 8, 2014 11:48 AM 3 193 Feb 8, 2014 11:48 AM
Last Post: Knautschtabak