«Demo: Loading a MIDI file, playing it with your own SynthDef» by Bruno Ruviaro

on 09 Nov'18 02:04 in midipbindfsimplemidifilequarks

Load a MIDI file and play it with your own SynthDef. Examples of how to use it with Pbindf

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
// you can create your midi file using MuseScore, Finale, Sibelius, Logic, etc.
// TIP: create one separate midi file per layer (melodies, chords, etc).

// ===========================================
// THE FOLLOWING TWO THINGS YOU ONLY DO ONCE
// ===========================================


// Install quark (do only once -- may take a few seconds)
Quarks.install("wslib");

// Recompile class library (need to do only once)
thisProcess.recompile

// if the above does not work, you need to install git on your computer:
// Mac: https://git-scm.com/download/mac
// PC: https://git-scm.com/download/win

// ===========================
// LOAD MIDI FILE AND TEST IT
// ===========================

// adjust the path
// download MIDI file, select it in your file browser (one click only), "copy" it (ctrl+C), paste (ctrl+V) path here:
~melody = SimpleMIDIFile.read( "/home/sclork/Downloads/melody.mid" );

// you can load as many midi files as you want, just create other variables for them (~melody1, ~melody2, ~chords, ~thingy, etc)

// remember to boot server!
s.boot;

// play it (cmd-. to stop)
~melody.p.play;

// ==================================
// PLAY WITH A DIFFERENT INSTRUMENT
// ==================================

// another SynthDef:
(
SynthDef("plucking", {arg amp = 0.1, freq = 440, decay = 2, dampen = 0.1, pan = 0;

	var env, snd;
	env = Env.linen(0, decay, 0).kr(doneAction: 2);
	snd = Pluck.ar(
		in: WhiteNoise.ar(amp),
		trig: Impulse.kr(0),
		maxdelaytime: 0.1,
		delaytime: freq.reciprocal,
		decaytime: decay,
		coef: dampen);
	snd = Pan2.ar(snd, pan);
	Out.ar(0, snd);
}).add;

~bpm = TempoClock.new(120/60).permanent_(true);
)

// Playing through Pbindf
// Specifying synth-specific parameters:

// ex. 1
(
Pbindf(~melody.p,
	\instrument, "plucking",
	\decay, 15,
	\dampen, 0.12,
	\amp, 1,
).play(~bpm);
)

// ex. 2
(
Pbindf(~melody.p,
	\instrument, "plucking",
	\decay, Pwhite(0.1, 15),
	\ctranspose, Pwhite(0, 1) + [-12, -7], // mess it up!
	\dampen, Pwhite(0.1, 0.6),
	\amp, Pwhite(0.3, 1)
).trace.play(~bpm);
)

// ex. 3
(
Pstretch(0.5,
	Pbindf(~melody.p, // Pstretch duratins (speed up or slow down);
		\instrument, "plucking",
		\decay, 1,
		\dampen, 0.1,
		\amp, 1,
		\ctranspose, [-24, -21, -17],
		\amp, Pseq([0.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.8, 1, 2], inf)
)).trace.play(~bpm);
)

// ex. 4
(
Pstretch(Pn(Pgeom(0.5, 1.1, 14)), // can also stretch by pattern!
	Pbindf(~melody.p,
		\instrument, "plucking",
		\decay, 1,
		\dampen, 0.1,
		\amp, 1,
		\ctranspose, [-24, -21, -17],
		\amp, Pwhite(0.5, 2)
)).trace.play(~bpm);
)

// ex. 5
(
Pbindf(~melody.p,
	\instrument, "plucking",
	\decay, 15,
	\dampen, 0.12,
	\amp, 1,
	\delta, Pseq([1/3, 1, 1/3, 1/4], inf) // CREATE NEW RHYTHM (ignoring original durs -- note we have to use \delta to override the original durations from the midi file
).play(~bpm);
)
raw 2958 chars (focus & ctrl+a+c to copy)
reception
comments