def record(self, sec, block=True): """Capture sound input for duration <sec>, save to a file. Return the path/name to the new file. Uses onset time (epoch) as a meaningful identifier for filename and log. """ RECORD_SECONDS = float(sec) self.onset = time.time() # note: report onset time in log, and use in filename logging.data('%s: Record: onset %.3f, capture %.3fs' % (self.loggingId, self.onset, RECORD_SECONDS) ) self.savedFile = self.wavOutFilename.replace(ONSET_TIME_HERE, '-%.3f' % self.onset) inputter = Input(chnl=0, mul=1) # chnl=[0,1] for stereo input t0 = time.time() # launch the recording, saving to file: recorder = Record(inputter, self.savedFile, chnls=2, fileformat=0, # .wav format sampletype=0, buffering=4) # 4 is default # launch recording, block as needed, and clean up: clean = Clean_objects(RECORD_SECONDS, recorder) # set up to stop recording clean.start() # the timer starts now, ends automatically whether block or not if block: time.sleep(RECORD_SECONDS - 0.0008) # Clean_objects() set-up takes ~0.0008s, for me logging.exp('%s: Record: stop. %.3f, capture %.3fs (est)' % (self.loggingId, time.time(), time.time() - t0) ) else: logging.exp('%s: Record: return immediately, no blocking' % (self.loggingId) ) self.duration = RECORD_SECONDS # used in playback() return self.savedFile # filename, or None
class _Recorder(object): """Class for internal object to make an audio recording using pyo. Never needed by end-users; only used internally in __init__: self.recorder = _Recorder(None) # instantiate, global Then in record(), do: self.recorder.run(file, sec) This sets recording parameters, starts recording. This class never handles blocking; SimpleAudioCapture has to do that. Motivation: Doing pyo Record from within a function worked most of the time, but failed catastrophically ~1% of time with a bus error. Seemed to be due to a namespace scoping issue, which using globals seemed to fix; see pyo mailing list, 7 April 2012. This draws heavily on Olivier Belanger's solution. """ def __init__(self, file, sec=0, sampletype=0): self.running = False if file: inputter = Input(chnl=0, mul=1) recorder = Record(inputter, file, chnls=2, fileformat=0, sampletype=sampletype, buffering=4) self.clean = Clean_objects(sec, recorder) def run(self, file, sec, sampletype): self.__init__(file, sec, sampletype) self.running = True self.clean.start() # controls recording onset (now) and offset (later) threading.Timer(sec, self.stop).start() # set running flag False def stop(self): self.running = False
def record_midi_wav(mid_): # write mid_ to file with open("test.mid", "wb") as bin_file: mid_.writeFile(bin_file) s = Server().boot().start() # create app synth midi_reader = Notein() amp = MidiAdsr(midi_reader['velocity']) pit = MToF(midi_reader['pitch']) osc = SineLoop(freq=pit, feedback=0, mul=amp).mix(1) rev = STRev(osc, revtime=1, cutoff=4000, bal=0.2).out() # create recorder rec = Record(rev, buffering=2, filename='beat.wav', quality=0.4) clean = Clean_objects(12, rec) clean.start() midi_reader = MidiFile('test.mid') # makeshift infinite loop for message in midi_reader.play(): s.addMidiEvent(*message.bytes()) s.stop()
class _Recorder(object): """Class for internal object to make an audio recording using pyo. Never needed by end-users; only used internally in __init__: self.recorder = _Recorder(None) # instantiate, global Then in record(), do: self.recorder.run(file, sec) This sets recording parameters, starts recording. To stop a recording that is in progress, do self.stop() This class never handles blocking; AudioCapture has to do that. Motivation: Doing pyo Record from within a function worked most of the time, but failed catastrophically ~1% of time with a bus error. Seemed to be due to a namespace scoping issue, which using globals seemed to fix; see pyo mailing list, 7 April 2012. This draws heavily on Olivier Belanger's solution. """ def __init__(self, file, sec=0, sampletype=0): self.running = False if file: inputter = Input(chnl=0, mul=1) self.recorder = Record(inputter, file, chnls=2, fileformat=0, sampletype=sampletype, buffering=4) self.clean = Clean_objects(sec, self.recorder) def run(self, file, sec, sampletype): self.__init__(file, sec, sampletype) self.running = True self.clean.start( ) # controls recording onset (now) and offset (later) threading.Timer(sec, self._stop).start() # set running flag False def stop(self): self.recorder.stop() self._stop() def _stop(self): self.running = False