def _set_source(self): """Data source: file_in, array, or microphone """ if os.path.isfile(self.file_in): _rate, self._sndTable = table_from_file(self.file_in, start=self.config['start'], stop=self.config['stop']) if _rate != self.rate: print('file sample rate differs from the voice-key rate.') self._source = pyo.TableRead(self._sndTable, freq=self._sndTable.getRate(), mul=self.config['vol']) self.sec = self._sndTable.getDur() elif len(self.array_in): self._sndTable = table_from_samples(self.array_in, start=self.config['start'], stop=self.config['stop'], rate=self.rate) self._source = pyo.TableRead(self._sndTable, freq=self._sndTable.getRate(), mul=self.config['vol']) self.sec = self._sndTable.size / self.rate else: # fall through to source = microphone ch = self.config['chnl_in'] self._source = pyo.Input(chnl=ch, mul=self.config['vol'])
def pyo_init(rate=44100, nchnls=1, buffersize=32, duplex=1): """Start and boot a global pyo server, restarting if needed. """ global pyo_server if rate < 16000: raise ValueError('sample rate must be 16000 or higher') # re-init if hasattr(pyo_server, 'shutdown'): pyo_server.stop() sleep(0.25) #make sure enough time passes for the server to shutdown pyo_server.shutdown() sleep(0.25) pyo_server.reinit(sr=rate, nchnls=nchnls, buffersize=buffersize, duplex=duplex) else: pyo_server = pyo.Server( sr=rate, nchnls=nchnls, # 1 = mono buffersize=buffersize, # ideal = 64 or higher duplex=duplex) # 1 = input + output pyo_server.boot().start() # avoid mac issue of losing first 0.5s if no sound played for ~1 minute: if sys.platform == 'darwin': z2 = np.zeros(2) _sndTable = pyo.DataTable(size=2, init=z2.T.tolist(), chnls=nchnls) _snd = pyo.TableRead(_sndTable, freq=rate, mul=0) _snd.play() time.sleep(0.510)