jevstrudel.git / website / src / pages / learn / csound.mdx
csound.mdx152 lines · 6.3 KB · raw
1---
2title: Csound
3layout: ../../layouts/MainLayout.astro
4---
5
6import { MiniRepl } from '../../docs/MiniRepl';
7import { JsDoc } from '../../docs/JsDoc';
8import Colors from '../../docs/Colors.jsx';
9
10# Using CSound with Strudel
11
12🧪 Strudel has experimental support for csound, using [@csound/browser](https://www.npmjs.com/package/@csound/browser).
13
14## Importing .orc files
15
16To use existing csound instruments, you can load and use an orc file from an URL like this:
17
18<MiniRepl
19  client:only="react"
20  tune={`// livecode.orc by Steven Yi
21await loadOrc('github:kunstmusik/csound-live-code/master/livecode.orc')
22note("c a f e").csound('FM1')
23`}
24/>
25
26Note that the above url uses the `github:` shortcut, which resolves to the raw file on github, but you can use any URL you like.
27
28The awesome [`livecode.orc by Steven Yi`](https://github.com/kunstmusik/csound-live-code) comes packed with many sounds ready for use:
29
30<MiniRepl
31  client:only="react"
32  tune={`// livecode.orc by Steven Yi
33await loadOrc('github:kunstmusik/csound-live-code/master/livecode.orc')
34note("c a f e").csound(cat(
35"Sub1", // 	Substractive Synth, 3osc
36"Sub2", // 	Subtractive Synth, two saws, fifth freq apart
37"Sub3", // 	Subtractive Synth, three detuned saws, swells in
38"Sub4", // 	Subtractive Synth, detuned square/saw, stabby. Nice as a lead in octave 2, nicely grungy in octave -2, -1
39"Sub5", // 	Subtractive Synth, detuned square/triangle
40"Sub6", // 	Subtractive Synth, saw, K35 filters
41"Sub7", // 	Subtractive Synth, saw + tri, K35 filters
42"Sub8", // 	Subtractive Synth, square + saw + tri, diode ladder filter
43"SynBrass", // 	SynthBrass subtractive synth
44"SynHarp", // 	Synth Harp subtracitve Synth
45"SSaw", // 	SuperSaw sound using 9 bandlimited saws (3 sets of detuned saws at octaves)
46"Mode1", // 	Modal Synthesis Instrument: Percussive/organ-y sound
47"Plk", // 	Pluck sound using impulses, noise, and waveguides
48"Organ1", // 	Wavetable Organ sound using additive synthesis
49"Organ2", // 	Organ sound based on M1 Organ 2 patch
50"Organ3", // 	Wavetable Organ using Flute 8' and Flute 4', wavetable based on Claribel Flute http://www.pykett.org.uk/the\_tonal\_structure\_of\_organ\_flutes.htm
51"Bass", // 	Subtractive Bass sound
52"ms20_bass", // 	MS20-style Bass Sound
53"VoxHumana", // 	VoxHumana Patch
54"FM1", // 	FM 3:1 C:M ratio, 2->0.025 index, nice for bass
55"Noi", // 	Filtered noise, exponential envelope
56"Wobble", // 	Wobble patched based on Jacob Joaquin's "Tempo-Synced Wobble Bass"
57"Sine", // 	Simple Sine-wave instrument with exponential envelope
58"Square", // 	Simple Square-wave instrument with exponential envelope
59"Saw", // 	Simple Sawtooth-wave instrument with exponential envelope
60"Squine1", // 	Squinewave Synth, 2 osc
61"Form1", // 	Formant Synth, buzz source, soprano ah formants
62"Mono", // 	Monophone synth using sawtooth wave and 4pole lpf. Use "start("Mono") to run the monosynth, then use MonoNote instrument to play the instrument.
63"MonoNote", // 	Note playing instrument for Mono synth. Be careful to use this and not try to create multiple Mono instruments!
64"Click", // 	Bandpass-filtered impulse glitchy click sound. p4 = center frequency (e.g., 3000, 6000)
65"NoiSaw", // 	Highpass-filtered noise+saw sound. Use NoiSaw.cut channel to adjust cutoff.
66"Clap", // 	Modified clap instrument by Istvan Varga (clap1.orc)
67"BD", // 	Bass Drum - From Iain McCurdy's TR-808.csd
68"SD", // 	Snare Drum - From Iain McCurdy's TR-808.csd
69"OHH", // 	Open High Hat - From Iain McCurdy's TR-808.csd
70"CHH", // 	Closed High Hat - From Iain McCurdy's TR-808.csd
71"HiTom", // 	High Tom - From Iain McCurdy's TR-808.csd
72"MidTom", // 	Mid Tom - From Iain McCurdy's TR-808.csd
73"LowTom", // 	Low Tom - From Iain McCurdy's TR-808.csd
74"Cymbal", // 	Cymbal - From Iain McCurdy's TR-808.csd
75"Rimshot", // 	Rimshot - From Iain McCurdy's TR-808.csd
76"Claves", // 	Claves - From Iain McCurdy's TR-808.csd
77"Cowbell", // 	Cowbell - From Iain McCurdy's TR-808.csd
78"Maraca", // 	Maraca - from Iain McCurdy's TR-808.csd
79"HiConga", // 	High Conga - From Iain McCurdy's TR-808.csd
80"MidConga", // 	Mid Conga - From Iain McCurdy's TR-808.csd
81"LowConga", // 	Low Conga - From Iain McCurdy's TR-808.csd
82))`}
83/>
84
85## Writing your own instruments
86
87You can define your own instrument(s) with `loadCsound` like this:
88
89<MiniRepl
90  client:only="react"
91tune={`await loadCsound\`
92instr CoolSynth
93    iduration = p3
94    ifreq = p4
95    igain = p5
96    ioct = octcps(ifreq)
97
98    kpwm = oscili(.05, 8)
99    asig = vco2(igain, ifreq, 4, .5 + kpwm)
100    asig += vco2(igain, ifreq * 2)
101
102    idepth = 2
103    acut = transegr:a(0, .005, 0, idepth, .06, -4.2, 0.001, .01, -4.2, 0) ; filter envelope
104    asig = zdf_2pole(asig, cpsoct(ioct + acut + 2), 0.5)
105
106    iattack = .01
107    isustain = .5
108    idecay = .1
109    irelease = .1
110    asig *= linsegr:a(0, iattack, 1, idecay, isustain, iduration, isustain, irelease, 0)
111
112    out(asig, asig)
113
114endin\`
115
116"<0 2 [4 6](3,4,2) 3\*2>"
117.off(1/4, add(2))
118.off(1/2, add(6))
119.scale('D minor')
120.note()
121.csound('CoolSynth')`}
122/>
123
124## Parameters
125
126The `.csound` function sends the following p values:
127
128|     |                                  |
129| --- | -------------------------------- |
130| p1  | instrument name e.g. `CoolSynth` |
131| p2  | time offset, when it should play |
132| p3  | the duration of the event / hap  |
133| p4  | frequency in Hertz               |
134| p5  | normalized `gain`, 0-1           |
135
136There is an alternative `.csoundm` function with a different flavor:
137
138|     |                                   |
139| --- | --------------------------------- |
140| p4  | midi key number, unrounded, 0-127 |
141| p5  | midi velocity, 0-127              |
142
143In both cases, p4 is derived from the value of `freq` or `note`.
144
145## Limitations / Future Plans
146
147Apart from the above listed p values, no other parameter can be patterned so far.
148This also means that [audio effects](/learn/effects/) will not work.
149In the future, the integration could be improved by passing all patterned control parameters to the csound instrument.
150This could work by a unique [channel](https://kunstmusik.github.io/icsc2022-csound-web/tutorial2-interacting-with-csound/#step-4---writing-continuous-data-channels)
151for each value. Channels could be read [like this](https://github.com/csound/csound/blob/master/Android/CsoundForAndroid/CsoundAndroidExamples/src/main/res/raw/multitouch_xy.csd).
152Also, it might make sense to have a standard library of csound instruments for strudel's effects.