import multiprocessing import time import os import wave import pyaudio from cStringIO import StringIO from itertools import cycle dir = '/home/mohit/Music/' p = pyaudio.PyAudio() def worker(time_now, buff): print 'worker ',multiprocessing.current_process().name,' called @ ',time.strftime('%H-%M-%S', time.localtime(time.time())) fname = os.path.abspath(dir+"/"+str(time_now)+".wav") # Create Wave file and set its parameters wf = wave.open(fname, 'wb') wf.setnchannels(1) wf.setsampwidth(p.get_sample_size(pyaudio.paInt16)) wf.setframerate(48000) # Write StringIO to the wavefile wf.writeframes(buff.read()) # Sleep.. just.. time.sleep(2) wf.close() print 'worker ',multiprocessing.current_process().name,' Done' if __name__ == '__main__': all_time = [] jobs = [] chunk = 4096 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 48000 buff1 = StringIO() buff2 = StringIO() counter = 0 # Alternate between 0 and 1 alternate = cycle(range(2)) # Open the audio stream (Change the Index value) stream = p.open(input_device_index=3, format = FORMAT, channels = CHANNELS, rate = RATE, input = True, frames_per_buffer = chunk) # Record Continuously while True: # Toggle the value and check if it is 1 if alternate.next(): print 'Writing to Buffer 1' while True: # Write to StringIO (It is the Fastest) buff1.write(stream.read(chunk)) unique_time = time.strftime('%H: %M: %S') time_now = time.time() current_minute = time.strftime('%S') # If current second = 30, append the current time # and reset the buffer position, start the process # and break the loop if (current_minute == '30' and unique_time not in all_time): all_time.append(str(unique_time)) buff1.seek(0) pr = multiprocessing.Process(target=worker, args=(time_now,buff1)) pr.start() break # If the toggle value = 0 else: print 'Writing to Buffer 2' while True: # Write to StringIO (It is the Fastest) buff2.write(stream.read(chunk)) unique_time = time.strftime('%H: %M: %S') time_now = time.time() current_minute = time.strftime('%S') # If current second = 30, append the current time # and reset the buffer position, start the process # and break the loop if (current_minute == '30' and unique_time not in all_time): all_time.append(unique_time) buff2.seek(0) pr = multiprocessing.Process(target=worker, args=(time_now,buff2)) pr.start() break
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question