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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:08:31+00:00 2026-06-12T17:08:31+00:00

I’m trying to create an app that generates a tone depending on where you

  • 0

I’m trying to create an app that generates a tone depending on where you touch on the screen (currently the frequency is determined by the y value of the touch point) however I’m not able to generate a tone of less than a second without crashing my app! I took the code from the answer here and tweaked it to play when i touch the screen, but when I change the duration variable to a value of less than 1 the app crashes! Is it possible to generate a tone of less than 1 second?

The only reason I want to create a tone of less than a second is that the audio player does not seem to cut off straight away when i pause or stop the audiotrack. these are my methods for playing and generating the tone:

RelativeLayout background;
SoundPool soundPool;
TextView textView;


private final float duration = 1f; // seconds
private final int sampleRate = 8000;//was 8000
private final int numSamples = (int) duration * sampleRate;
private final double sample[] = new double[numSamples];
private final double freqOfTone = 250; // hz

AudioTrack audioTrack ;

int dispWidth = 0, dispHeight = 0;

Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audio);
    background = (RelativeLayout) findViewById(R.id.background);
    textView = (TextView) findViewById(R.id.textView2);
    soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    dispWidth = AppHelper.getDisplayWidth(getApplicationContext());
    dispHeight = AppHelper.getDisplayHeight(getApplicationContext());
    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sampleRate, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, numSamples,
            AudioTrack.MODE_STREAM);
    final byte genTone[]= genTone(freqOfTone);

    background.setClickable(true);



         background.setOnTouchListener(new OnTouchListener() {

          @Override
         public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_UP)
            {
                Log.d(LOG_TAG, "Touch up! Stopping tone...");
                audioTrack.pause();
                audioTrack.flush();

            }
            else
            {
                //these apparently cause immediate stop
                audioTrack.pause();
                audioTrack.flush();

                int R = (int) (event.getY()%255);
                int G =  (int)(event.getX()%255);
                int B = (int) ((event.getY()+event.getX())%255);
                Log.i(LOG_TAG, "Changine colour to "+R+","+G+","+B);
                background.setBackgroundColor(Color.rgb(R, G, B));
                int colorFromPressure = (int) (event.getPressure()*255);
                textView.setTextColor(Color.rgb(colorFromPressure,colorFromPressure,colorFromPressure));
                playSoundAtFrequency(250+event.getY(), event.getX()/dispWidth);
            }
            return false;
    }
     });
}

byte[] genTone(double toneFrequency){
    final byte generatedSnd[] = new byte[2 * numSamples];
    // fill out the array
    for (int i = 0; i < numSamples; ++i) {
        sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/toneFrequency));
    }

    // convert to 16 bit pcm sound array
    // assumes the sample buffer is normalised.
    int idx = 0;
    for (double dVal : sample) {
        short val = (short) (dVal * 32767);
        generatedSnd[idx++] = (byte) (val & 0x00ff);
        generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
    }
    return generatedSnd;
}


void playSound(final byte[] generatedSnd, float volume){        

    //these apparently cause immediate stop
    audioTrack.pause();
    audioTrack.flush();

    audioTrack.write(generatedSnd, 0, numSamples);
    audioTrack.setStereoVolume(volume, volume);


    audioTrack.play();
    Log.d(LOG_TAG, "Playing tone...");
}

public void playSoundAtFrequency(final double toneFrequency,float volume)
{

    final byte genTone[]= genTone(toneFrequency);
    playSound(genTone, volume);

}
  • 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-12T17:08:32+00:00Added an answer on June 12, 2026 at 5:08 pm

    I think your crash is coming from using a buffer size that is too small for the AudioTrack you are trying to create.

    int audioBufferSize = AudioTract.getMinBufferSize(sampleRate,
                                                      AudioFormat.CHANNEL_OUT_MONO,
                                                      AudioFormat.ENCODING_PCM-16BIT);
    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sampleRate, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, audioBufferSize,
            AudioTrack.MODE_STREAM);
    

    Then numSamples still applies to your generating buffer and the AudioTrack will have the buffer size it wants.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:

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.