日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用DirectX播放音频数据流

發布時間:2023/12/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用DirectX播放音频数据流 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用DirectX播放音頻數據流

使用directX插件可以播放音頻數據流
現在做一個測試項目,首先將信號發生器產生的正弦波(cw波)輸入到語音編碼器編碼,再講編碼的數據用解碼算法解碼,解碼輸出的數據流就是cw波。這個過程主要目的為了測試解碼算法是否能恢復cw波。暫時不考慮這個目的
項目流程
信號發生器(cw波)—->語音編碼(encode)—–>解碼算法(cw波)—->解碼算法(cw波)——>寫入到DirectX緩沖區 —->播放

本文是要實現用DirectX音頻數據流,其實就是將音頻數據流寫到DirectX的緩沖區并播放,分為以下三步。
1.首先分析音頻文件wav的格式,wav格式
2. 將音頻數據流寫到wav格式中data chunk
3. DirectX播放wav數據流。
代碼如下

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; using System.IO;namespace playwav {public partial class Form1 : Form{public const int SampleBitWidth = 16; //每個sample 包含 16bitspublic const int SampleByteWidth = 16 >> 3;// 每個sample 包含 2個byte, 每幀包含160 samplepublic const int SampleRate = 8000; //每秒采樣8000個sample。每秒讀取 8000*2 =16000bytesBinaryWriter writer;MemoryStream mes;byte[] voice; public Form1(){InitializeComponent();}private void buttonPlay_Click(object sender, EventArgs e){write_wavfmt(9,1); //9*frame=9*160samplesBinaryReader stream = new BinaryReader(mse);Stream a = stream.BaseStream;Device dev = new Device();dev.SetCooperativeLevel(this, CooperativeLevel.Normal);SecondaryBuffer sec = new SecondaryBuffer(a,Convert.ToInt32(a.Length),dev);sec.Play(0, BufferPlayFlags.Looping);}public void write_wavfmt(int framecount, int channelcount){int datalength = framecount * 160 * 2 * channelcount; //每幀160sample =160*2bytesint riffLength = datalength + 46;voice = new byte[riffLength];mes = new MemoryStream(voice);writer = new BinaryWriter(mes);//////////////wavfmt///////this.writer.Write(ASCIIEncoding.ASCII.GetBytes("RIFF"));this.writer.Write((int)riffLength);this.writer.Write(ASCIIEncoding.ASCII.GetBytes("WAVEfmt"));this.writer.Write((int)16);this.writer.Write((short)1);this.writer.Write((short)channelcount);this.writer.Write((int)SampleRate);this.writer.Write((int)SampleRate * SampleByteWidth * channelcount);//byteRate = SampleRate * SampleByteWidththis.writer.Write((short)SampleBitWidth * channelcount);this.writer.Write((short)SampleBitWidth);this.writer.Write(ASCIIEncoding.ASCII.GetBytes("data"));this.writer.Write((int)datalength);byte[] data = Get_cw_Content(datalength);//讀取cw音頻數據。this.writer.Write(data);this.writer.BaseStream.Seek(0, SeekOrigin.Begin);}public byte[] Get_cw_Content(int datalength){List<short> listData = new List<short>();string cw_path2 = System.Environment.CurrentDirectory+ "\\tone.txt";string read = string.Empty;using (StreamReader sr = new StreamReader(cw_path2)){read = sr.ReadLine();while (read != null) {listData.Add(Convert.ToInt16(read));read = sr.ReadLine();}}byte[] cw_data = new byte[datalength];MemoryStream mestream = new MemoryStream(cw_data);BinaryWriter bwriter = new BinaryWriter(mestream);for (int i = 0; i < datalength/2; i++)bwriter.Write(listData[i]);bwriter.Close();return cw_data;}} }

總結

以上是生活随笔為你收集整理的使用DirectX播放音频数据流的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。