-
Notifications
You must be signed in to change notification settings - Fork 1
/
spectrogram.py
64 lines (54 loc) · 1.58 KB
/
spectrogram.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 27 09:45:09 2021
@author: amandabreton
"""
#
#import the pyplot and wavfile modules
import matplotlib.pyplot as plot
from scipy.io import wavfile
from playsound import playsound
import argparse
import yaml
import os
import random
import wave
import contextlib
from scipy.fft import fft, ifft, fftfreq
# learning stuff from here:
# https://makersportal.com/blog/2018/9/13/audio-processing-in-python-part-i-sampling-and-the-fast-fourier-transform
parser = argparse.ArgumentParser()
parser.add_argument('config_filename')
args = parser.parse_args()
CONFIG_FILE = args.config_filename
with open(CONFIG_FILE) as f:
configs = yaml.load(f, Loader=yaml.SafeLoader)
path = configs['path']
#path = '/Users/amandabreton/Documents/GitHub/gnatcatcher/sounds'
files = os.listdir(path)
sound = os.path.join(path, random.choice(files))
# Read the wav file (mono)
samplingFrequency, signalData = wavfile.read(sound)
wavdata = wave.open(sound, 'r')
print ( "parameters:",wavdata.getparams())
# Plot the signal read from wav file
plot.subplot(211)
plot.title('Recorded Sound')
plot.plot(signalData)
plot.xlabel('Sample')
plot.ylabel('Amplitude')
plot.subplot(212)
plot.title('Spectrogram')
plot.specgram(signalData,Fs=samplingFrequency)
plot.xlabel('Time (sec)')
plot.ylabel('Frequency (Hz)')
plot.show()
#playsound(sound)
with contextlib.closing(wave.open(sound,'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
print('Duration of sound:')
print(duration)
#yf = fft(signalData)