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?

【Node.js】OpenAI の API + 新モデル(gpt-4o-mini-tts)で音声合成

Last updated at Posted at 2025-03-22

はじめに

以下のニュースなどにも出ている、OpenAI の音声系の新モデルに関する記事です。

●OpenAI、自然で感情豊かに文章読をみ上る音声合成モデル 書き起こしも強化 - Impress Watch
 https://www.watch.impress.co.jp/docs/news/1671850.html

2025-03-23_01-12-33.jpg

今回試す内容

今回試す内容は、音声認識・音声合成のモデルがある中の音声合成のほう(gpt-4o-mini-tts)です。

さらに、それを Node.js で試していきます。

公式のデモページ

今回の内容は API を試すために行うものですが、音声合成をとりあえずで試すなら以下の公式デモを使うのが手軽で良いかも知れません。

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

image.png

実際に試す

公式の情報を確認する

実際に試していくために OpenAI公式の APIリファレンスを確認してみます。

●API Reference - OpenAI API
 https://platform.openai.com/docs/api-reference/audio/createSpeech?lang=javascript

2025-03-23_01-05-53.jpg

以下が公式サンプルです。

import fs from "fs";
import path from "path";
import OpenAI from "openai";

const openai = new OpenAI();

const speechFile = path.resolve("./speech.mp3");

async function main() {
  const mp3 = await openai.audio.speech.create({
    model: "gpt-4o-mini-tts",
    voice: "alloy",
    input: "Today is a wonderful day to build something people love!",
  });
  console.log(speechFile);
  const buffer = Buffer.from(await mp3.arrayBuffer());
  await fs.promises.writeFile(speechFile, buffer);
}
main();

Node.js で試す

上の公式サンプルをベースに、お試しを進めます。

その際、以下の TypeScript・JavaScript用のライブラリを使います。

●openai - npm
 https://www.npmjs.com/package/openai

上記を npmコマンドでインストールして、APIキーは環境変数に設定します。
(Mac の zhs上で exportコマンドを使うやり方で、一時的にキーを設定しました)

実装した内容は公式サンプルそのままで、文章だけ日本語のものに変えてみました。

import fs from "fs";
import path from "path";
import OpenAI from "openai";

const openai = new OpenAI();

const speechFile = path.resolve("./speech.mp3");

async function main() {
  const mp3 = await openai.audio.speech.create({
    model: "gpt-4o-mini-tts",
    voice: "alloy",
    input: "OpenAIの新しいモデルで音声合成を試してみます",
  });
  console.log(speechFile);
  const buffer = Buffer.from(await mp3.arrayBuffer());
  await fs.promises.writeFile(speechFile, buffer);
}
main();

実行結果

実行してみた結果、MP3ファイルが生成されました。

それを再生すると、以下のようになりました。

【追記】利用履歴の確認

ダッシュボードの Usage のページで、API の利用履歴を確認してみました。

gpt-4o-mini-tts が表示されるカテゴリは、Audio系ではなく Chat Completionsになるようです。

おわりに

今回は音声合成を試しました。
利用可能なパラメータで今回使っていないものもあるため(instructions など)、それも試せればと思っています。

また別途、音声認識の新しいモデル(gpt-4o-transcribe、gpt-4o-mini-transcribe)のほうも試せればと思います。

●API Reference - OpenAI API
 https://platform.openai.com/docs/api-reference/audio/createTranscription?lang=javascript

2025-03-23_01-04-43.jpg

【追記】音声認識も試しました

「gpt-4o-transcribe」「gpt-4o-mini-transcribe」も試してみて、そして記事を書きまし。

●【Node.js】OpenAI の API + 新モデル(gpt-4o-transcribe、gpt-4o-mini-transcribe)で音声認識 - Qiita
 https://qiita.com/youtoy/items/0bf21c4aa9b88c0da06e

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?