Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,8 @@ from voice_synthesizer import VoiceSynthesizer
|
|
| 14 |
from singing_converter import SingingConverter
|
| 15 |
import setup
|
| 16 |
import sys
|
| 17 |
-
import
|
|
|
|
| 18 |
nltk.download('punkt')
|
| 19 |
nltk.download('punkt_tab')
|
| 20 |
nltk.download('stopwords')
|
|
@@ -41,6 +42,38 @@ singing_converter = SingingConverter()
|
|
| 41 |
# Setup sentiment analysis
|
| 42 |
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def process_text_to_singing(text, voice_type="neutral", tempo=100, pitch_shift=0):
|
| 45 |
"""
|
| 46 |
Convert text to singing voice with accompaniment based on mood
|
|
@@ -56,7 +89,7 @@ def process_text_to_singing(text, voice_type="neutral", tempo=100, pitch_shift=0
|
|
| 56 |
"""
|
| 57 |
# Step 1: Analyze text for emotion/mood
|
| 58 |
emotions = te.get_emotion(text)
|
| 59 |
-
dominant_emotion = max(emotions.items(), key=lambda x: x[1])[0]
|
| 60 |
|
| 61 |
# Additional sentiment analysis
|
| 62 |
sentiment_result = sentiment_analyzer(text)[0]
|
|
@@ -90,7 +123,7 @@ def process_text_to_singing(text, voice_type="neutral", tempo=100, pitch_shift=0
|
|
| 90 |
)
|
| 91 |
|
| 92 |
# Step 5: Generate musical accompaniment based on mood
|
| 93 |
-
|
| 94 |
|
| 95 |
# Map emotion to musical key and style
|
| 96 |
emotion_key_map = {
|
|
@@ -106,27 +139,39 @@ def process_text_to_singing(text, voice_type="neutral", tempo=100, pitch_shift=0
|
|
| 106 |
|
| 107 |
# Adjust tempo based on emotion if not explicitly set
|
| 108 |
tempo_value = tempo
|
| 109 |
-
accompaniment_midi_path = "temp_accompaniment.mid"
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
accompaniment_path = "temp_accompaniment.wav"
|
| 122 |
convert_midi_to_wav(accompaniment_midi_path, accompaniment_path)
|
| 123 |
|
| 124 |
# Step 6: Mix singing voice with accompaniment
|
| 125 |
final_output_path = "output_song.wav"
|
| 126 |
|
| 127 |
-
# Load
|
| 128 |
singing = AudioSegment.from_file(singing_audio_path)
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
# Adjust volumes
|
| 132 |
singing = singing - 3 # Reduce singing volume slightly
|
|
@@ -146,32 +191,6 @@ def process_text_to_singing(text, voice_type="neutral", tempo=100, pitch_shift=0
|
|
| 146 |
|
| 147 |
return speech_audio_path, final_output_path
|
| 148 |
|
| 149 |
-
def convert_midi_to_wav(midi_path, wav_path, soundfont_path='/usr/share/sounds/sf2/FluidR3_GM.sf2'):
|
| 150 |
-
"""Convert MIDI file to WAV using fluidsynth"""
|
| 151 |
-
import subprocess
|
| 152 |
-
|
| 153 |
-
# Check if the MIDI file exists
|
| 154 |
-
if not os.path.exists(midi_path):
|
| 155 |
-
raise FileNotFoundError(f"MIDI file not found: {midi_path}")
|
| 156 |
-
|
| 157 |
-
try:
|
| 158 |
-
# Use fluidsynth to convert MIDI to WAV
|
| 159 |
-
subprocess.run([
|
| 160 |
-
'fluidsynth',
|
| 161 |
-
'-a', 'file',
|
| 162 |
-
'-F', wav_path,
|
| 163 |
-
soundfont_path,
|
| 164 |
-
midi_path
|
| 165 |
-
], check=True)
|
| 166 |
-
|
| 167 |
-
return wav_path
|
| 168 |
-
except subprocess.CalledProcessError as e:
|
| 169 |
-
print(f"Error converting MIDI to WAV: {e}")
|
| 170 |
-
raise
|
| 171 |
-
except FileNotFoundError:
|
| 172 |
-
print("fluidsynth not found. Please install it.")
|
| 173 |
-
raise
|
| 174 |
-
|
| 175 |
# Create Gradio interface
|
| 176 |
with gr.Blocks(title="Text2Sing-DiffSinger") as demo:
|
| 177 |
gr.Markdown("# Text2Sing-DiffSinger")
|
|
|
|
| 14 |
from singing_converter import SingingConverter
|
| 15 |
import setup
|
| 16 |
import sys
|
| 17 |
+
import subprocess
|
| 18 |
+
|
| 19 |
nltk.download('punkt')
|
| 20 |
nltk.download('punkt_tab')
|
| 21 |
nltk.download('stopwords')
|
|
|
|
| 42 |
# Setup sentiment analysis
|
| 43 |
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 44 |
|
| 45 |
+
def create_placeholder_audio(output_path, duration=5, sample_rate=22050):
|
| 46 |
+
"""Create a placeholder silence audio file"""
|
| 47 |
+
silence = np.zeros(int(duration * sample_rate))
|
| 48 |
+
sf.write(output_path, silence, sample_rate)
|
| 49 |
+
return output_path
|
| 50 |
+
|
| 51 |
+
def convert_midi_to_wav(midi_path, wav_path, soundfont_path='/usr/share/sounds/sf2/FluidR3_GM.sf2'):
|
| 52 |
+
"""Convert MIDI file to WAV using fluidsynth"""
|
| 53 |
+
# Check if the MIDI file exists
|
| 54 |
+
if not os.path.exists(midi_path):
|
| 55 |
+
print(f"MIDI file not found: {midi_path}")
|
| 56 |
+
print("Creating placeholder audio file instead")
|
| 57 |
+
return create_placeholder_audio(wav_path)
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
# Use fluidsynth to convert MIDI to WAV
|
| 61 |
+
subprocess.run([
|
| 62 |
+
'fluidsynth',
|
| 63 |
+
'-a', 'file',
|
| 64 |
+
'-F', wav_path,
|
| 65 |
+
soundfont_path,
|
| 66 |
+
midi_path
|
| 67 |
+
], check=True)
|
| 68 |
+
|
| 69 |
+
return wav_path
|
| 70 |
+
except subprocess.CalledProcessError as e:
|
| 71 |
+
print(f"Error converting MIDI to WAV: {e}")
|
| 72 |
+
return create_placeholder_audio(wav_path)
|
| 73 |
+
except FileNotFoundError:
|
| 74 |
+
print("fluidsynth not found. Using placeholder audio instead.")
|
| 75 |
+
return create_placeholder_audio(wav_path)
|
| 76 |
+
|
| 77 |
def process_text_to_singing(text, voice_type="neutral", tempo=100, pitch_shift=0):
|
| 78 |
"""
|
| 79 |
Convert text to singing voice with accompaniment based on mood
|
|
|
|
| 89 |
"""
|
| 90 |
# Step 1: Analyze text for emotion/mood
|
| 91 |
emotions = te.get_emotion(text)
|
| 92 |
+
dominant_emotion = max(emotions.items(), key=lambda x: x[1])[0] if emotions else "Happy"
|
| 93 |
|
| 94 |
# Additional sentiment analysis
|
| 95 |
sentiment_result = sentiment_analyzer(text)[0]
|
|
|
|
| 123 |
)
|
| 124 |
|
| 125 |
# Step 5: Generate musical accompaniment based on mood
|
| 126 |
+
accompaniment_midi_path = "temp_accompaniment.mid"
|
| 127 |
|
| 128 |
# Map emotion to musical key and style
|
| 129 |
emotion_key_map = {
|
|
|
|
| 139 |
|
| 140 |
# Adjust tempo based on emotion if not explicitly set
|
| 141 |
tempo_value = tempo
|
|
|
|
| 142 |
|
| 143 |
+
try:
|
| 144 |
+
# Try to generate the accompaniment MIDI
|
| 145 |
+
generate_accompaniment(
|
| 146 |
+
lyrics=text,
|
| 147 |
+
melody_path=singing_audio_path,
|
| 148 |
+
output_path=accompaniment_midi_path,
|
| 149 |
+
tempo_value=tempo_value,
|
| 150 |
+
key=key,
|
| 151 |
+
time_signature="4/4",
|
| 152 |
+
style=style
|
| 153 |
+
)
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"Error generating accompaniment: {e}")
|
| 156 |
+
# We'll handle this with the convert_midi_to_wav function that creates a placeholder
|
| 157 |
+
|
| 158 |
+
# Convert MIDI to WAV
|
| 159 |
accompaniment_path = "temp_accompaniment.wav"
|
| 160 |
convert_midi_to_wav(accompaniment_midi_path, accompaniment_path)
|
| 161 |
|
| 162 |
# Step 6: Mix singing voice with accompaniment
|
| 163 |
final_output_path = "output_song.wav"
|
| 164 |
|
| 165 |
+
# Load singing audio
|
| 166 |
singing = AudioSegment.from_file(singing_audio_path)
|
| 167 |
+
|
| 168 |
+
# Load accompaniment or create placeholder if loading fails
|
| 169 |
+
try:
|
| 170 |
+
accompaniment = AudioSegment.from_file(accompaniment_path)
|
| 171 |
+
except Exception as e:
|
| 172 |
+
print(f"Error loading accompaniment: {e}")
|
| 173 |
+
create_placeholder_audio(accompaniment_path)
|
| 174 |
+
accompaniment = AudioSegment.from_file(accompaniment_path)
|
| 175 |
|
| 176 |
# Adjust volumes
|
| 177 |
singing = singing - 3 # Reduce singing volume slightly
|
|
|
|
| 191 |
|
| 192 |
return speech_audio_path, final_output_path
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
# Create Gradio interface
|
| 195 |
with gr.Blocks(title="Text2Sing-DiffSinger") as demo:
|
| 196 |
gr.Markdown("# Text2Sing-DiffSinger")
|