Mozzi  version v2.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 # NOTE: the scikits.audiolab dependency requires Python 2!
21 # see https://github.com/Roger-random/mozzi_wilhelm/issues/1#issuecomment-770141226
22 #
23 #For help on options invoke with:
24 # audio2huff --help
25 
26 import sys,os.path
27 from itertools import imap,chain,izip
28 
29 try:
30  import numpy as N
31 except ImportError:
32  print >>sys.stderr, "Error: Numerical Python not found"
33  exit(-1)
34 
35 try:
36  from scikits.audiolab import Sndfile
37 except ImportError:
38  print >>sys.stderr, "Error: scikits.audiolab not found"
39  exit(-1)
40 
41 try:
42  import purehuff
43 except ImportError:
44  print >>sys.stderr, "Error: purehuff module not found"
45  exit(-1)
46 
47 def grouper(n,seq):
48  """group list elements"""
49  it = iter(seq)
50  while True:
51  l = [v for _,v in izip(xrange(n),it)]
52  if l:
53  yield l
54  if len(l) < n:
55  break
56 
57 def arrayformatter(seq,perline=40):
58  """format list output linewise"""
59  return ",\n".join(",".join(imap(str,s)) for s in grouper(perline,seq))
60 
61 if __name__ == "__main__":
62  from optparse import OptionParser
63  parser = OptionParser()
64  parser.add_option("--bits", type="int", default=8, dest="bits",help="bit resolution")
65  parser.add_option("--sndfile", dest="sndfile",help="input sound file")
66  parser.add_option("--hdrfile", dest="hdrfile",help="output C header file")
67  parser.add_option("--name", dest="name",help="prefix for tables and constants in file")
68  parser.add_option("--plothist", type="int", default=0, dest="plothist",help="plot histogram")
69  (options, args) = parser.parse_args()
70 
71  if not options.sndfile:
72  print >>sys.stderr,"Error: --sndfile argument required"
73  exit(-1)
74 
75  sndf = Sndfile(options.sndfile,'r')
76  sound = sndf.read_frames(sndf.nframes)
77  fs = sndf.samplerate
78  del sndf
79 
80  # mix down multi-channel audio
81  if len(sound.shape) > 1:
82  sound = N.mean(sound,axis=1)
83 
84  # convert to n bits (no dithering, except it has already been done with the same bit resolution for the soundfile)
85  sound8 = N.clip((sound*(2**(options.bits-1))).astype(int),-2**(options.bits-1),2**(options.bits-1)-1)
86  # the following mapping with int is necessary as numpy.int32 types are not digested well by the HuffmanTree class
87  dsound8 = map(int,chain((sound8[0],),imap(lambda x: x[1]-x[0],izip(sound8[:-1],sound8[1:]))))
88 
89  print >>sys.stderr,"min/max: %i/%i"%(N.min(sound8),N.max(sound8))
90  print >>sys.stderr,"data bits: %i"%(len(sound8)*options.bits)
91 
92  hist = purehuff.histogram(dsound8)
93 
94  if options.plothist:
95  try:
96  import pylab as P
97  except ImportError:
98  print >>sys.stderr, "Plotting needs pylab"
99 
100  from collections import defaultdict
101  d = defaultdict(float)
102  for n,v in hist:
103  d[v] += n
104  x = range(min(d.iterkeys()),max(d.iterkeys())+1)
105  y = [d[xi] for xi in x]
106 
107  P.title("Histogram of sample differentials, file %s"%os.path.split(options.sndfile)[-1])
108  P.plot(x,y,marker='x')
109  P.show()
110 
111  hufftree = purehuff.HuffTree(hist)
112 
113  # get decoder instance
114  decoder = hufftree.decoder()
115  # get encoder instance
116  encoder = hufftree.encoder()
117  # encode data
118  enc = encoder(dsound8)
119 
120  print >>sys.stderr,"encoded bits: %i"%len(enc)
121  print >>sys.stderr,"ratio: %.0f%%"%((len(enc)*100.)/(len(sound8)*8))
122  print >>sys.stderr,"decoder length: %.0f words"%(len(decoder.huff))
123 
124  if options.hdrfile:
125  hdrf = file(options.hdrfile,'wt')
126  print >>hdrf,"// generated by Mozzi/extras/python/audio2huff.py \n"
127  print >>hdrf,"#ifndef " + options.name + "_H_"
128  print >>hdrf,"#define " + options.name + "_H_\n"
129  print >>hdrf,'#if ARDUINO >= 100'
130  print >>hdrf,'#include <Arduino.h>\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_ */"