我想用python检测给定音乐或声音的节奏。然后将节奏时间点打印到一个文件中,以便在我的unity游戏中使用。我在这方面已经取得了一些进展,但仍然不是我想要的。少了一些笔记。这是我的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")
下面是我生成的一个带有点击效果的声音文件: Sound files
编辑:
我真的做了一个像钢琴瓷砖,吉塔尔英雄游戏。但我的游戏不会包含3行,它将只有一行击中笔记,所以这意味着我只需要一个事件来触发我的机制。你知道吗
下面是我想要的事件机制的示例视频: Example video
目前没有回答
相关问题 更多 >
编程相关推荐