diff options
author | Gareth Stockwell <ext-gareth.stockwell@nokia.com> | 2010-02-16 17:29:35 (GMT) |
---|---|---|
committer | Gareth Stockwell <ext-gareth.stockwell@nokia.com> | 2010-02-16 18:18:23 (GMT) |
commit | 77818e65f47fe211f328649085a297c9d2baeb45 (patch) | |
tree | 7a8709b0bc4f76154adb439395ca6bb33c94fefd /examples/multimedia | |
parent | fb829969ce3f406fcb927d8d31a84c5fb8f56d59 (diff) | |
download | Qt-77818e65f47fe211f328649085a297c9d2baeb45.zip Qt-77818e65f47fe211f328649085a297c9d2baeb45.tar.gz Qt-77818e65f47fe211f328649085a297c9d2baeb45.tar.bz2 |
Cleaned up memory management in audio input example
Reviewed-by: trustme
Diffstat (limited to 'examples/multimedia')
-rw-r--r-- | examples/multimedia/audioinput/audioinput.cpp | 69 | ||||
-rw-r--r-- | examples/multimedia/audioinput/audioinput.h | 21 |
2 files changed, 54 insertions, 36 deletions
diff --git a/examples/multimedia/audioinput/audioinput.cpp b/examples/multimedia/audioinput/audioinput.cpp index e18e3a7..48763c9 100644 --- a/examples/multimedia/audioinput/audioinput.cpp +++ b/examples/multimedia/audioinput/audioinput.cpp @@ -53,7 +53,7 @@ #include "audioinput.h" -#define BUFFER_SIZE 4096 +const int BufferSize = 4096; AudioInfo::AudioInfo(const QAudioFormat &format, QObject *parent) : QIODevice(parent) @@ -199,11 +199,28 @@ void RenderArea::setLevel(qreal value) InputTest::InputTest() + : m_canvas(0) + , m_modeButton(0) + , m_suspendResumeButton(0) + , m_deviceBox(0) + , m_audioInfo(0) + , m_audioInput(0) + , m_input(0) + , m_pullMode(false) + , m_buffer(BufferSize, 0) { - QWidget *window = new QWidget; - QVBoxLayout* layout = new QVBoxLayout; + initializeWindow(); + initializeAudio(); +} + +InputTest::~InputTest() {} - m_canvas = new RenderArea; +void InputTest::initializeWindow() +{ + QScopedPointer<QWidget> window(new QWidget); + QScopedPointer<QVBoxLayout> layout(new QVBoxLayout); + + m_canvas = new RenderArea(this); layout->addWidget(m_canvas); m_deviceBox = new QComboBox(this); @@ -224,12 +241,16 @@ InputTest::InputTest() connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspend())); 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(); +} +void InputTest::initializeAudio() +{ m_pullMode = true; m_format.setFrequency(8000); @@ -245,25 +266,21 @@ InputTest::InputTest() m_format = info.nearestFormat(m_format); } - if(m_format.sampleSize() != 16) { - qWarning()<<"audio device doesn't support 16 bit samples, example cannot run"; - m_audioInput = 0; - m_modeButton->setDisabled(true); - m_suspendResumeButton->setDisabled(true); - return; - } + m_audioInfo = new AudioInfo(m_format, this); + connect(m_audioInfo, SIGNAL(update()), SLOT(refreshDisplay())); + + createAudioInput(); +} - m_audioInput = new QAudioInput(m_format,this); +void InputTest::createAudioInput() +{ + m_audioInput = new QAudioInput(m_device, m_format, this); connect(m_audioInput, SIGNAL(notify()), SLOT(status())); connect(m_audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); - m_audioInfo = new AudioInfo(m_format, this); - connect(m_audioInfo, SIGNAL(update()), SLOT(refreshDisplay())); m_audioInfo->start(); m_audioInput->start(m_audioInfo); } -InputTest::~InputTest() {} - void InputTest::status() { qWarning()<<"bytesReady = "<<m_audioInput->bytesReady()<<" bytes, elapsedUSecs = "<<m_audioInput->elapsedUSecs()<<", processedUSecs = "<<m_audioInput->processedUSecs(); @@ -276,9 +293,9 @@ void InputTest::readMore() qint64 len = m_audioInput->bytesReady(); if(len > 4096) len = 4096; - qint64 l = m_input->read(m_buffer,len); + qint64 l = m_input->read(m_buffer.data(), len); if(l > 0) { - m_audioInfo->write(m_buffer,l); + m_audioInfo->write(m_buffer.constData(), l); } } @@ -339,10 +356,6 @@ void InputTest::deviceChanged(int index) m_audioInput->disconnect(this); delete m_audioInput; - m_device = m_deviceBox->itemData(idx).value<QAudioDeviceInfo>(); - m_audioInput = new QAudioInput(m_device, m_format, this); - connect(m_audioInput, SIGNAL(notify()), SLOT(status())); - connect(m_audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); - m_audioInfo->start(); - m_audioInput->start(m_audioInfo); + m_device = m_deviceBox->itemData(index).value<QAudioDeviceInfo>(); + createAudioInput(); } diff --git a/examples/multimedia/audioinput/audioinput.h b/examples/multimedia/audioinput/audioinput.h index b189e84..a32a374 100644 --- a/examples/multimedia/audioinput/audioinput.h +++ b/examples/multimedia/audioinput/audioinput.h @@ -45,6 +45,7 @@ #include <QMainWindow> #include <QPushButton> #include <QComboBox> +#include <QByteArray> #include <qaudioinput.h> @@ -97,6 +98,11 @@ public: InputTest(); ~InputTest(); +private: + void initializeWindow(); + void initializeAudio(); + void createAudioInput(); + private slots: void refreshDisplay(); void status(); @@ -107,19 +113,18 @@ private slots: void deviceChanged(int index); private: + // Owned by layout + RenderArea *m_canvas; + QPushButton *m_modeButton; + QPushButton *m_suspendResumeButton; + QComboBox *m_deviceBox; + AudioInfo *m_audioInfo; QAudioDeviceInfo m_device; QAudioFormat m_format; QAudioInput *m_audioInput; QIODevice *m_input; - RenderArea *m_canvas; - bool m_pullMode; - - QPushButton *m_modeButton; - QPushButton *m_suspendResumeButton; - QComboBox *m_deviceBox; - - char *m_buffer; + QByteArray m_buffer; }; |