Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,29 @@ import assemblyai as aai
aai.settings.api_key = "<YOUR_API_KEY>"

def on_open(session_opened: aai.RealtimeSessionOpened):
print("Session ID:", session_opened.session_id)
print("Session ID:", session_opened.session_id)

def on_data(transcript: aai.RealtimeTranscript):
if not transcript.text:
return
if not transcript.text:
return

if isinstance(transcript, aai.RealtimeFinalTranscript):
print(transcript.text, end="\r\n")
else:
print(transcript.text, end="\r")

def on_error(error: aai.RealtimeError):
print("An error occurred:", error)
print("An error occurred:", error)

def on_close():
print("Closing Session")
print("Closing Session")

transcriber = aai.RealtimeTranscriber(
sample_rate=16_000,
on_data=on_data,
on_error=on_error,
on_open=on_open,
on_close=on_close,
sample_rate=16_000,
on_data=on_data,
on_error=on_error,
on_open=on_open,
on_close=on_close,
)

transcriber.connect()
Expand All @@ -61,7 +61,6 @@ microphone_stream = aai.extras.MicrophoneStream(sample_rate=16_000)
transcriber.stream(microphone_stream)

transcriber.close()

````

</Tab>
Expand Down Expand Up @@ -302,10 +301,10 @@ def on_open(session_opened: aai.RealtimeSessionOpened):
print("Session ID:", session_opened.session_id)

def on_error(error: aai.RealtimeError):
print("An error occurred:", error)
print("An error occurred:", error)

def on_close():
print("Closing Session")
print("Closing Session")

````

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,69 +32,66 @@
import { SoxRecording } from './sox.js'

const run = async () => {
const client = new AssemblyAI({
apiKey: '<YOUR_API_KEY>'
})
const client = new AssemblyAI({
apiKey: '<YOUR_API_KEY>'
})

const SAMPLE_RATE = 16_000
const SAMPLE_RATE = 16_000

const transcriber = client.realtime.transcriber({
sampleRate: SAMPLE_RATE
})
const transcriber = client.realtime.transcriber({

Check warning on line 41 in fern/pages/01-getting-started/transcribe-streaming-audio-from-a-microphone/typescript.mdx

View workflow job for this annotation

GitHub Actions / lint

[vale] reported by reviewdog 🐶 [AssemblyAI.WordList] Use 'real-time' instead of 'realtime'. Raw Output: {"message": "[AssemblyAI.WordList] Use 'real-time' instead of 'realtime'.", "location": {"path": "fern/pages/01-getting-started/transcribe-streaming-audio-from-a-microphone/typescript.mdx", "range": {"start": {"line": 41, "column": 30}}}, "severity": "WARNING"}
sampleRate: SAMPLE_RATE
})

transcriber.on('open', ({ sessionId }) => {
console.log(`Session opened with ID: ${sessionId}`)
})
transcriber.on('open', ({ sessionId }) => {
console.log(`Session opened with ID: ${sessionId}`)
})

transcriber.on('error', (error: Error) => {
console.error('Error:', error)
})
transcriber.on('error', (error: Error) => {
console.error('Error:', error)
})

transcriber.on('close', (code: number, reason: string) =>
console.log('Session closed:', code, reason)
)
transcriber.on('close', (code: number, reason: string) => {
console.log('Session closed:', code, reason)
})

transcriber.on('transcript', (transcript: RealtimeTranscript) => {
if (!transcript.text) {
return
}
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('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
})
console.log('Starting recording')
const recording = new SoxRecording({
channels: 1,
sampleRate: SAMPLE_RATE,
audioType: 'wav' // Linear PCM
})

recording.stream().pipeTo(transcriber.stream())
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()
// Stop recording and close connection using Ctrl-C.
process.on('SIGINT', async () => {
console.log()
console.log('Stopping recording')
recording.stop()

console.log('Closing real-time transcript connection')
await transcriber.close()

process.exit()

})
})
}

run()

````
</Tab>

Expand Down
Loading