1

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();
}
}
user3532190
  • 45
  • 1
  • 8
  • Why don't you write data to a QByteArray and split it manually? – Amartel Jun 10 '15 at 13:07
  • Because this will cause a stuttering in the streaming if the sample rate is not enough high. I used the "notify()" signal to catch data, and when the array contains enough data, I write it on udp, but this is too slow, and as I sayed, this bring stuttering on the stream. – user3532190 Jun 10 '15 at 14:16

0 Answers0