用C#建立各種類型的wave檔案

來源:互聯網
上載者:User
 

最近的工作用到建立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();
            }
        }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.