-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundWave.py
156 lines (132 loc) · 3.95 KB
/
SoundWave.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import requests
from uuid import uuid4
import os
from pydub import AudioSegment
import numpy as np
import shutil
from PIL import Image, ImageDraw
class SoundWave:
bar_count = 50
skip_count = 5
def __init__(self, file):
"""
initialize sound wave from file
:param file:
"""
self._file = file
self._audio = AudioSegment.from_file(file.name)
self._frame_rate = self._audio.frame_rate
self._bar_count = self.bar_count
self._skip_count = self.skip_count
self._data = None
self._process_func = max
@classmethod
def from_file(cls, file):
"""
initialize SoundWave from sound file
:param file: file instance
:return: SoundWave
"""
return cls(file)
@classmethod
def from_path(cls, path):
"""
initialize SoundWave from sound path
:param path: sound path
:return: SoundWave
"""
return cls(open(path, 'rb'))
@classmethod
def from_url(cls, url):
"""
initialize SoundWave from sound url
:param url: sound url
:return: SoundWave
"""
uid = f'downloads/{uuid4().hex}.mp3'
with requests.get(url, stream=True) as stream:
with open(uid, 'wb') as file:
shutil.copyfileobj(stream.raw, file)
return cls(open(uid, 'rb'))
def with_bar_count(self, count):
"""
change bar count
:param count: new bar count
:return: self
"""
self._bar_count = count
return self
def with_skip_percent(self, ratio):
"""
percentage of the data to ignore
:param ratio: ignore ratio
:return:
"""
if ratio >= 1:
self._skip_count = 1
elif ratio >= 0:
self._skip_count = int(1 / (1 - ratio))
else:
raise Exception('invalid skip percentage')
return self
def using_maximum(self):
"""
use maximum function
:return: self
"""
self._process_func = max
return self
def using_average(self):
"""
use average function
:return: self
"""
def avg(lst):
lst = list(lst)
return sum(lst) / len(lst)
self._process_func = avg
return self
def process(self):
"""
start processing the data
:return:
"""
data = np.fromstring(self._audio._data, np.int16)
data = data[::self._skip_count]
height_list = []
max_height = 0
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
for segment in chunks(data, int(len(data) / self._bar_count)):
height = self._process_func(map(lambda x: abs(x), segment))
height_list.append(height)
if max_height < height:
max_height = height
self._data = list(map(lambda x: int(x / max_height * 100), height_list))
return self
def visualize(self, bar_height, bar_width):
"""
visualize this soundwave
:param bar_height: max height of the bars
:param bar_width: width of each bar
:return: self
"""
image = Image.new('RGBA', (self._bar_count * bar_width, bar_height), (255, 255, 255, 1))
draw = ImageDraw.Draw(image)
current_x = 1
for segment in self.data:
seg_height = segment / 100 * bar_height
current_y = (bar_height - seg_height) / 2
draw.line((current_x, current_y, current_x, current_y + seg_height), fill=(169, 171, 172), width=4)
current_x = current_x + bar_width
image.show()
return self
@property
def data(self):
""" get processed data """
return self._data
def delete_file(self):
""" delete file after processing """
os.remove(self._file.name)
return self