Skip to content

Commit 5e87e37

Browse files
authored
Create audioView.py
1 parent c3afa13 commit 5e87e37

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

audioView.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import matplotlib.pyplot as plt
2+
from pydub import AudioSegment
3+
import numpy as np
4+
from tkinter import Tk
5+
from tkinter.filedialog import askopenfilename
6+
7+
# Hide the main tkinter window
8+
Tk().withdraw()
9+
10+
# Open file dialog to select audio file
11+
filename = askopenfilename(filetypes=[("Audio files", "*.wav *.mp3 *.flac *.ogg *.m4a *.aac")])
12+
13+
if not filename:
14+
print("No file selected, exiting.")
15+
exit()
16+
17+
# Load audio with pydub
18+
audio = AudioSegment.from_file(filename)
19+
20+
21+
samples = np.array(audio.get_array_of_samples())
22+
if audio.channels == 2:
23+
samples = samples.reshape((-1, 2))
24+
samples = samples.mean(axis=1) # stereo to mono
25+
26+
rate = audio.frame_rate
27+
28+
# Plot spectrogram
29+
plt.specgram(samples, Fs=rate, NFFT=1024, noverlap=512, cmap="gray_r")
30+
plt.title(f"Spectrogram of {filename.split('/')[-1]}")
31+
plt.xlabel("Time (s)")
32+
plt.ylabel("Frequency (Hz)")
33+
plt.show()

0 commit comments

Comments
 (0)