Commits

anatoly techtonik  committed 609eb2a

Remove 100% CPU usage by sleeping during buffer playback time.

  • Participants
  • Parent commits d0b0a97
  • Tags 0.5-no-cpu-load

Comments (0)

Files changed (1)

File audiosocket.py

 0.4 - playback lag is killed by double buffering, still 100% CPU
       usage because of constant polling to check for processed
       blocks
+0.5 - remove 100% CPU usage by sleeping while a block is playing
 """
 
 import sys
+import time
 
 DEBUG = False
 def debug(msg):
     self.headers = [WAVEHDR(), WAVEHDR()]
 
     #: configurable size of chunks (data blocks) read from input stream
-    self.CHUNKSIZE = 100 * 2**10
+    self.BUFSIZE = 100 * 2**10
+
+    # Buffer playback time. The time after which the buffer is free
+    self.BUFPLAYTIME = float(self.BUFSIZE) / 176400  # AvgBytesPerSec
 
   def open(self):
     """ 1. Open default wave device, tune it for the incoming data flow
         if stopping:
           break
         debug("scheduling block %d" % i)
-        data = stream.read(self.CHUNKSIZE)
+        data = stream.read(self.BUFSIZE)
         if len(data) == 0:
           stopping = True
           break
       # waiting until buffer playback is finished by constantly polling
       # its status eats 100% CPU time. this counts how many checks are made
       pollsnum = 0
+      # avoid 100% CPU usage - with this pollsnum won't be greater than 1
+      time.sleep(self.BUFPLAYTIME)
 
       while True:
         pollsnum += 1
         if ret != MMSYSERR_NOERROR:
           sys.exit('Error: waveOutUnprepareHeader failed with code 0x%x' % ret)
         break
-      print "%s checks" % pollsnum
+      debug("  %s check(s)" % pollsnum)
 
       # Switch waiting pointer to the next block
       curblock = (curblock + 1) % len(self.headers)