### Install SenseVoice API Dependencies Source: https://github.com/0x5446/api4sensevoice/blob/main/README.md Steps to clone the repository and install necessary dependencies using conda and pip. This includes setting up a Python environment and installing core libraries. ```bash git clone https://github.com/0x5446/api4sensevoice.git cd api4sensevoice conda create -n api4sensevoice python=3.10 conda activate api4sensevoice conda install -c conda-forge ffmpeg pip install -r requirements.txt ``` -------------------------------- ### Run Single Sentence Recognition API Server Source: https://github.com/0x5446/api4sensevoice/blob/main/README.md Python code to run the FastAPI application for single sentence recognition. It allows configuring the port, SSL certificate, and key file via command-line arguments. ```python import argparse import uvicorn # Assuming 'app' is your FastAPI instance # from your_main_app_file import app if __name__ == "__main__": parser = argparse.ArgumentParser(description="Run the FastAPI app with a specified port.") parser.add_argument('--port', type=int, default=7000, help='Port number to run the FastAPI app on.') parser.add_argument('--certfile', type=str, default='path_to_your_certfile', help='SSL certificate file') parser.add_argument('--keyfile', type=str, default='path_to_your_keyfile', help='SSL key file') args = parser.parse_args() uvicorn.run(app, host="0.0.0.0", port=args.port, ssl_certfile=args.certfile, ssl_keyfile=args.keyfile) ``` -------------------------------- ### Run Streaming Real-time Recognition WebSocket Server Source: https://github.com/0x5446/api4sensevoice/blob/main/README.md Python code to run the FastAPI application for streaming real-time recognition via WebSockets. It allows configuring the port, SSL certificate, and key file. ```python import argparse import uvicorn # Assuming 'app' is your FastAPI instance # from your_main_app_file import app if __name__ == "__main__": parser = argparse.ArgumentParser(description="Run the FastAPI app with a specified port.") parser.add_argument('--port', type=int, default=27000, help='Port number to run the FastAPI app on.') parser.add_argument('--certfile', type=str, default='path_to_your_certfile', help='SSL certificate file') parser.add_argument('--keyfile', type=str, default='path_to_your_keyfile', help='SSL key file') args = parser.parse_args() uvicorn.run(app, host="0.0.0.0", port=args.port, ssl_certfile=args.certfile, ssl_keyfile=args.keyfile) ``` -------------------------------- ### Test WebSocket Server with Client Page Source: https://github.com/0x5446/api4sensevoice/blob/main/README.md JavaScript code snippet demonstrating how to connect to the streaming recognition WebSocket server. It shows how to configure the WebSocket URL, including enabling speaker verification. ```javascript const wsUrl = `wss://your_wss_server_address/ws/transcribe${sv ? '?sv=1' : ''}`; const ws = new WebSocket(wsUrl); ws.onopen = () => { console.log('WebSocket connection opened'); // Send audio data here }; ws.onmessage = (event) => { console.log('Message from server:', JSON.parse(event.data)); }; ws.onerror = (error) => { console.error('WebSocket error:', error); }; ws.onclose = () => { console.log('WebSocket connection closed'); }; ``` -------------------------------- ### Configure Speaker Verification for WebSocket Server Source: https://github.com/0x5446/api4sensevoice/blob/main/README.md Python snippet to configure speaker verification by specifying paths to reference audio files. These files are used to match against incoming audio streams. ```python # Modify this list to include your speaker voice audio files # Ensure files are 16000 sampling rate, single channel, 16-bit width, WAV format reg_spks_files = [ "speaker/speaker1_a_cn_16k.wav" ] ``` -------------------------------- ### SenseVoice Transcribe Audio API Source: https://github.com/0x5446/api4sensevoice/blob/main/README.md Details for the POST /transcribe endpoint used for transcribing audio files. It specifies request body format, parameters, and response schema. ```APIDOC Path: /transcribe Method: POST Summary: Transcribe audio Request Body: - Type: multipart/form-data - Parameters: - file (required): The audio file to transcribe (e.g., WAV, MP3) Response: - 200 Success: - Content Type: application/json - Schema: - code (integer): State number (0 for success) - msg (string): Status message (e.g., "Success") - data (object): Transcription result object Request Example: ```bash curl -X 'POST' \ 'http://yourapiaddress/transcribe' \ -H 'accept: application/json' \ -H 'Content-Type: multipart/form-data' \ -F 'file=@path_to_your_audio_file' ``` Response Example (200 Success): ```json { "code": 0, "msg": "Success", "data": { // Transcription result details here } } ``` ``` -------------------------------- ### JavaScript: WebSocket Audio Transcription Client Source: https://github.com/0x5446/api4sensevoice/blob/main/client_wss.html Handles user interface interactions for starting/stopping recording, managing WebSocket connections, and displaying transcription results. It connects to a WebSocket server for real-time audio processing and transcription. ```javascript var recordButton = document.getElementById('recordButton'); var transcriptionResult = document.getElementById('transcriptionResult'); navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia; var ws = null; var record = null; var timeInte = null; var isRecording = false; recordButton.onclick = function() { if (!isRecording) { startRecording(); } else { stopRecording(); } }; function startRecording() { console.log('Start Recording'); var speakerVerificationCheckbox = document.getElementById('speakerVerification'); var sv = speakerVerificationCheckbox.checked ? 1 : 0; var lang = document.getElementById("lang").value; // Construct the query parameters var queryParams = []; if (lang) { queryParams.push(`lang=${lang}`); } if (sv) { queryParams.push('sv=1'); } var queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : ''; ws = new WebSocket(`wss://your_wss_server_address/ws/transcribe${queryString}`); ws.binaryType = 'arraybuffer'; ws.onopen = function(event) { console.log('WebSocket connection established'); record.start(); timeInte = setInterval(function() { if(ws.readyState === 1) { var audioBlob = record.getBlob(); console.log('Blob size: ', audioBlob.size); // Read the Blob content for debugging var reader = new FileReader(); reader.onloadend = function() { console.log('Blob content: ', new Uint8Array(reader.result)); ws.send(audioBlob); console.log('Sending audio data'); record.clear(); }; reader.readAsArrayBuffer(audioBlob); } }, 500); }; ws.onmessage = function(evt) { console.log('Received message: ' + evt.data); try { resJson = JSON.parse(evt.data) if (resJson.code == 0) { var jsonResponse = JSON.stringify(resJson, null, 4); transcriptionResult.textContent += "\n" + (resJson.data || 'No speech recognized'); } } catch (e) { console.error('Failed to parse response data', e); transcriptionResult.textContent += "\n" + evt.data; } }; ws.onclose = function() { console.log('WebSocket connection closed'); }; ws.onerror = function(error) { console.error('WebSocket error: ' + error); }; recordButton.textContent = "Stop Recording"; recordButton.classList.add("recording"); isRecording = true; } function stopRecording() { console.log('Stop Recording'); if (ws) { ws.close(); record.stop(); clearInterval(timeInte); } recordButton.textContent = "Start Recording"; recordButton.classList.remove("recording"); isRecording = false; } function init(rec) { record = rec; } if (!navigator.getUserMedia) { alert('Your browser does not support audio input'); } else { navigator.getUserMedia( { audio: true }, function(mediaStream) { init(new Recorder(mediaStream)); }, function(error) { console.log(error); } ); } ``` -------------------------------- ### SenseVoice Streaming Recognition WebSocket API Source: https://github.com/0x5446/api4sensevoice/blob/main/README.md Details for the /ws/transcribe WebSocket endpoint. It covers query parameters for enabling speaker verification and the format of upstream (audio) and downstream (JSON) data. ```APIDOC Endpoint: /ws/transcribe Query Parameters: - sv (optional): Enables speaker verification. Default value is 0 (disabled). - Example: ?sv=1 to enable. Upstream Data (Client to Server): - Format: PCM binary - Specifications: - Channel number: 1 - Sample rate: 16000 Hz - Sample depth: 16-bit Downstream Data (Server to Client): - Format: JSON String - Schema: - code (integer): State number (0 for success) - info (string): Meta information or status message - data (object): Response object containing transcription results ``` -------------------------------- ### CSS: Transcription Result Styling Source: https://github.com/0x5446/api4sensevoice/blob/main/client_wss.html Provides styling for the transcription output area and the recording button. It ensures readability of text and visual feedback for the recording state. ```css #transcriptionResult { white-space: pre-wrap; /* Preserve whitespace and line breaks */ background-color: #f5f5f5; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace; } #recordButton.recording { background-color: red; color: white; } ``` -------------------------------- ### Audio Input Processing Handler Source: https://github.com/0x5446/api4sensevoice/blob/main/client_wss.html Handles the 'onaudioprocess' event from an audio recorder. It downsamples the input audio buffer and feeds the processed data into an audio data handler. This snippet demonstrates real-time audio stream processing. ```javascript recorder.onaudioprocess = function(e) { console.log('onaudioprocess called'); var resampledData = downsampleBuffer(e.inputBuffer.getChannelData(0), inputSampleRate, outputSampleRate); audioData.input(resampledData); }; ``` -------------------------------- ### JavaScript: Audio Recorder Class Source: https://github.com/0x5446/api4sensevoice/blob/main/client_wss.html A custom JavaScript class for recording audio from a MediaStream using the Web Audio API. It handles audio processing, downsampling, and encoding audio data into PCM format for transmission. ```javascript var Recorder = function(stream) { var sampleBits = 16; // Sample bits var inputSampleRate = 48000; // Input sample rate var outputSampleRate = 16000; // Output sample rate var channelCount = 1; // Single channel var context = new AudioContext(); var audioInput = context.createMediaStreamSource(stream); var recorder = context.createScriptProcessor(4096, channelCount, channelCount); var audioData = { size: 0, buffer: [], inputSampleRate: inputSampleRate, inputSampleBits: sampleBits, clear: function() { this.buffer = []; this.size = 0; }, input: function(data) { this.buffer.push(new Float32Array(data)); this.size += data.length; }, encodePCM: function() { var bytes = new Float32Array(this.size); var offset = 0; for (var i = 0; i < this.buffer.length; i++) { bytes.set(this.buffer[i], offset); offset += this.buffer[i].length; } var dataLength = bytes.length * (sampleBits / 8); var buffer = new ArrayBuffer(dataLength); var data = new DataView(buffer); offset = 0; for (var i = 0; i < bytes.length; i++, offset += 2) { var s = Math.max(-1, Math.min(1, bytes[i])); data.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true); } return new Blob([data], { type: 'audio/pcm' }); } }; this.start = function() { audioInput.connect(recorder); recorder.connect(context.destination); }; this.stop = function() { recorder.disconnect(); }; this.getBlob = function() { return audioData.encodePCM(); }; this.clear = function() { audioData.clear(); }; function downsampleBuffer(buffer, inputSampleRate, outputSampleRate) { if (outputSampleRate === inputSampleRate) { return buffer; } var sampleRateRatio = inputSampleRate / outputSampleRate; var newLength = Math.round(buffer.length / sampleRateRatio); var result = new Float32Array(newLength); var offsetResult = 0; var offsetBuffer = 0; while (offsetResult < result.length) { var nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio); var ``` -------------------------------- ### Buffer Downsampling and Averaging Source: https://github.com/0x5446/api4sensevoice/blob/main/client_wss.html A JavaScript function designed to downsample an audio buffer by averaging chunks of samples. It iterates through a buffer, calculates the average of a segment, and stores it in a result array. This is useful for reducing data volume while preserving signal characteristics. ```javascript function downsampleBuffer(buffer, inSampleRate, outSampleRate) { if (outSampleRate > inSampleRate) { throw "output sample rate must be less than input sample rate"; } var sampleRateRatio = inSampleRate / outSampleRate; var result = []; var offsetResult = 0; var offsetBuffer = 0; var nextOffsetBuffer = Math.floor(sampleRateRatio); while (offsetBuffer < buffer.length) { var accum = 0, count = 0; for (var i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) { accum += buffer[i]; count++; } result[offsetResult] = accum / count; offsetResult++; offsetBuffer = nextOffsetBuffer; nextOffsetBuffer = Math.floor(sampleRateRatio * (offsetResult + 1)); } return result; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.