Public Api.
Introduction
Speaqr provides real-time translation services through an api built on top of Socket.IO. Clients can connect, send audio data and receive processed results such as transcriptions, translations and synthesized speech.Speaqr is designed for conversations, where speed is crucial to deliver a quality experience. For this reason, the Speaqr api follows the "session on socket" philosophy.Once authenticated, the consumer maintains an open connection via socket until disconnection, handling most interactions through events, with a few exceptions.To use the api you need a valid api key. To obtain it, please contact us through the contracting form.Url
The url to access the api is:https://public.speaqr.ai/api
Postman
You can download the Postman collection and the environment to test the api methods.Remember to put in the environment your validapi key.
Download the collectionDownload the environmentMethods
The api has several batch methods. Their uses and configurations are detailed below.languages
Gets the list of languages supported by the SDK for transcription, translation and speech synthesis.Endpoint
GET https://public.speaqr.ai/api/languages
Headers
Example
const myHeaders = new Headers();
myHeaders.append("x-api-key", "cFVBACTRF6nPzqetBcA2bG"); //Your valid api key
const requestOptions = {
method: "GET",
headers: myHeaders
};
fetch("https://public.speaqr.ai/api/languages", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.error(error));
import requests
headers = {
"x-api-key": "cFVBACTRF6nPzqetBcA2bG" # Your valid api key
}
response = requests.get("https://public.speaqr.ai/api/languages", headers=headers)
if response.status_code == 200:
print("Languages available:", response.json())
else:
print("Error:", response.status_code, response.text)
import okhttp3.*;
public class Languages {
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://public.speaqr.ai/api/languages")
.header("x-api-key", "cFVBACTRF6nPzqetBcA2bG") //Your valid api key
.get()
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
System.out.println("Languages available: " + response.body().string());
} else {
System.out.println("Error: " + response.code());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Reply
batch/list-available-voices
Gets the list of available voices for speech synthesis (TTS) in a language and for an engine. Optionally you can filter the result by voice genre and/or voice engine.By this means it is possible to know if a language, voice and motor configuration can be realized, since voices are not available for all combinations.Endpoint
POST https://public.speaqr.ai/api/batch/list-available-voices
Headers
Parameters
BODY RAW
Example
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("x-api-key", "cFVBACTRF6nPzqetBcA2bG"); //Your valid api key
myHeaders.append("Cookie", "dev_language=es-es");
const raw = JSON.stringify({
"language": "en-gb",
"ttsEngine": "block",
"voiceGender": "M",
"voiceEngine": "generative"
});
const requestOptions = {
method: "POST",
headers: myHeaders,
body: raw
};
fetch("https://public.speaqr.ai/api/batch/list-available-voices", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.error(error));
import requests
data = {
"language": "es-es",
"ttsEngine": "block"
}
headers = {
"Content-Type": "application/json",
"x-api-key": "cFVBACTRF6nPzqetBcA2bG" # Your valid api key
}
response = requests.post("https://public.speaqr.ai/api/batch/list-available-voices", json=data, headers=headers)
if response.status_code == 200:
print("Voices available:", response.json())
else:
print("Error:", response.status_code, response.text)
import okhttp3.*;
import java.nio.charset.StandardCharsets;
public class List_available_voices {
public static void main(String[] args) {
// Crear cliente HTTP
OkHttpClient client = new OkHttpClient.Builder().build();
// Crear cuerpo de la solicitud
MediaType mediaType = MediaType.get("application/json; charset=utf-8");
String json = """
{
"language": "en-gb",
"ttsEngine": "block",
"voiceGender": "M",
"voiceEngine": "generative"
}
""";
RequestBody body = RequestBody.create(json.getBytes(StandardCharsets.UTF_8), mediaType);
// Construir la solicitud
Request request = new Request.Builder()
.url("https://public.speaqr.ai/api/batch/list-available-voices")
.post(body) // Se usa post() en vez de method()
.addHeader("Content-Type", "application/json")
.addHeader("x-api-key", "cFVBACTRF6nPzqetBcA2bG") //Your valid api key
.addHeader("Cookie", "dev_language=es-es")
.build();
// Ejecutar la solicitud
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
System.out.println("Voices available: " + response.body().string());
} else {
System.out.println("Error: " + response.code() + " - " + response.message());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Reply
batch/file
Processes an audio file and performs the operation specified inmode
. The result will depend on the selected mode.Endpoint
POST https://public.speaqr.ai/api/batch/file
Headers
Parameters
FORM DATA
Example
const myHeaders = new Headers();
myHeaders.append("x-api-key", "cFVBACTRF6nPzqetBcA2bG"); //Your valid api key
const wavFile = getWav();
const formData = new FormData();
formData.append("mode", "sts");
formData.append("languageIdTarget", "en-us");
formData.append("file", wavFile, "dummy.wav");
const requestOptions = {
method: "POST",
headers: myHeaders,
body: formData,
};
fetch("https://public.speaqr.ai/api/batch/file", requestOptions)
.then((response) => response.text())
.then((result) => console.log("Response:", result))
.catch((error) => console.error("Error:", error));
//Function to generate a dummy wav
function getWav(){
const sampleRate = 16000;
const numChannels = 1;
const duration = 2;
const bitsPerSample = 16;
//We generate empty data (silence)
const numSamples = sampleRate * duration;
const buffer = new ArrayBuffer(numSamples * (bitsPerSample / 8));
const view = new DataView(buffer);
//We write the WAV header
writeString(view, 0, "RIFF"); // ChunkID
view.setUint32(4, 36 + buffer.byteLength, true); // ChunkSize
writeString(view, 8, "WAVE"); // Format
writeString(view, 12, "fmt "); // Subchunk1ID
view.setUint32(16, 16, true); // Subchunk1Size
view.setUint16(20, 1, true); // AudioFormat (PCM)
view.setUint16(22, numChannels, true); // NumChannels
view.setUint32(24, sampleRate, true); // SampleRate
view.setUint32(28, sampleRate * numChannels * (bitsPerSample / 8), true); // ByteRate
view.setUint16(32, numChannels * (bitsPerSample / 8), true); // BlockAlign
view.setUint16(34, bitsPerSample, true); // BitsPerSample
writeString(view, 36, "data"); // Subchunk2ID
view.setUint32(40, buffer.byteLength, true); // Subchunk2Size
//We create a valid Blob in WAV format
const wavBlob = new Blob([buffer], { type: "audio/wav" });
return new File([wavBlob], "dummy.wav", { type: "audio/wav" });
}
//Function to write text to WAV header
function writeString(view, offset, string) {
for (let i = 0; i < string.length; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
import requests
import struct
# Function to generate a dummy wav
def generate_wav():
sample_rate = 16000
num_channels = 1
duration = 2
bits_per_sample = 16
num_samples = sample_rate * duration
byte_rate = sample_rate * num_channels * (bits_per_sample // 8)
block_align = num_channels * (bits_per_sample // 8)
# Cabecera WAV
wav_header = struct.pack(
'<4sI4s4sIHHIIHH4sI',
b'RIFF', # ChunkID
36 + num_samples * block_align, # ChunkSize
b'WAVE', # Format
b'fmt ', # Subchunk1ID
16, # Subchunk1Size
1, # AudioFormat (PCM)
num_channels, # NumChannels
sample_rate, # SampleRate
byte_rate, # ByteRate
block_align, # BlockAlign
bits_per_sample, # BitsPerSample
b'data', # Subchunk2ID
num_samples * block_align # Subchunk2Size
)
# We generate empty data (silence)
audio_data = b'\x00' * (num_samples * block_align)
with open("dummy.wav", "wb") as f:
f.write(wav_header + audio_data)
return "dummy.wav"
wav_file = generate_wav()
files = {
"file": ("dummy.wav", open(wav_file, "rb"), "audio/wav")
}
data = {
"mode": "sts",
"languageIdTarget": "en-us"
}
headers = {
"x-api-key": "cFVBACTRF6nPzqetBcA2bG" # Your valid api key
}
response = requests.post("https://public.speaqr.ai/api/batch/file", headers=headers, files=files, data=data)
print("Response:", response.text)
import okhttp3.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class BatchFile {
public static void main(String[] args) {
String apiUrl = "https://public.speaqr.ai/api/batch/file";
String apiKey = "cFVBACTRF6nPzqetBcA2bG"; //Your valid api key
// Generate dummy WAV file
String wavFilePath = "dummy.wav";
generateWavFile(wavFilePath);
// Send request to API
sendFileToApi(apiUrl, apiKey, wavFilePath);
}
// Function to generate a dummy WAV file
public static void generateWavFile(String filePath) {
int sampleRate = 16000;
int numChannels = 1;
int duration = 2;
int bitsPerSample = 16;
int numSamples = sampleRate * duration;
int byteRate = sampleRate * numChannels * (bitsPerSample / 8);
int blockAlign = numChannels * (bitsPerSample / 8);
int subchunk2Size = numSamples * blockAlign;
int chunkSize = 36 + subchunk2Size;
try (FileOutputStream fos = new FileOutputStream(filePath);
DataOutputStream dos = new DataOutputStream(fos)) {
// Write WAV header
dos.writeBytes("RIFF"); // ChunkID
dos.writeInt(Integer.reverseBytes(chunkSize)); // ChunkSize
dos.writeBytes("WAVE"); // Format
dos.writeBytes("fmt "); // Subchunk1ID
dos.writeInt(Integer.reverseBytes(16)); // Subchunk1Size
dos.writeShort(Short.reverseBytes((short) 1)); // AudioFormat (PCM)
dos.writeShort(Short.reverseBytes((short) numChannels)); // NumChannels
dos.writeInt(Integer.reverseBytes(sampleRate)); // SampleRate
dos.writeInt(Integer.reverseBytes(byteRate)); // ByteRate
dos.writeShort(Short.reverseBytes((short) blockAlign)); // BlockAlign
dos.writeShort(Short.reverseBytes((short) bitsPerSample)); // BitsPerSample
dos.writeBytes("data"); // Subchunk2ID
dos.writeInt(Integer.reverseBytes(subchunk2Size)); // Subchunk2Size
// Write silent audio data (zeros)
byte[] silence = new byte[subchunk2Size];
dos.write(silence);
System.out.println("WAV file generated: " + filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
// Function to send file to API
public static void sendFileToApi(String apiUrl, String apiKey, String filePath) {
OkHttpClient client = new OkHttpClient();
// Create file request body
File file = new File(filePath);
RequestBody fileBody = RequestBody.create(file, MediaType.get("audio/wav"));
// Create multipart form-data request
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("mode", "stt")
.addFormDataPart("languageIdTarget", "en-us")
.addFormDataPart("file", file.getName(), fileBody)
.build();
// Build HTTP request
Request request = new Request.Builder()
.url(apiUrl)
.post(requestBody)
.addHeader("x-api-key", apiKey)
.build();
// Execute request
try (Response response = client.newCall(request).execute()) {
String responseBody = (response.body() != null) ? response.body().string() : "No response body";
if (response.isSuccessful()) {
System.out.println("Response: " + responseBody);
} else {
System.out.println("Error: " + response.code() + " - " + response.message());
System.out.println("Response Body: " + responseBody); // Print JSON error response
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Reply
Socket connection
Compatibility
To use the api you have to develop in a language compatible with a socket.io client: Javascript, Java, C++, Swift, Dart, Python, .Net, Rust, Kotlin, PHP. https://socket.io/docs/v4Parameters
Example
const params = {
mode: 'translation',
languageIdTarget: 'fr-fr',
languageIdSource: 'en-gb'
};
const Sockets = io("https://public.speaqr.ai/api", {
transports: ['websocket'],
upgrade: false,
auth: {token: "cFVBACTRF6nPzqetBcA2bG"}, //Your valid api key
autoConnect: false,
reconnection: false,
query: params
});
Sockets.on('connect', () => console.log('connect!'));
Sockets.on('disconnect', reason => console.log('disconnect!', reason));
console.debug('Connecting to sockets...');
Sockets.connect();
setTimeout(() => Sockets.disconnect(), 5000);
import socketio
import urllib.parse
import threading
params = {
"mode": "translation",
"languageIdTarget": "fr-fr",
"languageIdSource": "en-gb"
}
query_string = urllib.parse.urlencode(params)
sio = socketio.Client()
@sio.event
def connect():
print("connect!")
@sio.event
def disconnect():
print("disconnect!")
print("Connecting to sockets...")
sio.connect(
f"https://public.speaqr.ai/api?{query_string}",
transports=["websocket"],
auth={"token": "cFVBACTRF6nPzqetBcA2bG"} # Your valid api key
)
print("Socket:", sio.connected)
print("Socket ID:", sio.sid)
print("Connected:", sio.connected)
print("Transport:", sio.transport())
threading.Timer(5, sio.disconnect).start()
sio.wait()
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import java.util.HashMap;
import java.util.Map;
import java.net.URI;
import java.net.URISyntaxException;
public class Sockets_connect {
public static void main(String[] args) {
try {
// Define connection parameters
String params = "mode=translation&languageIdTarget=fr-fr&languageIdSource=en-gb";
String url = "https://public.speaqr.ai/api?" + params;
// Configure WebSocket options
IO.Options options = new IO.Options();
options.transports = new String[]{"websocket"};
options.forceNew = true;
options.reconnection = false;
Map authData = new HashMap<>();
authData.put("token", "cFVBACTRF6nPzqetBcA2bG");
options.auth = authData;
// Create socket instance
Socket socket = IO.socket(new URI(url), options);
// Event listeners
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("connect!");
}
});
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("disconnect! Reason: " + (args.length > 0 ? args[0] : "unknown"));
System.exit(0);
}
});
// Connecting
System.out.println("Connecting to sockets...");
socket.connect();
// Disconnect after 5 seconds
Thread.sleep(5000);
socket.disconnect();
} catch (URISyntaxException | InterruptedException e) {
e.printStackTrace();
}
}
}
Sockets. Events.
The following is a description of the events specific to the api sockets.status_con
If this event is received, the connection has been successful. It always contains thesuccess
field set to true
, and the code
field with the string connected to Speaqr
. The urlStreaming
field will contain the url to listen to the streaming audio in case receiveType.type
is set to streaming
. The rest of the fields are those configured in the connection call.Reply
open
Until this event is received, the sockets will not be ready to receive text or audio, so before sending, it is important to wait for this event. The event does not receive data.Example of "status_con" and "open"
const params = {
mode: 'translation',
languageIdTarget: 'fr-fr',
languageIdSource: 'en-gb',
mode: 'sts',
sendType: JSON.stringify( {type: 'buffer'} ),
receiveType: JSON.stringify( {type: 'buffer', codec: 'mp3', samplerate: 16000} )
};
const Sockets = io("https://public.speaqr.ai/api", {
transports: ['websocket'],
upgrade: false,
auth: {token: "cFVBACTRF6nPzqetBcA2bG"}, //Your valid api key
autoConnect: false,
reconnection: false,
query: params
});
Sockets.on('connect', () => {
console.log('connect!');
console.log('Waiting to be able to send data...');
});
Sockets.on('status_con', (status) => {
console.log('Connection status', status);
});
Sockets.on('open', () => {
console.log('Speaqr is ready to receive data!');
setTimeout(() => Sockets.disconnect(), 5000);
});
Sockets.on('disconnect', reason => console.log('disconnect!', reason));
console.debug('Connecting to sockets...');
Sockets.connect();
import socketio
import urllib.parse
import os
params = {
"mode": "sts",
"languageIdTarget": "fr-fr",
"languageIdSource": "en-gb",
"sendType": '{"type": "buffer"}',
"receiveType": '{"type": "buffer", "codec": "mp3", "samplerate": 16000}'
}
query_string = urllib.parse.urlencode(params)
sio = socketio.Client()
@sio.event
def connect():
print("connect!")
print("Waiting to be able to send data...")
@sio.on("status_con")
def on_status_con(status):
print("Connection status", status)
@sio.on("open")
def on_open():
print("Speaqr is ready to receive data!")
sio.sleep(5)
sio.disconnect()
@sio.event
def disconnect(reason=None):
print("disconnect!", reason)
os._exit(0)
print("Connecting to sockets...")
sio.connect(
f"https://public.speaqr.ai/api?{query_string}",
transports=["websocket"],
auth={"token": "cFVBACTRF6nPzqetBcA2bG"} #Your valid api key
)
sio.wait()
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class Sockets_evt_status_con {
public static void main(String[] args) {
try {
// Define connection parameters
String params = "mode=sts&languageIdTarget=fr-fr&languageIdSource=en-gb" +
"&sendType=" + encode("{\"type\": \"buffer\"}") +
"&receiveType=" + encode("{\"type\": \"buffer\", \"codec\": \"mp3\", \"samplerate\": 16000}");
String url = "https://public.speaqr.ai/api?" + params;
// Configure WebSocket options
IO.Options options = new IO.Options();
options.transports = new String[]{"websocket"};
options.forceNew = true;
options.reconnection = false;
// Set authentication token
Map authData = new HashMap<>();
authData.put("token", "cFVBACTRF6nPzqetBcA2bG"); // Your valid API key
options.auth = authData;
// Create socket instance
Socket socket = IO.socket(new URI(url), options);
// Event listeners
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("connect!");
System.out.println("Waiting to be able to send data...");
}
});
socket.on("status_con", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("Connection status: " + args[0]);
}
});
socket.on("open", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("Speaqr is ready to receive data!");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
socket.disconnect();
}
});
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("disconnect! Reason: " + (args.length > 0 ? args[0] : "unknown"));
System.exit(0);
}
});
// Connecting
System.out.println("Connecting to sockets...");
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
System.exit(1);
}
}
// Helper method to encode JSON in query params
private static String encode(String value) {
return value.replace("{", "%7B").replace("}", "%7D")
.replace("\"", "%22").replace(":", "%3A")
.replace(",", "%2C").replace(" ", "%20");
}
}
result
The fields of the object received will depend on how the sockets were instantiated with themode
parameter. Below is an object for mode=sts
containing all possible fields.Reply
error
This event receives an object with different descriptions of errors that may occur, either due to programming errors in instantiating the modes of use, or in the data being processed.Reply
Sockets. Shipping.
The following describes the transmissions for data processing supported by the sockets. The sending functions receive anack
with the status of the sending.text
Send a text to be processed.Example
const params = {
mode: 'translator',
languageIdSource: 'en-gb',
languageIdTarget: 'fr-fr'
};
const Sockets = io("https://public.speaqr.ai/api", {
transports: ['websocket'],
upgrade: false,
auth: {token: "sU1cf9H5vWgBdSrEJyUry4"}, //Your valid api key
autoConnect: false,
reconnection: false,
query: params
});
Sockets.on('connect', () => {
console.log('connect!');
console.log('Waiting to be able to send data...');
});
Sockets.on('error', (error) => {
console.log('error!', error);
});
Sockets.on('status_con', (status) => {
console.log('Connection status', status);
});
Sockets.on('open', () => {
console.log('Speaqr is ready to receive data!');
console.log('I send text to translate...');
Sockets.emit('text', 'Hello word!. I want to translate it into French.', (ack) =>{
console.log('ack emit text', ack);
});
});
Sockets.on('result', (response) => {
console.log('response', response);
Sockets.disconnect();
});
Sockets.on('disconnect', reason => console.log('disconnect!', reason));
console.debug('Connecting to sockets...');
Sockets.connect();
import socketio
import urllib.parse
import os
params = {
"mode": "translator",
"languageIdSource": "en-gb",
"languageIdTarget": "fr-fr"
}
query_string = urllib.parse.urlencode(params)
sio = socketio.Client()
@sio.event
def connect():
print("connect!")
print("Waiting to be able to send data...")
@sio.on("error")
def on_error(error):
print("error!", error)
@sio.on("status_con")
def on_status_con(status):
print("Connection status", status)
@sio.on("open")
def on_open():
print("Speaqr is ready to receive data!")
print("I send text to translate...")
sio.emit("text", "Hello world! I want to translate it into French.", callback=on_ack)
@sio.on("result")
def on_result(response):
print("response", response)
sio.disconnect()
@sio.on("disconnect")
def on_disconnect(reason=None):
print("disconnect!", reason)
os._exit(0)
def on_ack(ack):
print("ack emit text", ack)
print("Connecting to sockets...")
sio.connect(
f"https://public.speaqr.ai/api?{query_string}",
transports=["websocket"],
auth={"token": "sU1cf9H5vWgBdSrEJyUry4"}
)
sio.wait()
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import io.socket.client.Ack;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class Sockets_emit_text {
public static void main(String[] args) {
try {
// Define connection parameters
String params = "mode=translator&languageIdSource=en-gb&languageIdTarget=fr-fr";
String url = "https://public.speaqr.ai/api?" + params;
// Configure WebSocket options
IO.Options options = new IO.Options();
options.transports = new String[]{"websocket"};
options.forceNew = true;
options.reconnection = false;
// Set authentication token
Map authData = new HashMap<>();
authData.put("token", "sU1cf9H5vWgBdSrEJyUry4"); // Your valid API key
options.auth = authData;
// Create socket instance
Socket socket = IO.socket(new URI(url), options);
// Event listeners
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("connect!");
System.out.println("Waiting to be able to send data...");
}
});
socket.on("error", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("error! " + (args.length > 0 ? args[0] : "unknown"));
}
});
socket.on("status_con", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("Connection status: " + (args.length > 0 ? args[0] : "unknown"));
}
});
socket.on("open", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("Speaqr is ready to receive data!");
System.out.println("I send text to translate...");
// Emit text and handle acknowledgment
socket.emit("text", "Hello world! I want to translate it into French.", new Ack() {
@Override
public void call(Object... ackArgs) {
System.out.println("ack emit text: " + (ackArgs.length > 0 ? ackArgs[0] : "No ack received"));
}
});
}
});
socket.on("result", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("response: " + (args.length > 0 ? args[0] : "unknown"));
socket.disconnect();
}
});
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("disconnect! Reason: " + (args.length > 0 ? args[0] : "unknown"));
System.exit(0);
}
});
// Connecting
System.out.println("Connecting to sockets...");
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
System.exit(1);
}
}
}
write_stream
Send an audio fragment to be processed.Example
In the following example, the device's microphone is captured and the first sentence heard is transcribed.
let mediaRecorder = null;
const params = {
mode: 'stt',
languageIdSource: 'en-gb',
sendType: JSON.stringify({type: 'buffer'})
};
const Sockets = io("https://public.speaqr.ai/api", {
transports: ['websocket'],
upgrade: false,
auth: {token: "sU1cf9H5vWgBdSrEJyUry4"}, //Your valid api key
autoConnect: false,
reconnection: false,
query: params
});
Sockets.on('connect', () => {
console.log('connect!');
console.log('Waiting to be able to send data...');
});
Sockets.on('error', (error) => {
console.log('error!', error);
});
Sockets.on('status_con', (status) => {
console.log('Connection status', status);
});
Sockets.on('open', () => {
console.log('Speaqr is ready to receive data. Speak now in English!!!!');
start();
});
Sockets.on('result', (response) => {
console.log('first response\n-----------------------\n\n');
console.log(response);
console.log('\n\n-----------------------\n\n');
console.log('We stopped listening!');
stop();
Sockets.disconnect();
});
Sockets.on('disconnect', reason => console.log('disconnect!', reason));
console.debug('Connecting to sockets...');
Sockets.connect();
// Start capturing and processing microphone audio
const start = async () => {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = ondataavailable;
mediaRecorder.start(500);
})
.catch(error => {
console.error("Error starting recording:", error);
});
}
// Here you receive each audio fragment in a Blob
const ondataavailable = (event) => {
if (event.data && event.data.size > 0) {
const audioChunk = event.data;
console.log('audio chunk', audioChunk);
Sockets.emit('write_stream', audioChunk, (ack) => {
if (!ack.status) console.error("Error write_stream", ack);
});
}
};
// Stop the process
const stop = async () => {
try {
//We stop the MediaRecorder
if (mediaRecorder){
mediaRecorder.stop();
mediaRecorder = null;
}
} catch (error) {
console.error("Error while stopping resources", error);
}
}
import socketio
import sounddevice as sd
import numpy as np
import os
import urllib.parse
# Audio parameters
SAMPLE_RATE = 16000 # 16kHz is the API's expected rate
CHANNELS = 1
CHUNK_SIZE = 1024 # Buffer size
params = {
"mode": "stt",
"languageIdSource": "en-gb",
"sendType": '{"type": "buffer"}'
}
query_string = urllib.parse.urlencode(params)
sio = socketio.Client()
@sio.event
def connect():
print("connect!")
print("Waiting to be able to send data...")
@sio.on("error")
def on_error(error):
print("error!", error)
@sio.on("status_con")
def on_status_con(status):
print("Connection status", status)
@sio.on("open")
def on_open():
print("Speaqr is ready to receive data. Speak now in English!!!!")
start_recording()
@sio.on("result")
def on_result(response):
print("first response\n-----------------------\n\n")
print(response)
print("\n\n-----------------------\n\n")
print("We stopped listening!")
sio.disconnect()
@sio.on("disconnect")
def on_disconnect(reason=None):
print("disconnect!", reason)
os._exit(0)
# Start capturing and processing microphone audio
def audio_callback(indata, frames, time, status):
if status:
print(status, flush=True)
audio_data = indata.astype(np.int16).tobytes()
sio.emit("write_stream", audio_data, callback=on_ack)
def on_ack(ack):
if not ack.get("status", True):
print("Error write_stream", ack)
# Start the process
def start_recording():
with sd.InputStream(samplerate=SAMPLE_RATE, channels=CHANNELS, dtype="int16", callback=audio_callback, blocksize=CHUNK_SIZE):
print("Recording... Press Ctrl+C to stop.")
print("Connecting to sockets...")
sio.connect(
f"https://public.speaqr.ai/api?{query_string}",
transports=["websocket"],
auth={"token": "sU1cf9H5vWgBdSrEJyUry4"}
)
sio.wait()
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import javax.sound.sampled.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Sockets_emit_write_stream {
private static Socket socket;
private static boolean recording = false;
private static TargetDataLine microphone;
public static void main(String[] args) {
try {
// Define connection parameters
String params = "mode=stt&languageIdSource=en-gb&sendType=%7B%22type%22%3A%22buffer%22%2C%22codec%22%3A%22linear16%22%2C%22samplerate%22%3A16000%7D";
String url = "https://public.speaqr.ai/api?" + params;
// Configure WebSocket options
IO.Options options = new IO.Options();
options.transports = new String[]{"websocket"};
options.forceNew = true;
options.reconnection = false;
// Set authentication token
Map authData = new HashMap<>();
authData.put("token", "sU1cf9H5vWgBdSrEJyUry4"); // Your valid API key
options.auth = authData;
// Create socket instance
socket = IO.socket(new URI(url), options);
// Event listeners
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("connect!");
System.out.println("Waiting to be able to send data...");
}
});
socket.on("error", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("error! " + (args.length > 0 ? args[0] : "unknown"));
}
});
socket.on("status_con", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("Connection status: " + (args.length > 0 ? args[0] : "unknown"));
}
});
socket.on("open", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("Speaqr is ready to receive data. Speak now in English!!!!");
startRecording();
}
});
socket.on("result", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("first response\n-----------------------\n\n");
System.out.println(args.length > 0 ? args[0] : "unknown");
System.out.println("\n\n-----------------------\n\n");
System.out.println("We stopped listening!");
stopRecording();
socket.disconnect();
}
});
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("disconnect! Reason: " + (args.length > 0 ? args[0] : "unknown"));
System.exit(0);
}
});
// Connecting
System.out.println("Connecting to sockets...");
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
System.exit(1);
}
}
// Start capturing and processing microphone audio
private static void startRecording() {
recording = true;
new Thread(() -> {
try {
AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.err.println("Microphone not supported");
return;
}
microphone = (TargetDataLine) AudioSystem.getLine(info);
microphone.open(format);
microphone.start();
byte[] buffer = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.out.println("Recording...");
while (recording) {
int bytesRead = microphone.read(buffer, 0, buffer.length);
if (bytesRead > 0) {
// Print a small portion of the buffer for debugging
System.out.println("Captured audio data: " + Arrays.toString(Arrays.copyOf(buffer, 10)) + "...");
socket.emit("write_stream", buffer);
}
}
microphone.stop();
microphone.close();
out.close();
System.out.println("Recording stopped.");
} catch (LineUnavailableException | IOException e) {
e.printStackTrace();
}
}).start();
}
// Stop the process
private static void stopRecording() {
recording = false;
}
}
This webpage uses cookies
The cookies in this website are used to customise contents and analyse traffic.
You may find details on cookies in the Privacy Policy section