Qt 5.1 or later:
I need to play a notification sound of frequency x for n milliseconds. It'd also be nice if I could combine tones like this: 1000Hz for 2 secs, then 3000Hz for 1 sec, ..
The easiest way is to use files (WAV, MP3, ..), e.g. as described here: How to play sound with Qt But then I have to generate such files for each of my scenarios.
Qt's audio output example generates such tones in memory ( Generator::generateData(const QAudioFormat &format, qint64 durationUs, int sampleRate) ). I could write such a generator for my purpose. But do I have to?
So what is the easiest way to just play frequency x for n milliseconds?
To generate a tone in Qt, we can pass our own QBuffer to the QAudioOutput to be played.
Take a look at the first example on the QAudioOutput page.
What I did was create my waveform in a QByteArray. Remember that
sin(2 * pi * frequency * i / sample_rate)will give you a sin tone of the desired frequency:Then we can take that buffer and do something like this to play it:
If you need more example code, you can see how I did it in a little project I just started here.