New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

phonic

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

phonic

Phonic Node.js SDK

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
decreased by-35.71%
Maintainers
0
Weekly downloads
 
Created
Source

Phonic Node.js SDK

Node.js library for the Phonic API.

  • Installation
  • Setup
  • Usage

Installation

npm i phonic

Setup

Grab an API key from Phonic settings and pass it to the Phonic constructor.

import { Phonic } from "phonic";

const phonic = new Phonic("ph_...");

Usage

Get voices

const { data, error } = await phonic.voices.list();

if (error === null) {
  console.log(data.voices);
}

Get voice by id

const { data, error } = await phonic.voices.get("australian-man");

if (error === null) {
  console.log(data.voice);
}

Text-to-speech via WebSocket

Open a WebSocket connection:

const { data, error } = await phonic.tts.websocket({
  model: "shasta",
  output_format: "mulaw_8000",
  voice_id: "australian-man",
});

if (error !== null) {
  throw new Error(error.message);
}

// Here we know that the WebSocket connection is open.
const { phonicWebSocket } = data;

Process audio chunks that Phonic sends back to you, by sending them to Twilio, for example:

phonicWebSocket.onMessage((message) => {
  if (message.type === "audio_chunk") {
    ws.send(
      JSON.stringify({
        event: "media",
        streamSid: "...",
        media: {
          payload: message.audio,
        },
      }),
    );
  }
});

Send text chunks to Phonic for audio generation as you receive them from LLM:

const stream = await openai.chat.completions.create(...);

for await (const chunk of stream) {
  const text = chunk.choices[0]?.delta?.content || "";

  if (text) {
    phonicWebSocket.generate({ text });
  }
}

Tell Phonic to finish generating audio for all text chunks you've sent:

phonicWebSocket.flush();

You can also tell Phonic to stop sending audio chunks back, e.g. if the user interrupts the conversation:

phonicWebSocket.stop();

To close the WebSocket connection:

phonicWebSocket.close();

To know when the last audio chunk has been received:

phonicWebSocket.onMessage((message) => {
  if (message.type === "flushed") {
    console.log("Last audio chunk received");
  }
});

You can also listen for close and error events:

phonicWebSocket.onClose((event) => {
  console.log(
    `Phonic WebSocket closed with code ${event.code} and reason "${event.reason}"`,
  );
});

phonicWebSocket.onError((event) => {
  console.log(`Error from Phonic WebSocket: ${event.message}`);
});

Release a new version to npm

  1. bunx changeset
  2. git add .
  3. git commit -m "Add changeset"
  4. git push

Git action will run and create a PR. Once this PR is merged, the new version will be released to npm.

License

MIT

Keywords

FAQs

Package last updated on 30 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc