Guest User

Untitled

a guest
Oct 17th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. Traceback (most recent call last):
  2. File "/home/pi/pythonsound/record.py", line 35, in <module>
  3. data = stream.read(CHUNK)
  4. File "/usr/local/lib/python2.7/dist-packages/pyaudio.py", line 605, in read
  5. return pa.read_stream(self._stream, num_frames)
  6. IOError: [Errno Input overflowed] -9981
  7.  
  8. import pyaudio
  9. import wave
  10. import sys
  11. CHUNK = 1024
  12. FORMAT = pyaudio.paInt16
  13. CHANNELS = 1
  14. RATE = 44100
  15. RECORD_SECONDS = 5
  16.  
  17. WAVE_OUTPUT_FILENAME = "output.wav"
  18. p = pyaudio.PyAudio()
  19. stream = p.open(format=FORMAT,
  20. channels=CHANNELS,
  21. rate=RATE,
  22. input=True,
  23. frames_per_buffer=CHUNK)
  24. print("* recording")
  25. frames = []
  26.  
  27. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  28. data = stream.read(CHUNK)
  29. frames.append(data)
  30.  
  31. print("* done recording")
  32. stream.stop_stream()
  33. stream.close()
  34. p.terminate()
  35.  
  36. wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
  37. wf.setnchannels(CHANNELS)
  38. wf.setsampwidth(p.get_sample_size(FORMAT))
  39. wf.setframerate(RATE)
  40. wf.writeframes(b''.join(frames))
  41. wf.close()
  42.  
  43. {'defaultSampleRate': 44100.0,
  44. 'defaultLowOutputLatency': 0.011609977324263039,
  45. 'defaultLowInputLatency': 0.011609977324263039,
  46. 'maxInputChannels': 1L,
  47. 'structVersion': 2L,
  48. 'hostApi': 0L,
  49. 'index': 0,
  50. 'defaultHighOutputLatency': 0.046439909297052155,
  51. 'maxOutputChannels': 2L,
  52. 'name': u
  53. 'USB PnP Sound Device: USB Audio (hw:0,0)',
  54. 'defaultHighInputLatency': 0.046439909297052155}
  55.  
  56. IOError: [Errno Input overflowed] -9981
  57.  
  58. import pyaudio, wave, time, sys
  59. from datetime import datetime
  60.  
  61. CHUNK = 8192
  62. FORMAT = pyaudio.paInt16
  63. CHANNELS = 1
  64. RATE = 44100
  65. RECORD_SECONDS = 5
  66.  
  67. current_time = str(datetime.now()) #"Date/Time for File Name"
  68. current_time = "_".join(current_time.split()).replace(":","-")
  69. current_time = current_time[:-7]
  70. WAVE_OUTPUT_FILENAME = 'Audio_'+current_time+'.wav'
  71.  
  72. p = pyaudio.PyAudio()
  73.  
  74. stream = p.open(format=FORMAT, channels = CHANNELS, rate = RATE, input = True, input_device_index = 0, frames_per_buffer = CHUNK)
  75.  
  76. print("* recording")
  77.  
  78. frames = []
  79. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  80. print i
  81. data = stream.read(CHUNK)
  82. frames.append(data)
  83.  
  84. print("* done recording")
  85.  
  86. stream.stop_stream()
  87. stream.close()
  88. p.terminate()
  89.  
  90. wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
  91. wf.setnchannels(CHANNELS)
  92. wf.setsampwidth(p.get_sample_size(FORMAT))
  93. wf.setframerate(RATE)
  94. wf.writeframes(b''.join(frames))
  95. wf.close()
  96.  
  97. import pyaudio
  98. import wave
  99. import datetime
  100. import signal
  101. import ftplib
  102. import sys
  103. import os
  104.  
  105. # configuration for assos_listen
  106. import config
  107.  
  108.  
  109. # run the audio capture and send sound sample processes
  110. # in parallel
  111. from multiprocessing import Process
  112.  
  113. # CONFIG
  114. CHUNK = config.chunkSize
  115. FORMAT = pyaudio.paInt16
  116. CHANNELS = 1
  117. RATE = config.samplingRate
  118. RECORD_SECONDS = config.sampleLength
  119.  
  120. # HELPER FUNCTIONS
  121.  
  122. # write to ftp
  123. def uploadFile(filename):
  124.  
  125. print("start uploading file: " + filename)
  126. # connect to container
  127. ftp = ftplib.FTP(config.ftp_server_ip, config.username, config.password)
  128.  
  129. # write file
  130. ftp.storbinary('STOR '+filename, open(filename, 'rb'))
  131. # close connection
  132. ftp.quit()
  133. print("finished uploading: " +filename)
  134.  
  135.  
  136. # write to sd-card
  137. def storeFile(filename,frames):
  138.  
  139. print("start writing file: " + filename)
  140. wf = wave.open(filename, 'wb')
  141. wf.setnchannels(CHANNELS)
  142. wf.setsampwidth(p.get_sample_size(FORMAT))
  143. wf.setframerate(RATE)
  144. wf.writeframes(b''.join(frames))
  145. wf.close()
  146. print(filename + " written")
  147.  
  148.  
  149. # abort the sampling process
  150. def signal_handler(signal, frame):
  151. print('You pressed Ctrl+C!')
  152.  
  153. # close stream and pyAudio
  154. stream.stop_stream()
  155. stream.close()
  156. p.terminate()
  157.  
  158. sys.exit(0)
  159.  
  160. # MAIN FUNCTION
  161. def recordAudio(p, stream):
  162.  
  163. sampleNumber = 0
  164. while (True):
  165. print("* recording")
  166. sampleNumber = sampleNumber +1
  167.  
  168. frames = []
  169. startDateTimeStr = datetime.datetime.now().strftime("%Y_%m_%d_%I_%M_%S_%f")
  170. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  171. data = stream.read(CHUNK)
  172. frames.append(data)
  173.  
  174. fileName = str(config.sensorID) + "_" + startDateTimeStr + ".wav"
  175.  
  176. # create a store process to write the file in parallel
  177. storeProcess = Process(target=storeFile, args=(fileName,frames))
  178. storeProcess.start()
  179.  
  180. if (config.upload == True):
  181. # since waiting for the upload to finish will take some time
  182. # and we do not want to have gaps in our sample
  183. # we start the upload process in parallel
  184. print("start uploading...")
  185. uploadProcess = Process(target=uploadFile, args=(fileName,))
  186. uploadProcess.start()
  187.  
  188.  
  189.  
  190. # ENTRYPOINT FROM CONSOLE
  191. if __name__ == '__main__':
  192.  
  193. p = pyaudio.PyAudio()
  194. stream = p.open(format=FORMAT,
  195. channels=CHANNELS,
  196. rate=RATE,
  197. input=True,
  198. frames_per_buffer=CHUNK)
  199.  
  200.  
  201. # directory to write and read files from
  202. os.chdir(config.storagePath)
  203.  
  204. # abort by pressing C
  205. signal.signal(signal.SIGINT, signal_handler)
  206. print('nn--------------------------npress Ctrl+C to stop the recording')
  207.  
  208. # start recording
  209. recordAudio(p, stream)
  210.  
  211. ### configuration file for assos_listen
  212. # upload
  213. upload = False
  214.  
  215. # config for this sensor
  216. sensorID = "al_01"
  217.  
  218. # sampling rate & chunk size
  219. chunkSize = 8192
  220. samplingRate = 44100 # 44100 needed for Aves sampling
  221. # choices=[4000, 8000, 16000, 32000, 44100] :: default 16000
  222.  
  223. # sample length in seconds
  224. sampleLength = 10
  225.  
  226. # configuration for assos_store container
  227. ftp_server_ip = "192.168.0.157"
  228. username = "sensor"
  229. password = "sensor"
  230.  
  231. # storage on assos_listen device
  232. storagePath = "/home/pi/assos_listen_pi/storage/"
Add Comment
Please, Sign In to add comment