qt語音解碼cg729(widget類)

#ifndefWIDGET_H

#defineWIDGET_H

#include

#include

#include

#include

#include"cg729encoder.h"

#include"cg729decoder.h"

#include"cudpthread.h"

#include

namespaceUi{

classWidget;

}

classWidget:publicQWidget

{

Q_OBJECT

public:

explicitWidget(QWidget*parent=0);

~Widget();

private:

Ui::Widget*ui;

CG729Encodercg729Encoder;

QAudioInput*audioInput;

QIODevice*streamIn;

CG729Decodercg729Decoder;

QAudioOutput*audioOutput;

QIODevice*streamOut;

QByteArraytempBuffer;

QByteArraytempframe;

QThread*udpThreadFather;

publicslots:

voidslogReadData();

voidslotSendData1(QByteArraybyte_array);

voidon_pushButton_clicked();

voidon_pushButtonSetIp_clicked();

signals:

voidsignalSendData1(QByteArraybyte_array);

};

#endif//WIDGET_H

#include"widget.h"

#include"ui_widget.h"

#include

#include"clientest.h"

#include"server.h"

constintBUFFER_SIZE=2048;

Widget::Widget(QWidget*parent):

QWidget(parent),

ui(newUi::Widget)

{

ui->setupUi(this);

//設置采樣格式

QAudioFormataudioFormat;

//設置采樣率

audioFormat.setSampleRate(8000);

//設置通道數(shù)

audioFormat.setChannelCount(1);

//設置采樣大小,一般為8位或16位

audioFormat.setSampleSize(16);

//設置編碼方式

audioFormat.setCodec("audio/pcm");

//設置字節(jié)序

audioFormat.setByteOrder(QAudioFormat::LittleEndian);

//設置樣本數(shù)據(jù)類型

audioFormat.setSampleType(QAudioFormat::UnSignedInt);

//獲取設備信息

QAudioDeviceInfoinfo=QAudioDeviceInfo::defaultInputDevice();

if(!info.isFormatSupported(audioFormat))

{

qDebug()<<"defaultformatnotsupportedtrytousenearest";

audioFormat=info.nearestFormat(audioFormat);

}

info=QAudioDeviceInfo::defaultOutputDevice();

if(!info.isFormatSupported(audioFormat)){

qDebug()<<"defaultformatnotsupportedtrytousenearest";

audioFormat=info.nearestFormat(audioFormat);

}

audioInput=newQAudioInput(audioFormat,this);

//將麥克風的音頻數(shù)據(jù)傳輸?shù)捷斎朐O備

streamIn=audioInput->start();

//當輸入設備檢測到數(shù)據(jù)時为严,調(diào)用槽函數(shù)slogReadData

connect(streamIn,SIGNAL(readyRead()),SLOT(slogReadData()));

audioOutput=newQAudioOutput(audioFormat,this);

//將音頻數(shù)據(jù)傳輸?shù)捷敵鲈O備,再由輸出設備寫入到揚聲器

streamOut=audioOutput->start();

////創(chuàng)建UDP線程

//CUdpThread*udpThread=newCUdpThread();

//udpThreadFather=newQThread();

//udpThread->moveToThread(udpThreadFather);

//connect(udpThreadFather,SIGNAL(started()),udpThread,SLOT(run()));

////啟動線程

//udpThreadFather->start();

//connect(this,SIGNAL(signalSendData(constQByteArray&)),udpThread,SLOT(slotSendData(constQByteArray&)));

//connect(udpThread,SIGNAL(signalSendData(constQByteArray&)),this,SLOT(slotSendData(constQByteArray&)));

server*serverTcp=newserver();

clientest*client=newclientest();

connect(this,SIGNAL(signalSendData1(QByteArray)),client,SLOT(sendVoiceData(QByteArray)));

connect(serverTcp,SIGNAL(signalSendData(QByteArray)),this,SLOT(slotSendData1(QByteArray)));

}

Widget::~Widget()

{

deleteui;

}

voidWidget::slogReadData()

