1

I'm trying to play a sound that is stored as a byte-array using the following method:

byte[] clickSamples = getAudioFileData("sound.wav");
ByteBuffer buffer = ByteBuffer.allocate(bufferSize*2);
int tick = 0;

for (int i = 0; i < clickSamples.length; i++) {
    buffer.putShort((short) clickSamples[i]);
    tick++;
    if (tick >= bufferSize/SAMPLE_SIZE) {
        line.write(buffer.array(), 0, buffer.position());
        buffer.clear();
        tick = 0;
    }
}

It's kind of hard to explain what's going wrong. I'm getting a sound but it's only like a "swoosh"-kind of noise.

I want to use this method with the byte-buffer and so on because my whole application is built around it. Using Clip or AudioInputStream is not really an option.

So I guess my question is:

How can I play sound directly from my byte-array using a byte-buffer?

Thank you for your help!

Macks
  • 1,696
  • 5
  • 23
  • 38
  • 1
    By any chance: what happens if you set your ByteBuffer order to little endian? – fge Jan 06 '13 at 23:04
  • I've also noticed that the sound I get kinda sounds like my sound file played backwards, but distorted and with a lot of white noise. By turning buffer order to little endian everything gets louder. – Macks Jan 06 '13 at 23:13
  • The method I use for getting the audio data is this one: http://snippi.com/s/ndi3f4x – Macks Jan 06 '13 at 23:16
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) *"I want to use this method with the byte-buffer and so on because my whole application is built around it."* Sometimes it is necessary to take drastic action with core functionality that does not work. – Andrew Thompson Jan 07 '13 at 00:01
  • Hey Andrew. Thank you for commenting. 1) Sure, I did my best. If you point out which information is missing, I'll be glad to add it. 2) I'm ok with that. But using the method I am trying to use (^^) seams ideal for my application, so I'd love to make it work. – Macks Jan 07 '13 at 00:09
  • Update: I've corrected the audio format provided for my SourceDataLine to provide the format of my audio file. Now everything is louder and gets quieter if I change my ByteBuffer order to little endian – Macks Jan 07 '13 at 00:14

1 Answers1

1

I've managed to make it work. Here is my new code:

    int tick = 0;
    for (int i = 0; i < clickSamples.length; i++) {
        tick++;
        if (tick >= bufferSize/SAMPLE_SIZE) {
            line.write(clickSamples, i-tick+1, tick);
            buffer.clear();
            tick = 0;
        }
    }

This may seem like a complicated way of playing sound just like with AudioInputStream but now I can do calculations on each tick++ and by doing that I can intervene to exact times if I need to.

If this sounds silly and there is an easier way of doing this, please let me know.

Also, the reason it sounded so distorted is that it seems like ByteBuffer drastically changed my sample-values. For what reason I don't know. If anybody knows, please let me know!

Thank you. :)

Macks
  • 1,696
  • 5
  • 23
  • 38
  • Hey, I'm a total newb at sound. Do you have any idea on how to fix my problem here: http://stackoverflow.com/questions/20184650/why-am-i-getting-this-lineunavailableexception ? – trusktr Nov 25 '13 at 04:54