Create diff_singer_infer.py
Browse files- diff_singer_infer.py +29 -0
diff_singer_infer.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torchaudio
|
| 3 |
+
import pyworld as pw
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
from scipy.interpolate import interp1d
|
| 6 |
+
|
| 7 |
+
def extract_pitch(audio, sr):
|
| 8 |
+
# Convert audio to double precision for pyworld
|
| 9 |
+
audio = audio.astype(np.float64)
|
| 10 |
+
|
| 11 |
+
_f0, t = pw.dio(audio, sr)
|
| 12 |
+
f0 = pw.stonemask(audio, _f0, t, sr)
|
| 13 |
+
return f0
|
| 14 |
+
|
| 15 |
+
def run_diffsinger_inference(input_audio_path):
|
| 16 |
+
waveform, sr = torchaudio.load(input_audio_path)
|
| 17 |
+
audio = waveform[0].numpy() # Mono
|
| 18 |
+
f0 = extract_pitch(audio, sr)
|
| 19 |
+
|
| 20 |
+
# Simple manipulation to simulate expressiveness
|
| 21 |
+
f0_smooth = interp1d(np.arange(len(f0)), f0, kind='cubic', fill_value="extrapolate")(np.linspace(0, len(f0)-1, len(f0)))
|
| 22 |
+
f0_smooth *= 1.15 # Slight pitch boost
|
| 23 |
+
|
| 24 |
+
# Pitch-shifted output
|
| 25 |
+
output_audio = pw.synthesize(f0_smooth, pw.cheaptrick(audio.astype(np.float64), f0, np.linspace(0, len(audio)/sr, len(f0)), sr), pw.d4c(audio.astype(np.float64), f0, np.linspace(0, len(audio)/sr, len(f0)), sr), sr)
|
| 26 |
+
|
| 27 |
+
output_path = "output_singing.wav"
|
| 28 |
+
sf.write(output_path, output_audio, sr)
|
| 29 |
+
return output_path
|