1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 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 | button->setDisabled(true);
|
---|
183 | button2->setDisabled(true);
|
---|
184 | audioOutput = 0;
|
---|
185 | return;
|
---|
186 | }
|
---|
187 |
|
---|
188 | audioOutput = new QAudioOutput(settings,this);
|
---|
189 | connect(audioOutput,SIGNAL(notify()),SLOT(status()));
|
---|
190 | connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State)));
|
---|
191 |
|
---|
192 | audioOutput->start(gen);
|
---|
193 | }
|
---|
194 |
|
---|
195 | AudioTest::~AudioTest()
|
---|
196 | {
|
---|
197 | delete [] buffer;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void AudioTest::deviceChanged(int idx)
|
---|
201 | {
|
---|
202 | timer->stop();
|
---|
203 | gen->stop();
|
---|
204 | audioOutput->stop();
|
---|
205 | audioOutput->disconnect(this);
|
---|
206 | delete audioOutput;
|
---|
207 |
|
---|
208 | device = deviceBox->itemData(idx).value<QAudioDeviceInfo>();
|
---|
209 | audioOutput = new QAudioOutput(device,settings,this);
|
---|
210 | connect(audioOutput,SIGNAL(notify()),SLOT(status()));
|
---|
211 | connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State)));
|
---|
212 | gen->start();
|
---|
213 | audioOutput->start(gen);
|
---|
214 | }
|
---|
215 |
|
---|
216 | void AudioTest::status()
|
---|
217 | {
|
---|
218 | qWarning() << "byteFree = " << audioOutput->bytesFree() << " bytes, elapsedUSecs = " << audioOutput->elapsedUSecs() << ", processedUSecs = " << audioOutput->processedUSecs();
|
---|
219 | }
|
---|
220 |
|
---|
221 | void AudioTest::writeMore()
|
---|
222 | {
|
---|
223 | if (!audioOutput)
|
---|
224 | return;
|
---|
225 |
|
---|
226 | if (audioOutput->state() == QAudio::StoppedState)
|
---|
227 | return;
|
---|
228 |
|
---|
229 | int l;
|
---|
230 | int out;
|
---|
231 |
|
---|
232 | int chunks = audioOutput->bytesFree()/audioOutput->periodSize();
|
---|
233 | while(chunks) {
|
---|
234 | l = gen->read(buffer,audioOutput->periodSize());
|
---|
235 | if (l > 0)
|
---|
236 | out = output->write(buffer,l);
|
---|
237 | if (l != audioOutput->periodSize())
|
---|
238 | break;
|
---|
239 | chunks--;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void AudioTest::toggle()
|
---|
244 | {
|
---|
245 | // Change between pull and push modes
|
---|
246 |
|
---|
247 | timer->stop();
|
---|
248 | audioOutput->stop();
|
---|
249 |
|
---|
250 | if (pullMode) {
|
---|
251 | button->setText("Click for Pull Mode");
|
---|
252 | output = audioOutput->start();
|
---|
253 | pullMode = false;
|
---|
254 | timer->start(20);
|
---|
255 | } else {
|
---|
256 | button->setText("Click for Push Mode");
|
---|
257 | pullMode = true;
|
---|
258 | audioOutput->start(gen);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | void AudioTest::togglePlay()
|
---|
263 | {
|
---|
264 | // toggle suspend/resume
|
---|
265 | if (audioOutput->state() == QAudio::SuspendedState) {
|
---|
266 | qWarning() << "status: Suspended, resume()";
|
---|
267 | audioOutput->resume();
|
---|
268 | button2->setText("Click To Suspend");
|
---|
269 | } else if (audioOutput->state() == QAudio::ActiveState) {
|
---|
270 | qWarning() << "status: Active, suspend()";
|
---|
271 | audioOutput->suspend();
|
---|
272 | button2->setText("Click To Resume");
|
---|
273 | } else if (audioOutput->state() == QAudio::StoppedState) {
|
---|
274 | qWarning() << "status: Stopped, resume()";
|
---|
275 | audioOutput->resume();
|
---|
276 | button2->setText("Click To Suspend");
|
---|
277 | } else if (audioOutput->state() == QAudio::IdleState) {
|
---|
278 | qWarning() << "status: IdleState";
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | void AudioTest::state(QAudio::State state)
|
---|
283 | {
|
---|
284 | qWarning() << " state=" << state;
|
---|
285 | }
|
---|