int main(int argc, char *argv[])
{
QCoreApplication b(argc, argv);
QBuffer *buffer;
QAudioOutput *a;
QAudioFormat format;
format.setSampleRate(8000);
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (info.isFormatSupported(format))
{
cout << "Format supported" << endl;
}
else
{
cout << "Format not supported" << endl;
}
char *data = (char*)malloc(32768 * sizeof(char));
//generating a sound
for (int i = 0; i<256; ++i)
{
for (int j = 0; j<128; ++j)
{
data[i * 128 + j] = (char)j;
}
}
cout << "Created samples" << endl;
//copying into the buffer
buffer = new QBuffer;
buffer->open(QIODevice::ReadWrite);
buffer->seek(0);
buffer->write(data, 32768);
cout << "Filled buffer" << endl;
//playing
QThread thr;
a = new QAudioOutput(format);
//a->moveToThread(&thr);
//thr.start();
//QMetaObject::invokeMethod(a, "start", Q_ARG(QIODevice*, buffer));
a->start(buffer);
system("pause");
return b.exec();
}
I am trying to make my console application output sound and I can't figure out why my QAudioOutput
object doesn't do that. I placed the code above. Can you tell me what did I do wrong?
P.S. If i write that vector to a file and play it as raw sound I can hear a low frequency buzz.
exec
. exec will start the the Qt internal main threads. So it may be because of that.QGuiApplication
orQApplication
for audio to work. Then, how do you intend your application to quit? You probably want to connect relevant audio finished signal toqApp
'squit()
slot.