Skip to main content
Skip table of contents

Speech Enhancement ‎

In this example, a POST request is sent to specify the enhancer to use. Upon receiving the response, the token is extracted and used to connect to the WebSocket. It starts with sending the audio to be processed and simultaneously, manages incoming packets by saving the processed audio data into a result file and logging errors.

C# Example
C#

using System.Text.Json;
using System.Net.WebSockets;
using System.Text;

const string rawAudioFile = @"C:\Users\vivoka\Music\coffee.wav";
const string resultRawAudioFile = @"C:\Users\vivoka\Music\coffee-result.raw";
const string requestUri = "http://localhost:39806/v1/speech-enhancement/enhance";
const string audioDataHeader = "data:audio/pcm;base64,";
var requestData = new { speech_enhancer = "enhancer-1" };

using (var client = new HttpClient())
{
    // Create result audio file
    using (File.Create(resultRawAudioFile)) { }

    var json = JsonSerializer.Serialize(requestData);
    var content = new StringContent(json, Encoding.UTF8, "application/json");
    var response = await client.PostAsync(requestUri, content);
    response.EnsureSuccessStatusCode();
    var responseBody = await response.Content.ReadAsStringAsync();

    // Step 2: Extract the WebSocket URL from the response
    var jsonResponse = JsonDocument.Parse(responseBody).RootElement;
    var token = jsonResponse.GetProperty("token").ToString();
    var webSocketUrl = $"ws://localhost:39806/v1/ws/{token}";

    // Step 3: Connect to the WebSocket and start sending/receiving audio data
    using (var webSocket = new ClientWebSocket())
    {
        await webSocket.ConnectAsync(new Uri(webSocketUrl), CancellationToken.None);
        var sending = Task.Run(() => SendAudioData(webSocket, rawAudioFile));
        var receiving = Task.Run(() => ReceiveMessages(webSocket));
        await Task.WhenAll(sending, receiving);
    }
}

async Task SendAudioData(ClientWebSocket webSocket, string audioFilePath)
{
    // Read audio data from a file and send it throw websocket
    using (var fs = File.OpenRead(audioFilePath))
    {
        int bytesRead;
        var buffer = new byte[1024];
        while ((bytesRead = await fs.ReadAsync(buffer, 0, buffer.Length)) > 0)
        {
            var base64 = Convert.ToBase64String(new ArraySegment<byte>(buffer, 0, bytesRead));
            var audioChunk = new
            {
                data = $"{audioDataHeader}{base64}",
                last = fs.Position == fs.Length
            };
            var json = JsonSerializer.Serialize(audioChunk);
            var bytes = Encoding.UTF8.GetBytes(json);
            await webSocket.SendAsync(new ArraySegment<byte>(bytes), WebSocketMessageType.Text, true, CancellationToken.None);
        }
    }
}

async Task ReceiveMessages(ClientWebSocket webSocket)
{
    var message = "";
    var buffer = new byte[1024];
    while (webSocket.State == WebSocketState.Open)
    {
        var packet = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
        if (packet.MessageType == WebSocketMessageType.Close)
        {
            await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
            break;
        }
        else
        {
            message = (message.Length > 0 ? message : "") + Encoding.UTF8.GetString(buffer, 0, packet.Count);
            if (packet.EndOfMessage)
            {
                HandleMessage(message);
                message = "";
            }
        }
    }
}

void HandleMessage(string message)
{
    try
    {
        using (var document = JsonDocument.Parse(message))
        {
            JsonElement root = document.RootElement;
            if (root.TryGetProperty("data", out JsonElement dataElement))
            {
                var data = dataElement.ToString();
                if (data.StartsWith(audioDataHeader))
                {
                    // Append result audio file
                    using (var fileStream = new FileStream(resultRawAudioFile, FileMode.Append, FileAccess.Write, FileShare.None))
                        new BinaryWriter(fileStream).Write(Convert.FromBase64String(data.Substring(audioDataHeader.Length)));
                }
            }
            else if (root.TryGetProperty("error", out JsonElement errorElement))
                Console.WriteLine($"Error received: {JsonSerializer.Serialize(errorElement)}");
            else
                Console.WriteLine("Unknown message type received.");
        }
    }
    catch (JsonException ex)
    {
        Console.WriteLine($"Failed to parse message: {ex.Message}");
    }
}
Python Example
PY
import asyncio
import base64
import json
import os
import requests
import websockets

raw_audio_file = r"C:\Users\vivoka\Music\coffee.raw"
result_raw_audio_file = r"C:\Users\vivoka\Music\coffee-result.raw"
request_uri = "http://localhost:39806/v1/speech-enhancement/enhance"
audio_data_header = "data:audio/pcm;base64,"
request_data = { "speech_enhancer": "enhancer-1" }

async def main():
    create_result_audio_file()
    response = requests.post(request_uri, json=request_data)
    response.raise_for_status()
    response_body = response.json()
    token = response_body["token"]
    web_socket_url = f"ws://localhost:39806/v1/ws/{token}"

    async with websockets.connect(web_socket_url) as websocket:
        sending = asyncio.create_task(send_audio_data(websocket, raw_audio_file))
        receiving = asyncio.create_task(handle_message(websocket))
        await asyncio.gather(sending, receiving)

def create_result_audio_file():
    open(result_raw_audio_file, "wb").close()

async def send_audio_data(websocket, audio_file_path):
    fileSize = os.path.getsize(audio_file_path)
    with open(audio_file_path, "rb") as f:
        while True:
            chunk = f.read(1024)
            if not chunk:
                break
            base64_chunk = base64.b64encode(chunk).decode("utf-8")
            audio_chunk = {
                "data": f"{audio_data_header}{base64_chunk}",
                "last": f.tell() == fileSize
            }
            await websocket.send(json.dumps(audio_chunk))
            await asyncio.sleep(0.01)  # To prevent overwhelming the server

async def handle_message(websocket):
    try:
        async for message in websocket:
            body = json.loads(message)
            if "data" in body:
                if body["data"].startswith(audio_data_header):
                    with open(result_raw_audio_file, "ab") as f:
                        f.write(base64.b64decode(body["data"][len(audio_data_header):]))
            elif "error" in body:
                print(f"Error received: {json.dumps(body['error'])}")
            else:
                print("Unknown message type received.")
    except json.JSONDecodeError as ex:
        print(f"Failed to parse message: {ex}")

if __name__ == "__main__":
    asyncio.run(main())

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.