[Qt-interest] QAudioOutput and push mode
Gabriel M. Beddingfield
gabrbedd at gmail.com
Thu Aug 26 05:35:52 CEST 2010
I have a program that is more or less an audio synthesizer.
When using QAudioOutput, I'm having trouble writing the data
at the right time. If I write too frequently, the song gets
played too fast (overlapping sections). Too infrequently,
the song is played too slow and is choppy.
What's the preferred way to do this?
Here's the way I've done it, simplified:
// JACK-style callback
typedef int (*process_callback_t)(uint32_t nframes,
void *arg);
class QtAudioSystemPrivate : public QThread
{
public:
//...
private:
virtual void run();
private:
AudioSystem::process_callback_t _callback;
void* _callback_arg;
enum { PLAY, STOP, SHUTDOWN } _state;
QAudioOutput *_dev;
};
void QtAudioSystemPrivate::run()
{
assert(_callback);
assert(_dev);
uint32_t nframes = 1024;
uint32_t bufsize = nframes * 2 * sizeof(float);
uint32_t used;
while(_state != SHUTDOWN) {
// ONLY call the callback if the device
// buffer is mostly empty.
used = _dev->bufferSize() - _dev->bytesFree();
if((_state == PLAY) && (used <= bufsize)){
(*_callback)(nframes, _callback_arg);
} else {
msleep(5);
}
}
}
int typical_audio_callback(uint32_t nframes, void* arg)
{
float interleaved_buffer[BUFSIZE];
// generate data in interleaved_buffer, 2 chans.
// Copy `nframes` samples to QIODevice returned
// by QAudioOutput::start()...
_output_buf->write( (const char*)buf,
2*nframes*sizeof(float) );
return 0;
}
Thanks in advance,
Gabriel
More information about the Qt-interest-old
mailing list