-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_demo_replacement.py
More file actions
61 lines (50 loc) · 2.03 KB
/
Copy pathcreate_demo_replacement.py
File metadata and controls
61 lines (50 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
import soundfile as sf
import gtts
import io
import librosa
def text_to_audio(text, sr=16000):
"""Convert text to audio using Google TTS"""
try:
tts = gtts.gTTS(text=text, lang='en', slow=False)
fp = io.BytesIO()
tts.write_to_fp(fp)
fp.seek(0)
y, _ = librosa.load(fp, sr=sr)
return y
except Exception as e:
print(f"Error in text_to_audio: {str(e)}")
raise
def create_demo_replacement():
"""Create demo audio files with word replacement"""
sr = 16000 # Sample rate
try:
# Generate synthetic speech for words
y1 = text_to_audio("the", sr=sr)
y2_original = text_to_audio("big", sr=sr)
y2_modified = text_to_audio("small", sr=sr)
y3 = text_to_audio("cat", sr=sr)
# Add small silence between words
silence = np.zeros(int(0.2 * sr)) # 200ms silence
# Create original audio with three words
original_audio = np.concatenate([y1, silence, y2_original, silence, y3])
# Create modified audio by replacing the middle word
modified_audio = np.concatenate([y1, silence, y2_modified, silence, y3])
# Save audio files
sf.write('original_replacement.wav', original_audio, sr)
sf.write('modified_replacement.wav', modified_audio, sr)
print("\nReplacement Demo Files Created:")
print(f"Original audio: 'the big cat'")
print(f"Modified audio: 'the small cat' (middle word replaced)")
return 'original_replacement.wav', 'modified_replacement.wav'
except Exception as e:
print(f"Error creating replacement demo: {str(e)}")
raise
if __name__ == "__main__":
try:
original_file, modified_file = create_demo_replacement()
from compare_audio_mel import MelSpectrogramComparator
comparator = MelSpectrogramComparator(original_file, modified_file)
comparator.compare()
except Exception as e:
print(f"Error: {str(e)}")