Skip to content

Commit

Permalink
Merge pull request #60 from AssemblyAI/E07417BDFEA3614F5967B1520F8B2F61
Browse files Browse the repository at this point in the history
Sync from internal repo (2024/06/21)
  • Loading branch information
Swimburger committed Jun 21, 2024
2 parents 66b6fd7 + 8c6cec1 commit 19a807c
Show file tree
Hide file tree
Showing 10 changed files with 957 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"rules": {
"tsdoc/syntax": "warn"
},
"ignorePatterns": ["/*.js", "/*.ts", "dist", "node_modules"],
"ignorePatterns": ["/*.js", "/*.ts", "samples", "dist", "node_modules"],
"overrides": [
{
"files": ["tests/**/*"],
Expand Down
1 change: 1 addition & 0 deletions samples/streaming-stt-from-mic/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ASSEMBLYAI_API_KEY=[YOUR_ASSEMBLYAI_API_KEY]
30 changes: 30 additions & 0 deletions samples/streaming-stt-from-mic/README.md
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.
47 changes: 47 additions & 0 deletions samples/streaming-stt-from-mic/index.js
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();
});
61 changes: 61 additions & 0 deletions samples/streaming-stt-from-mic/index.ts
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();
});
Loading

0 comments on commit 19a807c

Please sign in to comment.