Skip to content

Commit

Permalink
Merge pull request #45 from charlesneimog/develop
Browse files Browse the repository at this point in the history
minor fixes
  • Loading branch information
charlesneimog authored Aug 8, 2023
2 parents ea81163 + 5d6e03a commit 182f787
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 185 deletions.
8 changes: 4 additions & 4 deletions docs/examples/descriptors/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

def descriptors():
audio_arr, sr = af.read(pd.home() + "/flute.wav")
audio_arr, sr = af.read(pd.get_patch_dir() + "/flute.wav")
bft_obj = af.BFT(num=2049, samplate=sr, radix2_exp=12, slide_length=1024,
data_type=SpectralDataType.MAG,
scale_type=SpectralFilterBankScaleType.LINEAR)
Expand All @@ -24,11 +24,11 @@ def descriptors():
times = np.arange(0, len(hfc_arr)) * (bft_obj.slide_length / bft_obj.samplate)
fill_plot(times, hfc_arr, axes=ax[1], label='hfc')
fill_plot(times, cen_arr, axes=ax[2], label="Centroid")
tempfile = pd.tempfolder() + "/descritores.png"
tempfile = pd.get_temp_dir() + "/descritores.png"
plt.savefig(tempfile)
pd.show(tempfile)
pd.show_image(tempfile)
pd.print("Data plotted")

def py4pdLoadObjects():
pd.addobject(descriptors, "descritores", objtype="VIS", figsize=(640, 480))
pd.add_object(descriptors, "descritores", objtype=pd.VIS, figsize=(640, 480))

4 changes: 2 additions & 2 deletions docs/examples/descriptors/patch.pd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#N canvas 567 63 738 668 8;
#N canvas 423 56 738 668 8;
#X obj 29 19 py4pd -lib main;
#X obj 29 63 descritores 640 480;
#X obj 29 70 descritores 640 480;
#X obj 29 45 bng 15 250 50 0 empty empty empty 0 -8 0 8 #fcfcfc #000000 #000000;
#X obj 112 40 py4pd;
#X msg 112 19 pipinstall local audioflux;
Expand Down
109 changes: 56 additions & 53 deletions resources/py.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
import numpy as np
numpyIsInstalled = True
except Exception as e:
import platform
py4pdObjectFolder = pd.py4pdfolder()
if platform.system() != "Darwin":
pd.error("=== Numpy not installed, to solve this... ===")
pd.print("\n", show_prefix=False)
pd.print(" 1º Create new object with 'py4pd -lib py4pd'", show_prefix=False)
pd.print("\n", show_prefix=False)
pd.print(" 2º Create new object 'py.pip'", show_prefix=False)
pd.print("\n", show_prefix=False)
pd.print(" 3º Send the message 'global numpy' and wait for the installation", show_prefix=False)
pd.print("\n", show_prefix=False)
pd.error("==================================")
else:
pd.error("=== Numpy not installed, to solve this... ===")
pd.print(" Open the terminal and run this line:", show_prefix=False)
pd.print("\n")
pd.print("cd '" + py4pdObjectFolder + "' && pip install numpy -t ./resources/py-modules", show_prefix=False)
pd.print("\n", show_prefix=False)

py4pdObjectFolder = pd.get_py4pd_dir()
pd.error("=== Numpy not installed, to solve this... ===")
pd.print("\n", show_prefix=False)
pd.print(" 1º Create new object with 'py4pd'", show_prefix=False)
pd.print("\n", show_prefix=False)
pd.print(" 2º Send the message 'pipinstall local numpy' and wait for the installation", show_prefix=False)
pd.print("\n", show_prefix=False)
pd.error("==================================")
numpyIsInstalled = False

try:
from numba import jit


except Exception as e:
pd.pip_install("local", "numba")






# ================================================
# ============== Functions =====================
# ================================================
Expand Down Expand Up @@ -125,43 +126,45 @@ def printall(x, y):
# ================================================
# ================ Audio =========================
# ================================================
@jit(nopython=True, cache=True, fastmath=True)
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


@jit(nopython=True, cache=True, fastmath=True)
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):
vectorsize = pd.get_vec_size()
samplerate = pd.get_sample_rate()
if freqs is None or amps is None:
return None
if len(freqs) != len(amps):
return None
phases = pd.get_global_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, vectorsize, samplerate)
pd.set_global_var("PHASE", new_phases)
return out


def audioin(audio):
length = len(audio)
return length

def audioout(freq, amplitude):
if freq is None:
return np.zeros(pd.vecsize())
else:
phase = pd.getglobalvar("PHASE")
if phase is None:
phase = 0.0 # set the initial value of phase in the first call of the function
output = np.zeros(pd.vecsize())
increment = (freq * 2.0 * np.pi) / pd.samplerate()
for i in range(pd.vecsize()):
output[i] = np.sin(phase) * amplitude
phase += increment
if phase > 2 * np.pi:
phase -= 2 * np.pi
# ---------
pd.setglobalvar("PHASE", phase)
# it saves the value of phase for the next call of the function
# without this, we will not have a continuous sine wave.
return output

