-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (45 loc) · 1.72 KB
/
main.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
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
# to handle mp3 files, we install pydub library, on windows add ffmpeg, else librosa prefers .wav format
audio_data, sr = librosa.load( 'C:\\Users\\prabin\\OneDrive\\Desktop\\minor project\\chopin.wav' )
# print('hello')
# mfccs = librosa.feature.mfcc(y = audio_data, sr=sr)
# ###to convert into array
# mfcc_array = np.array(mfccs)
# # print(mfcc_array)
# #to display the audio features
# librosa.display.specshow(mfccs, sr=sr, x_axis='time')
# plt.colorbar()
# plt.savefig('mfcc_plot.png')
# plt.show()
# Convert the frequency axis from samples to Hz
stft = librosa.stft(audio_data)
frequencies = librosa.fft_frequencies(sr=100, n_fft=len(stft))
#print(frequencies)
for i in range(1,len(frequencies)):
print(librosa.hz_to_note(frequencies[i]))
import matplotlib.pyplot as plt
import librosa
# Load the audio file
y, sr = librosa.load("./c_major.ogg")
# Calculate the frame duration in samples
frame_duration = 30 # in ms
frame_samples = int((frame_duration / 1000) * sr)
# frameplt =[]
# Iterate over the frames
for i in range(10, len(y), frame_samples):
frame = y[i:i + frame_samples]
# frameplt.append(frame)
# Compute the short-time Fourier transform (STFT) of the frames
stft = librosa.stft(frame)
# Convert the STFT to frequency and magnitude spectrograms
frequency, magnitude = librosa.magphase(stft)
# Convert the frequency axis from samples to Hz
frequencies = librosa.fft_frequencies( sr=sr, n_fft=len(stft))
# midi_file = 'example.mid'
# midi_data = librosa.midi.read_midi(midi_file)
# # Extract notes from the MIDI file
# notes = librosa.midi_to_notes(midi_data)
# print(notes)