-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
23 lines (18 loc) · 822 Bytes
/
Copy pathcode
File metadata and controls
23 lines (18 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import librosa
import numpy as np
# Path to your MP3 file (update path as needed)
audio_path = "Clarx - Zig Zag [NCS Release].mp3"
# Load audio (sr=None preserves original sample rate)
y, sr = librosa.load(audio_path, sr=None)
# Extract melody using librosa's pYIN implementation
f0, voiced_flag, voiced_prob = librosa.pyin(
y,
fmin=librosa.note_to_hz('C2'),
fmax=librosa.note_to_hz('C7')
)
# Replace NaNs (unvoiced frames) with -1 to indicate a rest
melody_frequency_list = [freq if not np.isnan(freq) else -1 for freq in f0]
# Print the frequencies as a plain string, separated by commas, no spaces/brackets/type
# Also cast values to plain Python float or int for clean output
output = ",".join(str(int(freq)) if freq == int(freq) else str(float(freq)) for freq in melody_frequency_list)
print(output)