From fdd305d4186919c9f75330b1dfd8aaed520d8430 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 16 Feb 2010 15:58:10 +0200 Subject: Only use unix-like tools when not building for Symbian in Windows. Having sh.exe but not the other unix-like basic tools in the path made distclean break in Windows. Task-number: QTBUG-7883 Reviewed-by: axis --- mkspecs/common/symbian/symbian.conf | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mkspecs/common/symbian/symbian.conf b/mkspecs/common/symbian/symbian.conf index 6113746..7b2ee91 100644 --- a/mkspecs/common/symbian/symbian.conf +++ b/mkspecs/common/symbian/symbian.conf @@ -75,15 +75,7 @@ QMAKE_LIBS_COMPAT = QMAKE_LIBS_QT_ENTRY = -llibcrt0.lib QMAKE_LIBS_S60 = -lavkon -!isEmpty(QMAKE_SH) { - QMAKE_COPY = cp - QMAKE_COPY_DIR = cp -r - QMAKE_MOVE = mv - QMAKE_DEL_FILE = rm -f - QMAKE_MKDIR = mkdir - QMAKE_DEL_DIR = rmdir - QMAKE_CHK_DIR_EXISTS = test -d -} else { +contains(QMAKE_HOST.os,Windows) { QMAKE_COPY = copy /y QMAKE_COPY_DIR = xcopy /s /q /y /i QMAKE_MOVE = move @@ -91,6 +83,14 @@ QMAKE_LIBS_S60 = -lavkon QMAKE_MKDIR = mkdir QMAKE_DEL_DIR = rmdir QMAKE_CHK_DIR_EXISTS = if not exist +} else { + QMAKE_COPY = cp + QMAKE_COPY_DIR = cp -r + QMAKE_MOVE = mv + QMAKE_DEL_FILE = rm -f + QMAKE_MKDIR = mkdir + QMAKE_DEL_DIR = rmdir + QMAKE_CHK_DIR_EXISTS = test -d } QMAKE_MOC = $$[QT_INSTALL_BINS]$${DIR_SEPARATOR}moc.exe -- cgit v0.12 From 6c67fef3218ec5361b3f9a30fc53c529295da9e4 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 11:25:41 +0000 Subject: Added missing override in audio output example Because the QIODevice-derived class Generator owns its own internal buffer, it must override bytesAvailable(). Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 5 +++++ examples/multimedia/audiooutput/audiooutput.h | 1 + 2 files changed, 6 insertions(+) diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index b44accd..9a9af86 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -75,6 +75,11 @@ void Generator::start() open(QIODevice::ReadOnly); } +qint64 Generator::bytesAvailable() const +{ + return (SECONDS*SYSTEM_FREQ*2)-pos + QIODevice::bytesAvailable(); +} + void Generator::stop() { close(); diff --git a/examples/multimedia/audiooutput/audiooutput.h b/examples/multimedia/audiooutput/audiooutput.h index 372cf78..eba6446 100644 --- a/examples/multimedia/audiooutput/audiooutput.h +++ b/examples/multimedia/audiooutput/audiooutput.h @@ -72,6 +72,7 @@ public: qint64 readData(char *data, qint64 maxlen); qint64 writeData(const char *data, qint64 len); + qint64 bytesAvailable() const; private: int putShort(char *t, unsigned int value); -- cgit v0.12 From b03306503cc7de9513aa282b55bda40cea00e128 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 11:26:49 +0000 Subject: Update suspend/resume button label in audio output example When the mode is toggled, playback is always started, even if the audio output device was previously suspended. The label on the suspend/resume button must therefore be updated. Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index 9a9af86..d4bb294 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -262,6 +262,8 @@ void AudioTest::toggle() pullMode = true; audioOutput->start(gen); } + + button2->setText("Click To Suspend"); } void AudioTest::togglePlay() -- cgit v0.12 From 53dc6607cf669b977ff40de995bd9b1b8e395b78 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 12:55:23 +0000 Subject: Made data generation in audio output example work for all formats The previous code only worked correctly for mono PCM16 formats. This meant that: a) If the device did not support mono, but did support stereo PCM16, the tone would be played at the wrong pitch. b) If the device did not support little-endian PCM16 at all (admittedly unlikely), the example would not run. Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 128 +++++++++++++----------- examples/multimedia/audiooutput/audiooutput.h | 19 ++-- 2 files changed, 75 insertions(+), 72 deletions(-) diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index d4bb294..d459e50e 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -44,30 +44,27 @@ #include #include +#include +#include #include "audiooutput.h" -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - #define SECONDS 1 #define FREQ 600 #define SYSTEM_FREQ 44100 -Generator::Generator(QObject *parent) - :QIODevice( parent ) +Generator::Generator(const QAudioFormat &format, + qint64 durationUs, + int frequency, + QObject *parent) + : QIODevice(parent) + , pos(0) { - finished = false; - buffer = new char[SECONDS*SYSTEM_FREQ*4+1000]; - t=buffer; - len=fillData(t,FREQ,SECONDS); /* mono FREQHz sine */ - pos = 0; - total = len; + generateData(format, durationUs, frequency); } Generator::~Generator() { - delete [] buffer; + } void Generator::start() @@ -75,54 +72,67 @@ void Generator::start() open(QIODevice::ReadOnly); } -qint64 Generator::bytesAvailable() const -{ - return (SECONDS*SYSTEM_FREQ*2)-pos + QIODevice::bytesAvailable(); -} - void Generator::stop() { + pos = 0; close(); } -int Generator::putShort(char *t, unsigned int value) +void Generator::generateData(const QAudioFormat &format, qint64 durationUs, int frequency) { - *(unsigned char *)(t++)=value&255; - *(unsigned char *)(t)=(value/256)&255; - return 2; -} - -int Generator::fillData(char *start, int frequency, int seconds) -{ - int i, len=0; - int value; - for(i=0; i(buffer.data()); + int sampleIndex = 0; + + while (length) { + const qreal x = qSin(2 * M_PI * frequency * qreal(sampleIndex % format.frequency()) / format.frequency()); + for (int i=0; i((1.0 + x) / 2 * 255); + *reinterpret_cast(ptr) = value; + } else if (format.sampleSize() == 8 && format.sampleType() == QAudioFormat::SignedInt) { + const qint8 value = static_cast(x * 127); + *reinterpret_cast(ptr) = value; + } else if (format.sampleSize() == 16 && format.sampleType() == QAudioFormat::UnSignedInt) { + quint16 value = static_cast((1.0 + x) / 2 * 65535); + if (format.byteOrder() == QAudioFormat::LittleEndian) + qToLittleEndian(value, ptr); + else + qToBigEndian(value, ptr); + } else if (format.sampleSize() == 16 && format.sampleType() == QAudioFormat::SignedInt) { + qint16 value = static_cast(x * 32767); + if (format.byteOrder() == QAudioFormat::LittleEndian) + qToLittleEndian(value, ptr); + else + qToBigEndian(value, ptr); + } + + ptr += channelBytes; + length -= channelBytes; + } + ++sampleIndex; } - return len; } -qint64 Generator::readData(char *data, qint64 maxlen) +qint64 Generator::readData(char *data, qint64 len) { - int len = maxlen; - if (len > 16384) - len = 16384; - - if (len < (SECONDS*SYSTEM_FREQ*2)-pos) { - // Normal - memcpy(data,t+pos,len); - pos+=len; - return len; - } else { - // Whats left and reset to start - qint64 left = (SECONDS*SYSTEM_FREQ*2)-pos; - memcpy(data,t+pos,left); - pos=0; - return left; + qint64 total = 0; + while (len - total) { + const qint64 chunk = qMin((buffer.size() - pos), len - total); + memcpy(data, buffer.constData() + pos, chunk); + pos = (pos + chunk) % buffer.size(); + total += chunk; } + return total; } qint64 Generator::writeData(const char *data, qint64 len) @@ -133,6 +143,11 @@ qint64 Generator::writeData(const char *data, qint64 len) return 0; } +qint64 Generator::bytesAvailable() const +{ + return buffer.size() + QIODevice::bytesAvailable(); +} + AudioTest::AudioTest() { QWidget *window = new QWidget; @@ -160,15 +175,11 @@ AudioTest::AudioTest() buffer = new char[BUFFER_SIZE]; - gen = new Generator(this); - pullMode = true; timer = new QTimer(this); connect(timer,SIGNAL(timeout()),SLOT(writeMore())); - gen->start(); - settings.setFrequency(SYSTEM_FREQ); settings.setChannels(1); settings.setSampleSize(16); @@ -182,13 +193,8 @@ AudioTest::AudioTest() settings = info.nearestFormat(settings); } - if(settings.sampleSize() != 16) { - qWarning()<<"audio device doesn't support 16 bit samples, example cannot run"; - button->setDisabled(true); - button2->setDisabled(true); - audioOutput = 0; - return; - } + gen = new Generator(settings, SECONDS*1000000, FREQ, this); + gen->start(); audioOutput = new QAudioOutput(settings,this); connect(audioOutput,SIGNAL(notify()),SLOT(status())); diff --git a/examples/multimedia/audiooutput/audiooutput.h b/examples/multimedia/audiooutput/audiooutput.h index eba6446..2c62d84 100644 --- a/examples/multimedia/audiooutput/audiooutput.h +++ b/examples/multimedia/audiooutput/audiooutput.h @@ -49,6 +49,7 @@ #include #include #include +#include #include @@ -56,27 +57,22 @@ class Generator : public QIODevice { Q_OBJECT public: - Generator(QObject *parent); + Generator(const QAudioFormat &format, qint64 durationUs, int frequency, QObject *parent); ~Generator(); void start(); void stop(); - char *t; - int len; - int pos; - int total; - char *buffer; - bool finished; - int chunk_size; - qint64 readData(char *data, qint64 maxlen); qint64 writeData(const char *data, qint64 len); qint64 bytesAvailable() const; private: - int putShort(char *t, unsigned int value); - int fillData(char *start, int frequency, int seconds); + void generateData(const QAudioFormat &format, qint64 durationUs, int frequency); + +private: + qint64 pos; + QByteArray buffer; }; class AudioTest : public QMainWindow @@ -86,6 +82,7 @@ public: AudioTest(); ~AudioTest(); +private: QAudioDeviceInfo device; Generator* gen; QAudioOutput* audioOutput; -- cgit v0.12 From d67acf30db19288928e31b569c373c0d4a369628 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 13:03:46 +0000 Subject: Changed variable names in audio output example to match Qt code style Also changed some names to better convey their meanings, e.g. button -> m_modeButton button2 -> m_suspendResumeButton Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 158 ++++++++++++------------ examples/multimedia/audiooutput/audiooutput.h | 26 ++-- 2 files changed, 92 insertions(+), 92 deletions(-) diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index d459e50e..ce26664 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -57,7 +57,7 @@ Generator::Generator(const QAudioFormat &format, int frequency, QObject *parent) : QIODevice(parent) - , pos(0) + , m_pos(0) { generateData(format, durationUs, frequency); } @@ -74,7 +74,7 @@ void Generator::start() void Generator::stop() { - pos = 0; + m_pos = 0; close(); } @@ -89,8 +89,8 @@ void Generator::generateData(const QAudioFormat &format, qint64 durationUs, int Q_ASSERT(length % sampleBytes == 0); Q_UNUSED(sampleBytes) // suppress warning in release builds - buffer.resize(length); - unsigned char *ptr = reinterpret_cast(buffer.data()); + m_buffer.resize(length); + unsigned char *ptr = reinterpret_cast(m_buffer.data()); int sampleIndex = 0; while (length) { @@ -127,9 +127,9 @@ qint64 Generator::readData(char *data, qint64 len) { qint64 total = 0; while (len - total) { - const qint64 chunk = qMin((buffer.size() - pos), len - total); - memcpy(data, buffer.constData() + pos, chunk); - pos = (pos + chunk) % buffer.size(); + const qint64 chunk = qMin((m_buffer.size() - m_pos), len - total); + memcpy(data, m_buffer.constData() + m_pos, chunk); + m_pos = (m_pos + chunk) % m_buffer.size(); total += chunk; } return total; @@ -145,7 +145,7 @@ qint64 Generator::writeData(const char *data, qint64 len) qint64 Generator::bytesAvailable() const { - return buffer.size() + QIODevice::bytesAvailable(); + return m_buffer.size() + QIODevice::bytesAvailable(); } AudioTest::AudioTest() @@ -153,99 +153,99 @@ AudioTest::AudioTest() QWidget *window = new QWidget; QVBoxLayout* layout = new QVBoxLayout; - deviceBox = new QComboBox(this); + m_deviceBox = new QComboBox(this); foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) - deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo)); - connect(deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int))); - layout->addWidget(deviceBox); + m_deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo)); + connect(m_deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int))); + layout->addWidget(m_deviceBox); - button = new QPushButton(this); - button->setText(tr("Click for Push Mode")); - connect(button,SIGNAL(clicked()),SLOT(toggle())); - layout->addWidget(button); + m_modeButton = new QPushButton(this); + m_modeButton->setText(tr("Click for Push Mode")); + connect(m_modeButton,SIGNAL(clicked()),SLOT(toggle())); + layout->addWidget(m_modeButton); - button2 = new QPushButton(this); - button2->setText(tr("Click To Suspend")); - connect(button2,SIGNAL(clicked()),SLOT(togglePlay())); - layout->addWidget(button2); + m_suspendResumeButton = new QPushButton(this); + m_suspendResumeButton->setText(tr("Click To Suspend")); + connect(m_suspendResumeButton,SIGNAL(clicked()),SLOT(togglePlay())); + layout->addWidget(m_suspendResumeButton); window->setLayout(layout); setCentralWidget(window); window->show(); - buffer = new char[BUFFER_SIZE]; + m_buffer = new char[BUFFER_SIZE]; - pullMode = true; + m_pullMode = true; - timer = new QTimer(this); - connect(timer,SIGNAL(timeout()),SLOT(writeMore())); + m_timer = new QTimer(this); + connect(m_timer,SIGNAL(timeout()),SLOT(writeMore())); - settings.setFrequency(SYSTEM_FREQ); - settings.setChannels(1); - settings.setSampleSize(16); - settings.setCodec("audio/pcm"); - settings.setByteOrder(QAudioFormat::LittleEndian); - settings.setSampleType(QAudioFormat::SignedInt); + m_format.setFrequency(SYSTEM_FREQ); + m_format.setChannels(1); + m_format.setSampleSize(16); + m_format.setCodec("audio/pcm"); + m_format.setByteOrder(QAudioFormat::LittleEndian); + m_format.setSampleType(QAudioFormat::SignedInt); QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); - if (!info.isFormatSupported(settings)) { + if (!info.isFormatSupported(m_format)) { qWarning()<<"default format not supported try to use nearest"; - settings = info.nearestFormat(settings); + m_format = info.nearestFormat(m_format); } - gen = new Generator(settings, SECONDS*1000000, FREQ, this); - gen->start(); + m_generator = new Generator(m_format, SECONDS*1000000, FREQ, this); + m_generator->start(); - audioOutput = new QAudioOutput(settings,this); - connect(audioOutput,SIGNAL(notify()),SLOT(status())); - connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State))); + 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))); - audioOutput->start(gen); + m_audioOutput->start(m_generator); } AudioTest::~AudioTest() { - delete [] buffer; + delete [] m_buffer; } void AudioTest::deviceChanged(int idx) { - timer->stop(); - gen->stop(); - audioOutput->stop(); - audioOutput->disconnect(this); - delete audioOutput; - - device = deviceBox->itemData(idx).value(); - audioOutput = new QAudioOutput(device,settings,this); - connect(audioOutput,SIGNAL(notify()),SLOT(status())); - connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State))); - gen->start(); - audioOutput->start(gen); + m_timer->stop(); + m_generator->stop(); + m_audioOutput->stop(); + m_audioOutput->disconnect(this); + delete m_audioOutput; + + m_device = m_deviceBox->itemData(idx).value(); + 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); } void AudioTest::status() { - qWarning() << "byteFree = " << audioOutput->bytesFree() << " bytes, elapsedUSecs = " << audioOutput->elapsedUSecs() << ", processedUSecs = " << audioOutput->processedUSecs(); + qWarning() << "byteFree = " << m_audioOutput->bytesFree() << " bytes, elapsedUSecs = " << m_audioOutput->elapsedUSecs() << ", processedUSecs = " << m_audioOutput->processedUSecs(); } void AudioTest::writeMore() { - if (!audioOutput) + if (!m_audioOutput) return; - if (audioOutput->state() == QAudio::StoppedState) + if (m_audioOutput->state() == QAudio::StoppedState) return; int l; int out; - int chunks = audioOutput->bytesFree()/audioOutput->periodSize(); + int chunks = m_audioOutput->bytesFree()/m_audioOutput->periodSize(); while(chunks) { - l = gen->read(buffer,audioOutput->periodSize()); + l = m_generator->read(m_buffer,m_audioOutput->periodSize()); if (l > 0) - out = output->write(buffer,l); - if (l != audioOutput->periodSize()) + out = m_output->write(m_buffer,l); + if (l != m_audioOutput->periodSize()) break; chunks--; } @@ -255,39 +255,39 @@ void AudioTest::toggle() { // Change between pull and push modes - timer->stop(); - audioOutput->stop(); + m_timer->stop(); + m_audioOutput->stop(); - if (pullMode) { - button->setText("Click for Pull Mode"); - output = audioOutput->start(); - pullMode = false; - timer->start(20); + if (m_pullMode) { + m_modeButton->setText("Click for Pull Mode"); + m_output = m_audioOutput->start(); + m_pullMode = false; + m_timer->start(20); } else { - button->setText("Click for Push Mode"); - pullMode = true; - audioOutput->start(gen); + m_modeButton->setText("Click for Push Mode"); + m_pullMode = true; + m_audioOutput->start(m_generator); } - button2->setText("Click To Suspend"); + m_suspendResumeButton->setText("Click To Suspend"); } void AudioTest::togglePlay() { // toggle suspend/resume - if (audioOutput->state() == QAudio::SuspendedState) { + if (m_audioOutput->state() == QAudio::SuspendedState) { qWarning() << "status: Suspended, resume()"; - audioOutput->resume(); - button2->setText("Click To Suspend"); - } else if (audioOutput->state() == QAudio::ActiveState) { + m_audioOutput->resume(); + m_suspendResumeButton->setText("Click To Suspend"); + } else if (m_audioOutput->state() == QAudio::ActiveState) { qWarning() << "status: Active, suspend()"; - audioOutput->suspend(); - button2->setText("Click To Resume"); - } else if (audioOutput->state() == QAudio::StoppedState) { + m_audioOutput->suspend(); + m_suspendResumeButton->setText("Click To Resume"); + } else if (m_audioOutput->state() == QAudio::StoppedState) { qWarning() << "status: Stopped, resume()"; - audioOutput->resume(); - button2->setText("Click To Suspend"); - } else if (audioOutput->state() == QAudio::IdleState) { + m_audioOutput->resume(); + m_suspendResumeButton->setText("Click To Suspend"); + } else if (m_audioOutput->state() == QAudio::IdleState) { qWarning() << "status: IdleState"; } } diff --git a/examples/multimedia/audiooutput/audiooutput.h b/examples/multimedia/audiooutput/audiooutput.h index 2c62d84..ac24a61 100644 --- a/examples/multimedia/audiooutput/audiooutput.h +++ b/examples/multimedia/audiooutput/audiooutput.h @@ -71,8 +71,8 @@ private: void generateData(const QAudioFormat &format, qint64 durationUs, int frequency); private: - qint64 pos; - QByteArray buffer; + qint64 m_pos; + QByteArray m_buffer; }; class AudioTest : public QMainWindow @@ -83,19 +83,19 @@ public: ~AudioTest(); private: - QAudioDeviceInfo device; - Generator* gen; - QAudioOutput* audioOutput; - QIODevice* output; - QTimer* timer; - QAudioFormat settings; + QAudioDeviceInfo m_device; + Generator* m_generator; + QAudioOutput* m_audioOutput; + QIODevice* m_output; + QTimer* m_timer; + QAudioFormat m_format; - bool pullMode; - char* buffer; + bool m_pullMode; + char* m_buffer; - QPushButton* button; - QPushButton* button2; - QComboBox* deviceBox; + QPushButton* m_modeButton; + QPushButton* m_suspendResumeButton; + QComboBox* m_deviceBox; private slots: void status(); -- cgit v0.12 From 84f129eb53532c85f4803e97837e61cea20469d1 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 13:17:34 +0000 Subject: Cleaned up memory management in audio output example Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 64 ++++++++++++++++--------- 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 window(new QWidget); + QScopedPointer 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(); - 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 -#define BUFFER_SIZE 32768 - #include #include #include @@ -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(); -- cgit v0.12 From 658a559c918bd16b4e3f5af3e25242086a781220 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 13:24:14 +0000 Subject: Removed redundant strings from audio output example Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 32 +++++++++++++++---------- examples/multimedia/audiooutput/audiooutput.h | 5 ++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index 5e90b41..f60d7b6 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -48,11 +48,17 @@ #include #include "audiooutput.h" -#define SECONDS 1 -#define FREQ 600 -#define SYSTEM_FREQ 44100 +const QString AudioTest::PushModeLabel(tr("Enable push mode")); +const QString AudioTest::PullModeLabel(tr("Enable pull mode")); +const QString AudioTest::SuspendLabel(tr("Suspend playback")); +const QString AudioTest::ResumeLabel(tr("Resume playback")); + +const int DurationSeconds = 1; +const int ToneFrequencyHz = 600; +const int DataFrequencyHz = 44100; const int BufferSize = 32768; + Generator::Generator(const QAudioFormat &format, qint64 durationUs, int frequency, @@ -175,12 +181,12 @@ void AudioTest::initializeWindow() layout->addWidget(m_deviceBox); m_modeButton = new QPushButton(this); - m_modeButton->setText(tr("Click for Push Mode")); + m_modeButton->setText(PushModeLabel); connect(m_modeButton,SIGNAL(clicked()),SLOT(toggle())); layout->addWidget(m_modeButton); m_suspendResumeButton = new QPushButton(this); - m_suspendResumeButton->setText(tr("Click To Suspend")); + m_suspendResumeButton->setText(SuspendLabel); connect(m_suspendResumeButton,SIGNAL(clicked()),SLOT(togglePlay())); layout->addWidget(m_suspendResumeButton); @@ -198,7 +204,7 @@ void AudioTest::initializeAudio() m_pullMode = true; - m_format.setFrequency(SYSTEM_FREQ); + m_format.setFrequency(DataFrequencyHz); m_format.setChannels(1); m_format.setSampleSize(16); m_format.setCodec("audio/pcm"); @@ -211,7 +217,7 @@ void AudioTest::initializeAudio() m_format = info.nearestFormat(m_format); } - m_generator = new Generator(m_format, SECONDS*1000000, FREQ, this); + m_generator = new Generator(m_format, DurationSeconds*1000000, ToneFrequencyHz, this); createAudioOutput(); } @@ -277,17 +283,17 @@ void AudioTest::toggle() m_audioOutput->stop(); if (m_pullMode) { - m_modeButton->setText("Click for Pull Mode"); + m_modeButton->setText(PullModeLabel); m_output = m_audioOutput->start(); m_pullMode = false; m_timer->start(20); } else { - m_modeButton->setText("Click for Push Mode"); + m_modeButton->setText(PushModeLabel); m_pullMode = true; m_audioOutput->start(m_generator); } - m_suspendResumeButton->setText("Click To Suspend"); + m_suspendResumeButton->setText(SuspendLabel); } void AudioTest::togglePlay() @@ -296,15 +302,15 @@ void AudioTest::togglePlay() if (m_audioOutput->state() == QAudio::SuspendedState) { qWarning() << "status: Suspended, resume()"; m_audioOutput->resume(); - m_suspendResumeButton->setText("Click To Suspend"); + m_suspendResumeButton->setText(SuspendLabel); } else if (m_audioOutput->state() == QAudio::ActiveState) { qWarning() << "status: Active, suspend()"; m_audioOutput->suspend(); - m_suspendResumeButton->setText("Click To Resume"); + m_suspendResumeButton->setText(ResumeLabel); } else if (m_audioOutput->state() == QAudio::StoppedState) { qWarning() << "status: Stopped, resume()"; m_audioOutput->resume(); - m_suspendResumeButton->setText("Click To Suspend"); + m_suspendResumeButton->setText(SuspendLabel); } else if (m_audioOutput->state() == QAudio::IdleState) { qWarning() << "status: IdleState"; } diff --git a/examples/multimedia/audiooutput/audiooutput.h b/examples/multimedia/audiooutput/audiooutput.h index 5bf8091..ea47e8b 100644 --- a/examples/multimedia/audiooutput/audiooutput.h +++ b/examples/multimedia/audiooutput/audiooutput.h @@ -102,6 +102,11 @@ private: bool m_pullMode; QByteArray m_buffer; + static const QString PushModeLabel; + static const QString PullModeLabel; + static const QString SuspendLabel; + static const QString ResumeLabel; + private slots: void status(); void writeMore(); -- cgit v0.12 From 0ce346691dbf620db04a99699194d490f324b1be Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 13:38:50 +0000 Subject: Tidied up debug output in audio output example Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 12 +++++++----- examples/multimedia/audiooutput/audiooutput.h | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index f60d7b6..04e1923 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -213,7 +213,7 @@ void AudioTest::initializeAudio() QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); if (!info.isFormatSupported(m_format)) { - qWarning()<<"default format not supported try to use nearest"; + qWarning() << "Default format not supported - trying to use nearest"; m_format = info.nearestFormat(m_format); } @@ -238,19 +238,21 @@ AudioTest::~AudioTest() } -void AudioTest::deviceChanged(int idx) +void AudioTest::deviceChanged(int index) { m_timer->stop(); m_generator->stop(); m_audioOutput->stop(); m_audioOutput->disconnect(this); - m_device = m_deviceBox->itemData(idx).value(); + m_device = m_deviceBox->itemData(index).value(); createAudioOutput(); } void AudioTest::status() { - qWarning() << "byteFree = " << m_audioOutput->bytesFree() << " bytes, elapsedUSecs = " << m_audioOutput->elapsedUSecs() << ", processedUSecs = " << m_audioOutput->processedUSecs(); + qWarning() << "bytesFree = " << m_audioOutput->bytesFree() + << ", " << "elapsedUSecs = " << m_audioOutput->elapsedUSecs() + << ", " << "processedUSecs = " << m_audioOutput->processedUSecs(); } void AudioTest::writeMore() @@ -318,5 +320,5 @@ void AudioTest::togglePlay() void AudioTest::state(QAudio::State state) { - qWarning() << " state=" << state; + qWarning() << "state = " << state; } diff --git a/examples/multimedia/audiooutput/audiooutput.h b/examples/multimedia/audiooutput/audiooutput.h index ea47e8b..1880d8d 100644 --- a/examples/multimedia/audiooutput/audiooutput.h +++ b/examples/multimedia/audiooutput/audiooutput.h @@ -112,7 +112,7 @@ private slots: void writeMore(); void toggle(); void togglePlay(); - void state(QAudio::State s); - void deviceChanged(int idx); + void state(QAudio::State state); + void deviceChanged(int index); }; -- cgit v0.12 From 3b2d9d0644a57ac377a7bcbfd40f8bd6da32cd1f Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 13:41:18 +0000 Subject: Cleaned up implementation of audio output example Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 27 +++++++++---------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index 04e1923..0c44b4f 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -257,23 +257,16 @@ void AudioTest::status() void AudioTest::writeMore() { - if (!m_audioOutput) - return; - - if (m_audioOutput->state() == QAudio::StoppedState) - return; - - int l; - int out; - - int chunks = m_audioOutput->bytesFree()/m_audioOutput->periodSize(); - while(chunks) { - l = m_generator->read(m_buffer.data(), m_audioOutput->periodSize()); - if (l > 0) - out = m_output->write(m_buffer.data(), l); - if (l != m_audioOutput->periodSize()) - break; - chunks--; + if (m_audioOutput && m_audioOutput->state() != QAudio::StoppedState) { + int chunks = m_audioOutput->bytesFree()/m_audioOutput->periodSize(); + while (chunks) { + const qint64 len = m_generator->read(m_buffer.data(), m_audioOutput->periodSize()); + if (len) + m_output->write(m_buffer.data(), len); + if (len != m_audioOutput->periodSize()) + break; + --chunks; + } } } -- cgit v0.12 From 14c280f706c5612f6e01f5d6677cc85c88df7eb7 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 13:45:40 +0000 Subject: Changed function names in audio output example to clarify meaning Reviewed-by: trustme --- examples/multimedia/audiooutput/audiooutput.cpp | 31 +++++++++++-------------- examples/multimedia/audiooutput/audiooutput.h | 12 +++++----- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/examples/multimedia/audiooutput/audiooutput.cpp b/examples/multimedia/audiooutput/audiooutput.cpp index 0c44b4f..4866d36 100644 --- a/examples/multimedia/audiooutput/audiooutput.cpp +++ b/examples/multimedia/audiooutput/audiooutput.cpp @@ -156,7 +156,7 @@ qint64 Generator::bytesAvailable() const } AudioTest::AudioTest() - : m_timer(new QTimer(this)) + : m_pullTimer(new QTimer(this)) , m_modeButton(0) , m_suspendResumeButton(0) , m_deviceBox(0) @@ -182,12 +182,12 @@ void AudioTest::initializeWindow() m_modeButton = new QPushButton(this); m_modeButton->setText(PushModeLabel); - connect(m_modeButton,SIGNAL(clicked()),SLOT(toggle())); + connect(m_modeButton, SIGNAL(clicked()), SLOT(toggleMode())); layout->addWidget(m_modeButton); m_suspendResumeButton = new QPushButton(this); m_suspendResumeButton->setText(SuspendLabel); - connect(m_suspendResumeButton,SIGNAL(clicked()),SLOT(togglePlay())); + connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspendResume())); layout->addWidget(m_suspendResumeButton); window->setLayout(layout.data()); @@ -200,7 +200,7 @@ void AudioTest::initializeWindow() void AudioTest::initializeAudio() { - connect(m_timer, SIGNAL(timeout()), SLOT(writeMore())); + connect(m_pullTimer, SIGNAL(timeout()), SLOT(pullTimerExpired())); m_pullMode = true; @@ -227,8 +227,8 @@ 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))); + connect(m_audioOutput, SIGNAL(notify()), SLOT(notified())); + connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State))); m_generator->start(); m_audioOutput->start(m_generator); } @@ -240,7 +240,7 @@ AudioTest::~AudioTest() void AudioTest::deviceChanged(int index) { - m_timer->stop(); + m_pullTimer->stop(); m_generator->stop(); m_audioOutput->stop(); m_audioOutput->disconnect(this); @@ -248,14 +248,14 @@ void AudioTest::deviceChanged(int index) createAudioOutput(); } -void AudioTest::status() +void AudioTest::notified() { qWarning() << "bytesFree = " << m_audioOutput->bytesFree() << ", " << "elapsedUSecs = " << m_audioOutput->elapsedUSecs() << ", " << "processedUSecs = " << m_audioOutput->processedUSecs(); } -void AudioTest::writeMore() +void AudioTest::pullTimerExpired() { if (m_audioOutput && m_audioOutput->state() != QAudio::StoppedState) { int chunks = m_audioOutput->bytesFree()/m_audioOutput->periodSize(); @@ -270,18 +270,16 @@ void AudioTest::writeMore() } } -void AudioTest::toggle() +void AudioTest::toggleMode() { - // Change between pull and push modes - - m_timer->stop(); + m_pullTimer->stop(); m_audioOutput->stop(); if (m_pullMode) { m_modeButton->setText(PullModeLabel); m_output = m_audioOutput->start(); m_pullMode = false; - m_timer->start(20); + m_pullTimer->start(20); } else { m_modeButton->setText(PushModeLabel); m_pullMode = true; @@ -291,9 +289,8 @@ void AudioTest::toggle() m_suspendResumeButton->setText(SuspendLabel); } -void AudioTest::togglePlay() +void AudioTest::toggleSuspendResume() { - // toggle suspend/resume if (m_audioOutput->state() == QAudio::SuspendedState) { qWarning() << "status: Suspended, resume()"; m_audioOutput->resume(); @@ -311,7 +308,7 @@ void AudioTest::togglePlay() } } -void AudioTest::state(QAudio::State state) +void AudioTest::stateChanged(QAudio::State state) { qWarning() << "state = " << state; } diff --git a/examples/multimedia/audiooutput/audiooutput.h b/examples/multimedia/audiooutput/audiooutput.h index 1880d8d..889482b 100644 --- a/examples/multimedia/audiooutput/audiooutput.h +++ b/examples/multimedia/audiooutput/audiooutput.h @@ -86,7 +86,7 @@ private: void createAudioOutput(); private: - QTimer* m_timer; + QTimer* m_pullTimer; // Owned by layout QPushButton* m_modeButton; @@ -108,11 +108,11 @@ private: static const QString ResumeLabel; private slots: - void status(); - void writeMore(); - void toggle(); - void togglePlay(); - void state(QAudio::State state); + void notified(); + void pullTimerExpired(); + void toggleMode(); + void toggleSuspendResume(); + void stateChanged(QAudio::State state); void deviceChanged(int index); }; -- cgit v0.12 From 34236413600c8142d626c460fd4af97072054fa4 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 15:37:50 +0000 Subject: Update suspend/resume button label in audio input example When the mode is toggled, playback is always started, even if the audio input device was previously suspended. The label on the suspend/resume button must therefore be updated. Reviewed-by: trustme --- examples/multimedia/audioinput/audioinput.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/multimedia/audioinput/audioinput.cpp b/examples/multimedia/audioinput/audioinput.cpp index fbf4dc4..18b5ba1 100644 --- a/examples/multimedia/audioinput/audioinput.cpp +++ b/examples/multimedia/audioinput/audioinput.cpp @@ -263,6 +263,8 @@ void InputTest::toggleMode() pullMode = true; audioInput->start(audioinfo); } + + button2->setText("Click To Suspend"); } void InputTest::toggleSuspend() -- cgit v0.12 From d17060663236c0cf4d3eec79f8f76e30edfca636 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 15:42:55 +0000 Subject: Removed unused variable from audio input example Reviewed-by: trustme --- examples/multimedia/audioinput/audioinput.cpp | 6 ++---- examples/multimedia/audioinput/audioinput.h | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/examples/multimedia/audioinput/audioinput.cpp b/examples/multimedia/audioinput/audioinput.cpp index 18b5ba1..d00dd02 100644 --- a/examples/multimedia/audioinput/audioinput.cpp +++ b/examples/multimedia/audioinput/audioinput.cpp @@ -52,11 +52,9 @@ #define BUFFER_SIZE 4096 -AudioInfo::AudioInfo(QObject *parent, QAudioInput *device) +AudioInfo::AudioInfo(QObject *parent) :QIODevice(parent) { - input = device; - m_maxValue = 0; } @@ -222,7 +220,7 @@ InputTest::InputTest() audioInput = new QAudioInput(format,this); connect(audioInput, SIGNAL(notify()), SLOT(status())); connect(audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); - audioinfo = new AudioInfo(this,audioInput); + audioinfo = new AudioInfo(this); connect(audioinfo, SIGNAL(update()), SLOT(refreshDisplay())); audioinfo->start(); audioInput->start(audioinfo); diff --git a/examples/multimedia/audioinput/audioinput.h b/examples/multimedia/audioinput/audioinput.h index abe2be1..6f6c8eb 100644 --- a/examples/multimedia/audioinput/audioinput.h +++ b/examples/multimedia/audioinput/audioinput.h @@ -52,7 +52,7 @@ class AudioInfo : public QIODevice { Q_OBJECT public: - AudioInfo(QObject *parent, QAudioInput *device); + AudioInfo(QObject *parent); ~AudioInfo(); void start(); @@ -63,8 +63,6 @@ public: qint64 readData(char *data, qint64 maxlen); qint64 writeData(const char *data, qint64 len); - QAudioInput *input; - private: int m_maxValue; -- cgit v0.12 From 479e26ab077a9d43e37b0706db4f5cb61964b327 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 16:43:20 +0000 Subject: Made level calculation in audio input example work for all formats The previous code only worked correctly for little-endian PCM16 formats. If the audio input device does not support such a format, the example previously would not run. Reviewed-by: trustme --- examples/multimedia/audioinput/audioinput.cpp | 110 +++++++++++++++++--------- examples/multimedia/audioinput/audioinput.h | 12 +-- 2 files changed, 80 insertions(+), 42 deletions(-) diff --git a/examples/multimedia/audioinput/audioinput.cpp b/examples/multimedia/audioinput/audioinput.cpp index d00dd02..36fc5ac 100644 --- a/examples/multimedia/audioinput/audioinput.cpp +++ b/examples/multimedia/audioinput/audioinput.cpp @@ -48,14 +48,42 @@ #include #include + +#include + #include "audioinput.h" #define BUFFER_SIZE 4096 -AudioInfo::AudioInfo(QObject *parent) - :QIODevice(parent) +AudioInfo::AudioInfo(const QAudioFormat &format, QObject *parent) + : QIODevice(parent) + , m_format(format) + , m_maxAmplitude(0) + , m_level(0.0) + { - m_maxValue = 0; + switch (m_format.sampleSize()) { + case 8: + switch (m_format.sampleType()) { + case QAudioFormat::UnSignedInt: + m_maxAmplitude = 255; + break; + case QAudioFormat::SignedInt: + m_maxAmplitude = 127; + break; + } + break; + case 16: + switch (m_format.sampleType()) { + case QAudioFormat::UnSignedInt: + m_maxAmplitude = 65535; + break; + case QAudioFormat::SignedInt: + m_maxAmplitude = 32767; + break; + } + break; + } } AudioInfo::~AudioInfo() @@ -82,41 +110,49 @@ qint64 AudioInfo::readData(char *data, qint64 maxlen) qint64 AudioInfo::writeData(const char *data, qint64 len) { - int samples = len/2; // 2 bytes per sample - int maxAmp = 32768; // max for S16 samples - bool clipping = false; - - m_maxValue = 0; - - qint16 *s = (qint16*)data; - - // sample format is S16LE, only! - - for (int i = 0; i < samples; ++i) { - qint16 sample = *s; - s++; - if (abs(sample) > m_maxValue) m_maxValue = abs(sample); + if (m_maxAmplitude) { + Q_ASSERT(m_format.sampleSize() % 8 == 0); + const int channelBytes = m_format.sampleSize() / 8; + const int sampleBytes = m_format.channels() * channelBytes; + Q_ASSERT(len % sampleBytes == 0); + const int numSamples = len / sampleBytes; + + quint16 maxValue = 0; + const unsigned char *ptr = reinterpret_cast(data); + + for (int i = 0; i < numSamples; ++i) { + for(int j = 0; j < m_format.channels(); ++j) { + quint16 value = 0; + + if (m_format.sampleSize() == 8 && m_format.sampleType() == QAudioFormat::UnSignedInt) { + value = *reinterpret_cast(ptr); + } else if (m_format.sampleSize() == 8 && m_format.sampleType() == QAudioFormat::SignedInt) { + value = qAbs(*reinterpret_cast(ptr)); + } else if (m_format.sampleSize() == 16 && m_format.sampleType() == QAudioFormat::UnSignedInt) { + if (m_format.byteOrder() == QAudioFormat::LittleEndian) + value = qFromLittleEndian(ptr); + else + value = qFromBigEndian(ptr); + } else if (m_format.sampleSize() == 16 && m_format.sampleType() == QAudioFormat::SignedInt) { + if (m_format.byteOrder() == QAudioFormat::LittleEndian) + value = qAbs(qFromLittleEndian(ptr)); + else + value = qAbs(qFromBigEndian(ptr)); + } + + maxValue = qMax(value, maxValue); + ptr += channelBytes; + } + } + + maxValue = qMin(maxValue, m_maxAmplitude); + m_level = qreal(maxValue) / m_maxAmplitude; } - // check for clipping - if (m_maxValue >= (maxAmp - 1)) - clipping = true; - - float value = ((float)m_maxValue/(float)maxAmp); - if (clipping) - m_maxValue = 100; - else - m_maxValue = (int)(value*100); emit update(); - return len; } -int AudioInfo::LinearMax() -{ - return m_maxValue; -} - RenderArea::RenderArea(QWidget *parent) : QWidget(parent) { @@ -137,12 +173,12 @@ void RenderArea::paintEvent(QPaintEvent * /* event */) painter.viewport().top()+10, painter.viewport().right()-20, painter.viewport().bottom()-20)); - if (level == 0) + if (level == 0.0) return; painter.setPen(Qt::red); - int pos = ((painter.viewport().right()-20)-(painter.viewport().left()+11))*level/100; + int pos = ((painter.viewport().right()-20)-(painter.viewport().left()+11))*level; for (int i = 0; i < 10; ++i) { int x1 = painter.viewport().left()+11; int y1 = painter.viewport().top()+10+i; @@ -155,7 +191,7 @@ void RenderArea::paintEvent(QPaintEvent * /* event */) } } -void RenderArea::setLevel(int value) +void RenderArea::setLevel(qreal value) { level = value; repaint(); @@ -220,7 +256,7 @@ InputTest::InputTest() audioInput = new QAudioInput(format,this); connect(audioInput, SIGNAL(notify()), SLOT(status())); connect(audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); - audioinfo = new AudioInfo(this); + audioinfo = new AudioInfo(format, this); connect(audioinfo, SIGNAL(update()), SLOT(refreshDisplay())); audioinfo->start(); audioInput->start(audioinfo); @@ -292,7 +328,7 @@ void InputTest::state(QAudio::State state) void InputTest::refreshDisplay() { - canvas->setLevel(audioinfo->LinearMax()); + canvas->setLevel(audioinfo->level()); canvas->repaint(); } diff --git a/examples/multimedia/audioinput/audioinput.h b/examples/multimedia/audioinput/audioinput.h index 6f6c8eb..33b9ac6 100644 --- a/examples/multimedia/audioinput/audioinput.h +++ b/examples/multimedia/audioinput/audioinput.h @@ -52,19 +52,21 @@ class AudioInfo : public QIODevice { Q_OBJECT public: - AudioInfo(QObject *parent); + AudioInfo(const QAudioFormat &format, QObject *parent); ~AudioInfo(); void start(); void stop(); - int LinearMax(); + qreal level() const { return m_level; } qint64 readData(char *data, qint64 maxlen); qint64 writeData(const char *data, qint64 len); private: - int m_maxValue; + const QAudioFormat m_format; + quint16 m_maxAmplitude; + qreal m_level; // 0.0 <= m_level <= 1.0 signals: void update(); @@ -78,13 +80,13 @@ class RenderArea : public QWidget public: RenderArea(QWidget *parent = 0); - void setLevel(int value); + void setLevel(qreal value); protected: void paintEvent(QPaintEvent *event); private: - int level; + qreal level; QPixmap pixmap; }; -- cgit v0.12 From fb829969ce3f406fcb927d8d31a84c5fb8f56d59 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 16:47:42 +0000 Subject: Changed variable names in audio input example to match Qt code style Also changed some names to better convey their meanings, e.g. button -> m_modeButton button2 -> m_suspendResumeButton Reviewed-by: trustme --- examples/multimedia/audioinput/audioinput.cpp | 156 +++++++++++++------------- examples/multimedia/audioinput/audioinput.h | 30 ++--- 2 files changed, 93 insertions(+), 93 deletions(-) diff --git a/examples/multimedia/audioinput/audioinput.cpp b/examples/multimedia/audioinput/audioinput.cpp index 36fc5ac..e18e3a7 100644 --- a/examples/multimedia/audioinput/audioinput.cpp +++ b/examples/multimedia/audioinput/audioinput.cpp @@ -159,7 +159,7 @@ RenderArea::RenderArea(QWidget *parent) setBackgroundRole(QPalette::Base); setAutoFillBackground(true); - level = 0; + m_level = 0; setMinimumHeight(30); setMinimumWidth(200); } @@ -173,12 +173,12 @@ void RenderArea::paintEvent(QPaintEvent * /* event */) painter.viewport().top()+10, painter.viewport().right()-20, painter.viewport().bottom()-20)); - if (level == 0.0) + if (m_level == 0.0) return; painter.setPen(Qt::red); - int pos = ((painter.viewport().right()-20)-(painter.viewport().left()+11))*level; + int pos = ((painter.viewport().right()-20)-(painter.viewport().left()+11))*m_level; for (int i = 0; i < 10; ++i) { int x1 = painter.viewport().left()+11; int y1 = painter.viewport().top()+10+i; @@ -193,7 +193,7 @@ void RenderArea::paintEvent(QPaintEvent * /* event */) void RenderArea::setLevel(qreal value) { - level = value; + m_level = value; repaint(); } @@ -203,120 +203,120 @@ InputTest::InputTest() QWidget *window = new QWidget; QVBoxLayout* layout = new QVBoxLayout; - canvas = new RenderArea; - layout->addWidget(canvas); + m_canvas = new RenderArea; + layout->addWidget(m_canvas); - deviceBox = new QComboBox(this); + m_deviceBox = new QComboBox(this); QList devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); for(int i = 0; i < devices.size(); ++i) - deviceBox->addItem(devices.at(i).deviceName(), qVariantFromValue(devices.at(i))); + m_deviceBox->addItem(devices.at(i).deviceName(), qVariantFromValue(devices.at(i))); - connect(deviceBox, SIGNAL(activated(int)), SLOT(deviceChanged(int))); - layout->addWidget(deviceBox); + connect(m_deviceBox, SIGNAL(activated(int)), SLOT(deviceChanged(int))); + layout->addWidget(m_deviceBox); - button = new QPushButton(this); - button->setText(tr("Click for Push Mode")); - connect(button, SIGNAL(clicked()), SLOT(toggleMode())); - layout->addWidget(button); + m_modeButton = new QPushButton(this); + m_modeButton->setText(tr("Click for Push Mode")); + connect(m_modeButton, SIGNAL(clicked()), SLOT(toggleMode())); + layout->addWidget(m_modeButton); - button2 = new QPushButton(this); - button2->setText(tr("Click To Suspend")); - connect(button2, SIGNAL(clicked()), SLOT(toggleSuspend())); - layout->addWidget(button2); + m_suspendResumeButton = new QPushButton(this); + m_suspendResumeButton->setText(tr("Click To Suspend")); + connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspend())); + layout->addWidget(m_suspendResumeButton); window->setLayout(layout); setCentralWidget(window); window->show(); - buffer = new char[BUFFER_SIZE]; + m_buffer = new char[BUFFER_SIZE]; - pullMode = true; + m_pullMode = true; - format.setFrequency(8000); - format.setChannels(1); - format.setSampleSize(16); - format.setSampleType(QAudioFormat::SignedInt); - format.setByteOrder(QAudioFormat::LittleEndian); - format.setCodec("audio/pcm"); + m_format.setFrequency(8000); + m_format.setChannels(1); + m_format.setSampleSize(16); + m_format.setSampleType(QAudioFormat::SignedInt); + m_format.setByteOrder(QAudioFormat::LittleEndian); + m_format.setCodec("audio/pcm"); QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice()); - if (!info.isFormatSupported(format)) { + if (!info.isFormatSupported(m_format)) { qWarning()<<"default format not supported try to use nearest"; - format = info.nearestFormat(format); + m_format = info.nearestFormat(m_format); } - if(format.sampleSize() != 16) { + if(m_format.sampleSize() != 16) { qWarning()<<"audio device doesn't support 16 bit samples, example cannot run"; - audioInput = 0; - button->setDisabled(true); - button2->setDisabled(true); + m_audioInput = 0; + m_modeButton->setDisabled(true); + m_suspendResumeButton->setDisabled(true); return; } - audioInput = new QAudioInput(format,this); - connect(audioInput, SIGNAL(notify()), SLOT(status())); - connect(audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); - audioinfo = new AudioInfo(format, this); - connect(audioinfo, SIGNAL(update()), SLOT(refreshDisplay())); - audioinfo->start(); - audioInput->start(audioinfo); + m_audioInput = new QAudioInput(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 = "<bytesReady()<<" bytes, elapsedUSecs = "<elapsedUSecs()<<", processedUSecs = "<processedUSecs(); + qWarning()<<"bytesReady = "<bytesReady()<<" bytes, elapsedUSecs = "<elapsedUSecs()<<", processedUSecs = "<processedUSecs(); } void InputTest::readMore() { - if(!audioInput) + if(!m_audioInput) return; - qint64 len = audioInput->bytesReady(); + qint64 len = m_audioInput->bytesReady(); if(len > 4096) len = 4096; - qint64 l = input->read(buffer,len); + qint64 l = m_input->read(m_buffer,len); if(l > 0) { - audioinfo->write(buffer,l); + m_audioInfo->write(m_buffer,l); } } void InputTest::toggleMode() { // Change bewteen pull and push modes - audioInput->stop(); + m_audioInput->stop(); - if (pullMode) { - button->setText(tr("Click for Pull Mode")); - input = audioInput->start(); - connect(input, SIGNAL(readyRead()), SLOT(readMore())); - pullMode = false; + if (m_pullMode) { + m_modeButton->setText(tr("Click for Pull Mode")); + m_input = m_audioInput->start(); + connect(m_input, SIGNAL(readyRead()), SLOT(readMore())); + m_pullMode = false; } else { - button->setText(tr("Click for Push Mode")); - pullMode = true; - audioInput->start(audioinfo); + m_modeButton->setText(tr("Click for Push Mode")); + m_pullMode = true; + m_audioInput->start(m_audioInfo); } - button2->setText("Click To Suspend"); + m_suspendResumeButton->setText("Click To Suspend"); } void InputTest::toggleSuspend() { // toggle suspend/resume - if(audioInput->state() == QAudio::SuspendedState) { + if(m_audioInput->state() == QAudio::SuspendedState) { qWarning() << "status: Suspended, resume()"; - audioInput->resume(); - button2->setText("Click To Suspend"); - } else if (audioInput->state() == QAudio::ActiveState) { + m_audioInput->resume(); + m_suspendResumeButton->setText("Click To Suspend"); + } else if (m_audioInput->state() == QAudio::ActiveState) { qWarning() << "status: Active, suspend()"; - audioInput->suspend(); - button2->setText("Click To Resume"); - } else if (audioInput->state() == QAudio::StoppedState) { + m_audioInput->suspend(); + m_suspendResumeButton->setText("Click To Resume"); + } else if (m_audioInput->state() == QAudio::StoppedState) { qWarning() << "status: Stopped, resume()"; - audioInput->resume(); - button2->setText("Click To Suspend"); - } else if (audioInput->state() == QAudio::IdleState) { + m_audioInput->resume(); + m_suspendResumeButton->setText("Click To Suspend"); + } else if (m_audioInput->state() == QAudio::IdleState) { qWarning() << "status: IdleState"; } } @@ -328,21 +328,21 @@ void InputTest::state(QAudio::State state) void InputTest::refreshDisplay() { - canvas->setLevel(audioinfo->level()); - canvas->repaint(); + m_canvas->setLevel(m_audioInfo->level()); + m_canvas->repaint(); } -void InputTest::deviceChanged(int idx) +void InputTest::deviceChanged(int index) { - audioinfo->stop(); - audioInput->stop(); - audioInput->disconnect(this); - delete audioInput; - - device = deviceBox->itemData(idx).value(); - audioInput = new QAudioInput(device, format, this); - connect(audioInput, SIGNAL(notify()), SLOT(status())); - connect(audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); - audioinfo->start(); - audioInput->start(audioinfo); + m_audioInfo->stop(); + m_audioInput->stop(); + m_audioInput->disconnect(this); + delete m_audioInput; + + m_device = m_deviceBox->itemData(idx).value(); + 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); } diff --git a/examples/multimedia/audioinput/audioinput.h b/examples/multimedia/audioinput/audioinput.h index 33b9ac6..b189e84 100644 --- a/examples/multimedia/audioinput/audioinput.h +++ b/examples/multimedia/audioinput/audioinput.h @@ -86,8 +86,8 @@ protected: void paintEvent(QPaintEvent *event); private: - qreal level; - QPixmap pixmap; + qreal m_level; + QPixmap m_pixmap; }; class InputTest : public QMainWindow @@ -103,23 +103,23 @@ private slots: void readMore(); void toggleMode(); void toggleSuspend(); - void state(QAudio::State s); - void deviceChanged(int idx); + void state(QAudio::State state); + void deviceChanged(int index); private: - AudioInfo *audioinfo; - QAudioDeviceInfo device; - QAudioFormat format; - QAudioInput *audioInput; - QIODevice *input; - RenderArea *canvas; + AudioInfo *m_audioInfo; + QAudioDeviceInfo m_device; + QAudioFormat m_format; + QAudioInput *m_audioInput; + QIODevice *m_input; + RenderArea *m_canvas; - bool pullMode; + bool m_pullMode; - QPushButton *button; - QPushButton *button2; - QComboBox *deviceBox; + QPushButton *m_modeButton; + QPushButton *m_suspendResumeButton; + QComboBox *m_deviceBox; - char *buffer; + char *m_buffer; }; -- cgit v0.12 From 77818e65f47fe211f328649085a297c9d2baeb45 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 17:29:35 +0000 Subject: Cleaned up memory management in audio input example Reviewed-by: trustme --- examples/multimedia/audioinput/audioinput.cpp | 69 ++++++++++++++++----------- 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 window(new QWidget); + QScopedPointer 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 = "<bytesReady()<<" bytes, elapsedUSecs = "<elapsedUSecs()<<", processedUSecs = "<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(); - 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(); + 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 #include #include +#include #include @@ -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; }; -- cgit v0.12 From 81a96e9da45cbdf0bab221e3dd0b617ec7dcb890 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 17:35:38 +0000 Subject: Removed redundant strings from audio input example Reviewed-by: trustme --- examples/multimedia/audioinput/audioinput.cpp | 21 +++++++++++++-------- examples/multimedia/audioinput/audioinput.h | 5 +++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/multimedia/audioinput/audioinput.cpp b/examples/multimedia/audioinput/audioinput.cpp index 48763c9..10941ca 100644 --- a/examples/multimedia/audioinput/audioinput.cpp +++ b/examples/multimedia/audioinput/audioinput.cpp @@ -53,6 +53,11 @@ #include "audioinput.h" +const QString InputTest::PushModeLabel(tr("Enable push mode")); +const QString InputTest::PullModeLabel(tr("Enable pull mode")); +const QString InputTest::SuspendLabel(tr("Suspend recording")); +const QString InputTest::ResumeLabel(tr("Resume recording")); + const int BufferSize = 4096; AudioInfo::AudioInfo(const QAudioFormat &format, QObject *parent) @@ -232,12 +237,12 @@ void InputTest::initializeWindow() layout->addWidget(m_deviceBox); m_modeButton = new QPushButton(this); - m_modeButton->setText(tr("Click for Push Mode")); + m_modeButton->setText(PushModeLabel); connect(m_modeButton, SIGNAL(clicked()), SLOT(toggleMode())); layout->addWidget(m_modeButton); m_suspendResumeButton = new QPushButton(this); - m_suspendResumeButton->setText(tr("Click To Suspend")); + m_suspendResumeButton->setText(SuspendLabel); connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspend())); layout->addWidget(m_suspendResumeButton); @@ -305,17 +310,17 @@ void InputTest::toggleMode() m_audioInput->stop(); if (m_pullMode) { - m_modeButton->setText(tr("Click for Pull Mode")); + m_modeButton->setText(PullModeLabel); m_input = m_audioInput->start(); connect(m_input, SIGNAL(readyRead()), SLOT(readMore())); m_pullMode = false; } else { - m_modeButton->setText(tr("Click for Push Mode")); + m_modeButton->setText(PushModeLabel); m_pullMode = true; m_audioInput->start(m_audioInfo); } - m_suspendResumeButton->setText("Click To Suspend"); + m_suspendResumeButton->setText(SuspendLabel); } void InputTest::toggleSuspend() @@ -324,15 +329,15 @@ void InputTest::toggleSuspend() if(m_audioInput->state() == QAudio::SuspendedState) { qWarning() << "status: Suspended, resume()"; m_audioInput->resume(); - m_suspendResumeButton->setText("Click To Suspend"); + m_suspendResumeButton->setText(SuspendLabel); } else if (m_audioInput->state() == QAudio::ActiveState) { qWarning() << "status: Active, suspend()"; m_audioInput->suspend(); - m_suspendResumeButton->setText("Click To Resume"); + m_suspendResumeButton->setText(ResumeLabel); } else if (m_audioInput->state() == QAudio::StoppedState) { qWarning() << "status: Stopped, resume()"; m_audioInput->resume(); - m_suspendResumeButton->setText("Click To Suspend"); + m_suspendResumeButton->setText(SuspendLabel); } else if (m_audioInput->state() == QAudio::IdleState) { qWarning() << "status: IdleState"; } diff --git a/examples/multimedia/audioinput/audioinput.h b/examples/multimedia/audioinput/audioinput.h index a32a374..f1ab254 100644 --- a/examples/multimedia/audioinput/audioinput.h +++ b/examples/multimedia/audioinput/audioinput.h @@ -126,5 +126,10 @@ private: QIODevice *m_input; bool m_pullMode; QByteArray m_buffer; + + static const QString PushModeLabel; + static const QString PullModeLabel; + static const QString SuspendLabel; + static const QString ResumeLabel; }; -- cgit v0.12 From 96a6c921d940c802f7a282067f5b1790af725e6f Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 17:38:35 +0000 Subject: Tidied up debug output in audio input example Reviewed-by: trustme --- examples/multimedia/audioinput/audioinput.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/multimedia/audioinput/audioinput.cpp b/examples/multimedia/audioinput/audioinput.cpp index 10941ca..2258854 100644 --- a/examples/multimedia/audioinput/audioinput.cpp +++ b/examples/multimedia/audioinput/audioinput.cpp @@ -267,7 +267,7 @@ void InputTest::initializeAudio() QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice()); if (!info.isFormatSupported(m_format)) { - qWarning()<<"default format not supported try to use nearest"; + qWarning() << "Default format not supported - trying to use nearest"; m_format = info.nearestFormat(m_format); } @@ -288,7 +288,9 @@ void InputTest::createAudioInput() void InputTest::status() { - qWarning()<<"bytesReady = "<bytesReady()<<" bytes, elapsedUSecs = "<elapsedUSecs()<<", processedUSecs = "<processedUSecs(); + qWarning() << "bytesReady = " << m_audioInput->bytesReady() + << ", " << "elapsedUSecs = " <elapsedUSecs() + << ", " << "processedUSecs = "<processedUSecs(); } void InputTest::readMore() @@ -345,7 +347,7 @@ void InputTest::toggleSuspend() void InputTest::state(QAudio::State state) { - qWarning() << " state=" << state; + qWarning() << "state = " << state; } void InputTest::refreshDisplay() -- cgit v0.12 From c22299dd388af117e436c17258d7afc1623af9b6 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Tue, 16 Feb 2010 17:41:25 +0000 Subject: Changed function names in audio input example to clarify meaning Reviewed-by: trustme --- examples/multimedia/audioinput/audioinput.cpp | 9 +++++---- examples/multimedia/audioinput/audioinput.h | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/multimedia/audioinput/audioinput.cpp b/examples/multimedia/audioinput/audioinput.cpp index 2258854..8a97640 100644 --- a/examples/multimedia/audioinput/audioinput.cpp +++ b/examples/multimedia/audioinput/audioinput.cpp @@ -279,14 +279,15 @@ void InputTest::initializeAudio() 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))); + connect(m_audioInput, SIGNAL(notify()), SLOT(notified())); + connect(m_audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State))); m_audioInfo->start(); m_audioInput->start(m_audioInfo); } -void InputTest::status() +void InputTest::notified() { qWarning() << "bytesReady = " << m_audioInput->bytesReady() << ", " << "elapsedUSecs = " <elapsedUSecs() @@ -345,7 +346,7 @@ void InputTest::toggleSuspend() } } -void InputTest::state(QAudio::State state) +void InputTest::stateChanged(QAudio::State state) { qWarning() << "state = " << state; } diff --git a/examples/multimedia/audioinput/audioinput.h b/examples/multimedia/audioinput/audioinput.h index f1ab254..f87c682 100644 --- a/examples/multimedia/audioinput/audioinput.h +++ b/examples/multimedia/audioinput/audioinput.h @@ -105,11 +105,11 @@ private: private slots: void refreshDisplay(); - void status(); + void notified(); void readMore(); void toggleMode(); void toggleSuspend(); - void state(QAudio::State state); + void stateChanged(QAudio::State state); void deviceChanged(int index); private: -- cgit v0.12