diff options
author | Gareth Stockwell <ext-gareth.stockwell@nokia.com> | 2010-02-16 13:17:34 (GMT) |
---|---|---|
committer | Gareth Stockwell <ext-gareth.stockwell@nokia.com> | 2010-02-16 17:57:41 (GMT) |
commit | 84f129eb53532c85f4803e97837e61cea20469d1 (patch) | |
tree | ac482c3655e190ec7f5f6b0b4fe0d5dbfddc231c /examples/multimedia | |
parent | d67acf30db19288928e31b569c373c0d4a369628 (diff) | |
download | Qt-84f129eb53532c85f4803e97837e61cea20469d1.zip Qt-84f129eb53532c85f4803e97837e61cea20469d1.tar.gz Qt-84f129eb53532c85f4803e97837e61cea20469d1.tar.bz2 |
Cleaned up memory management in audio output example
Reviewed-by: trustme
Diffstat (limited to 'examples/multimedia')
-rw-r--r-- | examples/multimedia/audiooutput/audiooutput.cpp | 64 | ||||
-rw-r--r-- | examples/multimedia/audiooutput/audiooutput.h | 35 |
2 files changed, 61 insertions, 38 deletions
diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index ce26664..5e90b41 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -51,6 +51,7 @@ #define SECONDS 1 #define FREQ 600 #define SYSTEM_FREQ 44100 +const int BufferSize = 32768; Generator::Generator(const QAudioFormat &format, qint64 durationUs, @@ -149,9 +150,23 @@ qint64 Generator::bytesAvailable() const } AudioTest::AudioTest() + : m_timer(new QTimer(this)) + , m_modeButton(0) + , m_suspendResumeButton(0) + , m_deviceBox(0) + , m_generator(0) + , m_audioOutput(0) + , m_output(0) + , m_buffer(BufferSize, 0) { - QWidget *window = new QWidget; - QVBoxLayout* layout = new QVBoxLayout; + initializeWindow(); + initializeAudio(); +} + +void AudioTest::initializeWindow() +{ + QScopedPointer<QWidget> window(new QWidget); + QScopedPointer<QVBoxLayout> layout(new QVBoxLayout); m_deviceBox = new QComboBox(this); foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) @@ -169,16 +184,19 @@ AudioTest::AudioTest() connect(m_suspendResumeButton,SIGNAL(clicked()),SLOT(togglePlay())); layout->addWidget(m_suspendResumeButton); - window->setLayout(layout); - setCentralWidget(window); - window->show(); + window->setLayout(layout.data()); + layout.take(); // ownership transferred - m_buffer = new char[BUFFER_SIZE]; + setCentralWidget(window.data()); + QWidget *const windowPtr = window.take(); // ownership transferred + windowPtr->show(); +} - m_pullMode = true; +void AudioTest::initializeAudio() +{ + connect(m_timer, SIGNAL(timeout()), SLOT(writeMore())); - m_timer = new QTimer(this); - connect(m_timer,SIGNAL(timeout()),SLOT(writeMore())); + m_pullMode = true; m_format.setFrequency(SYSTEM_FREQ); m_format.setChannels(1); @@ -194,18 +212,24 @@ AudioTest::AudioTest() } m_generator = new Generator(m_format, SECONDS*1000000, FREQ, this); - m_generator->start(); - m_audioOutput = new QAudioOutput(m_format,this); - connect(m_audioOutput,SIGNAL(notify()),SLOT(status())); - connect(m_audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State))); + createAudioOutput(); +} +void AudioTest::createAudioOutput() +{ + delete m_audioOutput; + m_audioOutput = 0; + m_audioOutput = new QAudioOutput(m_format, this); + connect(m_audioOutput, SIGNAL(notify()), SLOT(status())); + connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); + m_generator->start(); m_audioOutput->start(m_generator); } AudioTest::~AudioTest() { - delete [] m_buffer; + } void AudioTest::deviceChanged(int idx) @@ -214,14 +238,8 @@ void AudioTest::deviceChanged(int idx) m_generator->stop(); m_audioOutput->stop(); m_audioOutput->disconnect(this); - delete m_audioOutput; - m_device = m_deviceBox->itemData(idx).value<QAudioDeviceInfo>(); - m_audioOutput = new QAudioOutput(m_device,m_format,this); - connect(m_audioOutput,SIGNAL(notify()),SLOT(status())); - connect(m_audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State))); - m_generator->start(); - m_audioOutput->start(m_generator); + createAudioOutput(); } void AudioTest::status() @@ -242,9 +260,9 @@ void AudioTest::writeMore() int chunks = m_audioOutput->bytesFree()/m_audioOutput->periodSize(); while(chunks) { - l = m_generator->read(m_buffer,m_audioOutput->periodSize()); + l = m_generator->read(m_buffer.data(), m_audioOutput->periodSize()); if (l > 0) - out = m_output->write(m_buffer,l); + out = m_output->write(m_buffer.data(), l); if (l != m_audioOutput->periodSize()) break; chunks--; diff --git a/examples/multimedia/audiooutput/audiooutput.h b/examples/multimedia/audiooutput/audiooutput.h index ac24a61..5bf8091 100644 --- a/examples/multimedia/audiooutput/audiooutput.h +++ b/examples/multimedia/audiooutput/audiooutput.h @@ -41,8 +41,6 @@ #include <math.h> -#define BUFFER_SIZE 32768 - #include <QObject> #include <QMainWindow> #include <QIODevice> @@ -83,19 +81,26 @@ public: ~AudioTest(); private: - QAudioDeviceInfo m_device; - Generator* m_generator; - QAudioOutput* m_audioOutput; - QIODevice* m_output; - QTimer* m_timer; - QAudioFormat m_format; - - bool m_pullMode; - char* m_buffer; - - QPushButton* m_modeButton; - QPushButton* m_suspendResumeButton; - QComboBox* m_deviceBox; + void initializeWindow(); + void initializeAudio(); + void createAudioOutput(); + +private: + QTimer* m_timer; + + // Owned by layout + QPushButton* m_modeButton; + QPushButton* m_suspendResumeButton; + QComboBox* m_deviceBox; + + QAudioDeviceInfo m_device; + Generator* m_generator; + QAudioOutput* m_audioOutput; + QIODevice* m_output; // not owned + QAudioFormat m_format; + + bool m_pullMode; + QByteArray m_buffer; private slots: void status(); |