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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:39:38+00:00 2026-06-04T15:39:38+00:00

I have a basic wave generator in java but I need something to remove

  • 0

I have a basic wave generator in java but I need something to remove the clicks I get from when the amplitude of a wave changes sharply. Namely when I start/stop playing a wave, especially if I have a beeping tone.

Phrogz’s answer on SO gave a really nice and simple function, but I’m not sure I’m implementing it right.

When I first tried to use it, I couldn’t get it to work, but then I seem to remember it working very well… I have since fiddled about a lot with my code and now it doesn’t seem to be working very well again.

So here’s the closest I could get to an SSCCE:

If you play this you will notice that when the filtering is on (filter = true) the wave is much quieter and the clicks slightly less, but this seems mainly due to the decrease in volume. There is still a noticeable “hit” on each beep, that I don’t want, and I don’t remember being there before…

import javax.sound.sampled.*;


public class Oscillator{

    private static int SAMPLE_RATE = 22050;
    private static short MAX_AMPLITUDE = Short.MAX_VALUE;   
    private static AudioFormat af = null;
    private static SourceDataLine line = null;
    private int frequency = 440; //Hz
    private int numLoops = 1000;
    private int beep = 100;

    // set to true to apply low-pass filter
    private boolean  filter = true;
    // set the amount of "smoothing" here
    private int smoothing = 100;
    private double oldValue;

    public Oscillator(){

        prepareLine();

    }


    public static void main(String[] args) {
        System.out.println("Playing oscillator");
        Oscillator osc = new Oscillator();
        osc.play();
    }


    private void prepareLine(){


        af =  new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, SAMPLE_RATE, 16, 2, 4, SAMPLE_RATE, false);

        try {

            DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);

            if (!AudioSystem.isLineSupported(info)) {
                System.out.println("Line does not support: " + af);
                System.exit(0);
            }
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(af);
        }
        catch (Exception e) { 
            System.out.println(e.getMessage());
            System.exit(0);
        }
    }

    private void play() {

        System.out.println("play");

         int maxSize = (int) Math.round( (SAMPLE_RATE * af.getFrameSize())/ frequency );  
         byte[] samples = new byte[maxSize];

         line.start();

         double volume = 1;

         int count = 0;
         for (int i = 0; i < numLoops; i ++){


             if (count == beep) {
                 if(volume==1) volume = 0;
                 else volume = 1;
                 count = 0;
             }

             count ++;

             playWave(frequency, volume, samples);


         }

         line.drain();
         line.stop();
         line.close();
         System.exit(0);
    }  

    private void playWave(int frequency, double volLevel, byte[] samples) {

         double amplitude = volLevel * MAX_AMPLITUDE;

         int numSamplesInWave = (int) Math.round( ((double) SAMPLE_RATE)/frequency );

         int index = 0;

         for (int i = 0; i < numSamplesInWave; i++) {

             double theta = (double)i/numSamplesInWave;

             double wave = getWave(theta);

             int sample = (int) (wave * amplitude);


             if (filter) sample = applyLowPassFilter(sample);



             // left sample
             samples[index + 0] = (byte) (sample & 0xFF);        
             samples[index + 1] = (byte) ((sample >> 8) & 0xFF); 
             // right sample
             samples[index + 2] = (byte) (sample & 0xFF);
             samples[index + 3] = (byte) ((sample >> 8) & 0xFF);
             index += 4;
         }

         int offset = 0;

         while (offset < index){
             double increment =line.write(samples, offset, index-offset);
             offset += increment;
         }
    }

    private double getWave(double theta){

        double value = 0;

        theta = theta * 2 * Math.PI;

        value = getSin(theta);
        //value = getSqr(theta);

        return value;

    }

    private double getSin(double theta){
        return Math.sin(theta);
    }

    private int getSqr(double theta){
        if (theta <= Math.PI) return 1;
        else return 0;
    }

    // implementation of basic low-pass filter
    private int applyLowPassFilter(int sample){

        int newValue = sample;
        double filteredValue = oldValue + (newValue - oldValue) / smoothing;

        oldValue = filteredValue;
        return (int) filteredValue;
    }
}

The relevant method is at the end. If anyone does test this, please be careful of the volume if you have headphones!

So either:

  1. It is working and I’m just expecting too much of such a simple implementation
  2. I’m doing something wrong, stupid and obvious…

If it’s just 1. How should/could I get rid of that harsh beat/hit/click from sudden amplitude changes?

If it’s 2. good, should be a v short answer for a too long question.

  • 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-04T15:39:40+00:00Added an answer on June 4, 2026 at 3:39 pm

    A low pass filter will not remove clicks from sudden amplitude changes. Instead you need to avoid sudden amplitude changes.

    You could use the lowpass filter to filter your amplitude level.

    **Pseudo code**
    
    for i = 0 to numSamplesInWave-1 do
    begin
      theta = i / numSamplesInWave;
      wave = getWave(theta);
      currentAmplitude = applyLowPassFilter(TargetAmplitude);   
      Sample[i] = wave * currentAmplitude;
    end;
    

    Using a lowpass filter as above is fine for smoothing input values. For example when the user changes a volume control.

    In other situations it might be more appropriate to create an envelope of some sort. For example synthesizers commonly use ADSR envelopes to smooth the amplitude changes when a new Voice/Sound starts and stops.

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

Sidebar

Related Questions

I have a basic class that derived subclasses inherit from, it carries the basic
I have the basic code to rewrite a subdomain to another page. But how
So I am really stumped because I have basic ideas but I am looking
I have basic client-side validation working in my MVC3 RC2 application, but I'm now
Currently, i have basic C++ and PHP skills. But, i want to switch to
I am new to Qt Programming, but I have basic on C++. I want
Possible Duplicate: How to remove the text in progressBar in Android? I have basic
I have basic knowledge about SQL. But I still have difficulty when write SQL
I have been working on android from 6 months. So I have basic idea
I have basic knowledge of Java and some experience with MVC pattern. I have

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.