0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Agent SDKでTTSする方法

Last updated at Posted at 2025-04-16

概要

公式ドキュメントにはVoicePipelineを使ってspeach-to-speachのchain型の音声対話エージェントを作る方法は載っていますが、純粋なTTS/STTを使う方法が紹介されていません!

Agents SDKでTTSを使う方法を紹介します。おそらく同様の方法でSTTもできると思いますが確かめていません。

TTS

import os 
import io
import wave
from agents.voice import TTSModelSettings, OpenAITTSModel
from openai import AsyncOpenAI
from pathlib import Path
from dotenv import load_dotenv

async def main() :
    
    load_dotenv(dotenv_path=dotenv_path)

    OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")

    custom_tts_settings = TTSModelSettings(
        instructions="あなたは音声案内エージェントです。"
        "音声: 日本語の女性の声で、明るく親しみやすいトーンを持つ。"
        "トーン: 親しみやすく、明瞭で安心感のある話し方で、聞き手が自信を持ち、落ち着ける雰囲気を作る。"
        "発音: はっきりと明瞭に、安定したリズムで話し、自然な会話の流れを保ちながらも指示がわかりやすく伝わるようにする。"
        "テンポ: 比較的速めに話し、質問の前後に短い間を挟む。"
        "感情: 温かく支えとなるような話し方で、共感と気遣いを込め、聞き手が安心してガイドを受けられるようにする。",
        voice="sage"
    )

    tts_model = OpenAITTSModel(
        model="gpt-4o-mini-tts",
        openai_client=AsyncOpenAI(
            api_key=OPENAI_API_KEY
            ),
        )
    text = "こんにちは、私はあなたの音声案内エージェントです。"
    response_chunks = []
    async for chunk in tts_model.run(text, custom_tts_settings):
        response_chunks.append(chunk)
    response_audio = b"".join(response_chunks)

    wav_buffer = io.BytesIO()
    with wave.open(wav_buffer, "wb") as wf:
        wf.setnchannels(1) # モノラル
        wf.setsampwidth(2) # 16ビットPCM
        wf.setframerate(24000) # 24kHz
        wf.writeframes(response_audio)
    
    wav_buffer.seek(0)
    with open("output.wav", "wb") as f:
        f.write(wav_buffer.read())
    wav_buffer.close()
    print("Audio saved as output.wav")
    
if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

こちらの例はAgents SDKでTTSし、wav音声ファイルとして保存するプログラムです。
VoicePipelineのような音声を再生する方法はおそらく使えないです。そのためwavファイルに変換する方法をとっています。プログラムを少し修正すればプログラムからの再生もできるはずです。

VoicePipeline

VoicePipelineの使い方に関しては以下の公式ドキュメントを参考にすると良いと思います。

OpenAI 公式ドキュメント

Github

こちらのサンプルプログラムのGithubです。
https://github.com/tetra-mix/agents-sdk-tts-sample

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?