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 8340725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:15:32+00:00 2026-06-09T05:15:32+00:00

Recently, I’ve been experimenting with mixing AudioInputStreams together. After reading this post , or

  • 0

Recently, I’ve been experimenting with mixing AudioInputStreams together. After reading this post, or more importantly Jason Olson’s answer, I came up with this code:

private static AudioInputStream mixAudio(ArrayList audio) throws IOException{
    ArrayList<byte[]> byteArrays = new ArrayList();
    long size = 0;
    int pos = 0;
    for(int i = 0; i < audio.size(); i++){
        AudioInputStream temp = (AudioInputStream) audio.get(i);
        byteArrays.add(convertStream(temp));
        if(size < temp.getFrameLength()){
            size = temp.getFrameLength();
            pos = i;
        }
    }

    byte[] compiledStream = new byte[byteArrays.get(pos).length];
    for(int i = 0; i < compiledStream.length; i++){
        int byteSum = 0;
        for(int j = 0; j < byteArrays.size(); j++){
            try{
                byteSum += byteArrays.get(j)[i];
            }catch(Exception e){
                byteArrays.remove(j);
            }
        }
        compiledStream[i] = (byte) (byteSum / byteArrays.size());
    }

    return new AudioInputStream(new ByteArrayInputStream(compiledStream), ((AudioInputStream)audio.get(pos)).getFormat(), ((AudioInputStream)audio.get(pos)).getFrameLength());
}

private static byte[] convertStream(AudioInputStream stream) throws IOException{
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int numRead;

    while((numRead = stream.read(buffer)) != -1){
        byteStream.write(buffer, 0, numRead);
    }

    return byteStream.toByteArray();
}

This code works very well for mixing audio files. However, it seems the more audio files being mixed, the more white noise that appears in the returned AudioInputStream. All of the files being combined are identical when it comes to formatting. If anyone has any suggestions\advice, thanks in advance.

  • 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-09T05:15:33+00:00Added an answer on June 9, 2026 at 5:15 am

    I could be wrong, but I think your problem has to do with the fact that you are messing with the bytes instead of what the bytes mean. For instance, if you are working with a 16 bit sampling rate, 2 bytes form the number that corresponds to the amplitude rather than just 1 byte. So, you end up getting something close but not quite right.

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

Sidebar

Related Questions

Recently I've been experimenting with the use of the Func<T> class, and so far
Recently I've been trying to upgrade this application that I'm working on from JSF
recently, while working on a db2 -> oracle migration project, we came across this
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
Recently, we discovered odd behavior in some old code. This code has worked for
Recently I've been doing quite the project mostly working with the DateTime class. Now,..
Recently I've read this performance guide Let's make the web faster and was puzzled
recently, while reading former's code in my current project, I encounter the problems below:
Recently I've been dealing with texts with mixed languages, including Chinese, English, and even
Recently I've been thinking about how to transform a complex polygon into a non-complex

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.