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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:32:54+00:00 2026-06-15T16:32:54+00:00

Should I not make it in a for loop that cycle’s the sample length?

  • 0

Should I not make it in a for loop that cycle’s the sample length? I don’t understand why it plays the sine wave formula but not the saw wave… This is modified from a beep class that is stuctured quite similarly, perhaps it is the for loop in the middle that generate too mamy rounds to get the sawtooth shape. What do you think? Here is my code:

class MakeSaw
{

  static void makeSaw(double f, int t) throws Exception
  {

    int nChannel = 1;         // number of channel : 1 or 2

    // samples per second
    float sampleRate = 16000;  // valid:8000,11025,16000,22050,44100
    int nBit = 16;             // 8 bit or 16 bit sample

    int bytesPerSample = nChannel*nBit/8;


    int bufferSize = (int) (nChannel*sampleRate*t*bytesPerSample);
    byte[] audioData = new byte[bufferSize];

    // "type cast" to ShortBuffer
    java.nio.ByteBuffer byteBuffer = java.nio.ByteBuffer.wrap(audioData);
    java.nio.ShortBuffer shortBuffer = byteBuffer.asShortBuffer();


    int sampleLength = audioData.length/bytesPerSample;

    // generate the wave
    double volume = 8192;   // 0-32767
    double PI = Math.PI;
    double sawWave=0;


        //make saw wave
        for(int i = 0; i < sampleLength; i++)
        {
            double time = i/sampleRate;
            sawWave = sawWave + Math.sin(2*PI*f*(i*t)/i);    //passing amplitude frequency and time
            short amplitude = (short) (volume*sawWave);

            for (int c=0;c<1;c++) //repeat once because audio playing on one channel
            {
                shortBuffer.put(amplitude);
            }
        }

    //end wave making//end generating sound wave sample


    boolean isSigned=true;
    boolean isBigEndian=true;

    // Define audio format
    javax.sound.sampled.AudioFormat audioFormat =
      new javax.sound.sampled.AudioFormat(sampleRate, nBit, nChannel, isSigned,isBigEndian);


    javax.sound.sampled.DataLine.Info dataLineInfo =
      new javax.sound.sampled.DataLine.Info(
         javax.sound.sampled.SourceDataLine.class, audioFormat);

    // get the SourceDataLine object
    javax.sound.sampled.SourceDataLine sourceDataLine = 
      (javax.sound.sampled.SourceDataLine)
      javax.sound.sampled.AudioSystem.getLine(dataLineInfo);

    sourceDataLine.open(audioFormat);
    sourceDataLine.start();

    // actually play the sound
    sourceDataLine.write(audioData,0,audioData.length);

    // "flush",  wait until the sound is completed
    sourceDataLine.drain();

  }


  public static void main(String[] args) throws Exception
  {

    makeSaw(400,2);
  }
}
  • 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-15T16:32:55+00:00Added an answer on June 15, 2026 at 4:32 pm

    Your calculation of sawWave is wrong. All the amplitudes come out as zero.

    I haven’t tried to debug this for you, but there are three things that stand out in

            double time = i/sampleRate;
            sawWave = sawWave + Math.sin(2*PI*f*(i*t)/i);    //passing amplitude frequency and time
    
    1. You’re not using time in sawWave calculation.
    2. That division by i looks suspicious.
    3. Why would you be using sin() when generating a sawtooth wave?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sandboxed Applet, but that should not make a difference. The Applet
While it does not make much sense in the general case as it should
How can I make such layout? Header should not scrolls and should be always
I'm a bit confused whether or not I should make the links on my
Does this make sense? MyValue can be true or false Should it not be
What technique should we use to make the httpsession object not loaded heavily with
I don't want anyone saying you should not reinvent the wheel, use an open
I was thinking that the SQLDataReader should not work if there is no connection
I'm running a loop in javascript that should take about a minute to complete.
I've written some code that should make a new image. My background image has

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.