Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Click Sound During Sine Wave Sounds

$
0
0
I am working on a simple WPF application that allows for multiple stereo sine wave sounds to be mixed together and played to the user.

The user is able to choose the amplitude, frequency, and channel of the sine waves in order to create different types of output. Everything is working well except for when the user chooses floating point frequencies which are not whole numbers (ex 100.5), I hear an audible "click" sound occurring on a regular period in the sound output. Everything sounds good for whole number frequencies.

Here is the code used to generate the sine waves:
public class SineWaveProvider32 : WaveProvider32, INotifyPropertyChanged
{
        public enum EStereoChannel
        {
            ChannelLeft = 0,
            ChannelRight,
            ChannelBoth
        };

        int sample;

        public SineWaveProvider32() :
            base(44100, 2) // 44.1 kHz sample, stereo 
        {
            Frequency = 1000.5;
            Amplitude = 0.25f; // let's not hurt our ears   
            ChannelMode = EStereoChannel.ChannelBoth;

        }

        private float freq;
        public float Frequency
        {
            get
            {
                return freq;
            }
            set
            {
                if (freq != value)
                {
                    freq = value;
                    OnPropertyChanged("Frequency");
                }
            }
        }

        private float amp;
        public float Amplitude
        {
            get
            {
                return amp;
            }
            set
            {
                if (amp != value)
                {
                    amp = value;
                    OnPropertyChanged("Amplitude");
                }
            }
        }

        private EStereoChannel channelMode;
        public EStereoChannel ChannelMode
        {
            get
            {
                return channelMode;
            }
            set
            {
                if (channelMode != value)
                {
                    channelMode = value;
                    OnPropertyChanged("ChannelMode");
                }
            }
        }

        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            int sampleRate = WaveFormat.SampleRate;
            float sound;
            float chan1;
            float chan2;
            for (int n = 0; n < sampleCount; n += 2)
            {
                sound = (float)(Amplitude * Math.Sin((2.0f * Math.PI * ((float)sample) * Frequency) / ((float)sampleRate)));
                
                switch (ChannelMode)
                {
                    
                    case EStereoChannel.ChannelRight:
                        chan1 = 0;
                        chan2 = sound;
                        break;
                    case EStereoChannel.ChannelLeft:
                        chan1 = sound;
                        chan2 = 0;
                        break;
                    case EStereoChannel.ChannelBoth: // fall through
                    default: // we default to mono
                        chan1 = sound;
                        chan2 = sound;
                        break;
                }

                buffer[n + offset] = chan1;
                buffer[n + offset + 1] = chan2;

                sample++;
                if (sample >= sampleRate)
                {
                    sample = 0;
                } 
            }
            return sampleCount;
        }
And this is the code used to generate the WaveOut:
                mixer = new MixingWaveProvider32();

                foreach (SineWaveProvider32 wav in WaveCollection)
                {
                    mixer.AddInputStream(wav);
                }

                waveOut = new WaveOut();
                waveOut.Init(mixer);
                waveOut.Play();
                IsPlaying = true;
                Seconds = 0;
                timer.Start();
Any idea where this clicking sound is coming from?

Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>