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?

OpenAI の API + 新モデル「gpt-4o-mini-tts」で音声合成 その2: ストリーミング・パラメータ追加を試す【Node.js】

Last updated at Posted at 2025-04-05

はじめに

以下の記事で扱っていた、OpenAI の音声合成の新モデル「gpt-4o-mini-tts」に関する記事です。

●【Node.js】OpenAI の API + 新モデル(gpt-4o-mini-tts)で音声合成 - Qiita
 https://qiita.com/youtoy/items/a35f7907eaa4259e417e

上記の記事では、音声ファイルを出力する形でしたが、今回はストリーミングを試します。また、上記では指定していなかったパラメータも追加してみます。

そして今回の実装も、前回と同じで Node.js で API を扱う形にします。

音声合成を手軽に試すなら「OpenAI.fm」

前回の記事にも書いたのですが、「gpt-4o-mini-tts」を使った音声合成を手軽に試すなら、以下の「OpenAI.fm」を使うのが良いです。

●OpenAI.fm
 https://www.openai.fm/

image.png

公式の技術情報を確認

それでは、公式の技術情報を見てみます。

Streaming realtime audio

公式ドキュメントに「Streaming realtime audio」という項目があります。

image.png

そこには以下のサンプルプログラムが掲載されています。

import OpenAI from "openai";
import { playAudio } from "openai/helpers/audio";

const openai = new OpenAI();

const response = await openai.audio.speech.create({
  model: "gpt-4o-mini-tts",
  voice: "coral",
  input: "Today is a wonderful day to build something people love!",
  instructions: "Speak in a cheerful and positive tone.",
  response_format: "wav",
});

await playAudio(response);

動作確認

このサンプルで、簡単な動作確認を行ってみました。

APIキーの設定と、npmコマンドでのパッケージインストールも行った状態で、上記を実行すると合成された音声が再生されました。

前回と比べると、保存されたファイルを再生するという手間がなく音を再生できました。単に出力の音声を確認したいという場合は、今回の実装のほうが手軽で良さそうです。

パラメータについて

API のパラメータについては、公式ドキュメントの「Create speech」という項目のところに書いてあります。

image.png

まず、必須パラメータは以下の 3つです。

image.png

記事執筆時点で、voice で指定できるものは以下となっているようです。

  • alloy
  • ash
  • ballad
  • coral
  • echo
  • fable
  • onyx
  • nova
  • sage
  • shimmer
  • verse

他に、以下の 3つのパラメータも指定できるようです。

image.png

パラメータを変更したもので試してみる

パラメータを変更したもので試してみます。

「speed」のパラメータを追加してみたりなど

上に掲載していたサンプルに、speed のパラメータを追加してみたり、「voice、input、instructions」の内容を変えてみたりしました。

import OpenAI from "openai";
import { playAudio } from "openai/helpers/audio";

const openai = new OpenAI();

const response = await openai.audio.speech.create({
  model: "gpt-4o-mini-tts",
  voice: "echo",
  input: "さらに音声合成を試してみます!スピードを0.25にしたけど、どうかな!",
  instructions: "前半は明るく、後半は落ち着いた感じで",
  response_format: "wav",
  speed: 0.25,
});

await playAudio(response);

さらに、speed のパラメータの値を変えたものも作ってみました。

  input: "さらに音声合成を試してみます!スピードを2.0にしたけど、どうかな!",
  ・・・
  speed: 2.0,

動作させた結果

上記の speed のパラメータを追加してみたものを試した結果は、以下のとおりです。

意図通りに動作していることが確認できました。

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?