Skip to content

Instantly share code, notes, and snippets.

@ptone
Created November 26, 2010 18:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptone/717060 to your computer and use it in GitHub Desktop.
Save ptone/717060 to your computer and use it in GitHub Desktop.
Basic Midi input with pyportmidi
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Preston Holmes on 2010-11-25.
preston@ptone.com
Copyright (c) 2010
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
import os
import pyportmidi as pypm
import freenect
import numpy as np
import time
from threading import Semaphore
mean = np.mean
lock = Semaphore()
def downsample(myarr,factor,estimator=mean):
"""
based on code from http://code.google.com/p/agpy/source/browse/trunk/agpy/downsample.py
Downsample a 2D array by averaging over *factor* pixels in each axis.
Crops upper edge if the shape is not a multiple of factor.
This code is pure numpy and should be fast.
keywords:
estimator - default to mean. You can downsample by summing or
something else if you want a different estimator
(e.g., downsampling error: you want to sum & divide by sqrt(n))
"""
ys,xs = myarr.shape
crarr = myarr[:ys-(ys % int(factor)),:xs-(xs % int(factor))]
dsarr = estimator( np.dstack([crarr[:,j::factor]
for j in range(factor)]), axis=2)
return dsarr
notes_on = np.array([])
def main():
pypm.init()
num_notes = 16
start_note = 41
# midi_notes = range(start_note, start_note + 16)
# blues scale
# midi_notes = [48,51,53,54,55,58,60]
# basic
midi_notes = [41,43,45,47,48,50,52,53,55,57,59,60]
midi_notes.reverse()
o = pypm.Output(2)
for n in midi_notes:
for i in range(10):
o.note_off(n)
def depth_callback(dev, data, timestamp):
"""libfreenect will call this func once per frame"""
global lock
global notes_on
lock.acquire()
dar = downsample(data, int(640 / len(midi_notes))) # get this down to n columns
chans = np.swapaxes(dar, 0, 1)
chans_filter_scatter = np.where(chans > 2000, 0, chans)
triggered = np.where(chans_filter_scatter > 1500, 1, 0)
notes = np.sum(triggered,axis=1)
strong_notes = np.where(notes > 40, notes, 0)
on_notes = np.flatnonzero(strong_notes)
new_notes = np.setdiff1d(on_notes, notes_on)
if len(new_notes):
print new_notes
off_notes = np.setdiff1d(notes_on, on_notes)
notes_on = np.copy(on_notes)
for n in new_notes:
o.note_on(midi_notes[n],velocity=127)
for n in off_notes:
o.note_off(midi_notes[n])
lock.release()
# freenect.runloop(depth=depth_callback)
freenect.runloop(lambda *x: depth_callback(*freenect.depth_cb_np(*x)))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment