1- const { createClient } = require ( "../../dist/main/index" ) ;
1+ const { createClient, LiveTranscriptionEvents } = require ( "../../dist/main/index" ) ;
2+ const fetch = require ( "cross-fetch" ) ;
23
3- const transcribeUrl = async ( ) => {
4- const deepgram = createClient ( process . env . DEEPGRAM_API_KEY ) ;
4+ const demonstrateAuth = async ( ) => {
5+ // Note that the ENV value here is different than our other examples. The SDK will automatically grab `DEEPGRAM_API_KEY` if it exists,
6+ // so by using a different name we can ensure that the second client is properly consuming the authentication token instead.
7+ const deepgram = createClient ( { key : process . env . DIFFERENT_DEEPGRAM_API_KEY } ) ;
58 const { result : token , error : tokenError } = await deepgram . auth . grantToken ( ) ;
69 if ( tokenError ) {
710 throw tokenError ;
811 }
9- console . log ( "Token" , token ) ;
12+ console . log ( "Token" , token . access_token ) ;
1013
1114 console . log ( "Transcribing URL" , "https://dpgr.am/spacewalk.wav" ) ;
12- deepgram . token = token . access_token ;
13- const { result, error } = await deepgram . listen . prerecorded . transcribeUrl (
15+ const deepgramWithToken = createClient ( { accessToken : token . access_token } ) ;
16+ const { result, error } = await deepgramWithToken . listen . prerecorded . transcribeUrl (
1417 {
1518 url : "https://dpgr.am/spacewalk.wav" ,
1619 } ,
@@ -22,6 +25,86 @@ const transcribeUrl = async () => {
2225
2326 if ( error ) console . error ( error ) ;
2427 if ( ! error ) console . dir ( result , { depth : 5 } ) ;
28+
29+ console . log ( "Transcribing live" ) ;
30+
31+ const url = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service" ;
32+
33+ let is_finals = [ ] ;
34+
35+ const connection = deepgramWithToken . listen . live ( {
36+ model : "nova-3" ,
37+ language : "en-US" ,
38+ // Apply smart formatting to the output
39+ smart_format : true ,
40+ // To get UtteranceEnd, the following must be set:
41+ interim_results : true ,
42+ utterance_end_ms : 1000 ,
43+ vad_events : true ,
44+ // Time in milliseconds of silence to wait for before finalizing speech
45+ endpointing : 300 ,
46+ // Keyterm Prompting allows you improve Keyword Recall Rate (KRR) for important keyterms or phrases up to 90%.
47+ keyterm : [ "BBC" ] ,
48+ } ) ;
49+
50+ connection . on ( LiveTranscriptionEvents . Open , ( ) => {
51+ fetch ( url )
52+ . then ( ( r ) => r . body )
53+ . then ( ( res ) => {
54+ res . on ( "readable" , ( ) => {
55+ connection . send ( res . read ( ) ) ;
56+ } ) ;
57+ } ) ;
58+ } ) ;
59+ connection . on ( LiveTranscriptionEvents . Close , ( ) => {
60+ console . log ( "Connection closed." ) ;
61+ } ) ;
62+
63+ connection . on ( LiveTranscriptionEvents . Metadata , ( data ) => {
64+ console . log ( `Deepgram Metadata: ${ data } ` ) ;
65+ } ) ;
66+
67+ connection . on ( LiveTranscriptionEvents . Transcript , ( data ) => {
68+ const sentence = data . channel . alternatives [ 0 ] . transcript ;
69+
70+ // Ignore empty transcripts
71+ if ( sentence . length == 0 ) {
72+ return ;
73+ }
74+ if ( data . is_final ) {
75+ // We need to collect these and concatenate them together when we get a speech_final=true
76+ // See docs: https://developers.deepgram.com/docs/understand-endpointing-interim-results
77+ is_finals . push ( sentence ) ;
78+
79+ // Speech final means we have detected sufficent silence to consider this end of speech
80+ // Speech final is the lowest latency result as it triggers as soon an the endpointing value has triggered
81+ if ( data . speech_final ) {
82+ const utterance = is_finals . join ( " " ) ;
83+ console . log ( `Speech Final: ${ utterance } ` ) ;
84+ is_finals = [ ] ;
85+ } else {
86+ // These are useful if you need real time captioning and update what the Interim Results produced
87+ console . log ( `Is Final: ${ sentence } ` ) ;
88+ }
89+ } else {
90+ // These are useful if you need real time captioning of what is being spoken
91+ console . log ( `Interim Results: ${ sentence } ` ) ;
92+ }
93+ } ) ;
94+
95+ connection . on ( LiveTranscriptionEvents . UtteranceEnd , ( _data ) => {
96+ const utterance = is_finals . join ( " " ) ;
97+ console . log ( `Deepgram UtteranceEnd: ${ utterance } ` ) ;
98+ is_finals = [ ] ;
99+ } ) ;
100+
101+ connection . on ( LiveTranscriptionEvents . SpeechStarted , ( _data ) => {
102+ // console.log("Deepgram SpeechStarted");
103+ } ) ;
104+
105+ connection . on ( LiveTranscriptionEvents . Error , ( err ) => {
106+ console . error ( err ) ;
107+ } ) ;
25108} ;
26109
27- transcribeUrl ( ) ;
110+ demonstrateAuth ( ) ;
0 commit comments