Skip to main content

ngChat Speech SDK: Realize ngChat Speech-to-Text recognition and Text-to-Speech synthesis.

Project description

ngChat Speech SDK

Please contact info@seasalt.ai if you have any questions.

Speech-to-Text Example:

Prerequisites

You will need a ngChat speech-to-text service url to run this example. Please contact info@seasalt.ai and apply for it.

Install and import

To install ngChat Speech SDK:

pip install ngchat-speech-sdk

To import ngChat Speech SDK:

import ngchat_speech.speech as speechsdk

Recognition

In the example below, we show how to recognize speech from an audio file. You can also apply recognition to an audio stream.

Speech Configuration

Use the following code to create SpeechConfig (contact info@seasalt.ai for the service host):

    speech_config = speechsdk.SpeechConfig(
        host="ws://NGCHAT_STT_SERVER/client/ws/speech"
    )

Audio Configuration

Use the following code to create AudioConfig.

    # Code commented out is an example for recognition on an audio stream.
    # audio_format = speechsdk.audio.AudioStreamFormat(
    #     samples_per_second=16000, bits_per_sample=16, channels=1)
    # audio_stream = speechsdk.audio.PushAudioInputStream(stream_format=audio_format)
    # audio_config = speechsdk.audio.AudioConfig(stream=audio_stream)
    audio_config = speechsdk.audio.AudioConfig(filename="test.wav")

Recognizer initialization

SpeechRecognizer can be initialzed as follows:

    speech_recognizer = speechsdk.SpeechRecognizer(
        speech_config=speech_config,
        audio_config=audio_config
    )

Callbacks connection

SpeechRecognizer has 5 kinds of callbacks:

  • Recognizing - called when recognition is in progress.
  • Recognized - called when a single utterance is recognized.
  • Canceled - called when a continuous recognition is interrupted.
  • Session_started - called when a recognition session is started.
  • Session_stopped - called when a recognition session is stopped.

To connect the callbacks:

    speech_recognizer.recognizing.connect(
        lambda evt: print(f"Recognizing: {evt.result.text}"))
    speech_recognizer.recognized.connect(
        lambda evt: print(f'Recognized: {evt.result.text}'))
    speech_recognizer.canceled.connect(
        lambda evt: print(f'Canceled: {evt}'))
    speech_recognizer.session_started.connect(
        lambda evt: print(f'Session_started: {evt}'))
    speech_recognizer.session_stopped.connect(
        lambda evt: print(f'Session_stopped: {evt}'))

Recognizing speech

Now it is ready to run SpeechRecognizer. SpeechRecognizer has two ways for speech recognition:

  • Single-shot recognition - Performs recognition once. This is to recognize a single audio file. It stops recognition after a single utterance is recognized.
  • Continuous recognition (async) - Asynchronously initiates continuous recognition on an audio stream. Recognition results are available through callback functions. To stop the continuous recognition, call stop_continuous_recognition_async().
    # Code commented out is for recognition on an audio stream.
    # speech_recognizer.start_continuous_recognition_async()
    speech_recognizer.recognize_once()

Putting everything together

Now, put everything together and run the example:

import speech as speechsdk
import audio as audio
import asyncio
import threading
import sys
import time

if __name__=="__main__":
    # this is an example to show how to use the ngChat Speech SDK to recognize once

    try:
        speech_config = speechsdk.SpeechConfig(
            host="ws://NGCHAT_STT_SERVER/client/ws/speech"
        )
        audio_config = audio.AudioConfig(filename="test.wav")
        speech_recognizer = speechsdk.SpeechRecognizer(
            speech_config=speech_config,
            audio_config=audio_config
        )

        speech_recognizer.recognizing.connect(
            lambda evt: print(f"Recognizing: {evt.result.text}"))
        speech_recognizer.recognized.connect(
            lambda evt: print(f'Recognized: {evt.result.text}'))
        speech_recognizer.canceled.connect(
            lambda evt: print(f'Canceled: {evt}'))
        speech_recognizer.session_started.connect(
            lambda evt: print(f'Session_started: {evt}'))
        speech_recognizer.session_stopped.connect(
            lambda evt: print(f'Session_stopped: {evt}'))

        speech_recognizer.recognize_once()
        time.sleep(3)

    except KeyboardInterrupt:
        print("Caught keyboard interrupt. Canceling tasks...")
    except Exception as e:
        print(f"Exception: {e}")
    finally:
        sys.exit()

Text-to-Speech Example:

Prerequisites

You will need a ngChat text-to-speech service url to run this example. Please contact info@seasalt.ai and apply for it.

Install and import

To install ngChat Speech SDK:

pip install ngchat-speech-sdk

