Qt QAudioInput how to send a fixed number of samples

672 views Asked by At

I'm tryng to send some audio stream from my Qt application to an other device with the udp protocol. My problem is that the target device wants a fixed number of samples(in my case 320) for every packet it receives. To do that I think I must use the setBufferSize function of QAudioInput object I use to catch the sound, but documentation is very odd and poor about the whole QAudioInput object. How can I control the number of samples I send? Thank you.

This is how I send the stream:

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

This is the way with 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

There are 0 answers