Csound Programming
CSOUND PROGRAMMING
AIST2010 Lecture 5P CSOUND
CSound is an environment for sound and music computing “A sound compiler” Non-interactive score-driven Real-time interaction It started with Max Mathews’s “Music1” in 1954 at Bell Labs “MusicN” series by Bell Labs, Princeton, Stanford, MIT, etc. CSound was created by Barry Vercoe in 1985 at MIT Media Lab Read about the journey: https://120years.net/music-n-max-mathews-usa-1957/
AIST2010 L5P — CSOUND PROGRAMMING 2 CSOUND FILE STRUCTURE
AIST2010 L5P — CSOUND PROGRAMMING 3 A VERY SIMPLE MUSIC
AIST2010 L5P — CSOUND PROGRAMMING 4 Image from: SAMPLING RATE AND CONTROL RATE http://floss.booktype.pro/csound/a- initialization-and-performance-pass/
The instruments are driven by the sampling rate A hidden loop repeats the code in relevant “instrument”, during the time specified by the “score” sr = 44100 Relationship between sampling rate and control rate There is also a control rate (k-rate) for data not changing so rapidly kr = 4410 ;usually 10% of sampling rate is sufficient ksmps = 10 ;it can also be a ratio Read more about timing and pass here: http://floss.booktype.pro/csound/a-initialization-and-performance-pass/
AIST2010 L5P — CSOUND PROGRAMMING 5 instr 1 ; ifreq only updated once VARIABLES ; during init ifreq = 440 ; kenv is a changing value ; updated every k-cycle Mainly 3 types of numerical variables, kenv linseg 0, 1, 1, 1, 0 differentiated by the first character ; aout is an audio signal: ; vector every k-cycle “a”udio variables: one value per sample aout oscili kenv, ifreq or, view it as a ksmps block of values per k-time out aout “k”ontrol variables: one value per k-time endin “i”nit variables: one value per note (in score) Waveform by the above code Global variables that can be read by all instruments Add “g” in front of a/k/i
AIST2010 L5P — CSOUND PROGRAMMING 6 INSTRUMENTS AND P-FIELDS
All instruments specified using instr…endin blocks can be called upon in the score Different methods to generate sounds, and receive parameters from score Besides numbers, you can also use a "string" to name the instruments
; instruments ; score instr 1 i1 0 1 0.5 440 aout oscili p4, p5 ;sine ; start=0, dur=1, p4=0.5, p5=440 out aout i2 0.5 2 0.8 220 endin ; start=0.5, dur=2, p4=0.8, p5=220 instr 2 aout vco2 p4, p5 ;sawtooth out aout endin AIST2010 L5P — CSOUND PROGRAMMING 7 OPCODES AND OPERATORS
Opcodes are basic unit generators They are like usual functions in other languages, and easily defined by user opcode … endop block Two ways to use opcodes, e.g. oscili aout oscili 1, 440 ;the traditional way aout = oscili(1, 440) ;easier for inline opcode
Common operators are available, with usual operator precedence + - * / ^ % = += -= *= /= == >= > < <= !=
AIST2010 L5P — CSOUND PROGRAMMING 8 i2 BUILDING ENVELOPES i4 i3
Lines can be built for affecting amplitude, i5 time frequency, or any values as an input i1 for opcodes idur1 idur2 idur3 idur4 xvar line ia, idur, ib A line built using linseg A line from ia to ib for idur seconds xvar linseg i1, idur1, i2, idur2, i3, idur3, …, in Line segments from i1 to in using multiple points with durations xvar expseg i1, idur1, i2, idur2, i3, idur3, …, in Exponential segments similar to linseg You can also use an oscili for building an envelope!
AIST2010 L5P — CSOUND PROGRAMMING 9 CONDITIONS AND LOOPS
Not common but exists… Looping using while if
AIST2010 L5P — CSOUND PROGRAMMING 10 ADDITIVE SYNTHESIS
… … instr 1 ; Trumpet instr 2 ; Nokia phone ifreq = p4 ifreq = p4 aout1 oscili .141, ifreq aout1 oscili .045, ifreq aout2 oscili .2 , ifreq*2 aout2 oscili .079, ifreq*2 aout3 oscili .141, ifreq*3 aout3 oscili .158, ifreq*3 aout4 oscili .112, ifreq*4 aout4 oscili .224, ifreq*4 aout5 oscili .079, ifreq*5 aout5 oscili .158, ifreq*5 aout6 oscili .056, ifreq*6 aout6 oscili .126, ifreq*6 aout7 oscili .05 , ifreq*7 aout7 oscili .158, ifreq*7 aout8 oscili .035, ifreq*8 aout8 oscili .045, ifreq*8 aout9 oscili .032, ifreq*9 aout9 oscili .032, ifreq*9 aout10 oscili .02, ifreq*10 aout10 oscili .025, ifreq*10 out (aout1+aout2+aout3+aout4+aout5\ ;nextline out (aout1+aout2+aout3+aout4+aout5\ ;nextline +aout6+aout7+aout8+aout9+aout10)/10 +aout6+aout7+aout8+aout9+aout10)/10 endin endin … … i1 0 1 783.99 i2 0 1 440 … … AIST2010 L5P — CSOUND PROGRAMMING 11 WAVETABLE SYNTHESIS Simplifying from additive synthesis with constant sinusoidal waves ;; a simple instrument
AIST2010 L5P — CSOUND PROGRAMMING 12 FREQUENCY MODULATION FM is to introduce a extra wave onto ifreq, with controllable parameters Basic vibrato… And FM? ifreq = 440 icarfreq = 440 ;use kvib for changing vib iratio = 2 ;kvib linseg 1, 5, 20 ; mod freq depends on ratio ;kmod oscili 1, kvib imodfreq = icarfreq*iratio kmod oscili 1, 10 index = 5 aout oscili 0.5, ifreq+kmod ; fmod amp depends on index out aout imodamp = imodfreq*index kmod oscili imodamp, imodfreq aout oscili 0.5, icarfreq+kmod out aout
AIST2010 L5P — CSOUND PROGRAMMING 13 CSOUND WITH MIDI
CSound by defaults maps MIDI channels 1–16 to instr 1–16 In Configure >> Run, choose the appropriate MIDI input interface Or you can use the virtual keyboard as well instr 1 kfreq cpsmidib 2 ;cpsmidib gets midi note messages with pitchbend ; a k-type variable is needed since the frequency may change iamp ampmidi 0dbfs ;ampmidi gets midi velocity, normalize to 0dbfs aout oscili iamp, kfreq out aout endin An extra line is needed in the score: f0 300 ; turn on engine for 5 mins, or use any timing you prefer There are many more in CSound MIDI for you to discover!
AIST2010 L5P — CSOUND PROGRAMMING 14 Freq (Hz) MIDI oct pch pch oct MIDI Freq (Hz) 27.5 21 22 29.135 30.868 23 C1 32.703 24 5.00 25 34.648 36.708 26 27 38.891 41.203 28 HOW TO EASILY NOTATE PITCHES? 43.654 29 30 46.249 48.999 31 32 51.913
MIDI 22 25 27 30 32 34 37 39 42 44 46 49 51 54 56 58 61 63 66 68 70 73 75 78 80 82 85 87 90 92 94 5597 99 33102 104 106 34 58.27 61.735 35 C2 oct 8.08 8.25 8.50 8.67 8.83 65.406 36 6.00 37 69.296 73.416 38 39 77.782 pch 8.01 8.03 8.06 8.08 8.10 82.407 40 87.307 41 42 92.499 97.999 43 44 103.83 C1 C2 C3 D3 E3 F3 G3 A3 B3 C4 D4 E4 F4 G4 A4 B4 C5 C6 C7 C8 110 45 46 116.54 123.47 47 C3 pch 5.00 6.00 7.00 8.00 8.02 8.04 8.05 8.07 8.09 8.11 9.00 10.00 130.8111.00 48 7.0012.00 49 138.59 D3 146.83 50 51 155.56 E3 oct 8.00 8.17 8.33 8.42 8.58 8.75 8.92 9.00 164.81 52
è F3 pch .00,174.61.01,53 …, .11 MIDI 21 23 24 26 28 29 31 33 35 36 38 40 41 43 45 47 48 50 52 53 55 57 59 60 62 64 65 67 69 71 72 74 76 77 79 81 83 84 86 88 89 91 93 95 96 98 100 101 103 105 107 108 54 185 G3 oct è 1/12,196 2/12,55 …, 11/12 56 207.65 A3 220 57 58 233.08 Pitch converters octcps(cps) cps to oct 246.94 59 B3 C4 261.63 60 8.00 8.00 8.01 8.08 61 277.18 D4 octpch(pch) pch to oct cpsmidinn(nn) midi# to cps293.66 62 8.17 8.02 8.03 8.25 63 311.13 329.63 64 8.33 8.04 E4 F4 cpspch(pch) pch to cps pchmidinn(nn) midi# to pch349.23 65 8.42 8.05 8.06 8.50 66 369.99 G4 392 67 8.58 8.07 8.08 8.67 68 415.3 A4 pchoct(oct) oct to pch octmidinn(nn) midi# to oct440 69 8.75 8.09 8.10 8.83 70 466.16 493.88 71 8.92 8.11 B4 C5 cpsoct(oct) oct to cps cps = cycle per second = Hz 523.25 72 9.00 9.00 73 554.37 587.33 74 75 622.25 AIST2010659.26 L5P — CSOUND76 PROGRAMMING 15 698.46 77 78 739.99 783.99 79 80 830.61 880 81 82 932.33 987.77 83 C6 1046.5 84 #### 85 1108.7 1174.7 86 87 1244.5 1318.5 88 1396.9 89 90 1480 1568 91 92 1661.2 1760 93 94 1864.7 1975.5 95 C7 2093 96 #### 97 2217.5 2349.3 98 99 2489 2637 100 2793.8 101 102 2960 3136 103 104 3322.4 3520 105 106 3729.3 3951.1 107 C8 4186 108 #### CSOUND API
CSound allow easy collaboration with other programming environments CSound API (in C) CSound on iOS/Android CSound in MaxMSP or PureData CSound as VST Web-based CSound
AIST2010 L5P — CSOUND PROGRAMMING 16 READ FURTHER
Chapter 3, “Fundamentals”, CSound. Chapter 9, “MIDI Input and Output”, CSound. Useful websites: https://csound.com/get-started.html https://csound.com/docs/manual/index.html http://floss.booktype.pro/csound
AIST2010 L5P — CSOUND PROGRAMMING 17