2
votes

I'm using a Java port of the sound effect generator SFXR, which involves lots of arcane music code that I don't understand, being something of a novice when it comes to anything to do with audio. What I do know is that the code can reliably generate and play sounds within Java, using a SourceDataLine object.

The data that the SDL object uses is stored in a byte[]. However, simply writing this out to a file doesn't work (presumably because of the lack of a WAV header, or so I thought).

However, I downloaded this WAV read/write class: http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/ which adds in header information when it writes a WAV file. Giving it the byte[] data from SFXR still produces files that can't be played by any music player I have.

I figure I must be missing something. Here's the relevant code when it plays the sound data:

public void play(int millis) throws Exception {
    AudioFormat stereoFormat = getStereoAudioFormat();
    SourceDataLine stereoSdl = AudioSystem.getSourceDataLine(stereoFormat);

    if (!stereoSdl.isOpen()) {
        try {
            stereoSdl.open();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }
    if (!stereoSdl.isRunning()) {
        stereoSdl.start();
    }

    double seconds = millis / 1000.0;

    int bufferSize = (int) (4 * 41000 * seconds);

    byte[] target = new byte[bufferSize];

    writeBytes(target);
    stereoSdl.write(target, 0, target.length);
}

That's from the SFXR port. Here's the save() file from the WavIO class (there's a lot of other code in that class of course, I figured this might be worth posting in case someone wants to see exactly how the buffer data is being handled:

    public boolean save()
{
    try
    {
        DataOutputStream outFile  = new DataOutputStream(new FileOutputStream(myPath));

        // write the wav file per the wav file format
        outFile.writeBytes("RIFF");                 // 00 - RIFF
        outFile.write(intToByteArray((int)myChunkSize), 0, 4);      // 04 - how big is the rest of this file?
        outFile.writeBytes("WAVE");                 // 08 - WAVE
        outFile.writeBytes("fmt ");                 // 12 - fmt 
        outFile.write(intToByteArray((int)mySubChunk1Size), 0, 4);  // 16 - size of this chunk
        outFile.write(shortToByteArray((short)myFormat), 0, 2);     // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
        outFile.write(shortToByteArray((short)myChannels), 0, 2);   // 22 - mono or stereo? 1 or 2?  (or 5 or ???)
        outFile.write(intToByteArray((int)mySampleRate), 0, 4);     // 24 - samples per second (numbers per second)
        outFile.write(intToByteArray((int)myByteRate), 0, 4);       // 28 - bytes per second
        outFile.write(shortToByteArray((short)myBlockAlign), 0, 2); // 32 - # of bytes in one sample, for all channels
        outFile.write(shortToByteArray((short)myBitsPerSample), 0, 2);  // 34 - how many bits in a sample(number)?  usually 16 or 24
        outFile.writeBytes("data");                 // 36 - data
        outFile.write(intToByteArray((int)myDataSize), 0, 4);       // 40 - how big is this data chunk
        outFile.write(myData);                      // 44 - the actual data itself - just a long string of numbers
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
        return false;
    }

    return true;
}

All I know is, I've got a bunch of data, and I want it to end up in a playable audio file of some kind (at this point I'd take ANY format!). What's the best way for me to get this byte buffer into a playable file? Or is this byte[] not what I think it is?

1
Do you have a link to the SFXR port you are talking about?Sam Goldberg

1 Answers

4
votes

I do not get much chance to play with the sound capabilities of Java so I'm using your question as a learning exercise (I hope you don't mind). The article that you referenced about Reading and Writing WAV Files in Java is very old in relation to Java history (1998). Also something about constructing the WAV header by hand didn't sit quite right with me (it seemed a little error prone). As Java is quite a mature language now I would expect library support for this kind of thing.

I was able to construct a WAV file from a byte array by hunting around the internet for sample code snippets. This is the code that I came up with (I expect it is sub-optimal but it seems to work):

// Generate bang noise data
// Sourced from http://www.rgagnon.com/javadetails/java-0632.html
public static byte[] bang() {
    byte[] buf = new byte[8050];
    Random r = new Random();
    boolean silence = true;
    for (int i = 0; i < 8000; i++) {
        while (r.nextInt() % 10 != 0) {
            buf[i] =
                    silence ? 0
                    : (byte) Math.abs(r.nextInt()
                    % (int) (1. + 63. * (1. + Math.cos(((double) i)
                    * Math.PI / 8000.))));
            i++;
        }
        silence = !silence;
    }
    return buf;
}


private static void save(byte[] data, String filename) throws IOException, LineUnavailableException, UnsupportedAudioFileException {
    InputStream byteArray = new ByteArrayInputStream(data);
    AudioInputStream ais = new AudioInputStream(byteArray, getAudioFormat(), (long) data.length);
    AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
}

private static AudioFormat getAudioFormat() {
    return new AudioFormat(
            8000f, // sampleRate
            8, // sampleSizeInBits
            1, // channels
            true, // signed
            false);      // bigEndian  
}

public static void main(String[] args) throws Exception {
    byte[] data  = bang();
    save(data, "test.wav");
}

I hope it helps.