Web Voice API Reference

The Soniox Web Voice library is based around the RecordTranscribe class, which represents a session of integrated audio recording and transcription.

Basic Usage

To start recording and transcribing, create an instance of RecordTranscribe, set the desired configuration and event handlers and then call start(). In the example below, all supported event handlers are set, but any of these can be left unset if it is not needed.

let recordTranscribe = new sonioxWebVoice.RecordTranscribe()
recordTranscribe.setIncludeNonFinal(true)
recordTranscribe.setOnStarted(onStarted)
recordTranscribe.setOnPartialResult(onPartialResult)
recordTranscribe.setOnFinished(onFinished)
recordTranscribe.setOnError(onError)
recordTranscribe.start()

IncludeNonFinal specifies if low-latency transcription results are desired. If it is desired to receive recognized words as they are spoken, set this to true; if only the complete transcript after the end of the recording is of interest, set this to false.

When start() is called, the library first requests access to the user's microphone and the user may be prompted to allow access and/or select the microphone. Then the library establishes a connection to the Soniox service and start transcription session. At this point, audio recording start and the onStarted event hander will be called if provided.

function onStarted() {
  console.log("Recoding/transcription started!")
}

After transcription/recording starts, partial transcription results will be provided to the setOnPartialResult handler if one is set. Note that if IncludeNonFinal is false, handling onPartialResult is likely not needed because the complete transcript will be available at the end of the transcription as described below.

function onPartialResult(result) {
  console.log("Partial result:")
  for (let i = 0; i < result.words.length; ++i) {
    let word = result.words[i];
    if (word.is_final) {
      console.log("  final word: " + word.text)
    } else {
      console.log("  non-final word: " + word.text)
    }
  }
}

As can be seen, words are returned as either final or non-final. Refer to Final vs Non-final Words for an explanation. Non-final words are returned only if IncludeNonFinal is true.

Partial results provided to onPartialResult are frozen objects which will not change and can safely be stored for later use.

To stop recording and finish the transcription, call stop(). Recording will immediately stop but it will take a short time to finished the transcription; during this time setOnPartialResult may still be called if provided. When the transcription is finished, the onFinished handler will be called if provided. After that point no more handlers will be called.

// Call when you want to stop.
recordTranscribe.stop();

function onFinished() {
  let result = recordTranscribe.getResult()
  let transcript = ""
  for (let i = 0; i < result.words.length; ++i) {
    transcript += " " + result.words[i].text
  }
  console.log("Finished:", transcript)
}

As demonstrated above, the complete result of the transcription can be retrieved using getResult(). If called after the transcription is finished, all words in the result will be final.

If getResult() is called before the transcription is finished, the result will only contain words recognized so far. In that case, if IncludeNonFinal is true, the result may contain non-final words at the end.

getResult() always returns the same internal object which is automatically updated based on new partial transcription results, and it is not permitted to modify this object. If you need to store the result before the transcription is finished, use getResultCopy(), which returns a frozen copy of the current result.

Errors, Canceling and States

An error can occur at any point after start() is called and until the transcription is finished as indicated by onFinished. When an error occurs, recording and transcription are immediately stopped and the onError handler is called if it is set. The onError handler is provided a status string and an error message.

function onError(status, message) {
  console.log("Error: status=" + status + ", message=", message)
}

It is possible to cancel all operations at any time using cancel(). If this is called before the transcription is finished, recording and transcription are immediately stopped; any audio already recorded but not yet transcribed will not be transcribed.

If audio recording stops due to an external reason, such as the user revoking access to the microphone, the library behaves as if stop() was called, rather than treating that as an error.

// Call when you want to cancel without completing the transcription.
recordTranscribe.cancel()

After an error or cancel(), no more handlers will be called.

For convenience, a getState() function is provided to determine the current state of the RecordTranscribe object:

// If you want to check the state.
console.log("State: " + recordTranscribe.getState())

getState returns one of the following states as a string: - Init: start() has not been called yet. - Starting: start() has been called, not yet recording or transcribing. - Running: Recording and transcribing. - Finishing: stop() has been called, finishing the transcription. - Finished: The transcription is finished. - Error: An error has occurred. - Cancel: cancel() has been called in a state other than Init, Finished or Error.

Multiple Transcriptions

A RecordTranscribe object can be used only for a single transcription. To perform another transcription, use a new RecordTranscribe object.

It is not permitted to perform multiple transcriptions at the same time, more specifically to have more than one RecordTranscribe object in Starting, Running or Finishing state. If you attempt to start() a transcription violating this, an exception will be thrown.