-
Notifications
You must be signed in to change notification settings - Fork 1
/
v1.py
100 lines (78 loc) · 2.43 KB
/
v1.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
from __future__ import print_function
from pydub import AudioSegment
import numpy as np
import math
import sys
import os
argv_l = len(sys.argv)
if argv_l < 2:
sys.exit('ERROR: Please supply a mp3 file path')
file_name = sys.argv[1]
# getting extension of file
_, ext = os.path.splitext(file_name)
# not mp3 extension
if ext != '.mp3':
print(ext)
sys.exit('ERROR: File must be a mp3 audio file')
# opening mp3 song
song = AudioSegment.from_mp3(sys.argv[1])
print()
print("Analyzing", os.path.splitext(os.path.basename(file_name))[0])
sys.stdout.flush()
# duration of song in seconds
t_s = int(len(song) / 1000)
# t_s = 10
# frames per secod of the song
fps = song.frame_rate
# splitting song into different
# channels for multi-channel songs
channels = song.split_to_mono()
# Initializing plot data array
plot_data = [0] * 22000
for i in range(t_s * 10):
# array.array object of song
data = channels[0][(i * 100):((i + 1) * 100)].get_array_of_samples()
# performing fourier transformation
res_fft = np.fft.rfft(data)
# getting the frequencies
freq_data = np.fft.rfftfreq(res_fft.size)
# populating plot data
for vfft, freq in zip(res_fft, freq_data):
hz = int(abs(freq * fps))
if hz > 20 and hz < 22000:
amp = np.abs(vfft)
if amp > 0:
plot_data[hz] = max(20 * math.log10(amp), plot_data[hz])
# output file name
if argv_l < 3:
print()
print("Output file name not specified!")
print("Defaulting to 'spectrum.plot'")
print()
file_name = 'spectrum.plot'
else:
file_name = sys.argv[2]
# opening output file
f = open(file_name, 'w')
# writing plot data
for index, item in enumerate(plot_data):
if(item > 0):
f.write(repr(index) + " " + repr(item) + "\n")
# # play Song
# import pyaudio
# p = pyaudio.PyAudio()
# stream = p.open(format=p.get_format_from_width(song.sample_width),
# channels=1,
# rate=song.frame_rate,
# output=True)
# for i in range(t_s):
# print (repr(i / 3600).zfill(2), end=':')
# print (repr((i % 3600) / 60).zfill(2), end=':')
# print (repr(i % 60).zfill(2), end=' ')
# print ("/", end=' ')
# print (repr(t_s / 3600).zfill(2), end=':')
# print (repr((t_s % 3600) / 60).zfill(2), end=':')
# print (repr(t_s % 60).zfill(2), end='\r')
# sys.stdout.flush()
# frame = channels[0][i * 1000:(i + 1) * 1000].raw_data
# stream.write(frame)