def audio(audio, amplitude):
if amplitude is None:
amplitude = 0.2
audio = np.multiply(audio, amplitude)
return audio


def py4pdLoadObjects():
global numpyIsInstalled
if numpyIsInstalled:
pd.addobject(audioin, "audioin", objtype="AUDIOIN")
pd.addobject(audioout, "audioout", objtype="AUDIOOUT")
pd.addobject(audio, "audio", objtype="AUDIO")
pd.add_object(sinusoids, 'sinusoids~', objtype=pd.AUDIOOUT)

115 changes: 51 additions & 64 deletions resources/py4pd-help.pd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#N canvas 879 42 1228 612 8;
#N canvas 494 62 1228 612 8;
#X msg 25 46 doc, f 6;
#N canvas 282 97 1541 723 embedded 0;
#X obj 9 192 py4pd;
Expand All @@ -10,9 +10,9 @@
#X obj 8 269 print pd.out;
#X msg 413 141 run;
#N canvas 0 50 450 250 (subpatch) 0;
#X array test 155 float 3;
#A 0 -0 0 0 0 -0 0 -0 0 -0 -0 0 -0 -0 0 0 -0 -0 -0 -0 0 0 0 -0 -0 0 0 0 0 -0 0 0 -0 0 0 0 0 0 -0 0 -0 0 -0 0 0 0 -0 0 -0 0 -0 0 0 -0 0 -0 0 0 0 -0 0 0 0 -0 -0 0 -0 0 -0 0 -0 0 0 0 -0 0 -0 -0 0 -0 0 0 0 -0 1 -0 -0 0 0 -0 -0 0 0 -0 0 0 -0 0 -0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
#X coords 0 1 155 -1 200 140 1;
#X array test 79 float 3;
#A 0 -0.55 0.24 0.78 0.79 -0.45 -0.82 0.81 -0.23 -0.89 -0.44 0.15 0.41 0.06 0.75 0.41 0.37 0.22 -0.5 -0.2 0.97 0.09 0 -0.46 -0.59 0.93 0.12 0.36 -0.43 -0.2 -0.5 0.03 -0.15 0.96 -0.4 0.95 -0.34 -0.16 0.93 -0.9 0.94 0.65 0.06 0.8 0.82 0 0.98 0.08 -0.04 0.13 -0.76 0.97 0.41 -0.17 -0.27 0.58 -0.37 -0.58 -0.33 0.89 0.03 -0.55 0.5 0.88 -0.25 -0.47 0.16 -0.17 0.39 -0.9 -0.36 -0.67 0.04 -0.22 0.1 0.06 -0.11 -0.55 0.46 0.77;
#X coords 0 1 79 -1 200 140 1;
#X restore 561 12 graph;
#X obj 413 92 tgl 20 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000 0 1;
#X obj 103 244 r py4pdreceiver;
Expand Down Expand Up @@ -105,83 +105,70 @@
#X text 320 74 Click on this bang for MacOS | Bottom of page;
#X text 320 101 Click on this bang for Windows | Bottom of page;
#X text 320 124 Click on this bang for Linux;
#N canvas 861 110 951 811 Libraries 0;
#N canvas 861 110 853 614 Libraries 0;
#X text 83 6 <= Load the library of the script called py.py. The script must be located in the patch folder or inside one folder with same of the script that are in the PureData Search patches. For example \, If I have ~/Documents/Pd/pd-ji/pd-ji.py you can use py4pd -lib pd-ji to load the library., f 66;
#X obj 239 205 switch~ 64;
#X obj 239 184 tgl 15 0 empty empty empty 0 -8 0 8 #fcfcfc #000000 #000000 0 1;
#X obj 239 121 loadbang;
#X text 259 181 !!! Turn on the Audio for this subpatch.;
#X text 39 118 Freqs in Hz.;
#X text 96 118 Freqs in Hz.;
#X obj 15 281 dac~;
#X msg 15 118 234;
#X text 98 328 The Python objects do not support Help Patches yet! But inside both libraries there is some Helps patches.;
#X text 97 283 I already upload some Python Libraries for py4pd. Go to Help->Find Externals then "pd-ji" or "orchidea".;
#X text 104 222 The Python objects do not support Help Patches yet! But inside both libraries there is some Helps patches.;
#X text 103 177 I already upload some Python Libraries for py4pd. Go to Help->Find Externals then "pd-ji" or "orchidea".;
#X obj 10 7 py4pd -lib py;
#X obj 15 181 audioout 440 1;
#X obj 85 139 hsl 50 10 0 1 0 0 empty empty empty -2 -8 0 8 #fcfcfc #000000 #000000 0 0;
#X obj 82 154 nbx 8 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 8 #fcfcfc #000000 #000000 0 256;
#X text 141 135 Amplitude;
#X msg 239 142 0;
#X obj 15 238 *~ 1, f 8;
#X connect 2 0 1 0;
#X connect 3 0 15 0;
#X connect 7 0 11 0;
#X connect 11 0 16 0;
#X connect 12 0 13 0;
#X connect 13 0 11 1;
#X connect 15 0 2 0;
#X connect 16 0 6 0;
#X connect 16 0 6 1;
#X msg 92 157 0.1 0.1 0.1 0.1;
#X msg 15 118 200 250 300 350;
#X obj 15 202 snake~ out 4;
#X obj 15 181 sinusoids~ -ch 4;
#X obj 15 73 loadbang;
#X text 263 97 !!! Start dsp!;
#X obj 15 94 t b b, f 17;
#X msg 247 127 \; pd dsp \$1;
#X obj 247 97 tgl 15 0 empty empty empty 0 -8 0 8 #fcfcfc #ff0400 #000000 0 1;
#X connect 6 0 9 1;
#X connect 7 0 9 0;
#X connect 8 0 2 0;
#X connect 8 1 2 1;
#X connect 8 2 2 0;
#X connect 8 3 2 1;
#X connect 9 0 8 0;
#X connect 10 0 12 0;
#X connect 12 0 7 0;
#X connect 12 1 6 0;
#X connect 14 0 13 0;
#X restore 14 233 pd Libraries;
#X text 292 160 IMPORTANT: This is just simple example \, the exaustive Docs are avaible in www.charlesneimog.com/py4pd;
#X text 80 232 <= Share your Python code as pd objects.;
#X text 109 210 <= Interact with Python from PureData.;
#X text 46 116 <= Click to open the script.;
#X obj 13 117 py4pd;
#X text 42 116 <= Click to open the script.;
#X obj 13 138 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 8 #fcfcfc #000000 #000000 0 256;
#X text 93 69 3 Run the function.;
#X text 64 46 2 See the docs of the function.;
#X text 82 16 1 Load function.;
#X obj 53 91 r py4pd;
#X msg 295 47 YOU NEED TO INSTALL PYTHON3.11;
#N canvas -61 -23 1920 977 py4pd 0;
#N canvas 240 118 658 503 py4pd 0;
#X obj 10 4 py4pd -lib py4pd;
#X obj 10 29 py.and -a 3;
#X obj 10 49 py.or -a 3;
#X obj 10 83 py.print;
#X obj 10 103 py2pd;
#X obj 10 124 pd2py;
#X obj 10 155 py.mklist;
#X obj 10 173 py.len;
#X obj 10 192 py.nth;
#X obj 10 29 py&& -a 3;
#X obj 10 49 py|| -a 3;
#X obj 9 212 py.list -a 3;
#X obj 9 232 py.max;
#X obj 9 251 py.min;
#X obj 9 270 py.reduce;
#X obj 9 288 py.mattrans;
#X obj 9 355 py.iterate;
#X obj 9 376 py.collect;
#X obj 99 30 py.sum;
#X obj 99 48 py.minus;
#X obj 99 67 py.times;
#X obj 99 86 py.div;
#X obj 99 105 py.abs;
#X obj 185 158 py.show 250 250;
#X text 795 55 <= py.show;
#X obj 10 192 py.nth;
#X obj 10 173 py.len;
#X obj 10 155 py.mklist;
#X obj 10 124 pd2py;
#X obj 10 103 py2pd;
#X obj 10 83 py.print;
#X obj 99 30 py+;
#X obj 99 48 py-;
#X obj 99 67 py*;
#X obj 99 87 py/;
#X obj 225 32 py.iterate;
#X obj 225 53 py.collect;
#X obj 295 31 f2mc;
#X obj 295 51 mc2f;
#X obj 295 71 mc2n;
#X msg 114 136 reload;
#X msg 185 103 /home/neimog/Documents/Git/py4pd/resources/descrip2tors.png;
#X msg 194 134 /home/neimog/Documents/Git/py4pd/resources/descriptors.png;
#X connect 26 0 21 0;
#X connect 27 0 21 0;
#X connect 28 0 21 0;
#X restore 13 261 pd py4pd extra objects;
#X text 131 260 py4pd come with object to use pip inside pd \, and minor others things. Check the subpatch.;
#X connect 0 0 20 0;
#X connect 6 0 20 0;
#X connect 7 0 20 0;
#X connect 20 0 21 0;
#X connect 21 0 3 0;
#X connect 25 0 20 0;
#X obj 13 117 py4pd;
#X connect 0 0 28 0;
#X connect 6 0 28 0;
#X connect 7 0 28 0;
#X connect 20 0 3 0;
#X connect 24 0 28 0;
#X connect 28 0 20 0;
Loading

0 comments on commit 182f787

Please sign in to comment.