1

我想用 python 检测给定音乐或声音的节奏。然后将节奏时间点打印到一个文件中,以便在我的统一游戏中使用。我在这方面取得了一些进展,但仍然不是我想要的。它缺少一些注释。这是我的python代码。我目前正在为此使用librosa模块。我的问题是,我走对了吗?如果是,那么我该如何改进我的代码?如果没有,那我该怎么办?

import librosa
import numpy as np
from os import listdir, path, remove
from os.path import isfile, join
from pydub import AudioSegment

SONGS_PATH = "./songs"
PS_FILES_PATH = "./ps_files"
detection_mode = "full"

# Get the song files from given dir
song_files = [f for f in listdir(SONGS_PATH) if isfile(join(SONGS_PATH, f))]


count = len(song_files)
# Process each song file
for i, song_file in enumerate(song_files):
    print(str(i+1) + "/" + str(count) + " Processing song: ", song_file)
    song_file_path = SONGS_PATH + "/" + song_file
    output_file = PS_FILES_PATH + "/" + song_file.replace(".mp3", "").replace(".wav", "")

    # If file type is mp3 then convert it to wav and remove mp3 file
    if ".mp3" in song_file:
        print("Not supported format. Converting to wav...")
        sound = AudioSegment.from_mp3(song_file_path)
        sound.export(song_file_path.replace("mp3", "wav"), format="wav")
        print("File converted. Deleting old one...")
        remove(song_file_path)
        song_file_path = song_file_path.replace("mp3", "wav")

    # Load file
    xy, sr = librosa.load(song_file_path)
    # Find onsets
    onset_frames = librosa.onset.onset_detect(xy, sr=sr, wait=1, pre_avg=1, post_avg=1, pre_max=1, post_max=1)
    # Get the onset times
    onset_times = librosa.frames_to_time(onset_frames)
    onset_clicks = librosa.clicks(frames=onset_frames, sr=sr, length=len(xy))

    # find tempo and beats
    tempo, beat_frames = librosa.beat.beat_track(y=xy, sr=sr)
    # Get the beat times
    beat_times = librosa.frames_to_time(beat_frames)
    beat_clicks = librosa.clicks(frames=beat_frames, sr=sr, length=len(xy))

    # Generate a txt file which is contains times, and create a audio file with click effect.
    print("Generating ps file...")
    if detection_mode == "only-onsets":
        librosa.output.write_wav(output_file + "onset_clicks.wav", xy + onset_clicks, sr)
        file = open(output_file + "_onset_times.txt","ab")
        np.savetxt(file, onset_times, '%.2f') 
    elif detection_mode == "only-beats":
        librosa.output.write_wav(output_file + "beat_clicks.wav", xy + beat_clicks, sr)
        file = open(output_file + "_beat_times.txt","ab")
        np.savetxt(file, beat_times, '%.2f') 
    else:
        librosa.output.write_wav(output_file + "full_clicks.wav", xy + onset_clicks + beat_clicks, sr)
        file = open(output_file + "_full_times.txt","ab")
        np.savetxt(file, np.concatenate(beat_times, onset_times), '%.2f') 
    print("Done.\n")

这是我生成的带有点击效果的声音文件之一: 声音文件

编辑:

我实际上正在制作一个像钢琴瓷砖,吉他英雄这样的游戏。但是我的游戏不会包含 3 行,它只有一行来敲击音符,这意味着我只需要一个事件来触发我的机制。

这是我想要的事件机制的示例视频: 示例视频

4

0 回答 0