Transcribe Short Audio#
Make sure you have completed Quickstart step before proceeding with this guide.
In this example, we will transcribe a short audio (< 60 seconds) and print out the result.
from soniox.transcribe_file import transcribe_file_short
from soniox.speech_service import SpeechClient
# Do not forget to set your API key in the SONIOX_API_KEY environment variable.
def main():
with SpeechClient() as client:
result = transcribe_file_short(
"../test_data/test_audio.flac",
client,
model="en_v2", # Do not forget to specify the model!
)
print(f"Text: " + "".join(word.text for word in result.words))
print("Tokens:")
for word in result.words:
print(f" '{word.text}' {word.start_ms} {word.duration_ms}")
if __name__ == "__main__":
main()
We create a SpeechClient
object which handles all the communication between your program and Soniox.
We use the with
statement to cleanup the client
object after it is no longer needed.
We then call the transcribe_file_short()
function with the created client
object and a short audio
file test_audio.flac
(part of soniox_examples GitHub repository).
The function reads the entire audio file and sends it to Soniox for transcription.
It returns an instance of the Result
structure, which contains the recognized tokens.
Each token is an instance of the Word
structure, which contains the text of the recognized token as well as its timestamps.
Run
python3 transcribe_file_short.py
Output
Text: He was two years out from the East and had not yet forgotten to be homesick at times
Tokens:
'He' 317 60
' ' 617 0
'was' 617 60
' ' 857 0
'two' 857 60
...
const { SpeechClient } = require("@soniox/soniox-node");
// Do not forget to set your API key in the SONIOX_API_KEY environment variable.
const speechClient = new SpeechClient();
(async function () {
const result = await speechClient.transcribeFileShort(
"../test_data/test_audio.flac",
{
model: "en_v2", // Do not forget to specify the model!
}
);
console.log("Text: " + result.words.map((word) => word.text).join(""));
console.log("Tokens:");
for (const word of result.words) {
console.log(` '${word.text}' ${word.start_ms} ${word.duration_ms}`);
}
})();
We create a SpeechClient
object which handles all the communication between your program and Soniox.
We then call the transcribeFileShort()
function with a short audio file test_audio.flac
(part of soniox_examples GitHub repository).
The function reads the entire audio file and sends it to Soniox for transcription.
It returns an instance of the Result
structure, which contains the recognized tokens.
Each token is an instance of the Word
structure, which contains the text of the recognized token as well as its timestamps.
Run
node transcribe_file_short.js
Output
Text: He was two years out from the East and had not yet forgotten to be homesick at times
Tokens:
'He' 317 60
' ' 617 0
'was' 617 60
' ' 857 0
'two' 857 60
...
using Soniox.Types;
using Soniox.Client;
using Soniox.Client.Proto;
// Do not forget to set your API key in the SONIOX_API_KEY environment variable.
using var client = new SpeechClient();
var completeResult = await client.TranscribeFileShort(
"../../test_data/test_audio.flac",
new TranscriptionConfig
{
Model = "en_v2", // Do not forget to specify the model!
});
Result result = (completeResult as SingleResult)!.Result;
var text = string.Join("", result.Words.Select(word => word.Text).ToArray());
Console.WriteLine("Text: " + text);
Console.WriteLine("Tokens:");
foreach (var word in result.Words)
{
Console.WriteLine($" '{word.Text}' {word.StartMs} {word.DurationMs}");
}
We create a SpeechClient
object which handles all the communication between your program and Soniox.
We then call the TranscribeFileShort()
function with a short audio file test_audio.flac
(part of soniox_examples GitHub repository).
The function reads the entire audio file and sends it to Soniox for transcription.
It returns an instance of the Result
structure, which contains the recognized tokens.
Each token is an instance of the Word
structure, which contains the text of the recognized token as well as its timestamps.
Run
cd soniox_examples/csharp/TranscribeFileShort
dotnet run
Output
Text: He was two years out from the East and had not yet forgotten to be homesick at times
Tokens:
'He' 317 60
' ' 617 0
'was' 617 60
' ' 857 0
'two' 857 60
...