File size: 5,356 Bytes
6b80de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import hashlib
import requests
import urllib.parse
from translation_data import (
    translation_dict_A,
    translation_dict_B,
    translation_dict_C,
    translation_dict_D,
    translation_dict_F
    # 必要に応じて translation_dict_E, translation_dict_G などを追加
)

# 設定
AUDIO_VARIANTS = 3  # 各フレーズあたりの音声バリアント数
OUTPUT_DIR = os.path.join('static', 'audio')  # 音声ファイルの保存先
HASH_LENGTH = 8  # ハッシュの長さ
VOICE_GENERATION_URL = "http://192.168.1.80:5000/voice"  # 音声生成APIのURL

# バリアントごとの model_id の設定
VARIANT_SETTINGS = {
    1: {'model_id': 9},
    2: {'model_id': 11},
    3: {'model_id': 12},
}

def generate_truncated_hash(text, length=HASH_LENGTH):
    """

    指定されたテキストのトランケートされたSHA-256ハッシュを生成します。



    Args:

        text (str): ハッシュ化するテキスト。

        length (int): トランケートするハッシュの長さ。



    Returns:

        str: トランケートされたハッシュ文字列。

    """
    hash_object = hashlib.sha256(text.encode('utf-8'))
    hex_dig = hash_object.hexdigest()
    return hex_dig[:length]

def create_directory(path):
    """

    指定されたパスにディレクトリを作成します。存在しない場合のみ作成します。



    Args:

        path (str): 作成するディレクトリのパス。

    """
    os.makedirs(path, exist_ok=True)

def generate_audio(text, model_id):
    """

    音声生成APIにリクエストを送り、音声データを取得します。

    `speaker_id` は固定値 0 を使用します。



    Args:

        text (str): 音声化するテキスト。

        model_id (int): 使用するモデルID。



    Returns:

        bytes: 取得した音声データのバイナリ。

    """
    params = {
        'text': text,
        'model_id': model_id,
        'speaker_id': 0,  # speaker_id は常に 0 で問題ない仕様
        'sdp_ratio': 0.2,
        'noise': 0.6,
        'noisew': 0.8,
        'length': 1.4,
        'language': 'JP',
        'auto_split': 'true',
        'split_interval': 0.5,
        'assist_text_weight': 1,
        'style': 'Neutral',
        'style_weight': 1
    }

    try:
        response = requests.get(VOICE_GENERATION_URL, params=params, timeout=30)
        response.raise_for_status()  # HTTPエラーがあれば例外を発生させる
        return response.content
    except requests.exceptions.RequestException as e:
        print(f"Error generating audio for text '{text}': {e}")
        return None

def process_translation_dicts():
    """

    すべての翻訳辞書を処理し、音声バリアントを生成してフォルダに保存します。

    """
    # 翻訳辞書のリスト
    translation_dicts: list[dict[str, str]] = [
        translation_dict_A,
        translation_dict_B,
        translation_dict_C,
        translation_dict_D,
        translation_dict_F
        # 必要に応じて translation_dict_E, translation_dict_G などを追加
    ]

    for dict_index, translation_dict in enumerate(translation_dicts, start=1):
        set_identifier = chr(64 + dict_index)  # 'A', 'B', 'C', etc.
        print(f"\nProcessing translation_dict_{set_identifier}")

        for chinese_sentence, japanese_sentence in translation_dict.items():
            # 日本語の文からハッシュを生成
            sentence_hash = generate_truncated_hash(japanese_sentence)
            folder_path = os.path.join(OUTPUT_DIR, sentence_hash)

            # フォルダを作成
            create_directory(folder_path)
            print(f"Created/Verified directory: {folder_path}")

            for variant_num in range(1, AUDIO_VARIANTS + 1):
                settings = VARIANT_SETTINGS.get(variant_num)
                if not settings:
                    print(f"No settings defined for variant {variant_num}. Skipping.")
                    continue

                model_id = settings['model_id']
                audio_filename = f"variant_{variant_num}.mp3"
                audio_filepath = os.path.join(folder_path, audio_filename)
                
                # 既にファイルが存在する場合はスキップ
                """

                if os.path.exists(audio_filepath):

                    print(f"Audio variant already exists: {audio_filepath}")

                    continue



                print(f"Generating {audio_filename} with model_id={model_id}, {japanese_sentence}")

                """
                # 音声生成
                audio_data = generate_audio(japanese_sentence, model_id)
                if audio_data:
                    # ファイルに保存
                    with open(audio_filepath, 'wb') as f:
                        f.write(audio_data)
                    print(f"Saved audio variant: {audio_filepath}, {japanese_sentence}")
                else:
                    print(f"Failed to generate audio for variant {variant_num} of sentence: {japanese_sentence}")

if __name__ == "__main__":
    process_translation_dicts()
    print("\nAudio variant generation completed.")