-
Notifications
You must be signed in to change notification settings - Fork 2
/
py.py
144 lines (111 loc) · 3.8 KB
/
py.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import pd
import sys
from random import randint
import numpy as np
# ================================================
# ============== Functions =====================
# ================================================
def pdsum(x, y):
"It sums two numbers."
x = int(x)
y = int(y)
return x + y
def arithm_ser(begin, end, step):
"It calculates the arithmetic series."
begin = int(begin)
end = int(end)
step = int(step)
list = []
for x in range(begin, end, step):
list.append(x)
return list
def fibonacci(n):
"Calculate the nth fibonacci number."
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
# ================================================
# ================== AUDIO ======================
# ================================================
def pd_tempfolder():
"It returns the temp folder path."
tempfolder = pd.tempfolder()
pd.print(str(tempfolder))
def pd_output():
"It sends some output to the py4pd output."
for x in range(10):
pd.out(x)
def example_pdout():
for x in range(2):
pd.out(x, symbol="myloop")
for x in range(2):
pd.out(x, symbol="myloop2")
return None
def pdout(x):
pd.out(x, symbol="myloop")
pd.out(x, symbol="myloop2")
return None
def pd_print():
"It sends a message to the py4pd message box."
pd.print("Hello from python!")
return None
def pd_error():
"It sends a message to the py4pd message box."
pd.error("pd error from Python, check the script.")
return None
def pd_send():
"It sends a message to the py4pdreceiver receive."
pd.send("py4pdreceiver", "hello from python!")
pd.send("py4pdreceiver", 1)
pd.send("py4pdreceiver", [1, 2, 3, 4, 5])
return 0
def pd_tabwrite():
"It sends a message to the py4pd message box."
list = []
tablen = randint(10, 200)
i = 0
while i < tablen:
# gerar aleatoric number between -1 and 1
list.append(randint(-100, 100) / 100)
i += 1
pd.tabwrite("test", list, resize=True)
def printall(x, y):
"It sends a message to the py4pd message box."
pd.print(str(x + y))
# ================================================
# ================ Audio =========================
# ================================================
def generate_sine_wave(frequency, amplitude, phase, num_samples, sampling_rate):
angular_frequency = 2 * np.pi * frequency
t = np.arange(num_samples) / sampling_rate
sine_wave = amplitude * np.sin(angular_frequency * t + phase)
last_phase = phase + angular_frequency * t[-1]
return sine_wave, last_phase
def mksenoide(freqs, amps, phases, vectorsize, samplerate):
n = len(freqs)
nframes = vectorsize
out = np.zeros((n, nframes), dtype=np.float64) # Modify the shape of the output array
new_phases = np.zeros(n, dtype=np.float64) # Array to store the new phases
for i in range(n):
out[i], new_phases[i] = generate_sine_wave(freqs[i], amps[i], phases[i], nframes, samplerate)
if new_phases[i] > 2 * np.pi:
new_phases[i] -= 2 * np.pi
return out, new_phases
def sinusoids(freqs, amps):
if freqs is None or amps is None:
pd.error("You need to set the amplitudes")
return np.zeros((1, 64))
if len(freqs) != len(amps):
pd.error("The length of freqs and amps are different")
return None
phases = pd.get_obj_var("PHASE", initial_value=np.zeros(len(freqs)))
freqs = np.array(freqs, dtype=np.float64)
amps = np.array(amps, dtype=np.float64)
out, new_phases = mksenoide(freqs, amps, phases, 64, pd.get_sample_rate())
pd.set_obj_var("PHASE", new_phases)
return out
def py4pdLoadObjects():
pd.add_object(sinusoids, 'sinusoids~', obj_type=pd.AUDIOOUT)