Skip to main content

Posts

My new musical instrument: Python

It's still very young, but here is the library I've been working on for generating music in (nothing but) Python . "test.py" is an example of what you can do. The real purpose isn't to code your music out this way, it's intended as a backend for generating / manipulating music. My end goal is a tab-based editor that plays back with this, but I'm going to keep working on this backend and hope some brave snake wrestler comes along and has the itch to write a proper interface (and if that doesn't happen, I'll start on the interface when I feel satisfied with the backend). The Timeline and Hit classes are just ideas right now. I'm not sure they'll stay in the form they are (I'm thinking a guitar-oriented timeline, holding fret/string/amplitude per hit, is more along the lines of what I need). I'm also on the hunt for efficient numpy/scipy implemented effects and sound sources. I've been playing around some with generating Karplus-S...

Procedural music with PyAudio and NumPy

Combining two of my favorite pastimes, programming and music... This is the hacky "reduced to it's basic components" version of a library I've been working on for generating music and dealing with music theory. Tweaking the harmonics by changing the shape of the harmonic components and ratios can produce some interesting sounds. This one only uses sine waveforms, but a square / saw generator is trivial with numpy. It takes a second to generate, so don't turn your volume up too loud in anticipation (it may be loud). import math import numpy import pyaudio import itertools from scipy import interpolate from operator import itemgetter class Note: NOTES = ['c','c#','d','d#','e','f','f#','g','g#','a','a#','b'] def __init__(self, note, octave=4): self.octave = octave if isinstance(note, int): self.index = note self.note = Note.NOTES[note] elif isin...

Javascript insort function

I recently had the need for some sorted insertion in Javascript arrays, thought I'd share this tiny bit of code: Array.prototype.bisect = function(x, lo, hi) { var mid; if(typeof(lo) == 'undefined') lo = 0; if(typeof(hi) == 'undefined') hi = this.length; while(lo mid = Math.floor((lo + hi) / 2); if(x else lo = mid + 1; } return lo } Array.prototype.insort = function(x) { this.splice(this.bisect(x), 0, x); } Of course, these methods only work on an already sorted (or empty) array, so if you're using them on an array that came from elsewhere, sort it first. But once sorted, these insertions will maintain the sortedness, no need to 'push' and 'sort' every time you need to insert something into a sorted array. (lo and hi parameters are optionally possible for the bisection method to allow restricted searches)

The names behind the languages

If we're going to continue to spill blood in these language wars, it's important that we all know who our generals are (and who our enemy generals are). Feel free to offer corrections or extensions to the list. C - Dennis Ritchie C++ -  Bjarne Stroustrup Forth -  Charles H. Moore Lisp - John McCarthy Perl - Larry Wall   PHP -  Rasmus Lerdorf Python -  Guido van Rossum Ruby -  Yukihiro Matsumoto Tcl -  John K. Ousterhout

Venn Diagrams with google charts

I recently had the need to view some data sets as venn diagrams , so I found google's chart api and hacked out a little AJAX application to do so, you can try it out here . And, for the fun of it, a VERY simple Python implementation: import urllib from PIL import Image import StringIO def venn_diagram_url(a, b, c, width=400, height=200): values = [width, height, len(a), len(b), len(c)] values.append(len(a & b)) values.append(len(a & c)) values.append(len(c & b)) values.append(len(a & b & c)) base_str = 'http://chart.apis.google.com/chart?' args_str = 'cht=v&chs=%sx%s&chd=t:%s,%s,%s,%s,%s,%s,%s' return base_str + (args_str % tuple(values)) a = set((1, 2, 3)) b = set((2, 4)) c = set((3, 4, 5)) data = StringIO.StringIO(urllib.urlopen(venn_diagram_url(a, b, c)).read()) Image.open(data).show() Not much to it... But maybe someone will find it useful.

Trampolining for recursion...

In essence, CPS (continuation passing style) is a method of calling functions where instead of returning a value, you pass the results to another function (the continuation) provided as an argument. I wont go too deep into the reasoning behind this, as it's usually not used directly but rather as a middle representation of programming languages by some compiler/interpreter (read more on the wikipedia link). The big downside is that you're bombarding the stack with function frames for every call, going deeper and deeper, but never returning (you just keep passing what would be the return value to another function). One nifty method of avoiding this is to use a trampoline . A trampoline resembles a stream/lazy-list to anyone who's read SICP . Essentially, instead of calling functions from within another function, you return a bounce "call" to the trampoline which makes the call for you, doing the same with the return value of that call until it gets a land (a non-...

Python: know when to be lazy

Lazy is good. One Python trend I've noticed quite a bit is the severe under-usage of generators. I often see list comprehension being used in situations where building a temporary list is not only wasteful, but more work. Example: print sum([x * 2 for x in something]) In this case, LC is being used to build a temporary list whose only purpose is to be iterated by "sum". Instead of constructing this list, why not pass an iterable object to "sum" and have it accumulate the sum from that? Well, not only can you, but it's even less to type (a whopping two characters less): print sum(x * 2 for x in something) Simply excluding the square brackets takes it from a list-comp to a generator expression, which is lazily evaluated. To demonstrate, proof that generators don't actually compute anything until they are pulled: def f(x): print x a = [f("LC") for x in xrange(10)] b = (f("GE") for x in xrange(10)) print "outside" b.next() p...