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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:29:34+00:00 2026-06-09T03:29:34+00:00

I am making a class that takes an array of frequencies values (i.e. 440Hz,

  • 0

I am making a class that takes an array of frequencies values (i.e. 440Hz, 880Hz, 1760Hz) and plays how they would sound combined into a single AudioTrack. I am not a sound programmer, so this is difficult for me to write myself, where I believe that it is a relatively easy problem to an experienced sound programmer. Here is some of the code below in the play method:

public void play() {
    // Get array of frequencies with their relative strengths
    double[][] soundData = getData();

    // TODO
    // Perform a calculation to fill an array with the mixed sound - then play it in an infinite loop
    // Need an AudioTrack that will play calculated loop
    // Track sample info
    int numOfSamples = DURATION * SAMPLE_RATE;
    double sample[] = new double[numOfSamples];
    byte sound[] = new byte[2 * numOfSamples];

    // fill out the array
    for (int i = 0; i < numOfSamples; ++i) {
            sample[i] = Math.sin(2 * Math.PI * i / (SAMPLE_RATE / 440));
    }

    int i = 0;
    for (double dVal : sample) {
        // scale to maximum amplitude
        final short val = (short) ((dVal * 32767));
        // in 16 bit wav PCM, first byte is the low order byte
        sound[i++] = (byte) (val & 0x00ff);
        sound[i++] = (byte) ((val & 0xff00) >>> 8);
    }

    // Obtain a minimum buffer size
    int minBuffer = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

    if (minBuffer > 0) {
        // Create an AudioTrack
        AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                AudioFormat.ENCODING_PCM_16BIT, numOfSamples, AudioTrack.MODE_STATIC);

        // Write audio data to track
        track.write(sound, 0, sound.length);

        // Begin playing track
        track.play();
    }

    // Once everything has successfully begun, indicate such.
    isPlaying = true;
}

Right now, this code simply plays a concert A (440Hz). It was to test whether this code works. Now, I need to take a bunch a frequencies, perform some kind of calculation, and write the sample data.

  • 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-09T03:29:35+00:00Added an answer on June 9, 2026 at 3:29 am

    Ok, so the answer did turn out to be a simple summation loop. Here it is, just replace this for loop with the original one:

        // fill out the array
        for (int i = 0; i < numOfSamples; ++i) {
                double valueSum = 0;
    
                for (int j = 0; j < soundData.length; j++) {
                    valueSum += Math.sin(2 * Math.PI * i / (SAMPLE_RATE / soundData[j][0]));
                }
    
                sample[i] = valueSum / soundData.length;
        }
    

    Now, what this does is simply take all possible frequencies, add them together into the variable, valueSum, and then divide that by the length of the frequency array, soundData, which is a simple average. This produces a nice sine wave mixture of an arbitrarily long array of frequencies.

    I haven’t tested performance, but I do have this running in a thread, otherwise it could crash the UI. So, hope this helps – I am marking this as the answer.

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

Sidebar

Related Questions

I was making an RAII class that takes in a System.Windows.Form control, and sets
I'm making a plugin system. I have a class extensionmanager that takes the name
Hey so I'm making a serialization function that takes a base class pointer 'Joint'
I am making a base class that has a virtual method called GetBaseAddresses(). It
I'm making a C++11 class that produces a huge amount of data. That data
I tried making a wrapper class that encapsulates an object, a string (for naming
I am making a class Customer that has the following data members and properties:
Im making a login/logout class that logs users in, sets cookies based on user's
Im making a program for class that manages a Hotel. I have a function
I am making a program for class that manages a Hotel. I am able

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.