-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrobars.py
77 lines (62 loc) · 1.96 KB
/
spectrobars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from math import log
import matplotlib.mlab as mlab
import numpy
from tangible import ast, scales
from tangible.backends.openscad import OpenScadBackend
from tangible.shapes.bars import BarsND
import wave
# Relative path to the WAV file
inPath = ""
# Relative path to the SCAD output file
outPath = "bars.scad"
# Play with this to move between linear and logarithmic scaling of amplitudes
loggishness = 0.00000000000004
# These parameters determine the "resolution" of the spectrogram
nfft = 2**18
padto = nfft/(2**13)
def main():
print "Reading and analyzing file..."
spectrum, freqs = ReadAndAnalyze(inPath)
print len(freqs)
print len(spectrum[0])
print "Scaling logarithmically (kinda)..."
for i, s in enumerate(spectrum):
spectrum[i] = map(loggish, s)
print "Generating linear scale..."
scale = scales.linear(domain=[spectrum.min(), spectrum.max()],
codomain=[1, 10])
print "Normalizing spectrum data..."
datapoints = map(lambda x: map(scale, x), spectrum)
print "Trimming spectrum data post-normalization..."
for i, x in enumerate(datapoints):
for j, v in enumerate(x):
if v > 9:
datapoints[i][j] = 9
print "Generating bars..."
bars = BarsND(datapoints,
bar_width=1,
bar_depth=1)
print "Rendering..."
code = bars.render(backend=OpenScadBackend)
print "Saving to file..."
with open(outPath, "w") as f:
f.write(code)
def loggish(v):
v = float(v)
L = loggishness
return L * (log(v) - v) + v
def ReadAndAnalyze(f):
wav = wave.open(f, 'r')
frames = wav.readframes(-1)
sound_info = numpy.fromstring(frames, 'Int16')
frame_rate = wav.getframerate()
wav.close()
specdata = mlab.specgram(sound_info,
NFFT=nfft,
pad_to=padto,
Fs=frame_rate)
spectrum = specdata[0]
freqs = specdata[1]
return spectrum, freqs
if __name__ == "__main__":
main()