views:

84

answers:

2

First of all, sorry. I don't know how all the Physics terms are named in English.

I have an oscillation: for each moment of time t, I have the x (this should have some name, but I don't know it)
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.

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

+2  A: 

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

It uses QAudioOutput to achieve PCM audio playback.

tenfour
This is actually Python Qt... I'll try to "translate" it to C++
BlaXpirit
Well, playing PCM audio I think is fairly common so it should still be easy to find C++ examples on Google. It sounds like you're more of a physics guy than a programmer though so Qt might be too heavy for your purposes. Look at Justin Peel's comment about just saving your data as .WAV, and playing with a simple API like `PlaySound`.
tenfour
I mustn't create any files!
BlaXpirit
A: 

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!

BlaXpirit