def voiceRecorder(secs, name): f = open(name, 'wb') # Minimum set of parameters we need to create Encoder cparams = { 'id': acodec.getCodecId('mp3'), 'bitrate': 128000, 'sample_rate': 44100, 'channels': 2 } ac = acodec.Encoder(cparams) snd = sound.Input(44100, 2, sound.AFMT_S16_LE) snd.start() # Loop until recorded position greater than the limit specified while snd.getPosition() <= secs: s = snd.getData() if s and len(s): for fr in ac.encode(s): # We definitely should use mux first, but for # simplicity reasons this way it'll work also f.write(fr) else: time.sleep(.003) # Stop listening the incoming sound from the microphone or line in snd.stop()
def run(self): f = open(self.file_name, 'wb') # Minimum set of parameters we need to create Encoder # getCodecID() cparams = { 'id': acodec.getCodecID('mp3'), 'bitrate': self.bitrate, 'sample_rate': 44100, 'channels': 2 } ac = acodec.Encoder(cparams) #print sound.getODevices() #print sound.getIDevices() snd = sound.Input(44100, 2, sound.AFMT_S16_LE, 0) snd.start() # Loop until recorded position greater than the limit specified #while snd.getPosition()<= secs: while self.status == 'record': #print snd.getPosition() s = snd.getData() if s and len(s): for fr in ac.encode(s): # We definitely should use mux first, but for # simplicity reasons this way it'll work also f.write(fr) else: time.sleep(.003) #print 'boo' # Stop listening the incoming sound from the microphone or line in snd.stop()
def __init__(self, sample_rate=44100, channels=2, format="S16_LE"): super(SoundInput, self).__init__() pformat = mapping_format_to_pymedia[format] self.snd = sound.Input(sample_rate, channels, pformat) self.sample_rate = sample_rate self.channels = channels self.format = format
def __init__(self, sample_rate=44100, channels=2, format="S16_LE"): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(Input, self).__init__() pformat = format2PyMediaFormat[format] self.snd = sound.Input(sample_rate, channels, pformat) self.sample_rate = sample_rate self.channels = channels self.format = format
def __init__(self, audio_id, out_dir='.'): threading.Thread.__init__(self) self.stop = threading.Event() self.recording = threading.Event() self.stopped = threading.Event() self.filename = out_dir+'/'+audio_id+'.mp3' self.encoder = acodec.Encoder({ 'id': acodec.getCodecID('mp3'), 'bitrate': 96000, 'sample_rate': 44100, 'channels': 2 }) self.recorder = sound.Input(44100, 2, sound.AFMT_S16_LE)
def __init__(self, output_file="default.wav", channels=2, samplerate=44100, filetype="wav"): self.output_file = output_file self.channels = int(channels) self.samplerate = int(samplerate) self.filetype = filetype self._format = sound.AFMT_S16_LE self._recording = False self._allowed_filetypes = ["wav", "mp3"] #ogg to be added self.input = sound.Input(self.samplerate, self.channels, self._format) threading.Thread.__init__(self)
def run(self): print "recording to", self.name self.f = open(self.name, 'wb') # Minimum set of parameters we need to create Encoder cparams = { 'id': acodec.getCodecID('mp3'), 'bitrate': 128000, 'sample_rate': 44100, 'channels': 1 } self.ac = acodec.Encoder(cparams) self.snd = sound.Input(44100, 1, sound.AFMT_S16_LE) self.mux = muxer.Muxer("mp3") self.stream_index = self.mux.addStream(muxer.CODEC_TYPE_AUDIO, self.ac.getParams()) block = self.mux.start() if block: self.f.write(block) # Loop until Ctrl-C pressed or finished set from outside self.finished = False thread = threading.Thread(target=self.record) thread.start() try: while not self.finished: time.sleep(.003) except KeyboardInterrupt: self.finished = True print "finishing recording to", self.name # Stop listening the incoming sound from the microphone or line in thread.join() footer = self.mux.end() if footer is not None: self.f.write(footer) self.f.close() print "finished recording to", self.name print "snipping leading zeroes..." f = open(self.name, "rb") buffer = f.read() f.close() buffer = buffer.lstrip(chr(0)) f = open(self.name, "wb") f.write(buffer) f.close() print "snipped leading zeroes"
def voiceRecorderPlayer(secs, delay, interval, name): # Turn on the microphone input open(name, 'wb').close() mixer = sound.Mixer() controls = mixer.getControls() micControls= filter( lambda x: \ x[ 'connection' ]== 'Microphone' and \ x[ 'destination' ]== 'Recording Control' and \ x[ 'name' ].count( 'Volume' )> 0, controls ) if len(micControls): micControls[0]['control'].setActive() print 'Setting microphone input as active' # Minimum set of parameters we need to create Encoder cparams = { 'id': acodec.getCodecID('mp3'), 'bitrate': 128000, 'sample_rate': 44100, 'channels': 2 } ac = acodec.Encoder(cparams) dm = muxer.Demuxer('mp3') dc = acodec.Decoder(cparams) sndIn = sound.Input(44100, 2, sound.AFMT_S16_LE) sndOut = sound.Output(44100, 2, sound.AFMT_S16_LE) sndIn.start() bytesWritten = 0 bytesRead = 0 t = time.time() paused = False # Loop until recorded position greater than the limit specified while sndIn.getPosition() <= secs: s = sndIn.getData() if s and len(s): f = open(name, 'ab') for fr in ac.encode(s): # We definitely should use mux first, but for # simplicity reasons this way it'll work also f.write(fr) bytesWritten += len(fr) f.close() else: # 1/10 of the second buffer for playback if sndOut.getLeft() < 0.1 and not paused: if bytesRead < bytesWritten + READ_CHUNK: f = open(name, 'rb') f.seek(bytesRead) s = f.read(READ_CHUNK) f.close() bytesRead += len(s) frames = dm.parse(s) if frames: for fr in frames: r = dc.decode(fr[1]) if r and r.data: sndOut.play(r.data) else: time.sleep(.01) # See if pause should be working i = int((time.time() - t) % (interval + delay)) paused = i > interval # Stop listening the incoming sound from the microphone or line in sndIn.stop()