| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QDebug>
|
|---|
| 43 | #include <QVBoxLayout>
|
|---|
| 44 |
|
|---|
| 45 | #include <QAudioOutput>
|
|---|
| 46 | #include <QAudioDeviceInfo>
|
|---|
| 47 | #include "audiooutput.h"
|
|---|
| 48 |
|
|---|
| 49 | #ifndef M_PI
|
|---|
| 50 | #define M_PI 3.14159265358979323846
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | #define SECONDS 1
|
|---|
| 54 | #define FREQ 600
|
|---|
| 55 | #define SYSTEM_FREQ 44100
|
|---|
| 56 |
|
|---|
| 57 | Generator::Generator(QObject *parent)
|
|---|
| 58 | :QIODevice( parent )
|
|---|
| 59 | {
|
|---|
| 60 | finished = false;
|
|---|
| 61 | buffer = new char[SECONDS*SYSTEM_FREQ*4+1000];
|
|---|
| 62 | t=buffer;
|
|---|
| 63 | len=fillData(t,FREQ,SECONDS); /* mono FREQHz sine */
|
|---|
| 64 | pos = 0;
|
|---|
| 65 | total = len;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | Generator::~Generator()
|
|---|
| 69 | {
|
|---|
| 70 | delete [] buffer;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | void Generator::start()
|
|---|
| 74 | {
|
|---|
| 75 | open(QIODevice::ReadOnly);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | void Generator::stop()
|
|---|
| 79 | {
|
|---|
| 80 | close();
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int Generator::putShort(char *t, unsigned int value)
|
|---|
| 84 | {
|
|---|
| 85 | *(unsigned char *)(t++)=value&255;
|
|---|
| 86 | *(unsigned char *)(t)=(value/256)&255;
|
|---|
| 87 | return 2;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | int Generator::fillData(char *start, int frequency, int seconds)
|
|---|
| 91 | {
|
|---|
| 92 | int i, len=0;
|
|---|
| 93 | int value;
|
|---|
| 94 | for(i=0; i<seconds*SYSTEM_FREQ; i++) {
|
|---|
| 95 | value=(int)(32767.0*sin(2.0*M_PI*((double)(i))*(double)(frequency)/SYSTEM_FREQ));
|
|---|
| 96 | putShort(start, value);
|
|---|
| 97 | start += 4;
|
|---|
| 98 | len+=2;
|
|---|
| 99 | }
|
|---|
| 100 | return len;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | qint64 Generator::readData(char *data, qint64 maxlen)
|
|---|
| 104 | {
|
|---|
| 105 | int len = maxlen;
|
|---|
| 106 | if(len > 16384)
|
|---|
| 107 | len = 16384;
|
|---|
| 108 |
|
|---|
| 109 | if(len < (SECONDS*SYSTEM_FREQ*2)-pos) {
|
|---|
| 110 | // Normal
|
|---|
| 111 | memcpy(data,t+pos,len);
|
|---|
| 112 | pos+=len;
|
|---|
| 113 | return len;
|
|---|
| 114 | } else {
|
|---|
| 115 | // Whats left and reset to start
|
|---|
| 116 | qint64 left = (SECONDS*SYSTEM_FREQ*2)-pos;
|
|---|
| 117 | memcpy(data,t+pos,left);
|
|---|
| 118 | pos=0;
|
|---|
| 119 | return left;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | qint64 Generator::writeData(const char *data, qint64 len)
|
|---|
| 124 | {
|
|---|
| 125 | Q_UNUSED(data);
|
|---|
| 126 | Q_UNUSED(len);
|
|---|
| 127 |
|
|---|
| 128 | return 0;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | AudioTest::AudioTest()
|
|---|
| 132 | {
|
|---|
| 133 | QWidget *window = new QWidget;
|
|---|
| 134 | QVBoxLayout* layout = new QVBoxLayout;
|
|---|
| 135 |
|
|---|
| 136 | deviceBox = new QComboBox(this);
|
|---|
| 137 | foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
|
|---|
| 138 | deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo));
|
|---|
| 139 | connect(deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int)));
|
|---|
| 140 | layout->addWidget(deviceBox);
|
|---|
| 141 |
|
|---|
| 142 | button = new QPushButton(this);
|
|---|
| 143 | button->setText(tr("Click for Push Mode"));
|
|---|
| 144 | connect(button,SIGNAL(clicked()),SLOT(toggle()));
|
|---|
| 145 | layout->addWidget(button);
|
|---|
| 146 |
|
|---|
| 147 | button2 = new QPushButton(this);
|
|---|
| 148 | button2->setText(tr("Click To Suspend"));
|
|---|
| 149 | connect(button2,SIGNAL(clicked()),SLOT(togglePlay()));
|
|---|
| 150 | layout->addWidget(button2);
|
|---|
| 151 |
|
|---|
| 152 | window->setLayout(layout);
|
|---|
| 153 | setCentralWidget(window);
|
|---|
| 154 | window->show();
|
|---|
| 155 |
|
|---|
| 156 | buffer = new char[BUFFER_SIZE];
|
|---|
| 157 |
|
|---|
| 158 | gen = new Generator(this);
|
|---|
| 159 |
|
|---|
| 160 | pullMode = true;
|
|---|
| 161 |
|
|---|
| 162 | timer = new QTimer(this);
|
|---|
| 163 | connect(timer,SIGNAL(timeout()),SLOT(writeMore()));
|
|---|
| 164 |
|
|---|
| 165 | gen->start();
|
|---|
| 166 |
|
|---|
| 167 | settings.setFrequency(SYSTEM_FREQ);
|
|---|
| 168 | settings.setChannels(1);
|
|---|
| 169 | settings.setSampleSize(16);
|
|---|
| 170 | settings.setCodec("audio/pcm");
|
|---|
| 171 | settings.setByteOrder(QAudioFormat::LittleEndian);
|
|---|
| 172 | settings.setSampleType(QAudioFormat::SignedInt);
|
|---|
| 173 |
|
|---|
| 174 | QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
|
|---|
| 175 | if (!info.isFormatSupported(settings)) {
|
|---|
| 176 | qWarning()<<"default format not supported try to use nearest";
|
|---|
| 177 | settings = info.nearestFormat(settings);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if(settings.sampleSize() != 16) {
|
|---|
| 181 | qWarning()<<"audio device doesn't support 16 bit samples, example cannot run";
|
|---|
| 182 | return;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | audioOutput = new QAudioOutput(settings,this);
|
|---|
| 186 | connect(audioOutput,SIGNAL(notify()),SLOT(status()));
|
|---|
| 187 | connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State)));
|
|---|
| 188 |
|
|---|
| 189 | audioOutput->start(gen);
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | AudioTest::~AudioTest()
|
|---|
| 193 | {
|
|---|
| 194 | delete [] buffer;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | void AudioTest::deviceChanged(int idx)
|
|---|
| 198 | {
|
|---|
| 199 | timer->stop();
|
|---|
| 200 | gen->stop();
|
|---|
| 201 | audioOutput->stop();
|
|---|
| 202 | audioOutput->disconnect(this);
|
|---|
| 203 | delete audioOutput;
|
|---|
| 204 |
|
|---|
| 205 | device = deviceBox->itemData(idx).value<QAudioDeviceInfo>();
|
|---|
| 206 | audioOutput = new QAudioOutput(device,settings,this);
|
|---|
| 207 | connect(audioOutput,SIGNAL(notify()),SLOT(status()));
|
|---|
| 208 | connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State)));
|
|---|
| 209 | gen->start();
|
|---|
| 210 | audioOutput->start(gen);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | void AudioTest::status()
|
|---|
| 214 | {
|
|---|
| 215 | qWarning()<<"byteFree = "<<audioOutput->bytesFree()<<" bytes, elapsedUSecs = "<<audioOutput->elapsedUSecs()<<", processedUSecs = "<<audioOutput->processedUSecs();
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | void AudioTest::writeMore()
|
|---|
| 219 | {
|
|---|
| 220 | if(!audioOutput)
|
|---|
| 221 | return;
|
|---|
| 222 |
|
|---|
| 223 | if(audioOutput->state() == QAudio::StoppedState)
|
|---|
| 224 | return;
|
|---|
| 225 |
|
|---|
| 226 | int l;
|
|---|
| 227 | int out;
|
|---|
| 228 |
|
|---|
| 229 | int chunks = audioOutput->bytesFree()/audioOutput->periodSize();
|
|---|
| 230 | while(chunks) {
|
|---|
| 231 | l = gen->read(buffer,audioOutput->periodSize());
|
|---|
| 232 | if(l > 0)
|
|---|
| 233 | out = output->write(buffer,l);
|
|---|
| 234 | if(l != audioOutput->periodSize())
|
|---|
| 235 | break;
|
|---|
| 236 | chunks--;
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | void AudioTest::toggle()
|
|---|
| 241 | {
|
|---|
| 242 | // Change between pull and push modes
|
|---|
| 243 |
|
|---|
| 244 | timer->stop();
|
|---|
| 245 | audioOutput->stop();
|
|---|
| 246 |
|
|---|
| 247 | if (pullMode) {
|
|---|
| 248 | button->setText("Click for Pull Mode");
|
|---|
| 249 | output = audioOutput->start();
|
|---|
| 250 | pullMode = false;
|
|---|
| 251 | timer->start(20);
|
|---|
| 252 | } else {
|
|---|
| 253 | button->setText("Click for Push Mode");
|
|---|
| 254 | pullMode = true;
|
|---|
| 255 | audioOutput->start(gen);
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | void AudioTest::togglePlay()
|
|---|
| 260 | {
|
|---|
| 261 | // toggle suspend/resume
|
|---|
| 262 | if(audioOutput->state() == QAudio::SuspendedState) {
|
|---|
| 263 | qWarning()<<"status: Suspended, resume()";
|
|---|
| 264 | audioOutput->resume();
|
|---|
| 265 | button2->setText("Click To Suspend");
|
|---|
| 266 | } else if (audioOutput->state() == QAudio::ActiveState) {
|
|---|
| 267 | qWarning()<<"status: Active, suspend()";
|
|---|
| 268 | audioOutput->suspend();
|
|---|
| 269 | button2->setText("Click To Resume");
|
|---|
| 270 | } else if (audioOutput->state() == QAudio::StoppedState) {
|
|---|
| 271 | qWarning()<<"status: Stopped, resume()";
|
|---|
| 272 | audioOutput->resume();
|
|---|
| 273 | button2->setText("Click To Suspend");
|
|---|
| 274 | } else if (audioOutput->state() == QAudio::IdleState) {
|
|---|
| 275 | qWarning()<<"status: IdleState";
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | void AudioTest::state(QAudio::State state)
|
|---|
| 280 | {
|
|---|
| 281 | qWarning()<<" state="<<state;
|
|---|
| 282 | }
|
|---|