Skip to content

Instantly share code, notes, and snippets.

@juniorxsound
Created December 14, 2016 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juniorxsound/c8d9fa07fda9dccaa95b249b8290ef67 to your computer and use it in GitHub Desktop.
Save juniorxsound/c8d9fa07fda9dccaa95b249b8290ef67 to your computer and use it in GitHub Desktop.
<CsoundSynthesizer>
<CsOptions>
--opcode-lib=serialOpcodes.dylib -+rtmidi=portmidi -Ma -odac
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 150 ; the default krate can be too fast for the arduino to handle
nchnls = 2
0dbfs = 5
; Master Serial port value
gkArduinoSerial init 0
; Since it's a midi device you have to init the output before it's used
gasine init 0
gasaw init 0
gasquare init 0
gapulse init 0
gkLforate init 0
; Assign MIDI Channel 1 to instr 2
massign 1,1
prealloc 1,10
/*A number range mapper opcode which outputs an normalised a rate variable*/
opcode map, a, kiiii
kval, imin_in, imax_in, imin_out, imax_out xin ; read input parameters
a1 = (kval - imin_in) * (imax_out - imin_out) / (imax_in - imin_in) + imin_out;
xout a1 ; write output
endop
opcode mapToK, k, kiiii
kval, imin_in, imax_in, imin_out, imax_out xin ; read input parameters
k1 = (kval - imin_in) * (imax_out - imin_out) / (imax_in - imin_in) + imin_out;
xout k1 ; write output
endop
instr 1 ;Basic MIDI sine synth
aenv init 0.01
; Get MIDI note from MIDI channel
inote cpsmidi
; Setup velocity
iveloc ampmidi 1
; Used for debug - just prints the velocity of each key
; print iveloc
aenv madsr 0.001, 5, 5, 0.1
al lfo 3, gkLforate
;pOscilator with sine wave function table
gasine poscil aenv*iveloc +al, inote, 1
;pOscilator with sine wave function table
gasaw poscil aenv*iveloc +al, inote, 2
;pOscilator with sine wave function table
gasquare poscil aenv*iveloc+al, inote, 3
;pOscilator with sine wave function table
gapulse poscil aenv*iveloc+al, inote, 4
endin
instr 98 ; Logic and Arduino communication
; Open the Arduino serial port
iPort serialBegin "/dev/cu.usbmodem1421", 9600
; Read the Arduino serial port
gkArduinoSerial serialRead iPort
; Used for printing serial event only if a value has changed
printk2 gkArduinoSerial
;Answer Arduino's handshake
if ( gkArduinoSerial == 112.00000 ) then
serialWrite iPort, 112.00000
endif
endin
instr 99 ;The Mixer
aModule1Left init 0
aModule1Right init 0
avolumeMult init 0
asineMult init 0
asawMult init 0
aModule2Left init 0
aModule2Right init 0
if (gkArduinoSerial != -1) then
;Use the map opcode to map ranges of the serial commands to different parameters
if(gkArduinoSerial >= 1 && gkArduinoSerial <= 100) then
;aOut kval, imin_in, imax_in, imin_out, imax_out
avolumeMult map gkArduinoSerial, 0, 99, 0, 1
endif
if(gkArduinoSerial >= 101 && gkArduinoSerial <= 151) then
asineMult map gkArduinoSerial, 101, 151, 1, 0
asawMult map gkArduinoSerial, 101, 151, 0, 1
endif
if(gkArduinoSerial >= 200 && gkArduinoSerial <= 255) then
gkLforate mapToK gkArduinoSerial, 200, 255, 0, 20
endif
endif
; Wave selector knob - Module 1
;Sine
aModule1Left += gasine*asineMult
aModule1Right += gasine*asineMult
;Saw
aModule1Left += gasaw*asawMult
aModule1Right += gasaw*asawMult
; Wave selector knob - Module 2
;Square
aModule2Left += gasquare*asineMult
aModule2Right += gasquare*asineMult
;Pulse
aModule2Left += gapulse*asawMult
aModule2Right += gapulse*asawMult
; Module 1 Master volume knob
aModule1Left *= avolumeMult
aModule1Right *= avolumeMult
aModule1Left *= asineMult
aModule1Right *=asineMult
aModule2Left *= avolumeMult
aModule2Right *= avolumeMult
aModule2Left *= asawMult
aModule2Right *= asawMult
outs (aModule1Left + aModule2Left), (aModule1Right + aModule2Right)
gasine = 0
gasaw = 0
gasquare = 0
gapulse = 0
; Zero them out:
aModule1Left = 0
aModule1Right = 0
aModule2Left = 0
aModule2Right = 0
endin
</CsInstruments>
<CsScore>
f1 0 16384 10 1 ;Nice Sine Wave
f2 0 1024 10 1 0.5 0.3 0.25 0.2 0.167 0.14 0.125 .111 ; Nice Sawtooth
f3 0 16384 10 1 0 0.3 0 0.2 0 0.14 0 .111 ; NiceSquare
f4 0 16384 10 1 1 1 1 0.7 0.5 0.3 0.1 ; Nice Pulse
;Play instrument #1 for as long as needed (maybe a week?)
i 98 0 100000 ; keep the arduino serial open
i 99 0 100000 ; keep the mixer open for sometime
e
</CsScore>
</CsoundSynthesizer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment