John O'Keefe

Week 11 - Input Devices

Changing the Python Program

#
# hello.mic.45.py
#
# plot microphone audio
#    hello.mic.45.py serial_port
#
# Neil Gershenfeld
# CBA MIT 10/31/10
#
# (c) Massachusetts Institute of Technology 2010
# Permission granted for experimental and personal use;
# license for commercial sale available from MIT
#

from Tkinter import *
import serial

NX = 2000
NY = 2000
nloop =100
path = []
baseline = 0
baseline_filt = 0.01
gain = 3

def idle(parent,canvas):
   global path, baseline
   #
   # idle routine
   #
   # look for framing
   #
   byte2 = 0
   byte3 = 0
   byte4 = 0
   while 1:
      byte1 = byte2
      byte2 = byte3
      byte3 = byte4
      byte4 = ord(ser.read())
      if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)):
         break
   path = []
   for i in range(nloop):
      lo = ord(ser.read())
      hi = ord(ser.read())
      reading = 256*hi + lo
      baseline = baseline_filt*reading + (1-baseline_filt)*baseline
      value = NY/2 + gain*(reading - baseline)
      path.append(i*NY/float(nloop))
      path.append(value)
   canvas.delete("path")
   canvas.create_line(path,tag="path",width=250,fill="#4C7D7E")
   parent.after_idle(idle,parent,canvas)

#
#  check command line arguments
#
if (len(sys.argv) != 2):
   print "command line: hello.mic.45.py serial_port"
   sys.exit()
port = sys.argv[1]
#
# open serial port
#
ser = serial.Serial(port,9600)
#
# start plotting
#
root = Tk()
root.title('hello.mic.45.py')
root.bind('q','exit')
canvas = Canvas(root, width=NX, height=NY, background='white')
canvas.pack()
root.after(100,idle,root,canvas)
root.mainloop()
Changed this line of code - canvas.create_line(path,tag="path",width=250,fill="#4C7D7E") parent.after_idle(idle,parent,canvas) - to affect line width and color - much fatter/rounder and teal. Code that effects appearance of the display is found in the last few lines (screen/display size is adjusted near the top).



Terminal Screen - Getting to the .py File Found serial port (7 digit alphanumeric at end of command) by typing 'ls /dev/tty.usb*' into the terminal. On this computer, I get back 'FTF53NJC'.


hello.mic Board Taped hello.mic board, plugged into serial port of the computer via FTDI/USB cable near the computer speaker.


Jungle Roots Dub There is a weird delay (obviously) where the music is being played and fed back into the computer through the serial - Reggae mellows out this discrepancy.