| Mozzi version v1.1.0 sound synthesis library for Arduino |

audio2huff.py
1 #! /usr/bin/python
2 #
3 # Generator for compressed Arduino audio data
4 # Thomas Grill, 2011
5 # http://grrrr.org
6 #
7 # Modified by TIm Barrass 2013
8 # - changed PROGMEM to CONSTTABLE_STORAGE, to stop compiler warning
9 # - moved huffman table to progmem
10 # - added --name argument to give all constants specific names
11 # - changed all constant names to upper case
12 # - added include guards, Arduino and avr includes
13 #
14 # Dependencies:
15 # Numerical Python (numpy): http://numpy.scipy.org/
16 # scikits.audiolab: http://pypi.python.org/pypi/scikits.audiolab/
17 # purehuff: https://grrrr.org/data/dev/purehuff/
18 # pylab / matplotlib (only for plotting): http://matplotlib.sourceforge.net/
19 #
20 # For help on options invoke with:
21 # audio2huff --help
22
23 import sys,os.path
24 from itertools import imap,chain,izip
25
26 try:
27 import numpy as N
28 except ImportError:
29 print >>sys.stderr, "Error: Numerical Python not found"
30 exit(-1)
31
32 try:
33 from scikits.audiolab import Sndfile
34 except ImportError:
35 print >>sys.stderr, "Error: scikits.audiolab not found"
36 exit(-1)
37
38 try:
39 import purehuff
40 except ImportError:
41 print >>sys.stderr, "Error: purehuff module not found"
42 exit(-1)
43
44 def grouper(n,seq):
45 """group list elements"""
46 it = iter(seq)
47 while True:
48 l = [v for _,v in izip(xrange(n),it)]
49 if l:
50 yield l
51 if len(l) < n:
52 break
53
54 def arrayformatter(seq,perline=40):
55 """format list output linewise"""
56 return ",\n".join(",".join(imap(str,s)) for s in grouper(perline,seq))
57
58 if __name__ == "__main__":
59 from optparse import OptionParser
60 parser = OptionParser()
61 parser.add_option("--bits", type="int", default=8, dest="bits",help="bit resolution")
62 parser.add_option("--sndfile", dest="sndfile",help="input sound file")
63 parser.add_option("--hdrfile", dest="hdrfile",help="output C header file")
64 parser.add_option("--name", dest="name",help="prefix for tables and constants in file")
65 parser.add_option("--plothist", type="int", default=0, dest="plothist",help="plot histogram")
66 (options, args) = parser.parse_args()
67
68 if not options.sndfile:
69 print >>sys.stderr,"Error: --sndfile argument required"
70 exit(-1)
71
72 sndf = Sndfile(options.sndfile,'r')
73 sound = sndf.read_frames(sndf.nframes)
74 fs = sndf.samplerate
75 del sndf
76
77 # mix down multi-channel audio
78 if len(sound.shape) > 1:
79 sound = N.mean(sound,axis=1)
80
81 # convert to n bits (no dithering, except it has already been done with the same bit resolution for the soundfile)
82 sound8 = N.clip((sound*(2**(options.bits-1))).astype(int),-2**(options.bits-1),2**(options.bits-1)-1)
83 # the following mapping with int is necessary as numpy.int32 types are not digested well by the HuffmanTree class
84 dsound8 = map(int,chain((sound8[0],),imap(lambda x: x[1]-x[0],izip(sound8[:-1],sound8[1:]))))
85
86 print >>sys.stderr,"min/max: %i/%i"%(N.min(sound8),N.max(sound8))
87 print >>sys.stderr,"data bits: %i"%(len(sound8)*options.bits)
88
89 hist = purehuff.histogram(dsound8)
90
91 if options.plothist:
92 try:
93 import pylab as P
94 except ImportError:
95 print >>sys.stderr, "Plotting needs pylab"
96
97 from collections import defaultdict
98 d = defaultdict(float)
99 for n,v in hist:
100 d[v] += n
101 x = range(min(d.iterkeys()),max(d.iterkeys())+1)
102 y = [d[xi] for xi in x]
103
104 P.title("Histogram of sample differentials, file %s"%os.path.split(options.sndfile)[-1])
105 P.plot(x,y,marker='x')
106 P.show()
107
108 hufftree = purehuff.HuffTree(hist)
109
110 # get decoder instance
111 decoder = hufftree.decoder()
112 # get encoder instance
113 encoder = hufftree.encoder()
114 # encode data
115 enc = encoder(dsound8)
116
117 print >>sys.stderr,"encoded bits: %i"%len(enc)
118 print >>sys.stderr,"ratio: %.0f%%"%((len(enc)*100.)/(len(sound8)*8))
119 print >>sys.stderr,"decoder length: %.0f words"%(len(decoder.huff))
120
121 if options.hdrfile:
122 hdrf = file(options.hdrfile,'wt')
123 print >>hdrf,"// generated by Mozzi/extras/python/audio2huff.py \n"
124 print >>hdrf,"#ifndef " + options.name + "_H_"
125 print >>hdrf,"#define " + options.name + "_H_\n"
126 print >>hdrf,'#if ARDUINO >= 100'
127 print >>hdrf,'#include "Arduino.h"'
128 print >>hdrf,'#else'
129 print >>hdrf,'#include "WProgram.h"'
130 print >>hdrf,'#endif \n'
131 print >>hdrf,'#include "mozzi_pgmspace.h"\n \n'
132 print >>hdrf,"#define " + options.name + "_SAMPLERATE %i"%fs
133 print >>hdrf,"#define " + options.name + "_SAMPLE_BITS %i"%options.bits
134 print >>hdrf,'CONSTTABLE_STORAGE(int) ' + options.name + '_HUFFMAN[%i] = {\n%s\n};'%(len(decoder.huff),arrayformatter(decoder.huff))
135 print >>hdrf,'unsigned long const ' + options.name + '_SOUNDDATA_BITS = %iL;'%len(enc)
136 print >>hdrf,'CONSTTABLE_STORAGE(unsigned char) ' + options.name + '_SOUNDDATA[] = {\n%s\n};'%arrayformatter(enc.data)
137 print >>hdrf,"#endif /* " + options.name + "_H_ */"