-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathv2.py
More file actions
91 lines (69 loc) · 2.01 KB
/
Copy pathv2.py
File metadata and controls
91 lines (69 loc) · 2.01 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
from __future__ import print_function
from pydub import AudioSegment
from matplotlib import pyplot as plt
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.0, 0.0)] * (t_s * 10)
plot_data = [0.0] * (t_s * 10)
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)
max_amp = 0
max_freq = 0
# populating plot data
for vfft, freq in zip(res_fft, freq_data):
hz = int(abs(freq * fps))
if hz > 20 and hz < 16000:
amp = np.abs(vfft)
if amp > max_amp:
max_amp = amp
max_freq = hz
# plot_data[i] = (20 * math.log10(max_amp), max_freq)
plot_data[i] = max_freq
plt.plot(plot_data)
plt.show()
# # 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")