midファイルを生成
pip install mido
from mido import Message, MidiFile, MidiTrack
# ファイル名(拡張子を除く)
filename = ""
mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)
# テンポ設定(ゆったり荘厳)
track.append(Message('program_change', program=48, time=0)) # Strings
# ノートの長さ
quarter = 480
# 低音のドローン(城の重厚感)
bass_notes = [36, 38, 41, 43] # C, D, F, G の低音
for note in bass_notes:
track.append(Message('note_on', note=note, velocity=60, time=0))
track.append(Message('note_off', note=note, velocity=60, time=quarter * 4))
# 中音域の和音(荘厳な雰囲気)
chords = [
[60, 64, 67], # C
[62, 65, 69], # Dm
[57, 60, 64], # A♭(雰囲気変化)
[55, 59, 62], # G
]
for chord in chords:
for note in chord:
track.append(Message('note_on', note=note, velocity=70, time=0))
for note in chord:
track.append(Message('note_off', note=note, velocity=70, time=quarter * 4))
# 高音アルペジオ(城の神秘感)
arp = [72, 76, 79, 84] # 高音 C メジャー
for _ in range(8):
for note in arp:
track.append(Message('note_on', note=note, velocity=50, time=0))
track.append(Message('note_off', note=note, velocity=50, time=quarter // 2))
mid.save(filename + ".mid")
midファイルをwavファイルに変換
pip install pyfluidsynth
pip install pretty_midi
FluidSynth 本体(DLL)ダウンロード
https://github.com/FluidSynth/fluidsynth/releases
fluidsynth 2.5.2
Assetsの黒い三角をクリック
fluidsynth-v2.5.2-win10-x64-cpp11.zip
"C:\tools\fluidsynth"フォルダを作る
fluidsynthフォルダの中にbinフォルダをコピペ
(fluidsynth-v2.5.2-win10-x64-cpp11のbin)
SoundFont(.sf2)ダウンロード
https://github.com/mrbumpy409/GeneralUser-GS/blob/main/GeneralUser-GS.sf2
Rawをクリックでダウンロード
GeneralUser-GS.sf2をスクリプトと同じフォルダに置く
import os
os.add_dll_directory(r"C:\tools\fluidsynth\bin")
import pretty_midi
import numpy as np
from scipy.io import wavfile
# ファイル名(拡張子を除く)
filename = ""
midi = pretty_midi.PrettyMIDI(filename + ".mid")
audio = midi.fluidsynth(fs=44100, synthesizer="GeneralUser-GS.sf2")
audio_int16 = (np.clip(audio, -1.0, 1.0) * 32767).astype(np.int16)
wavfile.write(filename + ".wav", 44100, audio_int16)