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);

//设置通道数

audioFormat.setChannelCount(1);

//设置采样大小,一般为8位或16位

audioFormat.setSampleSize(16);

//设置编码方式

audioFormat.setCodec("audio/pcm");

//设置字节序

audioFormat.setByteOrder(QAudioFormat::LittleEndian);

//设置样本数据类型

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);

//将麦克风的音频数据传输到输入设备

streamIn=audioInput->start();

//当输入设备检测到数据时,调用槽函数slogReadData

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

audioOutput=newQAudioOutput(audioFormat,this);

//将音频数据传输到输出设备,再由输出设备写入到扬声器

streamOut=audioOutput->start();

////创建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转short

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

//编码

cg729Encoder.encode(srcAudio,dstAudio);

QByteArrayframe;

//reinterpret_cast用于强制转换,这里将unsignedchar*转换为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转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

你可能感兴趣的:(qt语音解码cg729(widget类))