最近的工作用到创建wave文件,把方法share出来
现在只是能work但还有很大的缺陷
1) 正确的写入方法是开辟一块buffer,然后一个线程写入,一个读。或者用stream
但没弄出来开,着急赶工就采用了逐秒写入这种笨方法,
考虑过用内存映射文件,也卡住了,等明天贴上
C# create wave
/**//// <summary>
/// write data per second
/// y= a*sin(Wt + b) ; W = f*2*PI
/// </summary>
public void CreateWave(
WaveFormat Format,
Int32 LengthInSecs,
Int32 Amplitude,
Int32 AmplitudeIncremental,
Int32 Frequency,
Int32 FrequencyIncremental,
string FileName)
{
//WaveFormat Format = new WaveFormat(44100, 16, 2);
if(File.Exists(FileName))
{
File.Delete(FileName);
}
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
Int32 dataLength = (Int32)(Format.SamplesPerSec * LengthInSecs * Format.BitsPerSample / 8);
writer.Write(System.Text.Encoding.ASCII.GetBytes("RIFF".ToCharArray()));
writer.Write((Int32)(dataLength + 36)); //File length minus first 8 bytes of RIFF description
writer.Write(System.Text.Encoding.ASCII.GetBytes("WAVEfmt ".ToCharArray()));
writer.Write((Int32)16); //length of following chunk: 16
writer.Write(Format.FormatTag);
writer.Write(Format.Channels);
writer.Write(Format.SamplesPerSec);
writer.Write(Format.AvgBytesPerSec);
writer.Write(Format.BlockAlign);
writer.Write(Format.BitsPerSample);
writer.Write(System.Text.Encoding.ASCII.GetBytes("data".ToCharArray()));
writer.Write(dataLength);
FileStream fs = new FileStream(FileName, FileMode.Append);
stream.WriteTo(fs);
stream.Close();
writer.Close();
fs.Close();
double t = (2 * Math.PI) / Format.SamplesPerSec;//interval for 1 Hz
double X = 0;
short Y;
double ActualxStep;
float ActualAmplitude;
for (int i = 0; i < LengthInSecs; i++)
{
MemoryStream ms2 = new MemoryStream();
BinaryWriter bw2 = new BinaryWriter(ms2);
//interval for the requested frequency = 1Hz * frequencyHz
ActualxStep = t * (Frequency + FrequencyIncremental * i);
ActualAmplitude = Amplitude + (AmplitudeIncremental * i);
for (int j = Format.SamplesPerSec * Format.Channels * i; j < Format.SamplesPerSec * Format.Channels * (1 + i); j += Format.Channels)
{
X += ActualxStep;
Y = (short)(Math.Sin(X) * ActualAmplitude);
for (int channelIndex = 0; channelIndex < Format.Channels; channelIndex++)
{
//ChannelSample = Samples[j + channelIndex];
//ChannelSample = (short)((ChannelSample + Y) / 2);
//Samples[j + channelIndex] = ChannelSample;
bw2.Write(Y / 2);
}
}
FileStream fs2 = new FileStream(FileName, FileMode.Append);
ms2.WriteTo(fs2);
ms2.Close();
fs2.Close();
}
}
/**//// <summary>
/// write data per second
/// y= a*sin(Wt + b) ; W = f*2*PI
/// </summary>
public void CreateWave(
WaveFormat Format,
Int32 LengthInSecs,
Int32 Amplitude,
Int32 AmplitudeIncremental,
Int32 Frequency,
Int32 FrequencyIncremental,
string FileName)
{
//WaveFormat Format = new WaveFormat(44100, 16, 2);
if(File.Exists(FileName))
{
File.Delete(FileName);
}
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
Int32 dataLength = (Int32)(Format.SamplesPerSec * LengthInSecs * Format.BitsPerSample / 8);
writer.Write(System.Text.Encoding.ASCII.GetBytes("RIFF".ToCharArray()));
writer.Write((Int32)(dataLength + 36)); //File length minus first 8 bytes of RIFF description
writer.Write(System.Text.Encoding.ASCII.GetBytes("WAVEfmt ".ToCharArray()));
writer.Write((Int32)16); //length of following chunk: 16
writer.Write(Format.FormatTag);
writer.Write(Format.Channels);
writer.Write(Format.SamplesPerSec);
writer.Write(Format.AvgBytesPerSec);
writer.Write(Format.BlockAlign);
writer.Write(Format.BitsPerSample);
writer.Write(System.Text.Encoding.ASCII.GetBytes("data".ToCharArray()));
writer.Write(dataLength);
FileStream fs = new FileStream(FileName, FileMode.Append);
stream.WriteTo(fs);
stream.Close();
writer.Close();
fs.Close();
double t = (2 * Math.PI) / Format.SamplesPerSec;//interval for 1 Hz
double X = 0;
short Y;
double ActualxStep;
float ActualAmplitude;
for (int i = 0; i < LengthInSecs; i++)
{
MemoryStream ms2 = new MemoryStream();
BinaryWriter bw2 = new BinaryWriter(ms2);
//interval for the requested frequency = 1Hz * frequencyHz
ActualxStep = t * (Frequency + FrequencyIncremental * i);
ActualAmplitude = Amplitude + (AmplitudeIncremental * i);
for (int j = Format.SamplesPerSec * Format.Channels * i; j < Format.SamplesPerSec * Format.Channels * (1 + i); j += Format.Channels)
{
X += ActualxStep;
Y = (short)(Math.Sin(X) * ActualAmplitude);
for (int channelIndex = 0; channelIndex < Format.Channels; channelIndex++)
{
//ChannelSample = Samples[j + channelIndex];
//ChannelSample = (short)((ChannelSample + Y) / 2);
//Samples[j + channelIndex] = ChannelSample;
bw2.Write(Y / 2);
}
}
FileStream fs2 = new FileStream(FileName, FileMode.Append);
ms2.WriteTo(fs2);
ms2.Close();
fs2.Close();
}
}