Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
137 lines (117 sloc) 4.91 KB
#! /usr/bin/python
# Generator for compressed Arduino audio data
# Thomas Grill, 2011
# Modified by TIm Barrass 2013
# - changed PROGMEM to CONSTTABLE_STORAGE, to stop compiler warning
# - moved huffman table to progmem
# - added --name argument to give all constants specific names
# - changed all constant names to upper case
# - added include guards, Arduino and avr includes
# Dependencies:
# Numerical Python (numpy):
# scikits.audiolab:
# purehuff:
# pylab / matplotlib (only for plotting):
# For help on options invoke with:
# audio2huff --help
import sys,os.path
from itertools import imap,chain,izip
import numpy as N
except ImportError:
print >>sys.stderr, "Error: Numerical Python not found"
from scikits.audiolab import Sndfile
except ImportError:
print >>sys.stderr, "Error: scikits.audiolab not found"
import purehuff
except ImportError:
print >>sys.stderr, "Error: purehuff module not found"
def grouper(n,seq):
"""group list elements"""
it = iter(seq)
while True:
l = [v for _,v in izip(xrange(n),it)]
if l:
yield l
if len(l) < n:
def arrayformatter(seq,perline=40):
"""format list output linewise"""
return ",\n".join(",".join(imap(str,s)) for s in grouper(perline,seq))
if __name__ == "__main__":
from optparse import OptionParser
parser = OptionParser()
parser.add_option("--bits", type="int", default=8, dest="bits",help="bit resolution")
parser.add_option("--sndfile", dest="sndfile",help="input sound file")
parser.add_option("--hdrfile", dest="hdrfile",help="output C header file")
parser.add_option("--name", dest="name",help="prefix for tables and constants in file")
parser.add_option("--plothist", type="int", default=0, dest="plothist",help="plot histogram")
(options, args) = parser.parse_args()
if not options.sndfile:
print >>sys.stderr,"Error: --sndfile argument required"
sndf = Sndfile(options.sndfile,'r')
sound = sndf.read_frames(sndf.nframes)
fs = sndf.samplerate
del sndf
# mix down multi-channel audio
if len(sound.shape) > 1:
sound = N.mean(sound,axis=1)
# convert to n bits (no dithering, except it has already been done with the same bit resolution for the soundfile)
sound8 = N.clip((sound*(2**(options.bits-1))).astype(int),-2**(options.bits-1),2**(options.bits-1)-1)
# the following mapping with int is necessary as numpy.int32 types are not digested well by the HuffmanTree class
dsound8 = map(int,chain((sound8[0],),imap(lambda x: x[1]-x[0],izip(sound8[:-1],sound8[1:]))))
print >>sys.stderr,"min/max: %i/%i"%(N.min(sound8),N.max(sound8))
print >>sys.stderr,"data bits: %i"%(len(sound8)*options.bits)
hist = purehuff.histogram(dsound8)
if options.plothist:
import pylab as P
except ImportError:
print >>sys.stderr, "Plotting needs pylab"
from collections import defaultdict
d = defaultdict(float)
for n,v in hist:
d[v] += n
x = range(min(d.iterkeys()),max(d.iterkeys())+1)
y = [d[xi] for xi in x]
P.title("Histogram of sample differentials, file %s"%os.path.split(options.sndfile)[-1])
hufftree = purehuff.HuffTree(hist)
# get decoder instance
decoder = hufftree.decoder()
# get encoder instance
encoder = hufftree.encoder()
# encode data
enc = encoder(dsound8)
print >>sys.stderr,"encoded bits: %i"%len(enc)
print >>sys.stderr,"ratio: %.0f%%"%((len(enc)*100.)/(len(sound8)*8))
print >>sys.stderr,"decoder length: %.0f words"%(len(decoder.huff))
if options.hdrfile:
hdrf = file(options.hdrfile,'wt')
print >>hdrf,"// generated by Mozzi/extras/python/ \n"
print >>hdrf,"#ifndef " + + "_H_"
print >>hdrf,"#define " + + "_H_\n"
print >>hdrf,'#if ARDUINO >= 100'
print >>hdrf,'#include "Arduino.h"'
print >>hdrf,'#else'
print >>hdrf,'#include "WProgram.h"'
print >>hdrf,'#endif \n'
print >>hdrf,'#include "mozzi_pgmspace.h"\n \n'
print >>hdrf,"#define " + + "_SAMPLERATE %i"%fs
print >>hdrf,"#define " + + "_SAMPLE_BITS %i"%options.bits
print >>hdrf,'CONSTTABLE_STORAGE(int) ' + + '_HUFFMAN[%i] = {\n%s\n};'%(len(decoder.huff),arrayformatter(decoder.huff))
print >>hdrf,'unsigned long const ' + + '_SOUNDDATA_BITS = %iL;'%len(enc)
print >>hdrf,'CONSTTABLE_STORAGE(unsigned char) ' + + '_SOUNDDATA[] = {\n%s\n};'%arrayformatter(
print >>hdrf,"#endif /* " + + "_H_ */"
You can’t perform that action at this time.