p_audioFormat = new QAudioFormat;
p_audioFormat->setSampleRate(8000);
p_audioFormat->setChannelCount(1);
p_audioFormat->setSampleSize(16);
p_audioFormat->setCodec("audio/pcm");
p_audioFormat->setByteOrder(QAudioFormat::LittleEndian); //大小端;
p_audioFormat->setSampleType(QAudioFormat::UnSignedInt); //有无符合;
p_audioOutput = new QAudioOutput(*p_audioFormat,this);
device = p_audioOutput->start(); //QIODevice类
QTimer *mytimer = new QTimer(this); //控制播发;因为设备内存有大小,我的是640byte;
connect(mytimer,SIGNAL(timeout()),this,SLOT(my_timer_out()));
mytimer->start(100);
前四个字节是上一个PCM数据,未压缩的PCM;
index = a[2];
valpred = *((short*)a);
int delta;
int count=0;
unsigned int step;
bool odd = true;
int sign;
for(int i=4; i<256; ){
if(odd) {
delta = a[i]&0x0f;
}
else {
delta = (uchar)a[i]>>4;
}
if(index<0) index = 0;
if(index>88) index = 88;
sign = delta & 8;
delta = delta & 7;
step = stepsizeTable[index];
vpdiff = step >> 3;
if( (delta & 4) !=0 ) vpdiff += step;
if( (delta & 2) !=0 ) vpdiff += step>>1;
if( (delta & 1) !=0 ) vpdiff += step>>2;
index += indexTable[delta];
if( sign != 0 ) {
valpred -= vpdiff;
if(valpred < -32768)
valpred = -32768;
}
else {
valpred += vpdiff;
if(valpred > 32767)
valpred = 32767;
}
music.append((char*)&valpred,2);
odd = !odd;
if(odd)i++;
}
void Widget::my_timer_out() //音质流畅;
{
while(p_audioOutput->bytesFree()>=p_audioOutput->periodSize()) {
if(music.size()periodSize())break;
device->write(music.data(),p_audioOutput->periodSize());
music=music.mid(p_audioOutput->periodSize());
}
}