1 /**************************************************************************** |
1 /**************************************************************************** |
2 ** |
2 ** |
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
4 ** All rights reserved. |
4 ** All rights reserved. |
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
6 ** |
6 ** |
7 ** This file is part of the examples of the Qt Toolkit. |
7 ** This file is part of the examples of the Qt Toolkit. |
8 ** |
8 ** |
88 int maxAmp = 32768; // max for S16 samples |
88 int maxAmp = 32768; // max for S16 samples |
89 bool clipping = false; |
89 bool clipping = false; |
90 |
90 |
91 m_maxValue = 0; |
91 m_maxValue = 0; |
92 |
92 |
93 qint16* s = (qint16*)data; |
93 qint16 *s = (qint16*)data; |
94 |
94 |
95 // sample format is S16LE, only! |
95 // sample format is S16LE, only! |
96 |
96 |
97 for(int i=0;i<samples;i++) { |
97 for (int i = 0; i < samples; ++i) { |
98 qint16 sample = *s; |
98 qint16 sample = *s; |
99 s++; |
99 s++; |
100 if(abs(sample) > m_maxValue) m_maxValue = abs(sample); |
100 if (abs(sample) > m_maxValue) m_maxValue = abs(sample); |
101 } |
101 } |
102 // check for clipping |
102 // check for clipping |
103 if(m_maxValue>=(maxAmp-1)) clipping = true; |
103 if (m_maxValue >= (maxAmp - 1)) |
|
104 clipping = true; |
104 |
105 |
105 float value = ((float)m_maxValue/(float)maxAmp); |
106 float value = ((float)m_maxValue/(float)maxAmp); |
106 if(clipping) m_maxValue = 100; |
107 if (clipping) |
107 else m_maxValue = (int)(value*100); |
108 m_maxValue = 100; |
|
109 else |
|
110 m_maxValue = (int)(value*100); |
108 |
111 |
109 emit update(); |
112 emit update(); |
110 |
113 |
111 return len; |
114 return len; |
112 } |
115 } |
130 void RenderArea::paintEvent(QPaintEvent * /* event */) |
133 void RenderArea::paintEvent(QPaintEvent * /* event */) |
131 { |
134 { |
132 QPainter painter(this); |
135 QPainter painter(this); |
133 |
136 |
134 painter.setPen(Qt::black); |
137 painter.setPen(Qt::black); |
135 painter.drawRect(QRect(painter.viewport().left()+10, painter.viewport().top()+10, |
138 painter.drawRect(QRect(painter.viewport().left()+10, |
136 painter.viewport().right()-20, painter.viewport().bottom()-20)); |
139 painter.viewport().top()+10, |
137 |
140 painter.viewport().right()-20, |
138 if(level == 0) |
141 painter.viewport().bottom()-20)); |
|
142 if (level == 0) |
139 return; |
143 return; |
140 |
144 |
141 painter.setPen(Qt::red); |
145 painter.setPen(Qt::red); |
142 |
146 |
143 int pos = ((painter.viewport().right()-20)-(painter.viewport().left()+11))*level/100; |
147 int pos = ((painter.viewport().right()-20)-(painter.viewport().left()+11))*level/100; |
144 int x1,y1,x2,y2; |
148 for (int i = 0; i < 10; ++i) { |
145 for(int i=0;i<10;i++) { |
149 int x1 = painter.viewport().left()+11; |
146 x1 = painter.viewport().left()+11; |
150 int y1 = painter.viewport().top()+10+i; |
147 y1 = painter.viewport().top()+10+i; |
151 int x2 = painter.viewport().left()+20+pos; |
148 x2 = painter.viewport().left()+20+pos; |
152 int y2 = painter.viewport().top()+10+i; |
149 y2 = painter.viewport().top()+10+i; |
153 if (x2 < painter.viewport().left()+10) |
150 if(x2 < painter.viewport().left()+10) |
|
151 x2 = painter.viewport().left()+10; |
154 x2 = painter.viewport().left()+10; |
152 |
155 |
153 painter.drawLine(QPoint(x1,y1),QPoint(x2,y2)); |
156 painter.drawLine(QPoint(x1, y1),QPoint(x2, y2)); |
154 } |
157 } |
155 } |
158 } |
156 |
159 |
157 void RenderArea::setLevel(int value) |
160 void RenderArea::setLevel(int value) |
158 { |
161 { |
169 canvas = new RenderArea; |
172 canvas = new RenderArea; |
170 layout->addWidget(canvas); |
173 layout->addWidget(canvas); |
171 |
174 |
172 deviceBox = new QComboBox(this); |
175 deviceBox = new QComboBox(this); |
173 QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); |
176 QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); |
174 for(int i = 0; i < devices.size(); ++i) { |
177 for(int i = 0; i < devices.size(); ++i) |
175 deviceBox->addItem(devices.at(i).deviceName(), qVariantFromValue(devices.at(i))); |
178 deviceBox->addItem(devices.at(i).deviceName(), qVariantFromValue(devices.at(i))); |
176 } |
179 |
177 connect(deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int))); |
180 connect(deviceBox, SIGNAL(activated(int)), SLOT(deviceChanged(int))); |
178 layout->addWidget(deviceBox); |
181 layout->addWidget(deviceBox); |
179 |
182 |
180 button = new QPushButton(this); |
183 button = new QPushButton(this); |
181 button->setText(tr("Click for Push Mode")); |
184 button->setText(tr("Click for Push Mode")); |
182 connect(button,SIGNAL(clicked()),SLOT(toggleMode())); |
185 connect(button, SIGNAL(clicked()), SLOT(toggleMode())); |
183 layout->addWidget(button); |
186 layout->addWidget(button); |
184 |
187 |
185 button2 = new QPushButton(this); |
188 button2 = new QPushButton(this); |
186 button2->setText(tr("Click To Suspend")); |
189 button2->setText(tr("Click To Suspend")); |
187 connect(button2,SIGNAL(clicked()),SLOT(toggleSuspend())); |
190 connect(button2, SIGNAL(clicked()), SLOT(toggleSuspend())); |
188 layout->addWidget(button2); |
191 layout->addWidget(button2); |
189 |
192 |
190 window->setLayout(layout); |
193 window->setLayout(layout); |
191 setCentralWidget(window); |
194 setCentralWidget(window); |
192 window->show(); |
195 window->show(); |
212 qWarning()<<"audio device doesn't support 16 bit samples, example cannot run"; |
215 qWarning()<<"audio device doesn't support 16 bit samples, example cannot run"; |
213 return; |
216 return; |
214 } |
217 } |
215 |
218 |
216 audioInput = new QAudioInput(format,this); |
219 audioInput = new QAudioInput(format,this); |
217 connect(audioInput,SIGNAL(notify()),SLOT(status())); |
220 connect(audioInput, SIGNAL(notify()), SLOT(status())); |
218 connect(audioInput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State))); |
221 connect(audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); |
219 audioinfo = new AudioInfo(this,audioInput); |
222 audioinfo = new AudioInfo(this,audioInput); |
220 connect(audioinfo,SIGNAL(update()),SLOT(refreshDisplay())); |
223 connect(audioinfo, SIGNAL(update()), SLOT(refreshDisplay())); |
221 audioinfo->start(); |
224 audioinfo->start(); |
222 audioInput->start(audioinfo); |
225 audioInput->start(audioinfo); |
223 } |
226 } |
224 |
227 |
225 InputTest::~InputTest() {} |
228 InputTest::~InputTest() {} |
248 audioInput->stop(); |
251 audioInput->stop(); |
249 |
252 |
250 if (pullMode) { |
253 if (pullMode) { |
251 button->setText(tr("Click for Pull Mode")); |
254 button->setText(tr("Click for Pull Mode")); |
252 input = audioInput->start(); |
255 input = audioInput->start(); |
253 connect(input,SIGNAL(readyRead()),SLOT(readMore())); |
256 connect(input, SIGNAL(readyRead()), SLOT(readMore())); |
254 pullMode = false; |
257 pullMode = false; |
255 } else { |
258 } else { |
256 button->setText(tr("Click for Push Mode")); |
259 button->setText(tr("Click for Push Mode")); |
257 pullMode = true; |
260 pullMode = true; |
258 audioInput->start(audioinfo); |
261 audioInput->start(audioinfo); |
261 |
264 |
262 void InputTest::toggleSuspend() |
265 void InputTest::toggleSuspend() |
263 { |
266 { |
264 // toggle suspend/resume |
267 // toggle suspend/resume |
265 if(audioInput->state() == QAudio::SuspendedState) { |
268 if(audioInput->state() == QAudio::SuspendedState) { |
266 qWarning()<<"status: Suspended, resume()"; |
269 qWarning() << "status: Suspended, resume()"; |
267 audioInput->resume(); |
270 audioInput->resume(); |
268 button2->setText("Click To Suspend"); |
271 button2->setText("Click To Suspend"); |
269 } else if (audioInput->state() == QAudio::ActiveState) { |
272 } else if (audioInput->state() == QAudio::ActiveState) { |
270 qWarning()<<"status: Active, suspend()"; |
273 qWarning() << "status: Active, suspend()"; |
271 audioInput->suspend(); |
274 audioInput->suspend(); |
272 button2->setText("Click To Resume"); |
275 button2->setText("Click To Resume"); |
273 } else if (audioInput->state() == QAudio::StoppedState) { |
276 } else if (audioInput->state() == QAudio::StoppedState) { |
274 qWarning()<<"status: Stopped, resume()"; |
277 qWarning() << "status: Stopped, resume()"; |
275 audioInput->resume(); |
278 audioInput->resume(); |
276 button2->setText("Click To Suspend"); |
279 button2->setText("Click To Suspend"); |
277 } else if (audioInput->state() == QAudio::IdleState) { |
280 } else if (audioInput->state() == QAudio::IdleState) { |
278 qWarning()<<"status: IdleState"; |
281 qWarning() << "status: IdleState"; |
279 } |
282 } |
280 } |
283 } |
281 |
284 |
282 void InputTest::state(QAudio::State state) |
285 void InputTest::state(QAudio::State state) |
283 { |
286 { |
284 qWarning()<<" state="<<state; |
287 qWarning() << " state=" << state; |
285 } |
288 } |
286 |
289 |
287 void InputTest::refreshDisplay() |
290 void InputTest::refreshDisplay() |
288 { |
291 { |
289 canvas->setLevel(audioinfo->LinearMax()); |
292 canvas->setLevel(audioinfo->LinearMax()); |
297 audioInput->disconnect(this); |
300 audioInput->disconnect(this); |
298 delete audioInput; |
301 delete audioInput; |
299 |
302 |
300 device = deviceBox->itemData(idx).value<QAudioDeviceInfo>(); |
303 device = deviceBox->itemData(idx).value<QAudioDeviceInfo>(); |
301 audioInput = new QAudioInput(device, format, this); |
304 audioInput = new QAudioInput(device, format, this); |
302 connect(audioInput,SIGNAL(notify()),SLOT(status())); |
305 connect(audioInput, SIGNAL(notify()), SLOT(status())); |
303 connect(audioInput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State))); |
306 connect(audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(state(QAudio::State))); |
304 audioinfo->start(); |
307 audioinfo->start(); |
305 audioInput->start(audioinfo); |
308 audioInput->start(audioinfo); |
306 } |
309 } |