Spaces:
Sleeping
Sleeping
Download utils/audio_utils.py from garantus/MusicVideoComposer: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/garantus/MusicVideoComposer/resolve/main/utils/audio_utils.py
- Command line
-
hf download hf://spaces/garantus/MusicVideoComposer/utils/audio_utils.py
-
curl -L -o audio_utils.py https://huggingface.co/spaces/garantus/MusicVideoComposer/resolve/main/utils/audio_utils.py
1.06 kB
| import librosa | |
| import numpy as np | |
| def detect_beats_and_tempo(audio_path,image_duration_beats): | |
| """ | |
| Detects beats and tempo from an audio file. | |
| Args: | |
| audio_path (str): Path to the audio file. | |
| Returns: | |
| tuple: (beats, tempo) where beats is a list of beat timestamps and tempo is the estimated tempo. | |
| """ | |
| y, sr = librosa.load(audio_path) | |
| tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) | |
| beat_times = librosa.frames_to_time(beat_frames, sr=sr) | |
| return beat_times[1::image_duration_beats], tempo | |
| def detect_pitch(audio_path): | |
| """ | |
| Detects pitch from an audio file. | |
| This is a placeholder, as torchcrepe is optional. | |
| If implementing, replace this with torchcrepe logic. | |
| """ | |
| return [] #Return empty list for now. | |
| if __name__ == '__main__': | |
| # Example usage (for testing) | |
| audio_file = '../examples/myvoice_song.mp3' # Adjust path as needed | |
| beats, tempo = detect_beats_and_tempo(audio_file) | |
| print(f"Tempo: {tempo} BPM") | |
| print(f"Beats: {beats}") |