Commit 585f86a1 authored by dave griffiths's avatar dave griffiths
Browse files

added tangible lsystem music

parent 33a579b6
# invisible driver...
import RPi.GPIO as io
import time
def quick_setup(pins,i):
for pin in pins:
io.setup(pin,i)
def quick_set(pins,v):
debug = ""
for i in range(0,len(pins)):
debug+=str((v>>i)&1)
#if pins[i]==3:
# print(str(pins[i])+":"+str((v>>i)&1))
#time.sleep(0.1)
io.output(pins[i],(v>>i)&1)
#print(debug)
def quick_read(pins):
for i in range(0,len(pins)):
t = io.input(pins[i])
print("pin "+str(i)+" is "+str(t))
def value_read(pins):
v = 0
for i in range(0,len(pins)):
# low is 1, pull up resistor
t = not io.input(pins[i])
#print("pin:"+str(pins[i])+" is "+str(io.input(pins[i])))
v |= t<<i
return v
def value_read_inverse(pins):
v = 0
for i in range(0,len(pins)):
# low is 1, pull up resistor
t = not io.input(pins[len(pins)-i-1])
v |= t<<i
return v
value_pins = [7,8,25,24,23,18,15,14]
address_pins = [27,17,4,3,2]
def read_addr(addr):
quick_set(address_pins, addr)
return value_read(value_pins)
def read_addr_4bit(addr):
quick_set(address_pins, addr)
# flip addr zero due to plug hw error
if addr==0 or addr==1:
if addr%2==0:
return (value_read_inverse(value_pins)>>4) & 0x0f
else:
return value_read_inverse(value_pins) & 0x0f
if addr%2==0:
return (value_read(value_pins)>>4) & 0x0f
else:
return value_read(value_pins) & 0x0f
def read_code():
r = []
for i in range(0,16):
r.append(read_addr(i))
return r
def init():
try:
io.setmode(io.BCM)
quick_setup(value_pins, io.IN)
quick_setup(address_pins, io.OUT)
quick_set(address_pins, 0)
except:
print "error"
def read_all():
ret = []
for addr in range(0,32):
ret.append(read_addr_4bit(addr))
return ret
def read_addr(addr):
return read_addr_4bit(addr)
; lz/nz
(synth-init 50 22050)
(define (make-lz md d stk w h mem)
(vector md d stk w h mem))
(define (lz-md l) (vector-ref l 0))
(define (lz-d l) (vector-ref l 1))
(define (lz-stk l) (vector-ref l 2))
(define (lz-w l) (vector-ref l 3))
(define (lz-h l) (vector-ref l 4))
(define (lz-mem l) (vector-ref l 5))
(define (set-lz-d! l v) (vector-set! l 1 v))
(define (set-lz-stk! l v) (vector-set! l 2 v))
(define (set-lz-mem! l v) (vector-set! l 5 v))
(define (build-lz md w h)
(make-lz md 0 '((0 0)) w h (make-vector (* w h) #\ )))
(define (lz-poke lz x y s)
(vector-set! (lz-mem lz) (+ x (* (lz-w lz) y)) s))
(define (lz-peek lz x y)
(vector-ref (lz-mem lz) (+ x (* (lz-w lz) y))))
(define (lz-read lz)
(let ((top (lz-top lz)))
(lz-peek lz (car top) (cadr top))))
(define (lz-pop! lz)
(when (> (lz-d lz) 0)
(set-lz-d! lz (- (lz-d lz) 1)))
(let ((tmp (car (lz-stk lz))))
(when (not (eqv? (length (lz-stk lz)) 1))
(set-lz-stk! lz (cdr (lz-stk lz))))
tmp))
(define (lz-push! lz item)
(when (< (lz-d lz) (lz-md lz))
(set-lz-d! lz (+ (lz-d lz) 1))
(set-lz-stk! lz (cons item (lz-stk lz)))))
(define (lz-top lz)
(car (lz-stk lz)))
(define (set-lz-top! lz s)
(set-lz-stk! lz (cons s (cdr (lz-stk lz)))))
(define (lz-inc-pos lz)
(set-lz-top! lz (list (+ (car (lz-top lz)) 1) (cadr (lz-top lz)))))
(define (lz-tick lz)
(lz-inc-pos lz)
(when (>= (car (lz-top lz)) 8)
(cond
((eqv? (length (lz-stk lz)) 1)
(set-lz-top! lz (list 0 0)))
(else
(lz-pop! lz))))
(let ((pos (car (lz-top lz)))
(pat (cadr (lz-top lz)))
(data (lz-read lz)))
; (printf "~a ~a ~a ~a~n" pos pat data (lz-stk lz))
(cond
((char=? data #\ )
(cond
((eqv? (length (lz-stk lz)) 1)
(set-lz-top! lz (list 0 0)))
(else
(lz-pop! lz))))
((char=? data #\A) (lz-inc-pos lz) (lz-push! lz (list 0 0)))
((char=? data #\B) (lz-inc-pos lz) (lz-push! lz (list 0 1)))
((char=? data #\C) (lz-inc-pos lz) (lz-push! lz (list 0 2))))
(lz-read lz)))
(define (lz-prog lz pat str)
(let ((c 0))
(for-each
(lambda (item)
(lz-poke lz c pat item)
(set! c (+ c 1)))
(string->list str))))
; nz
(define max-vals 16)
(define (make-nz lz vals vx sz cur-t tk off)
(vector lz vals vx sz cur-t tk off))
(define (nz-lz n) (vector-ref n 0))
(define (nz-vals n) (vector-ref n 1))
(define (nz-vx n) (vector-ref n 2))
(define (nz-sz n) (vector-ref n 3))
(define (nz-cur-t n) (vector-ref n 4))
(define (nz-tk n) (vector-ref n 5))
(define (nz-off n) (vector-ref n 6))
(define (set-nz-vals! n v) (vector-set! n 1 v))
(define (set-nz-vx! n v) (vector-set! n 2 v))
(define (set-nz-cur-t! n v) (vector-set! n 4 v))
(define (t) (ntp-time))
(define (build-nz lz sz tk)
(make-nz lz '(40) 0 sz (ntp-time-add (t) 5) tk 1.0))
(define (nz-pop! nz)
(let ((tmp (car (nz-vals nz))))
(when (not (eqv? (length (nz-vals nz)) 1))
(set-nz-vals! nz (cdr (nz-vals nz))))
tmp))
(define (nz-push! nz item)
(when (< (length (nz-vals nz)) max-vals)
(set-nz-vals! nz (cons item (nz-vals nz)))))
(define (nz-dup! nz)
(nz-push! nz (car (nz-vals nz))))
(define (ntp>? a b)
(or (> (car a) (car b))
(and (eqv? (car a) (car b))
(> (cadr a) (cadr b)))))
(define (nz-tick nz)
(when (ntp>? (ntp-time-add (t) (nz-off nz)) (nz-cur-t nz))
(let ((t (lz-tick (nz-lz nz)))
(v (car (nz-vals nz))))
(when (or (char=? t #\a) (char=? t #\b) (char=? t #\c) (char=? t #\b) (char=? t #\ ))
(set-nz-cur-t! nz (ntp-time-add (nz-cur-t nz) (nz-tk nz))))
(cond
((char=? t #\+) (set-nz-vals! nz (cons (+ (car (nz-vals nz)) 1) (cdr (nz-vals nz)))))
((char=? t #\-) (set-nz-vals! nz (cons (- (car (nz-vals nz)) 1) (cdr (nz-vals nz)))))
((char=? t #\<) (set-nz-vx! nz (modulo (- (nz-vx nz) 1) (length (nz-sz nz)))))
((char=? t #\>) (set-nz-vx! nz (modulo (+ (nz-vx nz) 1) (length (nz-sz nz)))))
((char=? t #\a) (play (nz-cur-t nz) ((list-ref (list-ref (nz-sz nz) (nz-vx nz)) 0) v) 0))
((char=? t #\b) (play (nz-cur-t nz) ((list-ref (list-ref (nz-sz nz) (nz-vx nz)) 1) v) 0))
((char=? t #\c) (play (nz-cur-t nz) ((list-ref (list-ref (nz-sz nz) (nz-vx nz)) 2) v) 0))
((char=? t #\d) (play (nz-cur-t nz) ((list-ref (list-ref (nz-sz nz) (nz-vx nz)) 3) v) 0))
((char=? t #\[) (nz-dup! nz))
((char=? t #\]) (nz-pop! nz)))
)))
; --
(define l (build-lz 9 8 3))
;(lz-prog l 0 "cCBca-aa")
;(lz-prog l 1 "c-d-c<.d")
;(lz-prog l 2 "b++b+ACd")
;(lz-prog l 0 "aB")
;(lz-prog l 1 "-d>-AC-A")
;(lz-prog l 2 "b+b--bAB")
(lz-prog l 0 " ")
(lz-prog l 1 " ")
(lz-prog l 2 " ")
(define ss
(list
(list
(lambda (v) (mul (adsr 0 0.01 0.1 1) (sine (add (mul 20 (sine 4)) (note v)))))
(lambda (v) (mul (adsr 0 0.1 0 0) (mul 0.2 (add (saw (* 1.5 (note v)))
(saw (note v))))))
(lambda (v) (mul (adsr 0 0.1 0 0)
(moogbp (squ (add 10 (mul 1000 (pow (adsr 0 0.2 0 0) 10))))
(* v 0.1) 0.1)))
(lambda (v) (mul (adsr 0 0.02 0 0) (moogbp (white 4) (* v 0.01) 0.45))))
(list
(lambda (v) (mul (adsr 0 0.03 0.1 1) (mooghp (saw (* (note v) 0.5))
(mul 0.2 (adsr 0.5 0 0 0)) 0.45)))
(lambda (v) (mul (adsr 0 0.1 0.1 1) (mooglp (add (saw (* 1.5 (note v))) (saw (note v)))
(* v 0.12) 0.4)))
(lambda (v) (mul (adsr 0 0.1 0 0) (sine (add
(fmod (* v 50) 300)
(mul 1000 (pow (adsr 0 0.2 0 0) 10))))))
(lambda (v) (mul (adsr 0.04 0.02 0 0) (mooglp (white 4) (* v 0.01) 0.45))))
(list
(lambda (v) (mul (adsr 0.5 0.03 0.1 1) (crush (sine (* (note v) 0.5))
0.1 0.3)))
(lambda (v) (mul (adsr 0 0.03 0.1 1) (mooglp (white (* 0.125 (note v)))
(fmod (* v 0.04) 1) 0.4)))
(lambda (v) (mul (adsr 0 0.1 0.1 0.5)
(add
(saw (add (/ (note v) 4) (mul 1000 (pow (adsr 0.3 0.1 0 0) 3))))
(saw (add (+ 1 (/ (note v) 4)) (mul 1000 (pow (adsr 0.1 0.1 0 0) 3)))))))
(lambda (v) (mul (adsr 0 0.02 0 0) (mooglp (white 4) (* v 0.01) 0.45))))
(list
(lambda (v) (moogbp (mul (adsr 0 0.2 0.1 0.1) (pink 100)) (adsr 0 0.01 0.1 1) 0.3))
(lambda (v) (mul (adsr 0 0.01 0 1) (white 20)))
(lambda (v) (mul (adsr 0 0.1 0.1 1) (pink 50)))
(lambda (v) (mul (adsr 0 0.1 0.1 1) (sine (mul (adsr 0 0.1 0 0) 150)))))
(list
(lambda (v) (mul (adsr 0 0.01 0.1 1) (white (+ 440 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (white (+ 240 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (white (+ 140 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (white (mul (adsr 0 0.1 0 0) (+ 40 (* v 50)))))))
(list
(lambda (v) (mul (adsr 0 0.01 0.1 1) (saw (+ 440 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (saw (+ 240 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (saw (+ 140 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (saw (mul (adsr 0 0.1 0 0) (+ 40 (* v 50)))))))
(list
(lambda (v) (mul (adsr 0 0.01 0.1 1) (pink (+ 440 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (pink (+ 240 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (pink (+ 140 (* v 20)))))
(lambda (v) (mul (adsr 0 0.01 0.1 1) (pink (mul (adsr 0 0.1 0 0) (+ 40 (* v 50)))))))
)
)
;(define z (build-nz (vector 9 5 '((4 2) (4 1) (6 0) (3 2) (4 1) (6 0)) 8 3 (list->vector (string->list "BaaadBdcd--C+++ --Aba+dd"))) ss 0.2))
(define z (build-nz l ss 0.2))
(every-frame (nz-tick z))
# ======================================================================
# file: OSC.py
# author: stefan kersten <steve@k-hornz.de>
# contents: OSC client module for python
# license: public domain
# ======================================================================
# $Id: osc.py,v 1.1 2006/01/25 10:42:53 nebogeo Exp $
# ======================================================================
# copyright (c) 2000 stefan kersten
# ======================================================================
# this module provides simple OSC client functionality
# usage examples down at the end of the file
# ======================================================================
__revision__ = "$Revision: 1.1 $"
# ======================================================================
# imports
import cStringIO, exceptions, math, socket, struct, time, types
# ======================================================================
# constants
SECONDS_UTC_TO_UNIX_EPOCH = 2208988800.0
FLOAT_TO_INT_SCALE = pow(2.0, 32.0)
# ======================================================================
# types
class Value:
"""Abstract OSC value."""
def __init__(self, value):
self.value = value
def binary_value(self):
pass
def type_tag(self):
pass
class Int(Value):
"""32 bit integer value."""
def __init__(self, value):
Value.__init__(self, long(value))
def binary_value(self):
return struct.pack('!l', self.value)
def type_tag(self):
return 'i'
class Float(Value):
"""32 bit floating point value."""
def __init__(self, value):
Value.__init__(self, float(value))
def binary_value(self):
return struct.pack('!f', self.value)
def type_tag(self):
return 'f'
class String(Value):
"""Null-terminated string padded to multiples of 4 byte."""
def __init__(self, value):
Value.__init__(self, str(value))
def binary_value(self):
v = self.value
l = len(v)
return struct.pack('%ds%dx' % (l, self.pad_amount(l)), v)
def type_tag(self):
return 's'
def pad_amount(self, len):
return 4 - (len % 4)
class Time(Value):
"""64 bit timetag in NTP format."""
def __init__(self, value):
Value.__init__(self, float(value))
def __add__(self, time):
return Time(float(self.value + time.value))
def binary_value(self):
t = self.value
# FIXME: how to convert without overflows?
s = long(t)
f = long(math.fmod(t, 1.0)*FLOAT_TO_INT_SCALE)
return struct.pack('!LL', s, f)
# ======================================================================
# utilities
time_module = time
def time():
"""Return current time as float in OSC format."""
return SECONDS_UTC_TO_UNIX_EPOCH + time_module.time()
# ======================================================================
# classes
class Packet:
"""Abstract base class for all OSC-related containers.
Has methods for retrieving the proper binary representation
and its size.
"""
def __init__(self, packets):
stream = cStringIO.StringIO()
self._write_contents(packets, stream)
self._data = stream.getvalue()
def get_packet(self):
"""Return the binary representation of the receiver's contents.
This data is in the proper OSC format and can be sent over a
socket.
"""
return self._data
def get_size(self):
"""Return the size of the receiver's binary data."""
return len(self._data)
def _write_contents(self, packets, stream):
"""Write packets on stream.
Private.
Override in subclasses for specific behavior.
"""
pass
def __repr__(self):
return '<' + \
str(self.__class__.__name__) + \
' instance, size=' + \
str(self.get_size()) + \
'>'
def sendto(self, host, port):
"""Send the receiver's data through a UDP socket."""
s = socket.socket(socket.SOCK_DGRAM, socket.AF_INET)
packet = self.get_packet()
s.sendto(packet, (host, port))
s.close()
def sendlocal(self, port):
"""Send the receiver's data through a UDP socket locally."""
self.sendto('localhost', port)
def _value(x):
"""Convert x(int, float or string) to an OSC object."""
t = type(x)
if t == types.FloatType:
return Float(x)
if t == types.IntType or t == types.LongType:
return Int(x)
# return string representation as default
return String(str(x))
class Message(Packet):
"""Single OSC message with arguments.
Message(address, *args) -> Message
address -- OSC address string
*args -- message argument list
"""
def __init__(self, address, args=[]):
Packet.__init__(self, [String(address)] + map(lambda x: _value(x), args))
def _write_contents(self, args, stream):
t_stream = cStringIO.StringIO() # tag stream
v_stream = cStringIO.StringIO() # value stream
# open signature string
t_stream.write(',')
# collect tags and arguments
for v in args[1:]:
t_stream.write(v.type_tag())
v_stream.write(v.binary_value())
# write address
stream.write(args[0].binary_value())
# write signature
stream.write(String(t_stream.getvalue()).binary_value())
# write arguments
stream.write(v_stream.getvalue())
class Bundle(Packet):
"""OSC container type with timing information.
Bundle(time, packets) -> Bundle
time -- floating point timetag in OSC units
packets -- array of Packet(s)
"""
def __init__(self, time, packets):
Packet.__init__(self, [Time(time)] + packets)
def _write_contents(self, args, stream):
# write '#bundle' preamble
stream.write(String('#bundle').binary_value())
# write timetag
stream.write(args[0].binary_value())
# write packets, prefixed with a byte count
for packet in args[1:]:
data = packet.get_packet()
size = len(data)
stream.write(Int(size).binary_value())
stream.write(data)
def test(port):
"""Some example messages and bundles, sent to port."""
Message("/filter/cutoff", [145.1232]).sendlocal(port)
Message("/http", ["www dot k-hornz dot de", 12, 3.41, "bulb"]).sendlocal(port)
# print Int(len(Message("/msg").get_packet())).binary_value()
Bundle(0.1, [Message("/fubar")]).sendlocal(port)
Bundle(time(), [Message("/msg", [1.0, "+", 1, 61, "0"]), Message("/bang!")]).sendlocal(port)
def test2():
"""Some example messages and bundles, sent to port."""
Message("/noisepattern/start", ["hello"]).sendlocal(9898989)
Message("/noisepattern/modify", [1, "hello", "two"]).sendlocal(9898989)
print("/noisepattern/start")
if __name__ == "__main__":
"""Run dump on port 10000."""
#test(10000)
test2()
# EOF
# ======================================================================
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment