Ejemplo n.º 1
0
def filtered_square(delay, note, amp, dur=1.):

    a = np.sqrt(np.linspace(1., 0., 15))
    t = pyo.HarmTable(a.tolist())

    dur *= 2.

    env = pyo.Fader(fadein=.02, fadeout=0.02, dur=dur * 0.9,
                    mul=amp).play(dur=2.5 * dur, delay=delay)

    adsr = pyo.Adsr(attack=dur * 0.05,
                    sustain=0.707,
                    decay=0.1 * dur,
                    release=dur * 0.7,
                    dur=dur * 0.9,
                    mul=amp).play(dur=2.5 * dur, delay=delay)
    osc = pyo.Osc(t, freq=pyo.midiToHz(note), mul=adsr).mix(1)

    rev = pyo.Freeverb(osc, size=1., damp=0.5, bal=1.,
                       mul=env).play(dur=2.5 * dur, delay=delay)
    # rev.out(delay=delay, dur=dur)

    eq = pyo.Biquad(rev, freq=500, q=1., type=2).play(dur=2.5 * dur,
                                                      delay=delay)
    eq.out(delay=delay, dur=dur)
    # eq = None

    return osc, env, rev, eq
Ejemplo n.º 2
0
def filtered_saw(delay, note, amp, dur=1.):
    t = pyo.SawTable(order=15).normalize()

    dur *= 2.

    env = pyo.Fader(fadein=.02, fadeout=0.02, dur=dur * 0.9,
                    mul=amp).play(dur=2.5 * dur, delay=delay)

    adsr = pyo.Adsr(attack=dur * 0.05,
                    decay=0.05 * dur,
                    sustain=0.3,
                    release=dur * 0.7,
                    dur=dur * 0.9,
                    mul=amp).play(dur=2.5 * dur, delay=delay)
    osc = pyo.Osc(t, freq=pyo.midiToHz(note), mul=adsr).mix(1)

    rev = pyo.Freeverb(osc, size=1., damp=0.5, bal=1.,
                       mul=env).play(dur=2.5 * dur, delay=delay)
    # rev.out(delay=delay, dur=dur)

    eq = pyo.Biquad(rev, freq=800, q=1., type=0).play(dur=2.5 * dur,
                                                      delay=delay)
    eq.out(delay=delay, dur=dur)
    # eq = None

    return osc, env, rev, eq
Ejemplo n.º 3
0
    def __init__(self, chain):
        self.chain = chain

        self.pitch = pyo.Sig(0)
        self.vol = pyo.Sig(0)
        self.env = pyo.Adsr()

        self.sine = pyo.Sine(self.pitch, mul=self.vol * self.vol)
Ejemplo n.º 4
0
 def __init__(self):
     self.env = pyo.Adsr(attack=.01,
                         decay=.6,
                         sustain=.0,
                         release=.6,
                         dur=1,
                         mul=.5)
     sq = pyo.SawTable()
     self.osc = pyo.Osc(table=sq, mul=self.env, freq=440)
     self.lowpass = pyo.MoogLP(self.osc, 1000, self.env)
     self.lowpass.out()
Ejemplo n.º 5
0
def simple_sine(delay, note, amp, dur=1.):

    # -- Nice, angelic --
    env = pyo.Adsr(attack=dur * 0.1,
                   sustain=0.707,
                   decay=0.1 * dur,
                   release=dur * 0.5,
                   dur=dur * 0.9,
                   mul=amp).play(delay=delay, dur=2.5 * dur)
    osc = pyo.Sine(freq=pyo.midiToHz(note), mul=env).mix(1)
    osc.out(delay=delay, dur=dur)

    return osc, env
