Untitled

 avatar
unknown
plain_text
22 days ago
744 B
2
Indexable
import librosa.display
import numpy as np
import matplotlib.pyplot as plt

# Load the audio file again
y, sr = librosa.load(file_path, sr=None)

# Get beat timings
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)

# Convert beat frames to time (seconds)
beat_times = librosa.frames_to_time(beat_frames, sr=sr)

# Plot waveform with beat markers
plt.figure(figsize=(12, 4))
librosa.display.waveshow(y, sr=sr, alpha=0.6)
plt.vlines(beat_times, ymin=-1, ymax=1, color="r", linestyle="--", label="Beats")
plt.title(f"Waveform with Beat Tracking (Tempo: {tempo:.2f} BPM)")
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude")
plt.legend()
plt.show()

# Output tempo and beat times
tempo, beat_times[:10]  # Show first 10 beats for reference
Editor is loading...
Leave a Comment