76 Starting up the QAudioInput is simply a matter of calling start() |
75 Starting up the QAudioInput is simply a matter of calling start() |
77 with a QIODevice opened for writing. For instance, to record to a |
76 with a QIODevice opened for writing. For instance, to record to a |
78 file, you can: |
77 file, you can: |
79 |
78 |
80 \code |
79 \code |
|
80 QFile outputFile; // class member. |
|
81 QAudioInput* audio; // class member. |
|
82 \endcode |
|
83 |
|
84 \code |
81 { |
85 { |
82 QFile outputFile; |
|
83 outputFile.setFileName("/tmp/test.raw"); |
86 outputFile.setFileName("/tmp/test.raw"); |
84 outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate ); |
87 outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate ); |
85 |
88 |
86 QAudioFormat format; |
89 QAudioFormat format; |
87 // set up the format you want, eg. |
90 // set up the format you want, eg. |
90 format.setSampleSize(8); |
93 format.setSampleSize(8); |
91 format.setCodec("audio/pcm"); |
94 format.setCodec("audio/pcm"); |
92 format.setByteOrder(QAudioFormat::LittleEndian); |
95 format.setByteOrder(QAudioFormat::LittleEndian); |
93 format.setSampleType(QAudioFormat::UnSignedInt); |
96 format.setSampleType(QAudioFormat::UnSignedInt); |
94 |
97 |
95 QAudioInput *audio = new QAudioInput(format, this); |
98 if (QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice()); |
|
99 if (!info.isFormatSupported(format)) { |
|
100 qWarning()<<"default format not supported try to use nearest"; |
|
101 format = info.nearestFormat(format); |
|
102 } |
|
103 |
|
104 audio = new QAudioInput(format, this); |
96 QTimer::singleShot(3000, this, SLOT(stopRecording())); |
105 QTimer::singleShot(3000, this, SLOT(stopRecording())); |
97 audio->start(outputFile); |
106 audio->start(outputFile); |
98 // Records audio for 3000ms |
107 // Records audio for 3000ms |
99 } |
108 } |
100 \endcode |
109 \endcode |
119 suspend(), resume(), stop(), reset(), and start(). The current |
129 suspend(), resume(), stop(), reset(), and start(). The current |
120 state is reported by state(). QAudioOutput will also signal you |
130 state is reported by state(). QAudioOutput will also signal you |
121 when the state changes (stateChanged()). |
131 when the state changes (stateChanged()). |
122 |
132 |
123 QAudioInput provides several ways of measuring the time that has |
133 QAudioInput provides several ways of measuring the time that has |
124 passed since the start() of the recording. The \c totalTime() |
134 passed since the start() of the recording. The \c processedUSecs() |
125 function returns the length of the stream in microseconds written, |
135 function returns the length of the stream in microseconds written, |
126 i.e., it leaves out the times the audio input was suspended or idle. |
136 i.e., it leaves out the times the audio input was suspended or idle. |
127 The clock() function returns the time elapsed since start() was called regardless of |
137 The elapsedUSecs() function returns the time elapsed since start() was called regardless of |
128 which states the QAudioInput has been in. |
138 which states the QAudioInput has been in. |
129 |
139 |
130 If an error should occur, you can fetch its reason with error(). |
140 If an error should occur, you can fetch its reason with error(). |
131 The possible error reasons are described by the QAudio::Error |
141 The possible error reasons are described by the QAudio::Error |
132 enum. The QAudioInput will enter the \l{QAudio::}{StopState} when |
142 enum. The QAudioInput will enter the \l{QAudio::}{StoppedState} when |
133 an error is encountered. Connect to the stateChanged() signal to |
143 an error is encountered. Connect to the stateChanged() signal to |
134 handle the error: |
144 handle the error: |
135 |
145 |
136 \snippet doc/src/snippets/audio/main.cpp 0 |
146 \snippet doc/src/snippets/audio/main.cpp 0 |
137 |
147 |
174 { |
184 { |
175 delete d; |
185 delete d; |
176 } |
186 } |
177 |
187 |
178 /*! |
188 /*! |
179 Uses the \a device as the QIODevice to transfer data. |
189 Uses the \a device as the QIODevice to transfer data. |
180 If \a device is null then the class creates an internal QIODevice. |
190 Passing a QIODevice allows the data to be transfered without any extra code. |
|
191 All that is required is to open the QIODevice. |
|
192 |
|
193 \sa QIODevice |
|
194 */ |
|
195 |
|
196 void QAudioInput::start(QIODevice* device) |
|
197 { |
|
198 /* |
|
199 -If currently not StoppedState, stop |
|
200 -If previous start was push mode, delete internal QIODevice. |
|
201 -open audio input. |
|
202 If ok, NoError and ActiveState, else OpenError and StoppedState. |
|
203 -emit stateChanged() |
|
204 */ |
|
205 d->start(device); |
|
206 } |
|
207 |
|
208 /*! |
181 Returns a pointer to the QIODevice being used to handle the data |
209 Returns a pointer to the QIODevice being used to handle the data |
182 transfer. This QIODevice can be used to read() audio data |
210 transfer. This QIODevice can be used to read() audio data |
183 directly. |
211 directly. |
184 Passing a QIODevice allows the data to be transfered without any extra code. |
|
185 All that is required is to open the QIODevice. |
|
186 |
212 |
187 \sa QIODevice |
213 \sa QIODevice |
188 */ |
214 */ |
189 |
215 |
190 QIODevice* QAudioInput::start(QIODevice* device) |
216 QIODevice* QAudioInput::start() |
191 { |
217 { |
192 /* |
218 /* |
193 PULL MODE (valid QIODevice) |
219 -If currently not StoppedState, stop |
194 -If currently not StopState, stop |
|
195 -If previous start was push mode, delete internal QIODevice. |
|
196 -open audio input. |
|
197 If ok, NoError and ActiveState, else OpenError and StopState. |
|
198 -emit stateChanged() |
|
199 -return device |
|
200 |
|
201 PUSH MODE (device = 0) |
|
202 -If currently not StopState, stop |
|
203 -If no internal QIODevice, create one. |
220 -If no internal QIODevice, create one. |
204 -open audio input. |
221 -open audio input. |
205 -If ok, NoError and IdleState, else OpenError and StopState |
222 -If ok, NoError and IdleState, else OpenError and StoppedState |
206 -emit stateChanged() |
223 -emit stateChanged() |
207 -return internal QIODevice |
224 -return internal QIODevice |
208 */ |
225 */ |
209 return d->start(device); |
226 return d->start(0); |
210 } |
227 } |
211 |
228 |
212 /*! |
229 /*! |
213 Returns the QAudioFormat being used. |
230 Returns the QAudioFormat being used. |
214 */ |
231 */ |
253 void QAudioInput::suspend() |
270 void QAudioInput::suspend() |
254 { |
271 { |
255 /* |
272 /* |
256 -If not ActiveState|IdleState, return |
273 -If not ActiveState|IdleState, return |
257 -stop processing audio, saving all buffered audio data |
274 -stop processing audio, saving all buffered audio data |
258 -set NoError and SuspendState |
275 -set NoError and SuspendedState |
259 -emit stateChanged() |
276 -emit stateChanged() |
260 */ |
277 */ |
261 d->suspend(); |
278 d->suspend(); |
262 } |
279 } |
263 |
280 |
266 */ |
283 */ |
267 |
284 |
268 void QAudioInput::resume() |
285 void QAudioInput::resume() |
269 { |
286 { |
270 /* |
287 /* |
271 -If SuspendState, return |
288 -If SuspendedState, return |
272 -resume audio |
289 -resume audio |
273 -(PULL MODE): set ActiveState, NoError |
290 -(PULL MODE): set ActiveState, NoError |
274 -(PUSH MODE): set IdleState, NoError |
291 -(PUSH MODE): set IdleState, NoError |
275 -kick start audio if needed |
292 -kick start audio if needed |
276 -emit stateChanged() |
293 -emit stateChanged() |
355 /*! |
372 /*! |
356 Returns the amount of audio data processed since start() |
373 Returns the amount of audio data processed since start() |
357 was called in microseconds. |
374 was called in microseconds. |
358 */ |
375 */ |
359 |
376 |
360 qint64 QAudioInput::totalTime() const |
377 qint64 QAudioInput::processedUSecs() const |
361 { |
378 { |
362 return d->totalTime(); |
379 return d->processedUSecs(); |
363 } |
380 } |
364 |
381 |
365 /*! |
382 /*! |
366 Returns the microseconds since start() was called, including time in Idle and |
383 Returns the microseconds since start() was called, including time in Idle and |
367 Suspend states. |
384 Suspend states. |
368 */ |
385 */ |
369 |
386 |
370 qint64 QAudioInput::clock() const |
387 qint64 QAudioInput::elapsedUSecs() const |
371 { |
388 { |
372 return d->clock(); |
389 return d->elapsedUSecs(); |
373 } |
390 } |
374 |
391 |
375 /*! |
392 /*! |
376 Returns the error state. |
393 Returns the error state. |
377 */ |
394 */ |