-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_player.py
72 lines (54 loc) · 2.31 KB
/
twitter_player.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
#!/usr/bin/env python
### Below is literally a copy paste from pianoputer: https://github.com/Zulko/pianoputer
import numpy as np
import time
from os import getcwd
def speedx(snd_array, factor):
""" Speeds up / slows down a sound, by some factor. """
indices = np.round( np.arange(0, len(snd_array), factor) )
indices = indices[indices < len(snd_array)].astype(int)
return snd_array[ indices ]
def stretch(snd_array, factor, window_size, h):
""" Stretches/shortens a sound, by some factor. """
phase = np.zeros(window_size)
hanning_window = np.hanning(window_size)
result = np.zeros( len(snd_array) /factor + window_size)
for i in np.arange(0, len(snd_array)-(window_size+h), h*factor):
# two potentially overlapping subarrays
a1 = snd_array[i: i + window_size]
a2 = snd_array[i + h: i + window_size + h]
# the spectra of these arrays
s1 = np.fft.fft(hanning_window * a1)
s2 = np.fft.fft(hanning_window * a2)
# rephase all frequencies
phase = (phase + np.angle(s2/s1)) % 2*np.pi
a2_rephased = np.fft.ifft(np.abs(s2)*np.exp(1j*phase))
i2 = int(i/factor)
result[i2 : i2 + window_size] += hanning_window*a2_rephased
result = ((2**(16-4)) * result/result.max()) # normalize (16bit)
return result.astype('int16')
def pitchshift(snd_array, n, window_size=2**13, h=2**11):
""" Changes the pitch of a sound by ``n`` semitones. """
factor = 2**(1.0 * n / 12.0)
stretched = stretch(snd_array, 1.0/factor, window_size, h)
return speedx(stretched[window_size:], factor)
from scipy.io import wavfile
fps, bowl_sound = wavfile.read("pianoputer/bowl.wav")
tones = range(-25,25)
transposed_sounds = [pitchshift(bowl_sound, n) for n in tones]
import pygame
import random
pygame.mixer.init(fps, -16, 1, 100) # so flexible ;)
screen = pygame.display.set_mode((150,150)) # for the focus
keys = open('pianoputer/typewriter.kb').read().split('\n')
sounds = map(pygame.sndarray.make_sound, transposed_sounds)
key_sound = dict( zip(keys, sounds) )
#print key_sound
is_playing = {k: False for k in keys}
with open('saved.txt') as fil:
saved = fil.read()
for s in saved:
key_sound[s].play(fade_ms=1500)
key_sound[s].fadeout(900)
time.sleep(random.uniform(0.005, 0.4))
raise SystemExit