|
Main /
ConnectingDIYInterfaceToRaspberryPiForCSoundTopics
Simple exampleUsed the example codes from CSound Physical Modeling. The code used here is built on the wgbow_enhanced.csd file from that page. Instrument #1 is the main instrument which handles all the MIDI events. It uses certain global variables gkPlaying to check to see if the instrument is already being played or not. The instrument is triggered only when it is not being played currently (gkPlaying = 0). gkFreq is set on a control change MIDI event and is used by the wgbow opcode to specify the frequency of the note. Useful link - MIDI Status Codes CSound code: <CsoundSynthesizer> <CsOptions> -odac -Ma ;-+rtaudio=jack </CsOptions> <CsInstruments> sr = 44100 ksmps = 32 nchnls = 2 0dbfs = 1 seed 0 gisine ftgen 0,0,4096,10,1 gaSend init 0 gkFreq init 120 gkPlaying init 0 instr 1 ;printks "Reading midiin", 1 kstatus, kchan, kdata1, kdata2 midiin printk2 kstatus if kstatus == 144 then ;MIDI status note on printks "Note on\n", 1 if gkPlaying == 0 then printks "triggering event", 1 event "i", 2, 0, -1 gkPlaying = 1 endif elseif kstatus == 128 then ;MIDI status note off printks "Note off\n", 1 turnoff2 2, 0, 1 gkPlaying = 0 elseif kstatus == 176 then ;MIDI status control change printks "Control change\n", 1 gkFreq = kdata2 * 2 + 20 ;set frequency based on control change value printks "New freq: %d\n", 1, gkFreq endif endin instr 2 ; wgbow instrument kamp = 0.1 ;kfreq = p4 kpres = 0.2 krat rspline 0.006,0.988,0.1,0.4 kvibf = 4.5 kvibamp = 0 iminfreq = 20 aSig wgbow kamp,gkFreq,kpres,krat,kvibf,kvibamp,gisine,iminfreq aSig butlp aSig,2000 aSig pareq aSig,80,6,0.707 outs aSig,aSig gaSend = gaSend + aSig/3 endin instr 3 ; reverb aRvbL,aRvbR reverbsc gaSend,gaSend,0.9,7000 outs aRvbL,aRvbR clear gaSend endin </CsInstruments> <CsScore> i 1 0 3600 ; reverb instrument i 3 0 480 </CsScore> </CsoundSynthesizer> We used the Arduino Due since it was a requirement of the MIDIUSB library for the arduino to be recognized as a MIDI peripheral over USB. This code is patched together from the examples MIDIUSB_write and Button in the Arduino IDE. The main loop reads the status of the pushbutton and when it is high sends a MIDI note on event, otherwise it sends the MIDI note off event. The reading from the potentiometer is read in the main loop, and if the value has changed from the previous value, then a MIDI control change event is sent. Since this wiki is not allowing the pasting of the arduino code, the code for the Due can be found here CodeThe above code and a sample with 3 potentiometers connecting different parameters of the instruments can be found here - https://github.com/theisro/arduinomidicsound Physical Modeling ExamplesUsed the Van der Pol Oscillator from CSound Physical Modeling. Linked it to the Arduino Due multipot (multi-potentiometer) sketch. <CsoundSynthesizer> <CsOptions> -odac -Ma </CsOptions> <CsInstruments> sr = 44100 ksmps = 32 nchnls = 2 0dbfs = 1 ;Van der Pol Oscillator ;outputs a nonliniear oscillation ;inputs: a_excitation, k_frequency in Hz (of the linear part), nonlinearity (0 < mu < ca. 0.7) opcode v_d_p, a, akk setksmps 1 av init 0 ax init 0 ain,kfr,kmu xin kc = 2-2*cos(kfr*2*$M_PI/sr) aa = -kc*ax + kmu*(1-ax*ax)*av av = av + aa ax = ax + av + ain xout ax endop gkFreq init 830 gkFreq2 init 450 gkPlaying init 0 instr 1 ;printks "Reading midiin", 1 kstatus, kchan, kdata1, kdata2 midiin ;printk2 kstatus if kstatus == 144 then printks "Note on\n", 1 if gkPlaying == 0 then printks "triggering event", 1 event "i", 2, 0, 20 gkPlaying = 1 endif elseif kstatus == 128 then printks "Note off\n", 1 turnoff2 2, 0, 1 gkPlaying = 0 elseif kstatus == 176 then if kdata1 == 16 then printks "Control change\n", 1 gkFreq = kdata2 + 600 printks "New freq: %d\n", 1, gkFreq printks "kdata1: %d\n", 1, kdata1 else printks "Control2 change\n", 1 gkFreq2 = kdata2 + 200 printks "New freq: %d\n", 1, gkFreq2 endif endif endin instr 2 kaex = .001 kfex = 830 kamp = .15 kf = 455 kmu linseg 0,p3,.7 a1 poscil kaex,gkFreq ;a1 poscil kaex,kfex ;aout v_d_p a1,kf,kmu aout v_d_p a1,gkFreq2,kmu out kamp*aout,a1*100 endin </CsInstruments> <CsScore> i1 0 600 </CsScore> </CsoundSynthesizer> Problems with wgbrassThe sketches with wgbrass are not working, or produce sound intermittently. I posted on the CSound mailing list. The one response I got said that wgbrass is a problematic opcode. So for now we are going to avoid using this opcode. |