{

shortsrcAudio[L_FRAME]={0};

unsignedchardstAudio[L_FRAME_COMPRESSED]={'\0'};

if(!audioInput)

{

qDebug()<<"AudioInputError";

return;

}

QByteArraydataBuffer(BUFFER_SIZE,0);

qint64len1=audioInput->bytesReady();

if(len1>BUFFER_SIZE)

{

qDebug()<<"BUFFER_SIZEtoosmall";

return;

}

qint64len2=streamIn->read(dataBuffer.data(),len1);

tempBuffer.append(dataBuffer.data(),len2);

for(inti=0;i

{

//char轉(zhuǎn)short

memcpy(srcAudio,tempBuffer.data()+i*L_FRAME*2,L_FRAME*2);

//編碼

cg729Encoder.encode(srcAudio,dstAudio);

QByteArrayframe;

//reinterpret_cast用于強制轉(zhuǎn)換,這里將unsignedchar*轉(zhuǎn)換為constchar*斯够。

frame.append(reinterpret_cast(dstAudio),L_FRAME_COMPRESSED);

signalSendData1(frame);

}

tempBuffer.clear();

}

voidWidget::slotSendData1(QByteArraybyte_array)

{

qDebug()<<"rec"<

for(inti=0;i

{

unsignedcharsrcAudio[L_FRAME_COMPRESSED]={'\0'};

shortdstAudio[L_FRAME]={0};

memcpy(srcAudio,(unsignedchar*)byte_array.data()+i*L_FRAME_COMPRESSED,L_FRAME_COMPRESSED);

//G729解碼

cg729Decoder.decode(srcAudio,dstAudio,0);

//short轉(zhuǎn)char

tempframe.append((char*)dstAudio,L_FRAME*2);

if(audioOutput&&audioOutput->state()!=QAudio::StoppedState&&

audioOutput->state()!=QAudio::SuspendedState)

{

intchunks=audioOutput->bytesFree()/audioOutput->periodSize();

while(chunks)

{

if(tempframe.length()>=audioOutput->periodSize())

{

//寫入到揚聲器

streamOut->write(tempframe.data(),audioOutput->periodSize());

tempframe=tempframe.mid(audioOutput->periodSize());

}

else

{

//寫入到揚聲器

streamOut->write(tempframe);

tempframe.clear();

break;

}

--chunks;

}

}

}

}

voidWidget::on_pushButton_clicked()

{

if(audioInput->state()==QAudio::SuspendedState)

{

audioInput->resume();

ui->pushButton->setText(QStringLiteral("暫停"));

}

elseif(audioInput->state()==QAudio::ActiveState)

{

audioInput->suspend();

ui->pushButton->setText(QStringLiteral("開始"));

}

elseif(audioInput->state()==QAudio::StoppedState)

{

audioInput->resume();

ui->pushButton->setText(QStringLiteral("暫停"));

}

elseif(audioInput->state()==QAudio::IdleState)

{

//ToDo

}

}

voidWidget::on_pushButtonSetIp_clicked()

{

CUdpThread::IpAddress=ui->lineEdit->text();

}

ty\?u?U?t

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末啸蜜,一起剝皮案震驚了整個濱河市髓棋,隨后出現(xiàn)的幾起案子实檀,更是在濱河造成了極大的恐慌,老刑警劉巖按声,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件膳犹,死亡現(xiàn)場離奇詭異,居然都是意外死亡签则,警方通過查閱死者的電腦和手機须床,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來怀愧,“玉大人侨颈,你說我怎么就攤上這事余赢⌒疽澹” “怎么了?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵妻柒,是天一觀的道長扛拨。 經(jīng)常有香客問我,道長举塔,這世上最難降的妖魔是什么绑警? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮央渣,結果婚禮上计盒,老公的妹妹穿的比我還像新娘。我一直安慰自己芽丹,他們只是感情好北启,可當我...
    茶點故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般咕村。 火紅的嫁衣襯著肌膚如雪场钉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天懈涛,我揣著相機與錄音逛万,去河邊找鬼。 笑死批钠,一個胖子當著我的面吹牛宇植,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播埋心,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼当纱,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了踩窖?” 一聲冷哼從身側(cè)響起坡氯,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎洋腮,沒想到半個月后箫柳,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡啥供,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年悯恍,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伙狐。...
    茶點故事閱讀 38,617評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡涮毫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出贷屎,到底是詐尸還是另有隱情罢防,我是刑警寧澤,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布唉侄,位于F島的核電站咒吐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏属划。R本人自食惡果不足惜恬叹,卻給世界環(huán)境...
    茶點故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望同眯。 院中可真熱鬧绽昼,春花似錦、人聲如沸须蜗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至疏魏,卻和暖如春停做,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背大莫。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工蛉腌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人只厘。 一個月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓烙丛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親羔味。 傳聞我的和親對象是個殘疾皇子河咽,可洞房花燭夜當晚...
    茶點故事閱讀 43,486評論 2 348

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,749評論 25 707
  • 晨跑,微風吹拂赋元,草上露珠晶瑩忘蟹,路邊的小花競相開放,張開五彩繽紛的笑臉搁凸,散發(fā)誘人的清香媚值,令我駐足,沁人心脾护糖。 春的精...
    花樣芬芳閱讀 279評論 0 0
  • ?? 貓咪偷吃食物是很正常的事情嫡良∶谭觯可是這只貓咪偷吃食物后,居然還躲起來寝受,以為這樣子鏟屎就不知道了嗎坷牛? 藍貓:咦,鏟...
    丶lov3閱讀 161評論 0 1
  • 車站羡蛾,本就是一個離別的地方漓帅,總會帶著或濃或淡的傷感锨亏。李健的《車站》痴怨,那份濃濃的離別感傷更是撲面而來。 先是吉他輕柔...
    李玉敏99閱讀 361評論 1 2
  • 從事IOS開發(fā)器予,由自動打包腳本入手開始接觸到python浪藻;Mac電腦上自帶python環(huán)境,但是mac本地版本是p...
    uestc110閱讀 693評論 0 0