用C#创建各种类型的wave文件

 

最近的工作用到创建wave文件,把方法share出来

现在只是能work但还有很大的缺陷

1) 正确的写入方法是开辟一块buffer,然后一个线程写入,一个读。或者用stream

但没弄出来开,着急赶工就采用了逐秒写入这种笨方法,

考虑过用内存映射文件,也卡住了,等明天贴上

 

ContractedBlock.gif ExpandedBlockStart.gif C# create wave
ExpandedBlockStart.gifContractedBlock.gif/**//// <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)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
//WaveFormat Format = new WaveFormat(44100, 16, 2);
            if(File.Exists(FileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    X 
+= ActualxStep;
                    Y 
= (short)(Math.Sin(X) * ActualAmplitude);

                    
for (int channelIndex = 0; channelIndex < Format.Channels; channelIndex++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
//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();
            }

        }

 

 

 

转载于:https://www.cnblogs.com/hbreset/archive/2009/04/28/1445738.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值