Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # open a microphone in pyAudio and listen for taps
  4.  
  5. import pyaudio
  6. import struct
  7. import math
  8.  
  9. INITIAL_TAP_THRESHOLD = 0.010
  10. FORMAT = pyaudio.paInt16
  11. SHORT_NORMALIZE = (1.0/32768.0)
  12. CHANNELS = 2
  13. RATE = 44100
  14. INPUT_BLOCK_TIME = 0.05
  15. INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)
  16. # if we get this many noisy blocks in a row, increase the threshold
  17. OVERSENSITIVE = 15.0/INPUT_BLOCK_TIME
  18. # if we get this many quiet blocks in a row, decrease the threshold
  19. UNDERSENSITIVE = 120.0/INPUT_BLOCK_TIME
  20. # if the noise was longer than this many blocks, it's not a 'tap'
  21. MAX_TAP_BLOCKS = 0.15/INPUT_BLOCK_TIME
  22.  
  23. def get_rms( block ):
  24. # RMS amplitude is defined as the square root of the
  25. # mean over time of the square of the amplitude.
  26. # so we need to convert this string of bytes into
  27. # a string of 16-bit samples...
  28.  
  29. # we will get one short out for each
  30. # two chars in the string.
  31. count = len(block)/2
  32. format = "%dh"%(count)
  33. shorts = struct.unpack( format, block )
  34.  
  35. # iterate over the block.
  36. sum_squares = 0.0
  37. for sample in shorts:
  38. # sample is a signed short in +/- 32768.
  39. # normalize it to 1.0
  40. n = sample * SHORT_NORMALIZE
  41. sum_squares += n*n
  42.  
  43. return math.sqrt( sum_squares / count )
  44.  
  45. class TapTester(object):
  46. def __init__(self):
  47. self.pa = pyaudio.PyAudio()
  48. self.stream = self.open_mic_stream()
  49. self.tap_threshold = INITIAL_TAP_THRESHOLD
  50. self.noisycount = MAX_TAP_BLOCKS+1
  51. self.quietcount = 0
  52. self.errorcount = 0
  53.  
  54. def stop(self):
  55. self.stream.close()
  56.  
  57. def find_input_device(self):
  58. device_index = None
  59. for i in range( self.pa.get_device_count() ):
  60. devinfo = self.pa.get_device_info_by_index(i)
  61. print( "Device %d: %s"%(i,devinfo["name"]) )
  62.  
  63. for keyword in ["mic","input"]:
  64. if keyword in devinfo["name"].lower():
  65. print( "Found an input: device %d - %s"%(i,devinfo["name"]) )
  66. device_index = i
  67. return device_index
  68.  
  69. if device_index == None:
  70. print( "No preferred input found; using default input device." )
  71.  
  72. return device_index
  73.  
  74. def open_mic_stream( self ):
  75. device_index = self.find_input_device()
  76.  
  77. stream = self.pa.open( format = FORMAT,
  78. channels = CHANNELS,
  79. rate = RATE,
  80. input = True,
  81. input_device_index = device_index,
  82. frames_per_buffer = INPUT_FRAMES_PER_BLOCK)
  83.  
  84. return stream
  85.  
  86. def tapDetected(self):
  87. print("Tap!")
  88.  
  89. def listen(self):
  90. try:
  91. block = self.stream.read(INPUT_FRAMES_PER_BLOCK)
  92. except IOError as e:
  93. # dammit.
  94. self.errorcount += 1
  95. print( "(%d) Error recording: %s"%(self.errorcount,e) )
  96. self.noisycount = 1
  97. return
  98.  
  99. amplitude = get_rms( block )
  100. if amplitude > self.tap_threshold:
  101. # noisy block
  102. self.quietcount = 0
  103. self.noisycount += 1
  104. if self.noisycount > OVERSENSITIVE:
  105. # turn down the sensitivity
  106. self.tap_threshold *= 1.1
  107. else:
  108. # quiet block.
  109.  
  110. if 1 <= self.noisycount <= MAX_TAP_BLOCKS:
  111. self.tapDetected()
  112. self.noisycount = 0
  113. self.quietcount += 1
  114. if self.quietcount > UNDERSENSITIVE:
  115. # turn up the sensitivity
  116. self.tap_threshold *= 0.9
  117.  
  118. if __name__ == "__main__":
  119. tt = TapTester()
  120.  
  121. for i in range(1000):
  122. tt.listen()
  123.  
  124. import pyaudio
  125. import struct
  126. import math
  127.  
  128. INITIAL_TAP_THRESHOLD = 0.010
  129. FORMAT = pyaudio.paInt16
  130. SHORT_NORMALIZE = (1.0/32768.0)
  131. CHANNELS = 2
  132. RATE = 44100
  133. INPUT_BLOCK_TIME = 0.05
  134. INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)
  135.  
  136. OVERSENSITIVE = 15.0/INPUT_BLOCK_TIME
  137.  
  138. UNDERSENSITIVE = 120.0/INPUT_BLOCK_TIME # if we get this many quiet blocks in a row, decrease the threshold
  139.  
  140. MAX_TAP_BLOCKS = 0.15/INPUT_BLOCK_TIME # if the noise was longer than this many blocks, it's not a 'tap'
  141.  
  142. def get_rms(block):
  143.  
  144. # RMS amplitude is defined as the square root of the
  145. # mean over time of the square of the amplitude.
  146. # so we need to convert this string of bytes into
  147. # a string of 16-bit samples...
  148.  
  149. # we will get one short out for each
  150. # two chars in the string.
  151. count = len(block)/2
  152. format = "%dh"%(count)
  153. shorts = struct.unpack( format, block )
  154.  
  155. # iterate over the block.
  156. sum_squares = 0.0
  157. for sample in shorts:
  158. # sample is a signed short in +/- 32768.
  159. # normalize it to 1.0
  160. n = sample * SHORT_NORMALIZE
  161. sum_squares += n*n
  162.  
  163. return math.sqrt( sum_squares / count )
  164.  
  165. pa = pyaudio.PyAudio() #]
  166. #|
  167. stream = pa.open(format = FORMAT, #|
  168. channels = CHANNELS, #|---- You always use this in pyaudio...
  169. rate = RATE, #|
  170. input = True, #|
  171. frames_per_buffer = INPUT_FRAMES_PER_BLOCK) #]
  172.  
  173. tap_threshold = INITIAL_TAP_THRESHOLD #]
  174. noisycount = MAX_TAP_BLOCKS+1 #|---- Variables for noise detector...
  175. quietcount = 0 #|
  176. errorcount = 0 #]
  177.  
  178. for i in range(1000):
  179. try: #]
  180. block = stream.read(INPUT_FRAMES_PER_BLOCK) #|
  181. except IOError, e: #|---- just in case there is an error!
  182. errorcount += 1 #|
  183. print( "(%d) Error recording: %s"%(errorcount,e) ) #|
  184. noisycount = 1 #]
  185.  
  186. amplitude = get_rms(block)
  187. if amplitude > tap_threshold: # if its to loud...
  188. quietcount = 0
  189. noisycount += 1
  190. if noisycount > OVERSENSITIVE:
  191. tap_threshold *= 1.1 # turn down the sensitivity
  192.  
  193. else: # if its to quiet...
  194.  
  195. if 1 <= noisycount <= MAX_TAP_BLOCKS:
  196. print 'tap!'
  197. noisycount = 0
  198. quietcount += 1
  199. if quietcount > UNDERSENSITIVE:
  200. tap_threshold *= 0.9 # turn up the sensitivity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement