开发者

Play the sound that an oscillation/vibration would make

开发者 https://www.devze.com 2023-01-15 05:39 出处:网络
I have an oscillation: for each moment of time t, I have a set of samples. I need to play the sound of this oscillation (output to speakers).

I have an oscillation: for each moment of time t, I have a set of samples.

I need to play the sound of this oscillation (output to speakers).

Cross-platform C++ (or Qt) solution is preferred, solution for Win开发者_开发知识库dows is also good.

And please help me improve the question, if you know how...


Here is an example using Qt; http://diotavelli.net/PyQtWiki/Playing%20a%20sound%20with%20QtMultimedia

It uses QAudioOutput to achieve PCM audio playback.


Here is C++/Qt code:

#include<math.h>
#include<QBuffer>
#include<QAudioFormat>
#include<QAudioOutput>
...
QAudioFormat format;
format.setChannels(1);
format.setFrequency(22050);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioOutput* output=new QAudioOutput(format);
QBuffer* buffer=new QBuffer();
QByteArray data;
for (int i=0;i<22050*2;i++)
{
    short value=(/*Volume:*/10000*sin(2*3.1415*/*Frequency:*/600*i/22050.0));
    data.append((char*)&value,2);
}
buffer->setData(data);
buffer->open(QIODevice::ReadOnly);
buffer->seek(0);
output->start(buffer);

Quite dirty solution, and I think it has memory leaks... But it works!

0

精彩评论

暂无评论...
验证码 换一张
取 消