2008-10-15 140 views
20

如何產生特定頻率的音頻正弦波或方波?創建正弦波或方波在C#

我希望這樣做是爲了校準設備,所以如何準確將這些電波是什麼?

+2

你會更好,這裏用一個真實的信號發生器(與已知的校準) – 2008-10-15 06:50:59

回答

29

您可以使用NAudio並創建派生的WaveStream,輸出正弦波或方波,您可以輸出到聲卡或寫入WAV文件。如果您使用的32位浮點樣本,因爲它已經變爲-1和1

至於準確度之間,你的意思是完全正確的頻率,你可以寫值直接出了正弦函數,而不必向規模化,或者正確的波形?有一個真正的方波,甚至正弦波將有可能在其它頻率幾個很安靜的文物沒有這樣的事情。如果頻率的準確性很重要,那麼您就依賴於聲卡中時鐘的穩定性和準確性。話雖如此,我會想象大多數用途的準確性會足夠好。

下面是一些示例代碼,在一個8   kHz採樣率,並用16個採樣(即,不是浮點)做出1   kHz採樣:

int sampleRate = 8000; 
short[] buffer = new short[8000]; 
double amplitude = 0.25 * short.MaxValue; 
double frequency = 1000; 
for (int n = 0; n < buffer.Length; n++) 
{ 
    buffer[n] = (short)(amplitude * Math.Sin((2 * Math.PI * n * frequency)/sampleRate)); 
} 
+0

沒有這樣的事,作爲一個真正的方波,甚至正弦波:非常正確,我的意思是頻率,謝謝 – johnc 2008-10-16 05:27:33

19

這讓你給頻率,持續時間,幅度,它是100%.NET CLR代碼。沒有外部DLL的。它通過創建一個WAV格式的MemoryStream這就好比是在創建僅存儲器中的文件,沒有它存儲到磁盤上。然後,它扮演的是MemoryStreamSystem.Media.SoundPlayer

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Windows.Forms; 

public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383) 
{ 
    var mStrm = new MemoryStream(); 
    BinaryWriter writer = new BinaryWriter(mStrm); 

    const double TAU = 2 * Math.PI; 
    int formatChunkSize = 16; 
    int headerSize = 8; 
    short formatType = 1; 
    short tracks = 1; 
    int samplesPerSecond = 44100; 
    short bitsPerSample = 16; 
    short frameSize = (short)(tracks * ((bitsPerSample + 7)/8)); 
    int bytesPerSecond = samplesPerSecond * frameSize; 
    int waveSize = 4; 
    int samples = (int)((decimal)samplesPerSecond * msDuration/1000); 
    int dataChunkSize = samples * frameSize; 
    int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize; 
    // var encoding = new System.Text.UTF8Encoding(); 
    writer.Write(0x46464952); // = encoding.GetBytes("RIFF") 
    writer.Write(fileSize); 
    writer.Write(0x45564157); // = encoding.GetBytes("WAVE") 
    writer.Write(0x20746D66); // = encoding.GetBytes("fmt ") 
    writer.Write(formatChunkSize); 
    writer.Write(formatType); 
    writer.Write(tracks); 
    writer.Write(samplesPerSecond); 
    writer.Write(bytesPerSecond); 
    writer.Write(frameSize); 
    writer.Write(bitsPerSample); 
    writer.Write(0x61746164); // = encoding.GetBytes("data") 
    writer.Write(dataChunkSize); 
    { 
     double theta = frequency * TAU/(double)samplesPerSecond; 
     // 'volume' is UInt16 with range 0 thru Uint16.MaxValue (= 65 535) 
     // we need 'amp' to have the range of 0 thru Int16.MaxValue (= 32 767) 
     double amp = volume >> 2; // so we simply set amp = volume/2 
     for (int step = 0; step < samples; step++) 
     { 
      short s = (short)(amp * Math.Sin(theta * (double)step)); 
      writer.Write(s); 
     } 
    } 

    mStrm.Seek(0, SeekOrigin.Begin); 
    new System.Media.SoundPlayer(mStrm).Play(); 
    writer.Close(); 
    mStrm.Close(); 
} // public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383) 
4

嘗試從Creating sine and save to wave file in C#

private void TestSine() 
{ 
    IntPtr format; 
    byte[] data; 
    GetSineWave(1000, 100, 44100, -1, out format, out data); 
    WaveWriter ww = new WaveWriter(File.Create(@"d:\work\sine.wav"), 
     AudioCompressionManager.FormatBytes(format)); 
    ww.WriteData(data); 
    ww.Close(); 
} 

private void GetSineWave(double freq, int durationMs, int sampleRate, short decibel, out IntPtr format, out byte[] data) 
{ 
    short max = dB2Short(decibel);//short.MaxValue 
    double fs = sampleRate; // sample freq 
    int len = sampleRate * durationMs/1000; 
    short[] data16Bit = new short[len]; 
    for (int i = 0; i < len; i++) 
    { 
     double t = (double)i/fs; // current time 
     data16Bit[i] = (short)(Math.Sin(2 * Math.PI * t * freq) * max); 
    } 
    IntPtr format1 = AudioCompressionManager.GetPcmFormat(1, 16, (int)fs); 
    byte[] data1 = new byte[data16Bit.Length * 2]; 
    Buffer.BlockCopy(data16Bit, 0, data1, 0, data1.Length); 
    format = format1; 
    data = data1; 
} 

private static short dB2Short(double dB) 
{ 
    double times = Math.Pow(10, dB/10); 
    return (short)(short.MaxValue * times); 
}