Skip to content

Instantly share code, notes, and snippets.

@agermanidis
Last active October 3, 2016 17:36
Show Gist options
  • Save agermanidis/ee72ec91710488781654bc96ba8f881a to your computer and use it in GitHub Desktop.
Save agermanidis/ee72ec91710488781654bc96ba8f881a to your computer and use it in GitHub Desktop.
import sys
import numpy as np
import colorlover as cl
import scipy.io.wavfile as wav
from scipy.interpolate import interp1d
from python_speech_features import fbank
from PIL import Image
N_FILTERS = 10
N_FRAMES = 60
WINSTEP = 1
HEIGHT = 100
WIDTH = 1024
MIN_LOG_VALUE = 5
MAX_LOG_VALUE = 17
rate, sig = wav.read(sys.argv[1])
data, freqs = fbank(sig, winstep=WINSTEP, nfilt=N_FILTERS)
data = np.log(data)
data = data[1:N_FRAMES+1]
img = Image.new("RGB", (WIDTH, HEIGHT), "black")
pixels = img.load()
mapper = interp1d([MIN_LOG_VALUE, MAX_LOG_VALUE], [49, 0])
cscale = cl.scales['11']['div']['RdYlBu']
cmap = cl.interp(cscale, 50)
cmap = cl.to_numeric(cl.to_rgb(cmap))
for i, item in enumerate(data):
for j, value in enumerate(item):
for w in range(int(i*WIDTH/N_FRAMES),
int((i+1)*WIDTH/N_FRAMES)):
for h in range(int(j*HEIGHT/N_FILTERS),
int((j+1)*HEIGHT/N_FILTERS)):
value = max(MIN_LOG_VALUE, min(MAX_LOG_VALUE, value))
color = tuple(map(int, cmap[int(mapper(value))]))
pixels[w, h] = color
filename = sys.argv[1].split('.')[0]+'.png'
img.save(filename)
print(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment