Skip to content

Instantly share code, notes, and snippets.

View hosackm's full-sized avatar

Matt H hosackm

View GitHub Profile
@hosackm
hosackm / simple_ffi.py
Last active August 29, 2015 14:23
CFFI Intro Blog snippet 1
from cffi import FFI
ffi = FFI()
ffi.cdef('''
typedef struct
{
int x;
int y;
} point''')
@hosackm
hosackm / load_dll_cffi.py
Last active August 29, 2015 14:23
Blog post to show loading dll in CFFI
from cffi import FFI
ffi = FFI()
ffi.cdef('...super cool header file contents...')
lib = ffi.dlopen('libsupercool.dylib')
#call a function from libsupercool
lib.MyCoolFunction(42)
@hosackm
hosackm / compile_ffi.py
Last active August 29, 2015 14:23
How to compile ffi object
from cffi import FFI
ffi = FFI()
with open('supercool.h', 'r') as f:
ffi.cdef(f.read())
ffi.set_source('supercool', None)
ffi.compile()
@hosackm
hosackm / pymidi_backend.py
Created June 25, 2015 06:23
pymidi cffi backend
import subprocess
from cffi import FFI
__all__ = ('ffi',)
p = subprocess.Popen(['clang -E include/portmidi.h'],
shell=True, stdout=subprocess.PIPE)
code = p.communicate()[0]
@hosackm
hosackm / pymidi.py
Created June 25, 2015 06:24
Python wrapper for Portmidi
import sys
from _pymidi import ffi # , lib
'''Only initialize Portmidi once'''
SINGLETON = None
'''Detect OS and use correct file extension for shared library'''
if sys.platform == 'darwin':
ext = 'dylib'
elif sys.platform.startswith('linux'):
ext = 'so'
#!/usr/bin/env bash
# Sexy bash prompt by twolfson
# https://github.com/twolfson/sexy-bash-prompt
# Forked from gf3, https://gist.github.com/gf3/306785
#
# Updated color scheme by Matt Hosack
# If we are on a colored terminal
if tput setaf 1 &> /dev/null; then
# Reset the shell from our `if` check
@hosackm
hosackm / flaskaudiostream.py
Created September 2, 2015 22:30
Flask streaming an audio file
from flask import Flask, Response
app = Flask(__name__)
@app.route("/wav")
def streamwav():
def generate():
with open("signals/song.wav", "rb") as fwav:
data = fwav.read(1024)
@hosackm
hosackm / pm_input.c
Created September 17, 2015 23:45
Portmidi input
#include <stdio.h>
#include "portmidi.h"
int main(int argc, const char * argv[]) {
PortMidiStream *stream;
PmEvent events[512];
int i, num;
Pm_Initialize();
@hosackm
hosackm / decorator_explanation.py
Created September 30, 2015 22:25
Python decorators explained in a couple functions
# Two examples to remember how decorators should be written.
# The first has no arguments in the decorator
# The second shows how to pass arguments like @dec(...)
#no args to decorator tag. ie:
def decorator_name(func_getting_decorated):
def func_being_returned(args_to_func_getting_decorated):
#decoration step here
print "decoration occured"
#make sure the returned decorator does whatever
@hosackm
hosackm / fir_stuff.py
Last active June 21, 2016 04:28
FIR Filter using SciPy signal
import numpy as np
import scipy.signal as sig
from scipy.io.wavfile import write as write
from matplotlib import pyplot as plt
SR = 48000
SECS = 10.0
NSAMPS = int(SECS * SR)
NTAPS = 61
INT_MAX = 2**15-1