author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
106 |
if (len > 16384) |
0 | 107 |
len = 16384; |
108 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
109 |
if (len < (SECONDS*SYSTEM_FREQ*2)-pos) { |
0 | 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); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) |
0 | 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); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
if (!info.isFormatSupported(settings)) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
qWarning()<<"default format not supported try to use nearest"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
settings = info.nearestFormat(settings); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
if(settings.sampleSize() != 16) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
qWarning()<<"audio device doesn't support 16 bit samples, example cannot run"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
184 |
|
0 | 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 |
{ |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
215 |
qWarning() << "byteFree = " << audioOutput->bytesFree() << " bytes, elapsedUSecs = " << audioOutput->elapsedUSecs() << ", processedUSecs = " << audioOutput->processedUSecs(); |
0 | 216 |
} |
217 |
||
218 |
void AudioTest::writeMore() |
|
219 |
{ |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
220 |
if (!audioOutput) |
0 | 221 |
return; |
222 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
223 |
if (audioOutput->state() == QAudio::StoppedState) |
0 | 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()); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
232 |
if (l > 0) |
0 | 233 |
out = output->write(buffer,l); |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
234 |
if (l != audioOutput->periodSize()) |
0 | 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"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
249 |
output = audioOutput->start(); |
0 | 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 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
262 |
if (audioOutput->state() == QAudio::SuspendedState) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
263 |
qWarning() << "status: Suspended, resume()"; |
0 | 264 |
audioOutput->resume(); |
265 |
button2->setText("Click To Suspend"); |
|
266 |
} else if (audioOutput->state() == QAudio::ActiveState) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
267 |
qWarning() << "status: Active, suspend()"; |
0 | 268 |
audioOutput->suspend(); |
269 |
button2->setText("Click To Resume"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
270 |
} else if (audioOutput->state() == QAudio::StoppedState) { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
271 |
qWarning() << "status: Stopped, resume()"; |
0 | 272 |
audioOutput->resume(); |
273 |
button2->setText("Click To Suspend"); |
|
274 |
} else if (audioOutput->state() == QAudio::IdleState) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
275 |
qWarning() << "status: IdleState"; |
0 | 276 |
} |
277 |
} |
|
278 |
||
279 |
void AudioTest::state(QAudio::State state) |
|
280 |
{ |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
281 |
qWarning() << " state=" << state; |
0 | 282 |
} |