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

New Post: change frequency at runtime

$
0
0

Here is the code of the wavetone class:

  public class WaveTone : WaveStream
{
private double frequency;
private double amplitude;
private double time;

public WaveTone(double f, double a)
{
this.time = 0;
this.frequency = f;
this.amplitude = a;
}

public override long Position
{
get;
set;
}

public override long Length
{
get { return long.MaxValue; }
}

public override WaveFormat WaveFormat
{
get { return new WaveFormat(44100, 16, 1); }
}

public override int Read(byte[] buffer, int offset, int count)
{
int samples = count / 2;
for (int i = 0; i < samples; i++)
{
double sine = amplitude * Math.Sin(Math.PI * 2 * frequency * time);
time += 1.0 / 44100;
short truncated = (short)Math.Round(sine * (Math.Pow(2, 15) - 1));
buffer[i * 2] = (byte)(truncated & 0x00ff);
buffer[i * 2 + 1] = (byte)((truncated & 0xff00) >> 8);
}

return count;
}
}


Viewing all articles
Browse latest Browse all 5831

Trending Articles



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