src/multimedia/audio/qaudioinput_win32_p.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
parent 4 3b1da2848fc7
child 13 c0432d11811c
--- a/src/multimedia/audio/qaudioinput_win32_p.cpp	Mon Mar 15 12:43:09 2010 +0200
+++ b/src/multimedia/audio/qaudioinput_win32_p.cpp	Thu Apr 08 14:19:33 2010 +0300
@@ -192,9 +192,11 @@
         //set to pull mode
         pullMode = true;
         audioSource = device;
+        deviceState = QAudio::ActiveState;
     } else {
         //set to push mode
         pullMode = false;
+        deviceState = QAudio::IdleState;
         audioSource = new InputPrivate(this);
         audioSource->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
     }
@@ -306,7 +308,6 @@
     elapsedTimeOffset = 0;
     totalTimeValue = 0;
     errorState  = QAudio::NoError;
-    deviceState = QAudio::ActiveState;
     return true;
 }
 
@@ -315,9 +316,9 @@
     if(deviceState == QAudio::StoppedState)
         return;
 
+    deviceState = QAudio::StoppedState;
     waveInReset(hWaveIn);
     waveInClose(hWaveIn);
-    deviceState = QAudio::StoppedState;
 
     int count = 0;
     while(!finished && count < 500) {
@@ -352,7 +353,6 @@
     char*  p = data;
     qint64 l = 0;
     qint64 written = 0;
-
     while(!done) {
         // Read in some audio data
         if(waveBlocks[header].dwBytesRecorded > 0 && waveBlocks[header].dwFlags & WHDR_DONE) {
@@ -373,11 +373,12 @@
                     errorState = QAudio::IOError;
 
                 } else {
-                    totalTimeValue += waveBlocks[header].dwBytesRecorded
-                        /((settings.channels()*settings.sampleSize()/8))
-                        *10000/settings.frequency()*100;
+                    totalTimeValue += waveBlocks[header].dwBytesRecorded;
                     errorState = QAudio::NoError;
-                    deviceState = QAudio::ActiveState;
+                    if (deviceState != QAudio::ActiveState) {
+                        deviceState = QAudio::ActiveState;
+                        emit stateChanged(deviceState);
+                    }
 		    resuming = false;
                 }
             } else {
@@ -387,16 +388,17 @@
 #ifdef DEBUG_AUDIO
                 qDebug()<<"IN: "<<waveBlocks[header].dwBytesRecorded<<", OUT: "<<l;
 #endif
-                totalTimeValue += waveBlocks[header].dwBytesRecorded
-                    /((settings.channels()*settings.sampleSize()/8))
-                    *10000/settings.frequency()*100;
+                totalTimeValue += waveBlocks[header].dwBytesRecorded;
                 errorState = QAudio::NoError;
-                deviceState = QAudio::ActiveState;
+                if (deviceState != QAudio::ActiveState) {
+                    deviceState = QAudio::ActiveState;
+                    emit stateChanged(deviceState);
+                }
 		resuming = false;
             }
         } else {
             //no data, not ready yet, next time
-            return 0;
+            break;
         }
 
         waveInUnprepareHeader(hWaveIn,&waveBlocks[header], sizeof(WAVEHDR));
@@ -505,7 +507,13 @@
 
 qint64 QAudioInputPrivate::processedUSecs() const
 {
-    return totalTimeValue;
+    if (deviceState == QAudio::StoppedState)
+        return 0;
+    qint64 result = qint64(1000000) * totalTimeValue /
+        (settings.channels()*(settings.sampleSize()/8)) /
+        settings.frequency();
+
+    return result;
 }
 
 void QAudioInputPrivate::suspend()
@@ -535,6 +543,9 @@
     QTime now(QTime::currentTime());
     qDebug()<<now.second()<<"s "<<now.msec()<<"ms :deviceReady() INPUT";
 #endif
+    if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState)
+        return true;
+
     if(pullMode) {
         // reads some audio data and writes it to QIODevice
         read(0,0);
@@ -543,8 +554,6 @@
 	InputPrivate* a = qobject_cast<InputPrivate*>(audioSource);
 	a->trigger();
     }
-    if(deviceState != QAudio::ActiveState)
-        return true;
 
     if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) {
         emit notify();
@@ -577,7 +586,8 @@
 qint64 InputPrivate::readData( char* data, qint64 len)
 {
     // push mode, user read() called
-    if(audioDevice->deviceState != QAudio::ActiveState)
+    if(audioDevice->deviceState != QAudio::ActiveState &&
+            audioDevice->deviceState != QAudio::IdleState)
         return 0;
     // Read in some audio data
     return audioDevice->read(data,len);