File size: 2,801 Bytes
2f2406a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
import copy
from .TTS import synthesize_dictionary_batch, synthesize_dictionary
from .translate import translate_dictionary
from .audio_builder import build_audio


original_language = "en"
batch_tts_synthesize = False
skip_translation = False
stop_after_translation = False
skip_translation = False
skip_synthesize = False

two_pass_voice_synth = False # Azure doesn't need two pass voice synth, so disable it


def manually_prepare_dictionary(dictionaryToPrep):
    ### Do additional Processing to match the format produced by translation function
    # Create new key 'translated_text' and set it to the value of 'text'
    for key, value in dictionaryToPrep.items():
        dictionaryToPrep[key]['translated_text'] = value['text']
    
    # Convert the keys to integers and return the dictionary
    return {int(k): v for k, v in dictionaryToPrep.items()}


# Process a language: Translate, Synthesize, and Build Audio
def process_language(langData, originalLanguageSubsDict, totalAudioLength, translatedSrtFileName, outputFileName, outputFolder):
    langDict = {
        'targetLanguage': langData['translation_target_language'], 
        'sourceLanguage': langData['translation_source_language'], 
        'voiceName': langData['synth_voice_name'], 
        'languageCode': langData['synth_language_code'], 
        'voiceGender': langData['synth_voice_gender'],
        'translateService': langData['translate_service'],
        'formality': langData['formality']
        }

    individualLanguageSubsDict = copy.deepcopy(originalLanguageSubsDict)

    # Check for special case where original language is the same as the target language
    if langDict['languageCode'].lower() == original_language.lower():
        print("Original language is the same as the target language. Skipping translation.")
        individualLanguageSubsDict = manually_prepare_dictionary(individualLanguageSubsDict)

    elif skip_translation == False:
        # Translate
        individualLanguageSubsDict = translate_dictionary(individualLanguageSubsDict, langDict, translatedSrtFileName, skipTranslation=skip_translation)
        if stop_after_translation:
            print("Stopping at translation is enabled. Skipping TTS and building audio.")
            return

    # Synthesize
    if batch_tts_synthesize == True:
        individualLanguageSubsDict = synthesize_dictionary_batch(individualLanguageSubsDict, langDict, skipSynthesize=skip_synthesize)
    else:
        individualLanguageSubsDict = synthesize_dictionary(individualLanguageSubsDict, langDict, outputFolder, skipSynthesize=skip_synthesize)
    print(individualLanguageSubsDict)

    # Build audio
    individualLanguageSubsDict = build_audio(individualLanguageSubsDict, langDict, totalAudioLength, outputFileName, two_pass_voice_synth)