/
triosc.py
executable file
·110 lines (86 loc) · 3.12 KB
/
triosc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2017 Alexandros Drymonitis
#
# This code is based on the Pyo Python module and code by Olivier Belanger
# Pyo is released under the GNU GPL 3 Licence, so is this file
# A Licence copy should come with this code
# If not, please check <http://www.gnu.org/licenses/>
#
# This is a simple triangle wave oscillator
from pyo import *
class TriOsc(PyoObject):
"""
A simple triangle wave oscillator.
:Parent: :py:class:`PyoObject`
:Args:
freq: float or PyoObject, optional
Frequency in cycles per second. Defaults to 100.
phase: float or PyoObject, optional
Phase of sampling, expressed as a fraction of a cycle (0 to 1).
Defaults to 0.
>>> s = Server().boot()
>>> s.start()
>>> a = TriOsc(200, mul=.2).out()
"""
def __init__(self, freq=100, phase=0, mul=1, add=0):
PyoObject.__init__(self,mul,add)
self._freq = freq
self._phase = phase
self._freq,self._phase,mul,add,lmax=convertArgsToLists(freq,phase,mul,add)
self._phasor = Phasor(freq=self._freq, phase=self._phase)
self._inv_phasor = self._phasor * (-1) + 1
self._min = Min(self._phasor, self._inv_phasor, mul=4, add=-1)
# A Sig is the best way to properly handle "mul" and "add" arguments.
self._output = Sig(self._min, mul, add)
# Create the "_base_objs" attribute. This is the object's audio output.
self._base_objs = self._output.getBaseObjects()
def setFreq(self, x):
"""
Replace the `freq` attribute.
:Args:
x: float or PyoObject
New `freq` attribute.
"""
self._freq = x
self._phasor.freq = x
def setPhase(self, x):
"""
Replace the `phase` attribute.
:Args:
x: float or PyoObject
New `phase` attribute.
"""
self._phase = x
self._phasor.phase = x
def play(self, dur=0, delay=0):
for key in self.__dict__.keys():
if isinstance(self.__dict__[key], PyoObject):
self.__dict__[key].play(dur, delay)
return PyoObject.play(self, dur, delay)
def stop(self):
for key in self.__dict__.keys():
if isinstance(self.__dict__[key], PyoObject):
self.__dict__[key].stop()
return PyoObject.stop(self)
def out(self, chnl=0, inc=1, dur=0, delay=0):
for key in self.__dict__.keys():
if isinstance(self.__dict__[key], PyoObject):
self.__dict__[key].play(dur, delay)
return PyoObject.out(self, chnl, inc, dur, delay)
@property
def freq(self):
"""float or PyoObject. Fundamental frequency in cycles per second."""
return self._freq
@freq.setter
def freq(self, x): self.setFreq(x)
@property
def phase(self):
"""float or PyoObject. Phase of sampling between 0 and 1."""
return self._phase
@phase.setter
def phase(self, x): self.setPhase(x)
if __name__ == "__main__":
# Test case...
s = Server().boot()
tri = TriOsc(freq=200, mul=.2).out()
sc = Scope(tri)
s.gui(locals())