Skip to content

Client for the websocket server #113

Description

@funboarder13920

Hello,

I'm trying to use the websocket server for diarization.
I implemented a client as it is not provided. I get an output for the diarization but it is always speaker0.

Here is the js code for my client:

// Set up the start and stop buttons.
document.getElementById('start-button').onclick = start;
document.getElementById('stop-button').onclick = stop;

// Global variables to hold the audio stream and the WebSocket connection.
var stream;
var socket;

function b64encode(chunk) {
    // Convert the chunk array to a Float32Array
    const bytes = new Float32Array(chunk).buffer;

    // Encode the bytes as a base64 string
    let encoded = btoa(String.fromCharCode.apply(null, new Uint8Array(bytes)));

    // Return the encoded string as a UTF-8 encoded string
    return decodeURIComponent(encoded);
}


function start() {
    // Disable the start button.
    document.getElementById('start-button').disabled = true;
    // Enable the stop button.
    document.getElementById('stop-button').disabled = false;

    // Get access to the microphone.
    navigator.mediaDevices.getUserMedia({ audio: true }).then(function (s) {
        stream = s;

        // Create a new WebSocket connection.
        socket = new WebSocket('ws://localhost:7007');

        // When the WebSocket connection is open, start sending the audio data.
        socket.onopen = function () {
            var audioContext = new AudioContext();
            var source = audioContext.createMediaStreamSource(stream);
            var processor = audioContext.createScriptProcessor(1024, 1, 1);
            source.connect(processor);
            processor.connect(audioContext.destination);

            processor.onaudioprocess = function (event) {
                var data = event.inputBuffer.getChannelData(0);
                // var dataString = String.fromCharCode.apply(null, new Uint16Array(data));
                console.log("sending")
                socket.send(b64encode(data));
            };
        };

        socket.onmessage = function (e) {
            console.log(e);
        }
    }).catch(function (error) {
        console.error('Error getting microphone input:', error);
    });
}

function stop() {
    // Disable the stop button.
    document.getElementById('stop-button').disabled = true;
    // Enable the start button.
    document.getElementById('start-button').disabled = false;

    // Close the WebSocket connection.
    socket.close();
    // Stop the audio stream.
    stream.getTracks().forEach(function (track) { track.stop(); });
}

Do you have the code for the client you used ? I would like to make sure that I got that right before looking anywhere else.

Best,

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfeatureNew feature or requestquestionFurther information is requested

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions