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. 创建垂直正弦波
- 2. 正弦波绘图
- 3. 识别常见的周期性波形(正方形,正弦波,锯齿波,......)
- 4. C++正弦波跳跃弹跳公式
- 5. java SourceDataLine正弦波点击
- 6. 绘制正弦波,WPF
- 7. 正弦波点击Beep.BeepBeep
- 8. AUGraph正弦波损坏
- 9. Java快速正弦波音
- 10. n音讯正弦波
- 11. LSTM预测正弦波
- 12. 递归创建一个正弦波给定一个单一的正弦波值和周期
- 13. 如何在Cocoa中创建正弦波信号?
- 14. 如何在处理中创建正弦波?
- 15. 在iOS上绘制正弦波
- 16. 正弦波交替扭曲在Java
- 17. 在Python中生成正弦波声音
- 18. 在正弦波路径上动画UIView
- 19. 生成正弦波,方波,三角波,锯齿波的音频使用Android的AudioTrack类
- 20. 评估后缀符号含方波,正弦波,日志等功能在其
- 21. 用Python(正弦波?)拟合数据
- 22. SIMULINK - 奇怪的正弦波形图
- 23. 使用正弦波动画控件
- 24. 绘制正弦波的集合
- 25. JSyn:两个通道上的正弦波
- 26. TensorFlow中的正弦波预测
- 27. Android java:绘制正弦波形图
- 28. 正弦波360度是走出非零
- 29. 使用ADC生成正弦波
- 30. 使用opencv绘制正弦波
你会更好,这里用一个真实的信号发生器(与已知的校准) – 2008-10-15 06:50:59