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

New Post: Click Sound During Sine Wave Sounds

$
0
0
Thanks for pointing our my error. I didn't fully understand the code previously. Here's my fix if anyone is interested:
        int phaseAngle = 0;
        int zeroes = 0;

        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            float sampleRate = (float)WaveFormat.SampleRate;
            float sound;
            float chan1;
            float chan2;
            for (int n = 0; n < sampleCount; n += 2)
            {
                sound = (float)(Amplitude * Math.Sin((2 * Math.PI * phaseAngle * Frequency) / sampleRate));
                phaseAngle++;

                if (sound == 0)
                {
                    zeroes++;
                }

                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;

                // if we've completed 1 wave, reset phase and counters
                if (zeroes == 3)
                {
                    zeroes = 1;
                    phaseAngle = 1;
                }
                
            }
            return sampleCount;
        }

Viewing all articles
Browse latest Browse all 5831

Trending Articles



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