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? PS If i write that vector to a file and play it as raw sound I can hear a low frequency buzz.
First, you definitely shouldn't have system("pause")
before your main loop as @Hayt mentioned.
Second, you should seek to the beginning after you've written the data.
buffer->write(data, 32768);
buffer->seek(0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.