|
@@ -0,0 +1,58 @@
|
|
|
+#!/usr/bin/env python
|
|
|
+# -*- encoding: utf-8 -*-
|
|
|
+'''
|
|
|
+@Contact : liuyuqi.gov@msn.cn
|
|
|
+@Time : 2025/03/18 13:45:29
|
|
|
+@License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
|
|
|
+@Desc :
|
|
|
+
|
|
|
+批量把音频转换为abc乐谱格式
|
|
|
+pip install pydub music21
|
|
|
+
|
|
|
+apt-get update
|
|
|
+apt-get install ffmpeg
|
|
|
+
|
|
|
+'''
|
|
|
+
|
|
|
+import os
|
|
|
+from pydub import AudioSegment
|
|
|
+from music21 import *
|
|
|
+
|
|
|
+def audio_to_score(audio_file_path):
|
|
|
+ try:
|
|
|
+ # 读取音频文件
|
|
|
+ audio = AudioSegment.from_file(audio_file_path)
|
|
|
+ # 这里可以添加更复杂的音频特征提取逻辑,例如音高检测等
|
|
|
+ # 为了简化示例,我们假设已经提取到了音高和节奏信息
|
|
|
+ # 这里简单创建一个简单的音符序列
|
|
|
+ s = stream.Stream()
|
|
|
+ # 添加一个 C4 音符,持续时间为 1 拍
|
|
|
+ n = note.Note('C4', quarterLength=1)
|
|
|
+ s.append(n)
|
|
|
+ return s
|
|
|
+ except Exception as e:
|
|
|
+ print(f"音频转换为乐谱时出现错误: {e}")
|
|
|
+ return None
|
|
|
+
|
|
|
+def batch_audio_to_score(input_folder, output_folder):
|
|
|
+ # 确保输出文件夹存在
|
|
|
+ os.makedirs(output_folder, exist_ok=True)
|
|
|
+ # 遍历输入文件夹中的所有文件
|
|
|
+ for root, dirs, files in os.walk(input_folder):
|
|
|
+ for file in files:
|
|
|
+ if file.endswith(('.mp3', '.wav', '.ogg')):
|
|
|
+ audio_file_path = os.path.join(root, file)
|
|
|
+ score = audio_to_score(audio_file_path)
|
|
|
+ if score:
|
|
|
+ # 生成输出文件路径
|
|
|
+ output_file_name = os.path.splitext(file)[0] + '.midi'
|
|
|
+ output_file_path = os.path.join(output_folder, output_file_name)
|
|
|
+ # 保存乐谱
|
|
|
+ score.write('midi', fp=output_file_path)
|
|
|
+ print(f"已将 {audio_file_path} 转换为 {output_file_path}")
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ input_folder = 'your_input_audio_folder'
|
|
|
+ output_folder = 'your_output_score_folder'
|
|
|
+ batch_audio_to_score(input_folder, output_folder)
|
|
|
+
|