生活随笔
收集整理的這篇文章主要介紹了
使用DirectX播放音频数据流
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用DirectX播放音頻數(shù)據(jù)流
使用directX插件可以播放音頻數(shù)據(jù)流
現(xiàn)在做一個測試項目,首先將信號發(fā)生器產(chǎn)生的正弦波(cw波)輸入到語音編碼器編碼,再講編碼的數(shù)據(jù)用解碼算法解碼,解碼輸出的數(shù)據(jù)流就是cw波。這個過程主要目的為了測試解碼算法是否能恢復cw波。暫時不考慮這個目的
項目流程
信號發(fā)生器(cw波)—->語音編碼(encode)—–>解碼算法(cw波)—->解碼算法(cw波)——>寫入到DirectX緩沖區(qū) —->播放
本文是要實現(xiàn)用DirectX音頻數(shù)據(jù)流,其實就是將音頻數(shù)據(jù)流寫到DirectX的緩沖區(qū)并播放,分為以下三步。
1.首先分析音頻文件wav的格式,wav格式
2. 將音頻數(shù)據(jù)流寫到wav格式中data chunk
3. DirectX播放wav數(shù)據(jù)流。
代碼如下
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;
public const int SampleByteWidth =
16 >>
3;
public const int SampleRate =
8000; BinaryWriter writer;MemoryStream mes;
byte[] voice;
public Form1(){InitializeComponent();}
private void buttonPlay_Click(
object sender, EventArgs e){write_wavfmt(
9,
1); BinaryReader 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;
int riffLength = datalength +
46;voice =
new byte[riffLength];mes =
new MemoryStream(voice);writer =
new BinaryWriter(mes);
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);
this.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);
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播放音频数据流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。