| 1 |
<CsoundSynthesizer> |
| 2 |
<CsLicense> |
| 3 |
MANDELBROT SET ORBIT SONIFICATION |
| 4 |
Copyright (C) 2022 by Michael Gogins |
| 5 |
|
| 6 |
This piece demonstrates how to generate Csound scores from orbits of points on |
| 7 |
or near the boundary of the Mandelbrot set. |
| 8 |
|
| 9 |
See https://www.stefanbion.de/fraktal-generator/z-orbits.htm and similar |
| 10 |
resources in order to find such points by exploration. |
| 11 |
</CsLicense> |
| 12 |
|
| 13 |
<CsOptions> |
| 14 |
-fodac -m34 -d -+msg_color=0 |
| 15 |
; -o Mandel_Gogins.wav -W ;;; for file output any platform |
| 16 |
</CsOptions> |
| 17 |
<CsInstruments> |
| 18 |
sr = 48000 |
| 19 |
ksmps = 128 |
| 20 |
nchnls = 2 |
| 21 |
0dbfs = 1 |
| 22 |
|
| 23 |
alwayson "ReverbSC" |
| 24 |
alwayson "MasterOutput" |
| 25 |
connect "Plucked", "outleft", "ReverbSC", "inleft" |
| 26 |
connect "Plucked", "outright", "ReverbSC", "inright" |
| 27 |
connect "ReverbSC", "outleft", "MasterOutput", "inleft" |
| 28 |
connect "ReverbSC", "outright", "MasterOutput", "inright" |
| 29 |
gk_Plucked_midi_dynamic_range chnexport "gk_Plucked_midi_dynamic_range", 3 |
| 30 |
gk_Plucked_midi_dynamic_range init 30 |
| 31 |
gk_Plucked_space_left_to_right chnexport "gk_Plucked_space_left_to_right", 3 |
| 32 |
gk_Plucked_space_left_to_right init .5 |
| 33 |
gk_Plucked_level chnexport "gk_Plucked_level", 3 |
| 34 |
gk_Plucked_level init 0 |
| 35 |
gi_Plucked_sine ftgen 0, 0, 65537, 10, 1 |
| 36 |
|
| 37 |
instr Plucked |
| 38 |
; Author: Michael Gogins |
| 39 |
i_instrument = p1 |
| 40 |
i_time = p2 |
| 41 |
; Make indefinite notes last no longer than the physical decay. |
| 42 |
i_physical_decay = 20 |
| 43 |
if p3 == -1 then |
| 44 |
i_duration = i_physical_decay |
| 45 |
else |
| 46 |
i_duration = p3 |
| 47 |
endif |
| 48 |
i_midi_key = p4 |
| 49 |
i_midi_dynamic_range = i(gk_Plucked_midi_dynamic_range) |
| 50 |
i_midi_velocity = p5 ;* i_midi_dynamic_range / 127 + (63.5 - i_midi_dynamic_range / 2) |
| 51 |
i_midi_velocity ampmidid i_midi_velocity, i_midi_dynamic_range |
| 52 |
k_space_front_to_back = p6 |
| 53 |
if p7 == 0 then |
| 54 |
k_space_left_to_right = gk_Plucked_space_left_to_right |
| 55 |
else |
| 56 |
k_space_left_to_right = p7 |
| 57 |
endif |
| 58 |
k_space_bottom_to_top = p8 |
| 59 |
i_phase = p9 |
| 60 |
i_detune_cents = 1.5 |
| 61 |
i_detune = i_detune_cents / 100 |
| 62 |
i_frequency1 = cpsmidinn(i_midi_key - i_detune) |
| 63 |
i_frequency2 = cpsmidinn(i_midi_key) |
| 64 |
i_frequency3 = cpsmidinn(i_midi_key + i_detune) |
| 65 |
; Adjust the following value until "overall amps" at the end of performance is about -6 dB. |
| 66 |
i_overall_amps = 26 - 70 |
| 67 |
i_normalization = ampdb(-(i_overall_amps)) / 2 |
| 68 |
i_amplitude = ampdb(i_midi_velocity) * i_normalization |
| 69 |
k_gain = ampdb(gk_Plucked_level) |
| 70 |
asignal1 wgpluck2 0.1, 1.0, i_frequency1, 0.25, 0.222 |
| 71 |
asignal2 wgpluck2 0.1, 1.0, i_frequency2, 0.20, 0.223 |
| 72 |
asignal3 wgpluck2 0.1, 1.0, i_frequency3, 0.23, 0.225 |
| 73 |
a_signal = (asignal1 + asignal2 + asignal3) |
| 74 |
; As with most instruments that are based upon an impulse delivered to a |
| 75 |
; resonator, there are two envelopes, one for the physical decay with a |
| 76 |
; fixed release ending at zero, and one with a release segment to remove |
| 77 |
; clicks from the attack and release. |
| 78 |
; |
| 79 |
; As with most software instruments that are modeled on an impulse exciting a |
| 80 |
; resonator, there should be two envelopes. The "physical" envelope must have a |
| 81 |
; fixed decay ending at zero. |
| 82 |
i_declick_minimum = .001 |
| 83 |
i_attack = .001 / i_frequency2 + i_declick_minimum |
| 84 |
i_exponent = 7 |
| 85 |
a_physical_envelope transeg 0, i_attack, i_exponent, 1, i_physical_decay, -i_exponent, 0 |
| 86 |
; The de-clicking envelope must have attack and release segments that damp |
| 87 |
; artifacts in the signal. The duration of these segments depends on |
| 88 |
; the behavior of the instrument, and may vary as a function of frequency. |
| 89 |
i_declick_attack = i_attack |
| 90 |
i_declick_release = i_declick_minimum * 2 |
| 91 |
; The end of the note must be extended _past_ the end of the release segment. |
| 92 |
xtratim 1 |
| 93 |
a_declicking_envelope cossegr 0, i_declick_attack, 1, i_duration, 1, i_declick_release, 0 |
| 94 |
; The envelope of the instrument is the product of the physical envelope times |
| 95 |
; the declicking envelope. |
| 96 |
a_envelope = a_physical_envelope * a_declicking_envelope |
| 97 |
; That envelope is then low-pass filtered to remove most discontinuities. |
| 98 |
a_filtered_envelope tonex a_envelope, 40, 4 |
| 99 |
a_signal = a_signal * i_amplitude * a_filtered_envelope * k_gain *.001 |
| 100 |
#ifdef USE_SPATIALIZATION |
| 101 |
a_spatial_reverb_send init 0 |
| 102 |
a_bsignal[] init 16 |
| 103 |
a_bsignal, a_spatial_reverb_send Spatialize a_signal, k_space_front_to_back, k_space_left_to_right, k_space_bottom_to_top |
| 104 |
outletv "outbformat", a_bsignal |
| 105 |
outleta "out", a_spatial_reverb_send |
| 106 |
#else |
| 107 |
a_out_left, a_out_right pan2 a_signal, k_space_left_to_right |
| 108 |
outleta "outleft", a_out_left |
| 109 |
outleta "outright", a_out_right |
| 110 |
#endif |
| 111 |
prints "%-24s i %9.4f t %9.4f d %9.4f k %9.4f v %9.4f p %9.4f #%3d\n", nstrstr(p1), p1, p2, p3, p4, p5, p7, active(p1) |
| 112 |
endin |
| 113 |
|
| 114 |
gk_ReverbSC_feedback chnexport "gk_ReverbSC_feedback", 3 |
| 115 |
gk_ReverbSC_wet chnexport "gk_ReverbSC_wet", 3 |
| 116 |
gi_ReverbSC_delay_modulation chnexport "gi_ReverbSC_delay_modulation", 3 |
| 117 |
gk_ReverbSC_frequency_cutoff chnexport "gk_ReverbSC_frequency_cutoff", 3 |
| 118 |
gk_ReverbSC_feedback init 0.78 |
| 119 |
gk_ReverbSC_wet init 0.5 |
| 120 |
gi_ReverbSC_delay_modulation init 0.0075 |
| 121 |
gk_ReverbSC_frequency_cutoff init 15000 |
| 122 |
instr ReverbSC |
| 123 |
gk_ReverbSC_dry = 1.0 - gk_ReverbSC_wet |
| 124 |
aleftin init 0 |
| 125 |
arightin init 0 |
| 126 |
aleftout init 0 |
| 127 |
arightout init 0 |
| 128 |
aleftin inleta "inleft" |
| 129 |
arightin inleta "inright" |
| 130 |
aleftout, arightout reverbsc aleftin, arightin, gk_ReverbSC_feedback, gk_ReverbSC_frequency_cutoff, sr, gi_ReverbSC_delay_modulation |
| 131 |
aleftoutmix = aleftin * gk_ReverbSC_dry + aleftout * gk_ReverbSC_wet |
| 132 |
arightoutmix = arightin * gk_ReverbSC_dry + arightout * gk_ReverbSC_wet |
| 133 |
outleta "outleft", aleftoutmix |
| 134 |
outleta "outright", arightoutmix |
| 135 |
prints "%-24s i %9.4f t %9.4f d %9.4f k %9.4f v %9.4f p %9.4f #%3d\n", nstrstr(p1), p1, p2, p3, p4, p5, p7, active(p1) |
| 136 |
endin |
| 137 |
|
| 138 |
gk_MasterOutput_level chnexport "gk_MasterOutput_level", 3 ; 0 |
| 139 |
gS_MasterOutput_filename chnexport "gS_MasterOutput_filename", 3 ; "" |
| 140 |
gk_MasterOutput_level init 0 |
| 141 |
gS_MasterOutput_filename init "" |
| 142 |
instr MasterOutput |
| 143 |
aleft inleta "inleft" |
| 144 |
aright inleta "inright" |
| 145 |
k_gain = ampdb(gk_MasterOutput_level) |
| 146 |
printks2 "Master gain: %f\n", k_gain |
| 147 |
iamp init 1 |
| 148 |
aleft butterlp aleft, 18000 |
| 149 |
aright butterlp aright, 18000 |
| 150 |
outs aleft * k_gain, aright * k_gain |
| 151 |
; We want something that will play on my phone. |
| 152 |
i_amplitude_adjustment = ampdbfs(-3) / 32767 |
| 153 |
i_filename_length strlen gS_MasterOutput_filename |
| 154 |
if i_filename_length > 0 then |
| 155 |
prints sprintf("Output filename: %s\n", gS_MasterOutput_filename) |
| 156 |
fout gS_MasterOutput_filename, 18, aleft * i_amplitude_adjustment, aright * i_amplitude_adjustment |
| 157 |
endif |
| 158 |
prints "%-24s i %9.4f t %9.4f d %9.4f k %9.4f v %9.4f p %9.4f #%3d\n", nstrstr(p1), p1, p2, p3, p4, p5, p7, active(p1) |
| 159 |
endin |
| 160 |
|
| 161 |
; Generates notes based on an orbit of a point in the Mandelbrot set. |
| 162 |
; Points on the _boundary_ of the set orbit on a strange attractor. |
| 163 |
; Points _near_ the boundary are attracted to a fixed point, or to |
| 164 |
; infinity. |
| 165 |
; Implements z <= z^2 + c. |
| 166 |
opcode mandelbrot_orbit, kk, kkkk |
| 167 |
; a b |
| 168 |
; c d |
| 169 |
kk_z_r, kk_z_i, kk_c_r, kk_c_i xin |
| 170 |
; z1z2 = (ac - bd) + i(ad + bc) |
| 171 |
kk_z1_r = kk_z_r * kk_z_r - kk_z_i * kk_z_i |
| 172 |
kk_z1_i = kk_z_r * kk_z_i + kk_z_i * kk_z_r |
| 173 |
kk_z1_r = kk_z1_r + kk_c_r |
| 174 |
kk_z1_i = kk_z1_i + kk_c_i |
| 175 |
xout kk_z1_r, kk_z1_i |
| 176 |
endop |
| 177 |
|
| 178 |
; p4 is the real (or x) coordinate of the point, and |
| 179 |
; p5 is the imaginary (or y) coordinate of the point. |
| 180 |
instr MandelbrotOrbit |
| 181 |
kk_c_r init p4 |
| 182 |
kk_c_i init p5 |
| 183 |
kk_z_r init 0 |
| 184 |
kk_z_i init 0 |
| 185 |
kk_z1_r init 0 |
| 186 |
kk_z1_c init 0 |
| 187 |
i_interval init 4 |
| 188 |
k_iteration init 0 |
| 189 |
k_trigger metro i_interval |
| 190 |
if k_trigger == k(1) then |
| 191 |
k_iteration += 1 |
| 192 |
kk_z1_r, kk_z1_i mandelbrot_orbit kk_z_r, kk_z_i, kk_c_r, kk_c_i |
| 193 |
kk_z_r = kk_z1_r |
| 194 |
kk_z_i = kk_z1_i |
| 195 |
println "iteration: %4d kk_z_r: %9.4f kk_z_i: %9.4f", k_iteration, kk_z_r, kk_z_i |
| 196 |
k_instrument = 1 |
| 197 |
k_time = 0 |
| 198 |
k_duration = 1.5 / i_interval |
| 199 |
k_key_1 = floor(60 + kk_z_r * 30) |
| 200 |
k_key_2 = floor(60 + kk_z_i * 30) |
| 201 |
schedulek k_instrument, k_time, k_duration, k_key_1, 70, 0, .25 |
| 202 |
schedulek k_instrument, k_time, k_duration, k_key_2, 70, 0, .75 |
| 203 |
endif |
| 204 |
endin |
| 205 |
|
| 206 |
</CsInstruments> |
| 207 |
<CsScore> |
| 208 |
i "MandelbrotOrbit" 1 15 -.55 .44 |
| 209 |
i "MandelbrotOrbit" 20 15 .3 .4 |
| 210 |
i "MandelbrotOrbit" 40 15 .3 .1 |
| 211 |
</CsScore> |
| 212 |
</CsoundSynthesizer> |