Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7774727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:27:01+00:00 2026-06-01T17:27:01+00:00

I’m using a Java port of the sound effect generator SFXR, which involves lots

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:27:03+00:00Added an answer on June 1, 2026 at 5:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.