20
A
回答
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
這就好比是在創建僅存儲器中的文件,沒有它存儲到磁盤上。然後,它扮演的是MemoryStream
與System.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);
}
相關問題
- 1. 創建從在C++中的正弦波
- 2. 創建垂直正弦波
- 3. 正弦波繪圖
- 4. 動畫正弦波
- 5. 識別常見的週期性波形(正方形,正弦波,鋸齒波,......)
- 6. C++正弦波跳躍彈跳公式
- 7. 錯誤密謀正弦波
- 8. Java快速正弦波音
- 9. 正弦波點擊Beep.BeepBeep
- 10. java SourceDataLine正弦波點擊
- 11. 採樣頻率正弦波
- 12. 使正弦波更陡峭?
- 13. AUGraph正弦波損壞
- 14. LSTM預測正弦波
- 15. n音訊正弦波
- 16. 繪製正弦波,WPF
- 17. 加工正在處理的正弦波
- 18. 遞歸創建一個正弦波給定一個單一的正弦波值和週期
- 19. 在指定範圍內創建單個正弦波
- 20. 如何在處理中創建正弦波?
- 21. 如何在Cocoa中創建正弦波信號?
- 22. 三角波代替繪製正弦波的在MATLAB
- 23. 在MATLAB中繪製正弦波
- 24. 在Python中生成正弦波聲音
- 25. 正弦波交替扭曲在Java
- 26. 在iOS上繪製正弦波
- 27. 在正弦波路徑上動畫UIView
- 28. 在Tkinter中移動正弦波
- 29. 畫布,沿正方形路徑使用正弦波形狀創建動畫
- 30. 如何創建類似正弦波的Android Custome進度條?
你會更好,這裏用一個真實的信號發生器(與已知的校準) – 2008-10-15 06:50:59