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

on 08 Nov'18 20:04 in midipbindfsimplemidifilequarks

Load a MIDI file and play it with your own SynthDef.

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
// download a bach prelude from
// http://www.bachcentral.com/wtcMidi1.html

// ===========================================
// 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

// ===========================
// 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:
m = SimpleMIDIFile.read( "/home/ruviaro/Downloads/Prelude2.mid" );

// remember to boot server!
s.boot;

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

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

// here's a demo synth def
(
SynthDef( "organ", { arg freq = 440, amp = 0.4, gate = 1;

	var snd, env;
	env = Env.asr(attackTime: 0.025, sustainLevel: amp, releaseTime: 0.025).kr(doneAction: 2, gate: gate);
	snd = LFPar.ar(freq: freq * [1,2,3,5], mul: amp/[2,4,5,7] * env);
	snd = Splay.ar(snd);
	Out.ar(0, snd)

}).add;
);

// and here's how to use it with the MIDI file:
x = m.p(\organ).play;



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

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);
    Out.ar(0, [snd, snd]);
}).add;
)

// Try it:
x = m.p(\plucking).play;
x.stop;

// Specifying synth-specific parameters:

// ex. 1
(
Pbindf(m.p,
	\instrument, "plucking",
	\decay, 3,
	\dampen, 0.6,
	\amp, 0.1
).play
)

// ex. 2
(
Pbindf(m.p,
	\instrument, "plucking",
	\decay, Pwhite(0.1, 1.5),
	\dampen, Pwhite(0.1, 0.6),
	\amp, Pwhite(0.1, 0.3)
).play
)

// ex. 3
(
Pbindf(m.p,
	\instrument, "plucking",
	\decay, 1,
	\dampen, 0.01,
	\amp, Pstutter(2, Pseq([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.6, 0.6], inf))
).play
)

// ex 4
(
Pbindf(m.p,
	\instrument, "plucking",
	\decay, Pseq([
		\rest,
		\rest,
		Pstutter(2, Pseq([3, Pn(0.2, 7)], inf))
	], inf),
	\dampen, Pstutter(2, Pseq((1..8).normalize(0.1, 0.8), inf))
).play
)

// ex 5
(
Pbindf(m.p,
	\instrument, "plucking",
	\decay, 1.2,
	\dampen, Pstutter(2, Pseq((1..8).normalize(0.1, 0.3), inf)),
	\amp, Pwhite(0.01, 0.1),
	\ctranspose, [0, 12, 24]
).play;

Pbindf(m.p,
	\instrument, "plucking",
	\decay, 7,
	\dampen, Pwhite(0.01, 0.1),
	\amp, Prand([0, 0, 0, 0, 0.2], inf),
	\ctranspose, 36
).play
)
raw 2790 chars (focus & ctrl+a+c to copy)
reception
comments