To import ngChat Speech SDK:

import ngchat_speech.speech as speechsdk

Synthesis

In the example below, we show how to synthesize text to an audio file. You can also receive synthesis results from an audio stream.

Speech Configuration

Use the following code to create SpeechConfig (contact info@seasalt.ai for the service host):

    speech_config = speechsdk.SpeechConfig(
        host="ws://NGCHAT_TTS_SERVER"
    )

Audio Configuration

Use the following code to create AudioOutputConfig.

    import ngchat_speech.audio as audio
    audio_config = audio.AudioOutputConfig(filename="output.wav")

Synthesizer initialization

Synthesizer can be initialzed as follows:

    speech_synthesizer = speechsdk.SpeechSynthesizer(
        speech_config=speech_config,
        audio_config=audio_config
    )

Callbacks connection

SpeechSynthesizer has 4 kinds of callbacks:

  • Synthesis_started - called when synthesis is started.
  • Synthesizing - called when each time part of synthesis result is given.
  • Synthesis_completed - called when all text was synthesized.
  • Synthesis_canceled - called when synthesis is interrupted.

To connect the callbacks:

    speech_synthesizer.synthesis_started.connect(
        lambda : print("synthesis started"))
    speech_synthesizer.synthesizing.connect(
        lambda : print("synthesizing"))
    speech_synthesizer.synthesis_completed.connect(
        lambda : print("synthesis completed"))
    speech_synthesizer.synthesis_canceled.connect(
        lambda : print("synthesis canceled"))

Synthesizing text

Now it is ready to run SpeechSynthesizer. There are two ways to run SpeechSynthesizer:

  • Synchronized - Perform synthesis until got all result.
  • Asynchronized - Start synthesis and return a speechsdk.ResultFuture, which you could call its get() function to wait and get synthesis result.
    # Code commented out is for synchronized synthesis
    #result = speech_synthesizer.speak_text("Input your text to synthesize here.")
    result = speech_synthesizer.speak_text_async("Input your text to synthesize here.").get()

Judge result reason:

Both synchronized and asynchronized ways return a speechsdk.SpeechSynthesisResult, which has result to judge if synthesis was completed successfully:

    if result.reason == speechsdk.ResultReason.ResultReason_SynthesizingAudioCompleted:
        print("finished speech synthesizing")

Putting everything together

Now, put everything together and run the example:

from ngchat_speech import speech as speechsdk
from ngchat_speech import audio as audio

if __name__ == "__main__":
    speech_config = speechsdk.SpeechConfig(
        host="ws://NGCHAT_TTS_SERVER"
    )
    audio_config = audio.AudioOutputConfig(filename="output.wav")
    speech_synthesizer = speechsdk.SpeechSynthesizer(
        speech_config=speech_config,
        audio_config=audio_config
    )
    speech_synthesizer.synthesis_started.connect(
        lambda : print("synthesis started"))
    speech_synthesizer.synthesizing.connect(
        lambda : print("synthesizing"))
    speech_synthesizer.synthesis_completed.connect(
        lambda : print("synthesis completed"))
    speech_synthesizer.synthesis_canceled.connect(
        lambda : print("synthesis canceled"))

    # result = speech_synthesizer.speak_text("Seasalt.ai is a service company focusing on multi-modal AI solutions.")
    result = speech_synthesizer.speak_text_async("Seasalt.ai is a service company focusing on multi-modal AI solutions.").get()

    if result.reason == speechsdk.ResultReason.ResultReason_SynthesizingAudioCompleted:
        print("finished speech synthesizing")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ngchat-speech-sdk-0.1.4.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

ngchat_speech_sdk-0.1.4-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file ngchat-speech-sdk-0.1.4.tar.gz.

File metadata

  • Download URL: ngchat-speech-sdk-0.1.4.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.9

File hashes

Hashes for ngchat-speech-sdk-0.1.4.tar.gz
Algorithm Hash digest
SHA256 f78da5d039c72a28d3562207e084772e941f6cbc0a1625d01294982c4c72d30b
MD5 5f95250c022ce2725b4ea001a0a50958
BLAKE2b-256 3967ad6e17b1b8515e88796cc536e219300e98e609d4e48f85464aa1c92a0b2b

See more details on using hashes here.

File details

Details for the file ngchat_speech_sdk-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: ngchat_speech_sdk-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.9

File hashes

Hashes for ngchat_speech_sdk-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 829ec1dd3bad402d2aca000b0e6226d6c9f185ae2c2189beb46d19460285c553
MD5 38acccc07aaf9e9fbff732c4b8c123fd
BLAKE2b-256 3d2d958e3ef109d159020f5279652f4a783afda42dc77980fef2615787b2e1bc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page