Ejemplo n.º 6
0
    def __init__(self, chain, extractor):
        self.chain = chain
        self.extractor = extractor

        self.table = pyo.NewTable(self.extractor.table.length)
        self.window = pyo.HannTable(self.table.size)
        self.samples = np.asarray(self.table.getBuffer())
        self.pitch = pyo.Sig(0)
        self.vol = pyo.Sig(0)
        self.env = pyo.Adsr()

        self.osc = pyo.Granule(self.table, self.window, dens=100)
        self.osc.setSync(False)

        self.sin = pyo.Blit(self.pitch, mul=self.vol)
        self.vocoder = pyo.Vocoder(self.sin, self.osc, self.pitch)
        self.compressor = pyo.Compress(self.vocoder)
    def __init__(self, parent=None):
        global app
        QtGui.QMainWindow.__init__(self, parent)
        #super(Scope, self).__init__(parent)
        #QtGui.QApplication.setGraphicsSystem("raster")
        #try:
        #    self.app = QtGui.QApplication(sys.argv)
        #except RuntimeError:
        #    self.app = QtGui.QApplication.instance()

        self.save_data = SAVE_DATA
        self.sound = ENABLE_SONIFICATION  # trigger sounds for each pulse or not

        self.app = app
        self.app.aboutToQuit.connect(self.close)
        self.pcounter = 0
        self.creation_time = datetime.datetime.now()

        self.df = pd.DataFrame(columns=['ts', 'ptype'])
        self.ptypes = pd.Series(
            ["alpha", "beta", "betagamma", "x-ray", "muon", "unknown"],
            dtype="category")

        self.thl = THL
        self.hl = -1243  # green cursor, highlight line for measuring only
        self.peaks = []
        self.paused = False

        if ENABLE_SONIFICATION:
            # setup some wild Karplus-Strong oscillator
            self.lf = pyo.Sine(.03, mul=.2, add=1)
            self.rg = Ring(fport=[
                random.choice([62.5, 125, 187.5, 250]) *
                random.uniform(.99, 1.01) for i in range(8)
            ],
                           fmod=self.lf * [
                               random.choice([25, 50, 75, 100]) *
                               random.uniform(.99, 1.01) for i in range(8)
                           ],
                           amp=0.1)
            self.env = pyo.Adsr(attack=0.01,
                                decay=0.1,
                                sustain=0.5,
                                release=1.5,
                                dur=5,
                                mul=0.1)
            self.res = pyo.Waveguide(
                self.rg.sig(),
                freq=[30.1, 60.05, 119.7, 181, 242.5, 303.33],
                dur=30,
                mul=1 * self.env).out()

        def audio_callback(in_data, frame_count, time_info, status):
            now = time.time()
            samples = np.frombuffer(in_data, dtype=np.int16)
            peak = samples.min()
            if peak < self.thl:
                t = pd.datetime.fromtimestamp(now)
                print("* ", t, end="")
                pulse = pd.DataFrame()
                pulse = pulse.assign(ts=[t])
                if peak < MIN_ALPHA_PEAK:
                    pulse = pulse.assign(ptype=[self.ptypes[0]])  #alpha
                    print("   alpha  ", end="")
                else:
                    pulse = pulse.assign(ptype=[self.ptypes[1]
                                                ])  #beta/electron
                    print("   elect   ", end="")
                if self.sound:
                    self.lf.setMul(abs(int(peak) / 16000))
                    self.env.dur = abs(int(peak) / 500)
                    self.env.play()
                print(self.pcounter, "  ", end="")
                print(peak)
                minima = argrelextrema(samples, np.less)
                self.peaks.append(sum(minima[0]) / len(minima[0] / 2))
                self.peaks = self.peaks[
                    -100:]  #only keep the last 100 for averaging
                pulse = pulse.assign(pulse=[samples])
                if self.save_data:
                    self.df = self.df.append(pulse,
                                             ignore_index=True,
                                             sort=False)
                self.pcounter += 1
                # calculate pulse rate in counts per second
                dt = (now - self.lastupdate)
                if dt <= 0:
                    dt = 0.000000000001
                cps2 = 1.0 / dt
                self.lastupdate = now
                self.cps = self.cps * 0.9 + cps2 * 0.1  # simple weighted average
                tx = 'Mean pulse rate:  {cps:.1f} CPS'.format(cps=self.cps)
                self.label.setText(tx + ", THL (red): " + str(self.thl) +
                                   ", cursor(green): " + str(self.hl) +
                                   ", (avg peak: " +
                                   str(round(sum(self.peaks) / 100, 1)) + ")")
                self.ydata = np.frombuffer(in_data, dtype=np.int16)
                self.frame_counter += frame_count
                if not self.paused:
                    self.h2.setData(self.ydata)
            self.thlp.setData(FRAME_SIZE * [self.thl])
            self.hlp.setData(FRAME_SIZE *
                             [self.hl])  #draw green highlight line

            return (in_data, pyaudio.paContinue)

        #### Create Gui Elements ###########
        self.mainbox = QtGui.QWidget()
        self.setCentralWidget(self.mainbox)
        self.mainbox.setLayout(QtGui.QVBoxLayout())

        self.canvas = pg.GraphicsLayoutWidget()
        self.mainbox.layout().addWidget(self.canvas)

        self.label = QtGui.QLabel()
        self.mainbox.layout().addWidget(self.label)

        self.otherplot = self.canvas.addPlot()
        self.h2 = self.otherplot.plot(pen='y')
        self.thlp = self.otherplot.plot(pen='r')
        self.hlp = self.otherplot.plot(pen='g')

        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(format=pyaudio.paInt16,
                                  channels=1,
                                  rate=RATE,
                                  input=True,
                                  frames_per_buffer=FRAME_SIZE,
                                  stream_callback=audio_callback)

        #### Set Data  #####################

        self.x = np.linspace(0, 50., num=100)
        self.X, self.Y = np.meshgrid(self.x, self.x)

        self.frame_counter = 0
        self.cps = 0.
        self.lastupdate = time.time()

        # keyboard shortcuts

        self.sh = QtGui.QShortcut(QtGui.QKeySequence("+"), self, self.thl_down)
        self.sh.setContext(QtCore.Qt.ApplicationShortcut)
        self.sh2 = QtGui.QShortcut(QtGui.QKeySequence("-"), self, self.thl_up)
        self.sh2.setContext(QtCore.Qt.ApplicationShortcut)

        self.sh3 = QtGui.QShortcut(QtGui.QKeySequence("7"), self,
                                   partial(self.hl_up, 1))
        self.sh3.setContext(QtCore.Qt.ApplicationShortcut)
        self.sh4 = QtGui.QShortcut(QtGui.QKeySequence("1"), self,
                                   partial(self.hl_down, 1))
        self.sh4.setContext(QtCore.Qt.ApplicationShortcut)

        self.sh5 = QtGui.QShortcut(QtGui.QKeySequence("8"), self,
                                   partial(self.hl_up, 10))
        self.sh5.setContext(QtCore.Qt.ApplicationShortcut)
        self.sh6 = QtGui.QShortcut(QtGui.QKeySequence("2"), self,
                                   partial(self.hl_down, 10))
        self.sh6.setContext(QtCore.Qt.ApplicationShortcut)

        self.sh7 = QtGui.QShortcut(QtGui.QKeySequence("9"), self,
                                   partial(self.hl_up, 100))
        self.sh7.setContext(QtCore.Qt.ApplicationShortcut)
        self.sh8 = QtGui.QShortcut(QtGui.QKeySequence("3"), self,
                                   partial(self.hl_down, 100))
        self.sh8.setContext(QtCore.Qt.ApplicationShortcut)

        self.sh9 = QtGui.QShortcut(QtGui.QKeySequence(" "), self,
                                   self.toggle_pause)
        self.sh9.setContext(QtCore.Qt.ApplicationShortcut)

        #self.sh3 = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+C"), self.close)
        #self.sh.setContext(QtCore.Qt.ApplicationShortcut)

        #### Start  #####################
        self.stream.start_stream()