Advertisement
Guest User

Untitled

a guest
Dec 30th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. import pyaudio
  2. import math
  3. import struct
  4. import wave
  5. import time
  6. import os
  7. import threading
  8.  
  9. #modify this param for decibels
  10. ThresholdStart = 250
  11. ThresholdEnd = 100
  12.  
  13. # lenght of sound
  14. #current interval can detect dog bark
  15. SOUND_LENGTH_MIN = 0.25
  16. SOUND_LENGTH_MAX = 0.35
  17. #modify this param for timeout after sound
  18. TIMEOUT_LENGTH = 0.05
  19. #modify this for how many time to replay
  20. Replay = 1
  21.  
  22.  
  23. # Audio settings
  24. SHORT_NORMALIZE = (1.0/32768.0)
  25. chunk = 512
  26. FORMAT = pyaudio.paInt16
  27. CHANNELS = 1
  28. RATE = 16000
  29. swidth = 2
  30.  
  31. #audio heard will be stored here
  32. rec = []
  33.  
  34.  
  35. # this will compute the power of the sound
  36. def rms(frame):
  37. count = len(frame) / swidth
  38. format = "%dh" % (count)
  39. shorts = struct.unpack(format, frame)
  40.  
  41. sum_squares = 0.0
  42. for sample in shorts:
  43. n = sample * SHORT_NORMALIZE
  44. sum_squares += n * n
  45. rms = math.pow(sum_squares / count, 0.5)
  46.  
  47. return rms * 1000
  48.  
  49.  
  50.  
  51.  
  52.  
  53. # this will record after detection
  54. def record(data):
  55. print('Sound detected, recording beginning')
  56. lrec = [data]
  57. start = current = time.time()
  58. end = time.time() + TIMEOUT_LENGTH
  59.  
  60.  
  61. while current <= end:
  62.  
  63. data = stream.read(chunk)
  64. myrms = rms(data)
  65.  
  66. if myrms >= ThresholdEnd: end = time.time() + TIMEOUT_LENGTH
  67.  
  68. current = time.time()
  69. lrec.append(data)
  70.  
  71. if SOUND_LENGTH_MIN <= current - start <= SOUND_LENGTH_MAX :
  72. print('Sound added')
  73. rec.append(lrec)
  74.  
  75. print ("time ", time.time() - start)
  76.  
  77.  
  78. print('End sound')
  79.  
  80.  
  81.  
  82.  
  83. #this will play the detected sound
  84. def play(playevent):
  85.  
  86. while True:
  87. # wait until there is something to play
  88. playevent.wait()
  89. playevent.clear()
  90. while rec :
  91. lrec = rec.pop(0)
  92. for i in range(Replay):
  93. for f in lrec:
  94. stream.write(f)
  95.  
  96.  
  97.  
  98.  
  99. #start the player thread
  100. playevent = threading.Event()
  101. threading.Thread(target=play, args=[playevent],daemon=True).start()
  102.  
  103.  
  104.  
  105. # init pyaudio
  106. p = pyaudio.PyAudio()
  107. stream = p.open(format=FORMAT,
  108. channels=CHANNELS,
  109. rate=RATE,
  110. input=True,
  111. output=True,
  112. frames_per_buffer=chunk)
  113.  
  114.  
  115. print('Listening beginning')
  116. while True:
  117. # get sound
  118. input = stream.read(chunk)
  119. # compute power
  120. rms_val = rms(input)
  121. # then start recording if we met our power we want
  122. if rms_val > ThresholdStart:
  123. record(input)
  124. playevent.set()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement