頭
#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