Qt QAudioInput如何发送固定数量的样本

时间:2015-06-10 12:57:09

标签: c++ qt sample audiostreamer qudpsocket

我尝试使用udp协议将一些音频流从我的Qt应用程序发送到其他设备。我的问题是目标设备需要为它接收的每个数据包提供固定数量的样本(在我的情况下为320)。 要做到这一点,我想我必须使用我用来捕捉声音的QAudioInput对象的setBufferSize函数,但是关于整个QAudioInput对象的文档非常奇怪和差。如何控制发送的样品数量? 谢谢。

这就是我发送流的方式:

QAudioFormat format;
format.setSampleRate(48000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);

//If format isn't supported find the nearest supported
QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
if (!info.isFormatSupported(format))
    format = info.nearestFormat(format);

input = new QAudioInput(format);

socket = new QUdpSocket();

socket->connectToHost(ip, port);
input->start(socket);

这是QByteArray的方式:

QAudioFormat format;
format.setSampleRate(8000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);

//If format isn't supported find the nearest supported
QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
if (!info.isFormatSupported(format))
    format = info.nearestFormat(format);

input = new QAudioInput(format);
input->setNotifyInterval((int)20);
connect(input, SIGNAL(notify()), this, SLOT(readMore()));
socket = new QUdpSocket();

socket->connectToHost(ip, port);
array.clear();
input->start(socket);


void UDPSender::readMore()
{
array.append(device->readAll());
qDebug()<<"Array size"<<array.length();

if(array.length()>=5120) \\ at this time, I don't care about exceeded data
{
    socket->write(array.mid(0,5120));
    array.clear();
}
}

0 个答案:

没有答案