Hi,
Here is a class that acts like SC's DetectSilence:
#!/usr/bin/env python
# encoding: utf-8
from pyo import *
s = Server().boot()
class DetectSilence:
def __init__(self, input, thresh=0.001, time=0.1, callback=None):
self.input = input
self.thresh = thresh
self.callback = callback
# little bit tricky just to get the sampling rate!
sr = input.getBaseObjects()[0].getServer().getSamplingRate()
# waiting time in samples
timeInSamps = int(time * sr)
# envelope follower
self.follow = Follower(self.input)
# if follower is below threshold, self.gate holds 1, as audio
# stream. Otherwise it holds 0
self.gate = self.follow < self.thresh
# send a trigger to reset the counter on both threshold crossing
self.reset = Select(self.gate, value=[0,1]).mix(1)
self.count = Count(self.reset)
# if the count reach timeInSamps while the gate holds 1 (below
# threshold), send a trigger to call self.detected method
self.trig = Select(self.count, value=timeInSamps, mul=self.gate)
self.func = TrigFunc(self.trig, self.detected)
def detected(self):
# if callback is defined, call it
if self.callback != None:
self.callback()
# otherwise just print to the console
else:
print "Silence detected!"
# play the table once...
t = SndTable(SNDS_PATH+"/transparent.aif")
src = TableRead(t, t.getRate()).out()
#src = Input(0)
# callback on detected silence
def replay():
src.out()
silence = DetectSilence(src, thresh=0.001, time=0.1, callback=replay)
s.gui(locals())