Linux 播放振荡/振动会产生的声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3677210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play the sound that an oscillation/vibration would make
提问by Oleh Prypin
I have an oscillation: for each moment of time t, I have a set of samples.
我有一个振荡:对于时间t 的每个时刻,我有一组样本。
I need to play the sound of this oscillation (output to speakers).
我需要播放这种振荡的声音(输出到扬声器)。
Cross-platform C++ (or Qt) solution is preferred, solution for Windows is also good.
首选跨平台 C++(或 Qt)解决方案,Windows 解决方案也不错。
And please help me improve the question, if you know how...
请帮助我改进问题,如果您知道如何...
采纳答案by Oleh Prypin
Here is C++/Qt code:
这是 C++/Qt 代码:
#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!
相当肮脏的解决方案,我认为它有内存泄漏......但它有效!
回答by tenfour
Here is an example using Qt; http://diotavelli.net/PyQtWiki/Playing%20a%20sound%20with%20QtMultimedia
这是一个使用 Qt 的例子;http://diotavelli.net/PyQtWiki/Playing%20a%20sound%20with%20QtMultimedia
It uses QAudioOutputto achieve PCM audio playback.
它使用QAudioOutput来实现 PCM 音频播放。