-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
187 additions
and
57 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/protobuf.min.js"></script> | ||
<title>WebSocket Audio Stream</title> | ||
</head> | ||
|
||
|
@@ -14,65 +15,51 @@ <h1>WebSocket Audio Stream</h1> | |
<script> | ||
let audioContext; | ||
let microphoneStream; | ||
let mediaRecorder; | ||
let refresher; | ||
let audioCtx; | ||
|
||
|
||
function startAudio() { | ||
async function startAudio() { | ||
audioCtx = new (window.AudioContext || window.webkitAudioContext)(); | ||
|
||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { | ||
alert('getUserMedia is not supported in your browser.'); | ||
return; | ||
} | ||
|
||
navigator.mediaDevices.getUserMedia({ audio: true }) | ||
.then(function (stream) { | ||
console.log("creating audio context") | ||
audioContext = new AudioContext(options = { sampleRate: 16000 }); | ||
microphoneStream = audioContext.createMediaStreamSource(stream); | ||
mediaRecorder = new MediaRecorder(stream); | ||
const captureNode = audioContext.createScriptProcessor(8192, 1, 1); | ||
captureNode.onaudioprocess = function (e) { | ||
console.log("on audio process") | ||
debugger; | ||
var left = e.inputBuffer.getChannelData(0); | ||
ws.send(left.buffer); | ||
}; | ||
/* | ||
mediaRecorder.ondataavailable = function (event) { | ||
console.log("ondataavailable") | ||
if (event.data.size > 0 && ws && ws.readyState === ws.OPEN) { | ||
console.log("Sending audio blob:", event.data.size, 'bytes') | ||
debugger | ||
ws.send(event.data); | ||
} | ||
}; | ||
*/ | ||
mediaRecorder.start(); | ||
|
||
refresher = setInterval(function () { | ||
console.log("sending audio") | ||
mediaRecorder.requestData(); | ||
}, 1000); | ||
|
||
}) | ||
.catch(function (error) { | ||
console.error('Error accessing microphone:', error); | ||
}); | ||
// Get the microphone stream. | ||
microphoneStream = await navigator.mediaDevices.getUserMedia({ audio: true }); | ||
|
||
// Create an AudioContext. | ||
const context = new AudioContext(); | ||
|
||
// Create a ScriptProcessorNode. | ||
const scriptProcessor = context.createScriptProcessor(8192, 1, 1); | ||
|
||
// Connect the microphone stream to the ScriptProcessorNode. | ||
const source = context.createMediaStreamSource(microphoneStream); | ||
source.connect(scriptProcessor); | ||
|
||
// Connect the ScriptProcessorNode to the destination. | ||
scriptProcessor.connect(context.destination); | ||
|
||
scriptProcessor.onaudioprocess = (event) => { | ||
const rawLeftChannelData = event.inputBuffer.getChannelData(0); | ||
scaledToInt = [] | ||
for (var i = 0; i < rawLeftChannelData.length; i++) { | ||
// Convert each item from -1.0-1.0 to a 16-bit signed integer | ||
scaledToInt[i] = (rawLeftChannelData[i] * 32767 + 32767) % 65535; | ||
} | ||
ws.send(scaledToInt); | ||
}; | ||
|
||
|
||
initWebSocket(); | ||
} | ||
|
||
function stopAudio() { | ||
if (mediaRecorder && mediaRecorder.state === 'recording') { | ||
clearInterval(refresher); | ||
mediaRecorder.stop(); | ||
microphoneStream.disconnect(); | ||
ws.close(); | ||
ws = undefined; | ||
} | ||
microphoneStream.disconnect(); | ||
ws.close(); | ||
ws = undefined; | ||
} | ||
|
||
function initWebSocket() { | ||
|
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
Binary file not shown.
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,25 @@ | ||
syntax = "proto3"; | ||
|
||
package dailyai_proto; | ||
|
||
message TextFrame { | ||
string text = 1; | ||
} | ||
|
||
message AudioFrame { | ||
bytes audio = 1; | ||
} | ||
|
||
message TranscriptionFrame { | ||
string text = 1; | ||
string participant_id = 2; | ||
string timestamp = 3; | ||
} | ||
|
||
message Frame { | ||
oneof frame { | ||
TextFrame text = 1; | ||
AudioFrame audio = 2; | ||
TranscriptionFrame transcription = 3; | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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