-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFT.py
More file actions
119 lines (90 loc) · 3.31 KB
/
FFT.py
File metadata and controls
119 lines (90 loc) · 3.31 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import threading
import time
import wave
import numpy
import simpleaudio as sa
from scipy.fftpack import dct
import Animations
import Direction
import Game
import Util as util
import api
class AudioVis(Game.CubeGame, threading.Thread):
_name = 'Audio Visualizer'
_version = 'v0.1'
_menu_frame = [1, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1]
def __init__(self, cube_size, frame_size):
Game.CubeGame.__init__(self, cube_size, frame_size, self._name)
threading.Thread.__init__(self)
self.an = None
self.finished = False
self.file_name = "music.wav"
self.status = 'stopped'
self.N = self.cube_size # num of bars
self.HEIGHT = self.cube_size # height of a bar
self.WIDTH = self.cube_size # width of a bar
# process wave data
self.f = wave.open(self.file_name, 'rb')
self.params = self.f.getparams()
self.nchannels, self.sampwidth, self.framerate, self.nframes = self.params[:4]
self.str_data = self.f.readframes(self.nframes)
self.f.close()
self.wave_data = numpy.frombuffer(self.str_data, dtype=numpy.short)
self.wave_data.shape = -1, 2
self.wave_data = self.wave_data.T
self.num = self.nframes
self.amplifier = 12
self.fps = 24
self.frames = []
def get_menu_frame(self):
return self._menu_frame
def has_menu_animation(self):
return True
def start_game(self):
pass
def play_animation(self):
self.an = Animations.TickerAnimation("audiovis")
self.an.start()
def close_animation(self):
self.an.stop()
def stopped_animation(self):
return self.an.stopped()
def is_animation_alive(self):
return self.an.is_alive()
def done(self):
pass
def run(self):
musicfile = sa.WaveObject.from_wave_file(self.file_name)
musicfile.play()
Direction.direction_p_1.value = 0
while Direction.direction_p_1.value != 7:
num = int(self.num)
h = abs(dct(self.wave_data[0][self.nframes - num:self.nframes - num + self.N]))
h = [min(self.HEIGHT, int(i ** (1 / 2.5) * self.HEIGHT / 100)) * self.amplifier for i in h]
self.frames = [h] + self.frames
"""
i = 0
for f in self.frames:
if i < self.cube_size:
api.change_face(api.Face.FRONT, i, util.construct_2D_audio_frame(self.cube_size, f))
i += 1
else:
self.frames.remove(f)
"""
for x in range(self.cube_size):
api.change_face(api.Face.FRONT, self.cube_size - 1 - x,
util.construct_2D_audio_frame(self.cube_size, h, x))
self.num -= (1 / self.fps) * self.framerate
if self.num < 0:
self.finished = True
time.sleep(1 / (self.fps * 2.2))
sa.stop_all()
self.finished = True
Direction.direction_p_1.value = 0