132 { |
132 { |
133 QWidget *window = new QWidget; |
133 QWidget *window = new QWidget; |
134 QVBoxLayout* layout = new QVBoxLayout; |
134 QVBoxLayout* layout = new QVBoxLayout; |
135 |
135 |
136 deviceBox = new QComboBox(this); |
136 deviceBox = new QComboBox(this); |
137 foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::deviceList(QAudio::AudioOutput)) |
137 foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) |
138 deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo)); |
138 deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo)); |
139 connect(deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int))); |
139 connect(deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int))); |
140 layout->addWidget(deviceBox); |
140 layout->addWidget(deviceBox); |
141 |
141 |
142 button = new QPushButton(this); |
142 button = new QPushButton(this); |
168 settings.setChannels(1); |
168 settings.setChannels(1); |
169 settings.setSampleSize(16); |
169 settings.setSampleSize(16); |
170 settings.setCodec("audio/pcm"); |
170 settings.setCodec("audio/pcm"); |
171 settings.setByteOrder(QAudioFormat::LittleEndian); |
171 settings.setByteOrder(QAudioFormat::LittleEndian); |
172 settings.setSampleType(QAudioFormat::SignedInt); |
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 |
173 audioOutput = new QAudioOutput(settings,this); |
185 audioOutput = new QAudioOutput(settings,this); |
174 connect(audioOutput,SIGNAL(notify()),SLOT(status())); |
186 connect(audioOutput,SIGNAL(notify()),SLOT(status())); |
175 connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State))); |
187 connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State))); |
176 |
188 |
177 audioOutput->start(gen); |
189 audioOutput->start(gen); |
198 audioOutput->start(gen); |
210 audioOutput->start(gen); |
199 } |
211 } |
200 |
212 |
201 void AudioTest::status() |
213 void AudioTest::status() |
202 { |
214 { |
203 qWarning()<<"byteFree = "<<audioOutput->bytesFree()<<" bytes, clock = "<<audioOutput->clock()/1000<<"ms, totalTime = "<<audioOutput->totalTime()/1000<<"ms"; |
215 qWarning()<<"byteFree = "<<audioOutput->bytesFree()<<" bytes, elapsedUSecs = "<<audioOutput->elapsedUSecs()<<", processedUSecs = "<<audioOutput->processedUSecs(); |
204 } |
216 } |
205 |
217 |
206 void AudioTest::writeMore() |
218 void AudioTest::writeMore() |
207 { |
219 { |
208 if(!audioOutput) |
220 if(!audioOutput) |
209 return; |
221 return; |
210 |
222 |
211 if(audioOutput->state() == QAudio::StopState) |
223 if(audioOutput->state() == QAudio::StoppedState) |
212 return; |
224 return; |
213 |
225 |
214 int l; |
226 int l; |
215 int out; |
227 int out; |
216 |
228 |
245 } |
257 } |
246 |
258 |
247 void AudioTest::togglePlay() |
259 void AudioTest::togglePlay() |
248 { |
260 { |
249 // toggle suspend/resume |
261 // toggle suspend/resume |
250 if(audioOutput->state() == QAudio::SuspendState) { |
262 if(audioOutput->state() == QAudio::SuspendedState) { |
251 qWarning()<<"status: Suspended, resume()"; |
263 qWarning()<<"status: Suspended, resume()"; |
252 audioOutput->resume(); |
264 audioOutput->resume(); |
253 button2->setText("Click To Suspend"); |
265 button2->setText("Click To Suspend"); |
254 } else if (audioOutput->state() == QAudio::ActiveState) { |
266 } else if (audioOutput->state() == QAudio::ActiveState) { |
255 qWarning()<<"status: Active, suspend()"; |
267 qWarning()<<"status: Active, suspend()"; |
256 audioOutput->suspend(); |
268 audioOutput->suspend(); |
257 button2->setText("Click To Resume"); |
269 button2->setText("Click To Resume"); |
258 } else if (audioOutput->state() == QAudio::StopState) { |
270 } else if (audioOutput->state() == QAudio::StoppedState) { |
259 qWarning()<<"status: Stopped, resume()"; |
271 qWarning()<<"status: Stopped, resume()"; |
260 audioOutput->resume(); |
272 audioOutput->resume(); |
261 button2->setText("Click To Suspend"); |
273 button2->setText("Click To Suspend"); |
262 } else if (audioOutput->state() == QAudio::IdleState) { |
274 } else if (audioOutput->state() == QAudio::IdleState) { |
263 qWarning()<<"status: IdleState"; |
275 qWarning()<<"status: IdleState"; |