1<CsoundSynthesizer>
2<CsOptions>
3-odac -d -F midifile.mid
4</CsOptions>
5<CsInstruments>
6nchnls=2
7ksmps = 64
8
9
10ichn = 1
11lp1: massign   ichn, 0
12loop_le   ichn, 1, 16, lp1
13pgmassign 0, 0
14gisf   sfload "sf_GMbank.sf2"
15sfpassign  0, gisf
16
17/* this instrument parses MIDI input
18   to trigger the GM soundfont synthesis
19   instrument (instr 10
20*/
21instr 1
22idkit = 317 /* drum-kit preset was 317*/
23tableiw idkit, 9, 1
24irel = 0.5 /* release envelope */
25
26ipg = 1
27ivol = 2
28ipan = 3
29
30nxt:
31  kst, kch, kd1, kd2 midiin
32
33  if (kst != 0) then
34    kch = kch - 1
35    if (kst == 144 && kd2 != 0) then ; note on
36        kpg table kch, ipg
37        /* instrument identifier is 10.[chn][note] */
38        kinst = 10 + kd1/100000 + kch/100
39        if kch == 9 then
40         /* exclusive identifiers */
41         if kpg == idkit+7 then
42           krel = 2    /* add extra release time for orch perc*/
43         else
44           krel = 0.5
45         endif
46         if (kd1 == 29 || kd1 == 30) then ; EXC7
47          kinst = 10.97
48         elseif (kd1 == 42 || kd1 == 44 || kd1 == 46 || kd1 == 49) then ; EXC1
49           kinst = 10.91
50         elseif (kd1 == 71 || kd1 == 72) then ; EXC2
51           kinst = 10.92
52         elseif (kd1 == 73 || kd1 == 74) then ; EXC3
53           kinst = 10.93
54         elseif (kd1 == 78 || kd1 == 79) then ; EXC4
55           kinst = 10.94
56         elseif (kd1 == 80 || kd1 == 81) then ; EXC5
57           kinst = 10.95
58         elseif (kd1 == 86 || kd1 == 87) then ; EXC6
59           kinst = 10.96
60         endif
61        else
62         krel = 0.5
63        endif
64        event "i", kinst, 0, -1, kd1, kd2, kpg, kch,krel
65
66    elseif (kst == 128 || (kst == 144 && kd2 == 0)) then ; note off
67        kpg table kch, ipg
68        kinst = 10 +  kd1/100000 + kch/100
69        if kch == 9 then
70         if (kd1 == 29 || kd1 == 30) then ; EXC7
71          kinst = 10.97
72         elseif (kd1 == 42 || kd1 == 44 || kd1 == 46 || kd1 == 49) then ; EXC1
73           kinst = 10.91
74         elseif (kd1 == 71 || kd1 == 72) then ; EXC2
75           kinst = 10.92
76         elseif (kd1 == 73 || kd1 == 74) then ; EXC3
77           kinst = 10.93
78         elseif (kd1 == 78 || kd1 == 79) then ; EXC4
79           kinst = 10.94
80         elseif (kd1 == 80 || kd1 == 81) then ; EXC5
81           kinst = 10.95
82         elseif (kd1 == 86 || kd1 == 87) then ; EXC6
83           kinst = 10.96
84         endif
85        else
86         kpg = 0
87        endif
88        event "i", -kinst, 0, 1
89
90    elseif (kst == 192) then /* program change msgs */
91       if kch == 9 then
92         kpg = idkit
93         if kd1 == 8 then
94         kpg = idkit+1
95         elseif kd1 == 16 then
96         kpg = idkit+2
97         elseif kd1 == 24 then
98         kpg = idkit+3
99         elseif kd1 == 25 then
100         kpg = idkit+4
101         elseif kd1 == 32 then
102         kpg = idkit+5
103         elseif kd1 == 40 then
104         kpg = idkit+6
105         elseif kd1 == 48 then
106         kpg = idkit+7
107         endif
108       else
109       kpg = kd1
110       endif
111       tablew  kpg, kch, ipg
112    elseif (kst == 176 && kd1 == 11) then /* volume msgs */
113       tablew kd2, kch, ivol
114    elseif (kst == 176 && kd1 == 7) then /* pan msgs    */
115       tablew kd2, kch, ipan
116    endif
117     kgoto nxt
118  endif
119
120endin
121
122/* this is the GM soundfont synthesizer instrument */
123instr 10
124kenv linenr 10,0.001,p8,0.001
125iamp table p5, 5
126a1, a2 sfplay p5, p4, iamp,1, p6, 0, 0, 2
127kv table p7, 2
128kvol tablei kv, 5
129kpan  table p7, 3
130kpan = (kpan - 64)/128
131       outs 0.2*a1*kvol*(0.5-kpan/2)*kenv, 0.2*a2*kvol*(0.5+kpan/2)*kenv
132endin
133
134instr 11
135
136endin
137
138</CsInstruments>
139<CsScore>
140/* program preset (memory) table */
141f1 0 16 -2 0 0 0 0 0 0 0 0 226 0 0 0 0 0 0 0
142/* velocity (memory) table */
143f2 0 16 -2 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127
144/* pan (memory) table */
145f3 0 16 -2 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64
146f5 0 128 5 0.1 128 1   /* velocity mapping: less nuanced */
147f6 0 128 5 0.01  128 1 /* velocity mapping: more nuanced */
148i 1 0 360000
149e
150</CsScore>
151</CsoundSynthesizer>
152