Skip to content

Instantly share code, notes, and snippets.

@jacobjoaquin
Created August 30, 2010 17:24
Show Gist options
  • Save jacobjoaquin/557723 to your computer and use it in GitHub Desktop.
Save jacobjoaquin/557723 to your computer and use it in GitHub Desktop.
int leftPin = 0;
int rightPin = 1;
int leftMin = 400;
int leftMax = 600;
int rightMin = 400;
int rightMax = 600;
/*
int leftMin = 600;
int leftMax = 940;
int rightMin = 580;
int rightMax = 930;
*/
void setup() {
Serial.begin(9600);
}
void loop() {
int leftSig = analogRead(leftPin);
int rightSig = analogRead(rightPin);
leftSig = constrain(leftSig, leftMin, leftMax);
leftSig = map(leftSig, leftMin, leftMax, 0, 255);
// Serial.println(leftSig);
Serial.print(leftSig, BYTE);
delay(10);
rightSig = constrain(rightSig, rightMin, rightMax);
rightSig = map(rightSig, rightMin, rightMax, 0, 255);
// Serial.println(rightSig);
Serial.print(rightSig, BYTE);
delay(10);
// delay(1000 / 10);
}
/*
Csound Photo-Theremin
By Jacob Joaquin
csoundblog.com
Uses two photoresistors to control amplitude and pitch of a Csound
synthesizer, emulating a Theremin.
Code borrows from Processing example Serial Duplex by Tom Igoe.
Sand instrument adapted from Trapped in Convert by Dr. Richard Boulanger
*/
import processing.serial.*;
import csoundo.*;
Csoundo cs;
Serial myPort;
int byte0 = 0;
int byte1 = 0;
void setup() {
size(640, 480);
frameRate(30);
background(0);
smooth();
println(Serial.list());
// Tom Igoe says:
// I know that the first port in the serial list on my macfl
// is always my FTDI adaptor, so I open Serial.list()[0].
// In Windows, this usually opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
cs = new Csoundo(this, "synth.csd");
//cs = new Csoundo(this, "trapped_instr.csd");
cs.run();
}
void draw() {
noStroke();
fill(0, 32);
rect(0, 0, width, height);
stroke(255);
float right = cs.getChn("right");
float left = 1 - cs.getChn("left");
line(right * width, 0, right * width, height);
line(0, left * height, width, left * height);
}
void serialEvent(Serial myPort) {
byte[] buffer = new byte[2];
while(myPort.available() > 1) {
buffer = myPort.readBytes();
byte0 = convert(buffer[0]);
byte1 = convert(buffer[1]);
cs.setChn("left", map(byte0, 0, 255, 0, 1));
cs.setChn("right", map(byte1, 0, 255, 1, 0));
}
}
// Converts signed to unsigned
int convert(byte b) {
if (b < 0) {
return 127 + (129 - b * -1);
}
return b;
}
<CsoundSynthesizer>
<CsInstruments>
sr = 44100
kr = 441
ksmps = 100
nchnls = 1
0dbfs = 1
gi_wave ftgen 1, 0, 8192, 9, 0.5, 1, 0
chn_k "left", 1
chn_k "right", 1
chnset 0, "left"
chnset 0, "right"
instr 1
idur = p3
iamp = p4
kamp chnget "left"
kpitch chnget "right"
kamp portk kamp, 0.05
kpitch portk kpitch, 0.05
a1 oscil iamp * kamp, 100 * 16 ^ kpitch, gi_wave
out a1
endin
</CsInstruments>
<CsScore>
i 1 0 [60 * 60 * 24] 0.707
</CsScore>
</CsoundSynthesizer>
;============================================================================;
;============================================================================;
;============================================================================;
; Sand Instrument ;
; Adapted from ;
; == TRAPPED IN CONVERT == ;
; Richard Boulanger ;
;============================================================================;
;============================================================================;
; ORCHESTRA ;
;============================================================================;
;============================================================================;
;============================================================================;
; written July 1979 in music11 ;
; M.I.T. Experimental Music Studio ;
; revised June 1986 in Csound ;
; M.I.T. Media Lab ;
; revised July 1996 in SHARCsound ;
; Analog Devices Inc. ;
;============================================================================;
;============================================================================;
;============================================================================;
<CsoundSynthesizer>
<CsInstruments>
sr = 44100
kr = 4410
ksmps = 10
nchnls = 2
0dbfs = 1
garvb init 0
gadel init 0
chn_k "left", 1
chn_k "right", 1
chnset 0, "left"
chnset 0, "right"
instr 9 ; p4 = delay send factor
kamp chnget "left"
kpitch chnget "right"
kamp portk kamp, 0.05
kpitch portk kpitch, 0.05
kfreq = 30 * 4 ^ kpitch
ifreq = cpspch(p5) ; p5 = freq
; p6 = amp
k2 randh p8, p9, .1 ; p7 = reverb send factor
k3 randh p8 * .98, p9 * .91, .2 ; p8 = rand amp
k4 randh p8 * 1.2, p9 * .96, .3 ; p9 = rand freq
k5 randh p8 * .9, p9 * 1.3
;kenv linen p6, p3 *.1, p3, p3 * .8
kenv = kamp * p6
a1 oscil kenv, kfreq + k2, 1, .2
a2 oscil kenv * .91, (kfreq + .004) + k3, 2, .3
a3 oscil kenv * .85, (kfreq + .006) + k4, 3, .5
a4 oscil kenv * .95, (kfreq + .009) + k5, 4, .8
amix = (a1 + a2 + a3 + a4)
outs (a1 + a3), (a2 + a4)
garvb = garvb + (amix * p7)
gadel = gadel + (amix * p4)
endin
;============================================================================;
;==================================== SMEAR =================================;
;============================================================================;
instr 98
asig delay gadel, .08
outs asig, asig
gadel = 0
endin
;============================================================================;
;==================================== SWIRL =================================;
;============================================================================;
instr 99 ; p4 = panrate
k1 oscil .5, p4, 1
k2 = .5 + k1
k3 = 1 - k2
asig reverb garvb, 2.1
outs asig * k2, (asig * k3) * (-1)
garvb = 0
endin
</CsInstruments>
<CsScore>
f1 0 8192 10 1
f2 0 512 10 10 8 0 6 0 4 0 1
f3 0 512 10 10 0 5 5 0 4 3 0 1
f4 0 2048 10 10 0 9 0 0 8 0 7 0 4 0 2 0 1
;i 1 0 [60 * 60 * 24] 0.707
i 9 0 [60 * 60 * 24] 0.4 4.113 0.2 0.2 6.2 320
i98 0 [60 * 60 * 24]
i99 0 [60 * 60 * 24] 0.0618
</CsScore>
</CsoundSynthesizer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment