-
Notifications
You must be signed in to change notification settings - Fork 1
/
music_player.py
51 lines (41 loc) · 1.31 KB
/
music_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
import time
import numpy as np
import pygame as pg
from const import *
class MusicPlayer:
def __init__(self, sound: pg.mixer.Sound):
self.sound = sound
self.snd_array = pg.sndarray.array(self.sound)
self.length = self.snd_array.size
self.size = len(self.snd_array)
self.is_playing = False
self.start_time = 0
self.current_time = 0
self.start_index = 0
self.end_index = 0
self.window_length = 0.1
def play(self):
self.sound.stop()
self.sound.play()
self.start_time = time.time()
self.is_playing = True
def pause(self):
self.sound.stop()
self.start_time = 0
self.current_time = 0
self.is_playing = False
def busy(self):
return pg.mixer.get_busy()
def get_amp_level(self):
start_index = max(
0, int(SAMPLE_RATE * (self.current_time - self.window_length))
)
end_index = min(
self.size,
int(SAMPLE_RATE * (self.current_time + self.window_length)),
)
sliced_data = self.snd_array[start_index:end_index]
rms = np.mean(np.abs(sliced_data))
return rms
def tick(self):
self.current_time = time.time() - self.start_time