|
4 | 4 | <script src="../../dist/umd/deepgram.js"></script> |
5 | 5 | <script> |
6 | 6 | const { createClient } = deepgram; |
7 | | - const _deepgram = createClient("deepgram-api-key", { |
8 | | - global: { |
9 | | - fetch: { |
10 | | - options: { |
11 | | - url: "https://api.mock.deepgram.com", |
12 | | - }, |
13 | | - }, |
14 | | - }, |
15 | | - }); |
| 7 | + const _deepgram = createClient("deepgram-api-key"); |
16 | 8 |
|
17 | 9 | console.log("Deepgram Instance: ", _deepgram); |
18 | 10 |
|
19 | 11 | (async () => { |
20 | | - const { result, error } = await _deepgram.manage.getProjects(); |
| 12 | + const url = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"; |
21 | 13 |
|
22 | | - console.log(result, error); |
| 14 | + // We will collect the is_final=true messages here so we can use them when the person finishes speaking |
| 15 | + let is_finals = []; |
| 16 | + |
| 17 | + const connection = _deepgram.listen.live({ |
| 18 | + model: "nova-2", |
| 19 | + language: "en-US", |
| 20 | + // Apply smart formatting to the output |
| 21 | + smart_format: true, |
| 22 | + // To get UtteranceEnd, the following must be set: |
| 23 | + interim_results: true, |
| 24 | + utterance_end_ms: 1000, |
| 25 | + vad_events: true, |
| 26 | + // Time in milliseconds of silence to wait for before finalizing speech |
| 27 | + endpointing: 300, |
| 28 | + }); |
| 29 | + |
| 30 | + connection.on("open", () => { |
| 31 | + connection.on("close", () => { |
| 32 | + console.log("Connection closed."); |
| 33 | + }); |
| 34 | + |
| 35 | + connection.on("Metadata", (data) => { |
| 36 | + console.log(`Deepgram Metadata: ${data}`); |
| 37 | + }); |
| 38 | + |
| 39 | + connection.on("Results", (data) => { |
| 40 | + const sentence = data.channel.alternatives[0].transcript; |
| 41 | + |
| 42 | + // Ignore empty transcripts |
| 43 | + if (sentence.length == 0) { |
| 44 | + return; |
| 45 | + } |
| 46 | + if (data.is_final) { |
| 47 | + // We need to collect these and concatenate them together when we get a speech_final=true |
| 48 | + // See docs: https://developers.deepgram.com/docs/understand-endpointing-interim-results |
| 49 | + is_finals.push(sentence); |
| 50 | + |
| 51 | + // Speech final means we have detected sufficent silence to consider this end of speech |
| 52 | + // Speech final is the lowest latency result as it triggers as soon an the endpointing value has triggered |
| 53 | + if (data.speech_final) { |
| 54 | + const utterance = is_finals.join(" "); |
| 55 | + console.log(`Speech Final: ${utterance}`); |
| 56 | + is_finals = []; |
| 57 | + } else { |
| 58 | + // These are useful if you need real time captioning and update what the Interim Results produced |
| 59 | + console.log(`Is Final: ${sentence}`); |
| 60 | + } |
| 61 | + } else { |
| 62 | + // These are useful if you need real time captioning of what is being spoken |
| 63 | + console.log(`Interim Results: ${sentence}`); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + connection.on("UtteranceEnd", (data) => { |
| 68 | + const utterance = is_finals.join(" "); |
| 69 | + console.log(`Deepgram UtteranceEnd: ${utterance}`); |
| 70 | + is_finals = []; |
| 71 | + }); |
| 72 | + |
| 73 | + connection.on("SpeechStarted", (data) => { |
| 74 | + // console.log("Deepgram SpeechStarted"); |
| 75 | + }); |
| 76 | + |
| 77 | + connection.on("error", (err) => { |
| 78 | + console.error(err); |
| 79 | + }); |
| 80 | + |
| 81 | + fetch(url) |
| 82 | + .then((response) => response.body) |
| 83 | + .then((body) => { |
| 84 | + const reader = body.getReader(); |
| 85 | + function read() { |
| 86 | + reader |
| 87 | + .read() |
| 88 | + .then(({ done, value }) => { |
| 89 | + if (done) { |
| 90 | + console.log("Stream complete"); |
| 91 | + return; |
| 92 | + } |
| 93 | + connection.send(value); |
| 94 | + read(); |
| 95 | + }) |
| 96 | + .catch((error) => { |
| 97 | + console.error("Stream read error:", error); |
| 98 | + }); |
| 99 | + } |
| 100 | + read(); |
| 101 | + }) |
| 102 | + .catch((error) => { |
| 103 | + console.error("Fetch error:", error); |
| 104 | + }); |
| 105 | + }); |
23 | 106 | })(); |
24 | 107 |
|
25 | 108 | // ... |
|
0 commit comments