-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from AssemblyAI/E07417BDFEA3614F5967B1520F8B2F61
Sync from internal repo (2024/06/21)
- Loading branch information
Showing
10 changed files
with
957 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ASSEMBLYAI_API_KEY=[YOUR_ASSEMBLYAI_API_KEY] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Transcribe streaming audio from a microphone in TypeScript | ||
|
||
This sample lets you transcribe audio from your microphone in real time using AssemblyAI Streaming Speech-to-Text. | ||
For step-by-step instructions on how to build this sample yourself, see [Transcribe streaming audio from a microphone in TypeScript](https://www.assemblyai.com/docs/getting-started/transcribe-streaming-audio-from-a-microphone/typescript). | ||
|
||
To run the sample, you'll need the following: | ||
|
||
- [Node.js](https://nodejs.org/) | ||
- [SoX](https://sourceforge.net/projects/sox/) | ||
- An AssemblyAI account with a credit card set up | ||
|
||
Install the dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
Configure the `ASSEMBLYAI_API_KEY` environment variable in your shell, or create a `.env` file with the following contents and replace `[YOUR_ASSEMBLYAI_API_KEY]` with your API key: | ||
|
||
```plaintext | ||
ASSEMBLYAI_API_KEY=[YOUR_ASSEMBLYAI_API_KEY] | ||
``` | ||
|
||
Run the sample: | ||
|
||
```bash | ||
npm run start | ||
``` | ||
|
||
Credits: `sox.ts` is adapted from the [node-record-lpcm16](https://github.com/gillesdemey/node-record-lpcm16) project by Gilles De Mey. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import "dotenv/config"; | ||
import { AssemblyAI } from "assemblyai"; | ||
import { SoxRecording } from "./sox.js"; | ||
const SAMPLE_RATE = 16000; | ||
const client = new AssemblyAI({ | ||
apiKey: process.env.ASSEMBLYAI_API_KEY, | ||
}); | ||
const transcriber = client.realtime.transcriber({ | ||
sampleRate: SAMPLE_RATE, | ||
}); | ||
transcriber.on("open", ({ sessionId }) => { | ||
console.log(`Session opened with ID: ${sessionId}`); | ||
}); | ||
transcriber.on("error", (error) => { | ||
console.error("Error:", error); | ||
}); | ||
transcriber.on("close", (code, reason) => | ||
console.log("Session closed:", code, reason), | ||
); | ||
transcriber.on("transcript", (transcript) => { | ||
if (!transcript.text) { | ||
return; | ||
} | ||
if (transcript.message_type === "PartialTranscript") { | ||
console.log("Partial:", transcript.text); | ||
} else { | ||
console.log("Final:", transcript.text); | ||
} | ||
}); | ||
console.log("Connecting to real-time transcript service"); | ||
await transcriber.connect(); | ||
console.log("Starting recording"); | ||
const recording = new SoxRecording({ | ||
channels: 1, | ||
sampleRate: SAMPLE_RATE, | ||
audioType: "wav", // Linear PCM | ||
}); | ||
recording.stream().pipeTo(transcriber.stream()); | ||
// Stop recording and close connection using Ctrl-C. | ||
process.on("SIGINT", async function () { | ||
console.log(); | ||
console.log("Stopping recording"); | ||
recording.stop(); | ||
console.log("Closing real-time transcript connection"); | ||
await transcriber.close(); | ||
process.exit(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import "dotenv/config"; | ||
import { AssemblyAI, RealtimeTranscript } from "assemblyai"; | ||
import { SoxRecording } from "./sox.js"; | ||
|
||
const SAMPLE_RATE = 16_000; | ||
|
||
const client = new AssemblyAI({ | ||
apiKey: process.env.ASSEMBLYAI_API_KEY!, | ||
}); | ||
|
||
const transcriber = client.realtime.transcriber({ | ||
sampleRate: SAMPLE_RATE, | ||
}); | ||
|
||
transcriber.on("open", ({ sessionId }) => { | ||
console.log(`Session opened with ID: ${sessionId}`); | ||
}); | ||
|
||
transcriber.on("error", (error: Error) => { | ||
console.error("Error:", error); | ||
}); | ||
|
||
transcriber.on("close", (code: number, reason: string) => | ||
console.log("Session closed:", code, reason), | ||
); | ||
|
||
transcriber.on("transcript", (transcript: RealtimeTranscript) => { | ||
if (!transcript.text) { | ||
return; | ||
} | ||
|
||
if (transcript.message_type === "PartialTranscript") { | ||
console.log("Partial:", transcript.text); | ||
} else { | ||
console.log("Final:", transcript.text); | ||
} | ||
}); | ||
|
||
console.log("Connecting to real-time transcript service"); | ||
await transcriber.connect(); | ||
|
||
console.log("Starting recording"); | ||
const recording = new SoxRecording({ | ||
channels: 1, | ||
sampleRate: SAMPLE_RATE, | ||
audioType: "wav", // Linear PCM | ||
}); | ||
|
||
recording.stream().pipeTo(transcriber.stream()); | ||
|
||
// Stop recording and close connection using Ctrl-C. | ||
process.on("SIGINT", async function () { | ||
console.log(); | ||
console.log("Stopping recording"); | ||
recording.stop(); | ||
|
||
console.log("Closing real-time transcript connection"); | ||
await transcriber.close(); | ||
|
||
process.exit(); | ||
}); |
Oops, something went wrong.