### Python SDK Installation and Setup
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-python
Instructions for installing the Python SDK for one-sentence speech recognition, including prerequisites and dependency installation.
```APIDOC
## Prerequisites
* Read the API description before using the SDK.
* The SDK only supports Python 3.
* Ensure `setuptools` is installed. If not, run: `pip install setuptools`
## Download and Install
**Note**: Execute the following commands in the SDK's root directory.
1. **Download Python SDK**: Get the SDK from Github or download `streamInputTts-github-python` directly.
2. **Install SDK Dependencies**: Navigate to the SDK root directory and run: `python -m pip install -r requirements.txt`
3. **Install SDK**: After dependencies are installed, run: `python -m pip install .`
4. **Import SDK**: After installation, import the SDK using:
```python
# -*- coding: utf-8 -*-
import nls
```
```
--------------------------------
### Python SDK - Installation and Setup
Source: https://help.aliyun.com/zh/isi/developer-reference/long-text-to-speech-synthesis-for-cosyvoice-python-sdk
Instructions for downloading, installing, and setting up the Python SDK for Aliyun Intelligent Speech Service.
```APIDOC
## Python SDK Installation and Setup
### Description
This section details the steps to download and install the Python SDK for Aliyun's Intelligent Speech Service, including dependency installation and SDK import.
### Installation Steps
1. **Download SDK:**
Obtain the Python SDK from Github or download `streamInputTts-github-python` directly.
2. **Install Dependencies:**
Navigate to the SDK's root directory and run:
```bash
python -m pip install -r requirements.txt
```
3. **Install SDK:**
After dependency installation, install the SDK using:
```bash
python -m pip install .
```
4. **Import SDK:**
Once installed, import the SDK in your Python code:
```python
# -*- coding: utf-8 -*-
import nls
```
**Note:** All commands must be executed from the SDK's root directory.
```
--------------------------------
### Install Aliyun NLS Go SDK
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-go-2
Installs the Aliyun NLS Go SDK using the Go get command. Ensure you have Go 1.16 or later installed and configured.
```go
go get github.com/aliyun/alibabacloud-nls-go-sdk
```
--------------------------------
### Python SDK: Streaming TTS Example Setup
Source: https://help.aliyun.com/zh/isi/developer-reference/siso-text-to-speech-synthesis-for-cosyvoice-python-sdk
Initializes settings for a streaming text-to-speech example, including enabling file saving for synthesized audio and importing necessary modules. It also includes instructions for installing PyAudio.
```python
# coding=utf-8
#
# Installation instructions for pyaudio:
# APPLE Mac OS X
# brew install portaudio
# pip install pyaudio
# Debian/Ubuntu
# sudo apt-get install python-pyaudio python3-pyaudio
# or
# pip install pyaudio
# CentOS
# sudo yum install -y portaudio portaudio-devel && pip install pyaudio
# Microsoft Windows
# python -m pip install pyaudio
import nls
import time
# 设置打开日志输出
nls.enableTrace(False)
# 将音频保存进文件
SAVE_TO_FILE = True
```
--------------------------------
### Install and Import Aliyun Go SDK for Speech Interaction
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-go-1
This snippet shows how to install the Aliyun NLS Go SDK using the go get command and how to import it into your Go project for use in speech recognition functionalities.
```go
go get github.com/aliyun/alibabacloud-nls-go-sdk
```
```go
import ("github.com/aliyun/alibabacloud-nls-go-sdk")
```
--------------------------------
### Real-time Speech Recognition Demo (C#)
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-c-1
A comprehensive C# example demonstrating the setup and operation of the NLS SDK for real-time speech recognition. It includes initializing the client, setting up audio file handling, starting worker threads, and managing the recognition process. Dependencies include system libraries and the nlsCsharpSdk.
```csharp
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using nlsCsharpSdk;
namespace nlsCsharpSdkDemo
{
public partial class nlsCsharpSdkDemo : Form
{
private NlsClient nlsClient;
private SpeechTranscriberRequest stPtr;
private NlsToken tokenPtr;
private UInt64 expireTime;
private string appKey;
private string akId;
private string akSecret;
private string token;
private string url;
static bool running;
static bool st_send_audio_flag = false;
static bool st_audio_loop_flag = false;
static Thread st_send_audio;
static string cur_st_result;
static string cur_st_completed;
static string cur_st_closed;
private void FlushLab()
{
while (running)
{
if (cur_st_result != null && cur_st_result.Length > 0)
{
stResult.Text = cur_st_result;
}
if (cur_st_completed != null && cur_st_completed.Length > 0)
{
stCompleted.Text = cur_st_completed;
}
if (cur_st_closed != null && cur_st_closed.Length > 0)
{
stClosed.Text = cur_st_closed;
}
Thread.Sleep(200);
}
}
///
/// 实时识别的音频推送线程.
///
private void STAudioLab()
{
string file_name = System.Environment.CurrentDirectory + @"\audio_files\test3.wav";
System.Diagnostics.Debug.WriteLine("st audio file_name = {0}", file_name);
FileStream fs = new FileStream(file_name, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
while (st_audio_loop_flag)
{
if (st_send_audio_flag)
{
byte[] byData = br.ReadBytes((int)3200);
if (byData.Length > 0)
{
// 音频数据以PCM格式,byData.Length字节进行推送。
stPtr.SendAudio(stPtr, byData, (UInt64)byData.Length, EncoderType.ENCODER_PCM);
}
else
{
/*
* 音频推送完成,重新打开循环继续
*/
br.Close();
fs.Dispose();
fs = new FileStream(file_name, FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);
}
}
/*
* 上面推送3200字节音频数据,相当于模拟100MS的音频。
* 真实环境从录音获得音频数据,不需要调用此Sleep。
*/
Thread.Sleep(100);
}
br.Close();
fs.Dispose();
}
public nlsCsharpSdkDemo()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
nlsClient = new NlsClient();
}
private void button1_Click(object sender, EventArgs e)
{
// 开启日志系统,以Debug级别将名字为nlsLog.log的日志以单个400MB 10个日志文件循环的形式储存。
int ret = nlsClient.SetLogConfig("nlsLog", LogLevel.LogDebug, 400, 10);
if (ret == 0)
nlsResult.Text = "OpenLog Success";
else
nlsResult.Text = "OpenLog Failed";
}
private void button1_Click_1(object sender, EventArgs e)
{
// 获取当前SDK的版本号
string version = nlsClient.GetVersion();
nlsResult.Text = version;
}
private void button1_Click_2(object sender, EventArgs e)
{
// -1则表示启动CPU核数的事件池数量,用于进行内部事件的处理。
// 单路调用的情况下,建议入参为1,即启动单个事件池进行处理。
// 高并发(几百路)的情况下,入参建议4 ~ CPU核数,入参越大,处理延迟越低但是CPU占用越高。
nlsClient.StartWorkThread(1);
nlsResult.Text = "StartWorkThread and init NLS success.";
running = true;
Thread t = new Thread(FlushLab);
t.Start();
}
private void button1_Click_3(object sender, EventArgs e)
{
// 释放NlsClient,与StartWorkThread互为反操作
nlsClient.ReleaseInstance();
nlsResult.Text = "Release NLS success.";
}
#region Info
private void textBox1_TextChanged(object sender, EventArgs e)
{
akId = tAkId.Text;
}
```
--------------------------------
### Aliyun SDK Speech Synthesis Initialization and Usage (Go)
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-go-2
Provides code examples for initializing and using the speech synthesis feature within the Aliyun SDK. This involves setting up parameters, starting the synthesis process with provided text and parameters, and shutting down the service. Proper handling of the returned channel and error is crucial.
```go
type SpeechSynthesisStartParam struct {
// Parameters for speech synthesis
}
func DefaultSpeechSynthesisParam() SpeechSynthesisStartParam {
// Returns default parameters
return SpeechSynthesisStartParam{}
}
func NewSpeechSynthesis(...) (*SpeechSynthesis, error) {
// Initializes a new SpeechSynthesis instance
return &SpeechSynthesis{}, nil
}
func (tts *SpeechSynthesis) Start(text string, param SpeechSynthesisStartParam, extra map[string]interface{}) (chan bool, error) {
// Starts the speech synthesis process
return nil, nil
}
func (tts *SpeechSynthesis) Shutdown() {
// Shuts down the speech synthesis service
}
```
--------------------------------
### Install Python SDK Dependencies and Package
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-python-1
These commands install the 'setuptools' package if not already present, then install the SDK dependencies from 'requirements.txt', and finally install the SDK itself. Ensure these commands are run from the SDK's root directory.
```bash
pip install setuptools
python -m pip install -r requirements.txt
python -m pip install .
```
--------------------------------
### C++ SDK Compilation and Running (Linux)
Source: https://help.aliyun.com/zh/isi/developer-reference/c-sdk-3
Step-by-step guide to compiling and running the C++ SDK on a Linux environment, including prerequisites and example commands.
```APIDOC
## C++ SDK Compilation and Running (Linux Platform)
### Description
This section details the process of compiling and running the C++ SDK on a Linux system.
### Prerequisites
Ensure you have the following minimum tool versions installed:
- CMake: 3.0
- Glibc: 2.5
- Gcc: 4.8.5
### Compilation Steps
1. **Navigate to SDK Root Directory:** Open a Linux terminal and change the directory to the root of the SDK source code.
2. **Build SDK:** Execute the build script to generate SDK library files and executable demo programs.
```bash
./scripts/build_linux.sh
```
This will create demo executables such as `srDemo` (one-sentence recognition), `stDemo` (real-time recognition), `syDemo` (speech synthesis), `daDemo` (dialogue), and `fsDemo` (streaming synthesis).
3. **Run Demo:** Navigate to the demo directory and run an example executable.
```bash
cd build/demo
./fsDemo
```
Replace `./fsDemo` with the desired demo executable (e.g., `./syDemo` for speech synthesis).
```
--------------------------------
### Install Python Package Manager (setuptools) - Python
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-python
Installs the setuptools package, a dependency manager for Python, using pip. This is a prerequisite for installing the Aliyun speech recognition SDK.
```bash
pip install setuptools
```
--------------------------------
### SpeechSynthesizer Instance Creation and Configuration
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-java-9
Guides on creating a SpeechSynthesizer instance and configuring its parameters like speaker, encoding, and sample rate before starting a synthesis task.
```APIDOC
## POST /api/speech/synthesizer
### Description
Creates a SpeechSynthesizer instance using an NlsClient and SpeechSynthesizerListener. Allows setting synthesis parameters and starting the task.
### Method
POST
### Endpoint
`/api/speech/synthesizer`
### Parameters
#### Request Body
- **client** (Object) - Required - An initialized `NlsClient` instance.
- **listener** (Object) - Required - An initialized `SpeechSynthesizerListener` instance.
### Request Example
```javascript
// Assuming client and listener are already initialized
const synthesizer = new SpeechSynthesizer(client, listener);
```
### Response
#### Success Response (200)
- **message** (String) - Confirmation of SpeechSynthesizer instance creation.
#### Response Example
```json
{
"message": "SpeechSynthesizer instance created successfully."
}
```
## POST /api/speech/synthesizer/appkey
### Description
Sets the project AppKey for the SpeechSynthesizer instance, which is required for the synthesis process.
### Method
POST
### Endpoint
`/api/speech/synthesizer/appkey`
### Parameters
#### Request Body
- **appKey** (String) - Required - The AppKey associated with your Aliyun project.
### Request Example
```javascript
synthesizer.setAppKey("YOUR_APP_KEY");
```
### Response
#### Success Response (200)
- **message** (String) - Confirmation that the AppKey has been set.
#### Response Example
```json
{
"message": "AppKey set successfully."
}
```
```
--------------------------------
### Python: Install and Import StreamInputTts SDK
Source: https://help.aliyun.com/zh/isi/developer-reference/stream-input-tts-sdk-quick-start
This Python code illustrates the steps for installing and importing the Aliyun Stream Input TTS SDK. It covers downloading the SDK from GitHub, installing dependencies, and the final installation command. Ensure these commands are executed from the SDK's root directory.
```bash
python -m pip install -r requirements.txt
python -m pip install .
```
```python
# -*- coding: utf-8 -*-
import nls
```
--------------------------------
### Go Aliyun NLS Speech Recognition Setup and Event Handling
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-go-1
This Go code demonstrates how to set up and use the Aliyun NLS Speech Recognition SDK. It includes functions to handle various events such as task failure, recognition start, result changes, completion, and closure. It also shows how to manage connection configurations and logger settings.
```Go
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"sync"
"time"
"github.com/aliyun/alibabacloud-nls-go-sdk"
)
const (
AKID = "Your AKID"
AKKEY = "Your AKKEY"
//online key
APPKEY = "Your APPKEY" //获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist
TOKEN = "Your TOKEN" //获取Token具体操作,请参见:https://help.aliyun.com/document_detail/450514.html
)
func onTaskFailed(text string, param interface{}) {
logger, ok := param.(*nls.NlsLogger)
if !ok {
log.Default().Fatal("invalid logger")
return
}
logger.Println("TaskFailed:", text)
}
func onStarted(text string, param interface{}) {
logger, ok := param.(*nls.NlsLogger)
if !ok {
log.Default().Fatal("invalid logger")
return
}
logger.Println("onStarted:", text)
}
func onResultChanged(text string, param interface{}) {
logger, ok := param.(*nls.NlsLogger)
if !ok {
log.Default().Fatal("invalid logger")
return
}
logger.Println("onResultChanged:", text)
}
func onCompleted(text string, param interface{}) {
logger, ok := param.(*nls.NlsLogger)
if !ok {
log.Default().Fatal("invalid logger")
return
}
logger.Println("onCompleted:", text)
}
func onClose(param interface{}) {
logger, ok := param.(*nls.NlsLogger)
if !ok {
log.Default().Fatal("invalid logger")
return
}
logger.Println("onClosed:")
}
func waitReady(ch chan bool, logger *nls.NlsLogger) error {
select {
case done := <-ch:
{
if !done {
logger.Println("Wait failed")
return errors.New("wait failed")
}
logger.Println("Wait done")
}
case <-time.After(20 * time.Second):
{
logger.Println("Wait timeout")
return errors.New("wait timeout")
}
}
return nil
}
var lk sync.Mutex
var fail = 0
var reqNum = 0
func testMultiInstance(num int) {
pcm, err := os.Open("tests/test1.pcm")
if err != nil {
log.Default().Fatalln(err)
}
buffers := nls.LoadPcmInChunk(pcm, 320)
param := nls.DefaultSpeechRecognitionParam()
config, _ := nls.NewConnectionConfigWithAKInfoDefault(nls.DEFAULT_URL, APPKEY, AKID, AKKEY)
var wg sync.WaitGroup
for i := 0; i < num; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
strId := fmt.Sprintf("ID%d ", id)
logger := nls.NewNlsLogger(os.Stderr, strId, log.LstdFlags|log.Lmicroseconds)
logger.SetLogSil(false)
logger.SetDebug(true)
logger.Printf("Test Normal Case for SpeechRecognition:%s", strId)
sr, err := nls.NewSpeechRecognition(config, logger,
onTaskFailed, onStarted, onResultChanged,
onCompleted, onClose, logger)
if err != nil {
logger.Fatalln(err)
return
}
test_ex := make(map[string]interface{})
test_ex["test"] = "hello"
for {
lk.Lock()
reqNum++
lk.Unlock()
logger.Println("SR start")
ready, err := sr.Start(param, test_ex)
if err != nil {
lk.Lock()
fail++
lk.Unlock()
sr.Shutdown()
continue
}
err = waitReady(ready, logger)
if err != nil {
lk.Lock()
fail++
lk.Unlock()
sr.Shutdown()
continue
}
for _, data := range buffers.Data {
if data != nil {
sr.SendAudioData(data.Data)
time.Sleep(10 * time.Millisecond)
}
}
}
}(i)
}
wg.Wait()
fmt.Printf("Total Request: %d, Failed: %d\n", reqNum, fail)
}
```
--------------------------------
### Using Python Example to Call Recording File Transcription
Source: https://help.aliyun.com/zh/isi/getting-started/examples-of-using-sdks-and-managing-restful-apis
This guide demonstrates how to transcribe pre-recorded audio files using a Python script. It requires Python 3, the Aliyun Python SDK, and proper environment variable configuration for credentials.
```APIDOC
## Using Python Example to Call Recording File Transcription
### Description
This section provides instructions for using a Python script to transcribe audio files. It covers installing the necessary SDK, configuring credentials via environment variables, and setting up the script with your specific parameters.
### Method
Python Script (SDK)
### Endpoint
N/A (SDK handles communication)
### Prerequisites:
- Python 3 installed.
- Aliyun Python SDK (`aliyun-python-sdk-core`) installed.
- Environment variables configured:
- `ALIYUN_AK_ID`: Your Aliyun AccessKey ID.
- `ALIYUN_AK_SECRET`: Your Aliyun AccessKey Secret.
- `NLS_APP_KEY`: Your Intelligent Speech Interaction AppKey.
### Parameters:
- **AccessKeyID** (string) - Required - Your Aliyun AccessKey ID (configure as environment variable `ALIYUN_AK_ID`).
- **AccessKeySecret** (string) - Required - Your Aliyun AccessKey Secret (configure as environment variable `ALIYUN_AK_SECRET`).
- **Appkey** (string) - Required - Your Intelligent Speech Interaction AppKey (configure as environment variable `NLS_APP_KEY`).
- **FileLink** (string) - Optional - The URL of the audio file to transcribe. If testing with other recordings, upload them to OSS and ensure public access or obtain a valid URL. Defaults to a demo recording.
### Setup Steps:
1. **Install Python SDK**: `pip install aliyun-python-sdk-core==2.13.3`
2. **Configure Environment Variables**: Set `ALIYUN_AK_ID`, `ALIYUN_AK_SECRET`, and `NLS_APP_KEY`.
3. **Copy and Modify Example Code**: Paste the provided Python example code into a file (e.g., `demo.py`) and replace placeholder parameters with your actual values.
### Request Example (Conceptual Python Code Snippet):
```python
# Assume environment variables are set and SDK is installed
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
# --- Parameters --- (Replace with your actual values or ensure env vars are set)
access_key_id = "YOUR_ACCESS_KEY_ID" # Or get from os.environ.get('ALIYUN_AK_ID')
access_key_secret = "YOUR_ACCESS_KEY_SECRET" # Or get from os.environ.get('ALIYUN_AK_SECRET')
app_key = "YOUR_APP_KEY" # Or get from os.environ.get('NLS_APP_KEY')
file_link = "URL_TO_YOUR_AUDIO_FILE" # Optional
# --- Initialize Client ---
client = AcsClient(access_key_id, access_key_secret, 'cn-shanghai') # Region may vary
# --- Create Request ---
request = CommonRequest()
request.set_method('POST')
request.set_domain('nls.aliyuncs.com')
request.set_version('2018-05-18') # Check for the correct API version
request.set_action_name('RecognizeCallback') # Example action, might differ for file transcription
# --- Set Parameters for Transcription ---
request.add_parameter('AppKey', app_key)
request.add_parameter('FileLink', file_link)
# Add other required parameters based on the specific file transcription API
# --- Send Request and Process Response ---
try:
response = client.do_action(request)
print(response.decode('utf-8'))
except Exception as e:
print(str(e))
```
### Response
*(The structure of the response will depend on the specific file transcription API action used. Refer to the Aliyun Intelligent Speech Interaction API documentation for detailed response formats.)*
```
--------------------------------
### Starting Speech Synthesis (start)
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-node-js-1
Details the `start` method used to initiate the speech synthesis task with specified parameters.
```APIDOC
## Starting Speech Synthesis (`start`)
### Description
The `start` method initiates the speech synthesis process based on the provided parameters. The server will then use the registered callback functions (via `on`) to return synthesis results and status updates.
### Method
`async start(param, enablePing, pingInterval)`
### Parameters
- **param** (Object) - An object containing the speech synthesis parameters (e.g., text, voice, format, etc.). See "Setting Speech Synthesis Parameters" for details.
- **enablePing** (Boolean) - Optional - Whether to automatically send ping requests to the server. Defaults to `false`.
- `true`: Send ping requests.
- `false`: Do not send ping requests.
- **pingInterval** (Number) - Optional - The interval (in milliseconds) for sending ping requests. Defaults to `6000` ms.
### Return Value
- A Promise object that resolves when the task starts successfully or rejects with an error if an issue occurs during initiation.
```
--------------------------------
### Pyaudio Installation
Source: https://help.aliyun.com/zh/isi/developer-reference/stream-input-tts-sdk-quick-start
Installation instructions for Pyaudio on macOS, Debian/Ubuntu, CentOS, and Windows.
```APIDOC
## Pyaudio Installation
### APPLE Mac OS X
```bash
brew install portaudio
pip install pyaudio
```
### Debian/Ubuntu
```bash
sudo apt-get install python-pyaudio python3-pyaudio
# or
pip install pyaudio
```
### CentOS
```bash
sudo yum install -y portaudio portaudio-devel && pip install pyaudio
```
### Microsoft Windows
```bash
python -m pip install pyaudio
```
```
--------------------------------
### Speech Transcription API Setup and Configuration
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-c-5
This section outlines the initial setup required for using the Speech Transcription API, including configuring environment variables for authentication and setting up the client.
```APIDOC
## Speech Transcription API Setup and Configuration
### Description
This section outlines the initial setup required for using the Speech Transcription API, including configuring environment variables for authentication and setting up the client.
### Environment Variables
Before calling the API, configure the following environment variables to provide access credentials:
- **NLS_AK_ENV**: Aliyun AccessKey ID
- **NLS_SK_ENV**: Aliyun AccessKey Secret
- **NLS_APPKEY_ENV**: Aliyun AppKey
### SDK Initialization
```cpp
#include "nlsClient.h"
#include "speechTranscriberRequest.h"
using namespace AlibabaNlsCommon;
using AlibabaNls::NlsClient;
using AlibabaNls::SpeechTranscriberRequest;
// Initialize NlsClient
NlsClient* client = NlsClient::getInstance();
// Create a SpeechTranscriberRequest object
SpeechTranscriberRequest* request = new SpeechTranscriberRequest();
```
### Server URL Configuration
The server URL can be configured based on network access (e.g., internal or external). Use the default public network URL or an internal URL for specific deployments like Alibaba Cloud ECS.
```cpp
// For internal access (e.g., Alibaba Cloud Shanghai ECS)
request->setUrl("ws://nls-gateway-cn-shanghai-internal.aliyuncs.com/ws/v1");
// For public network access (default)
// request->setUrl("ws://nls-gateway.aliyuncs.com/ws/v1");
```
### Token Generation
It is recommended to generate a token once and reuse it until it expires. Re-generating the token on every API call is inefficient. The `generateToken` function handles the process of obtaining a valid token and its expiration time using provided AccessKey ID and Secret.
```cpp
/**
* Generates a token using AccessKey ID and Secret and retrieves its expiration timestamp.
*/
int generateToken(std::string akId, std::string akSecret, std::string* token, long* expireTime) {
AlibabaNls::NlsToken nlsTokenRequest;
nlsTokenRequest.setAccessKeyId(akId);
nlsTokenRequest.setKeySecret(akSecret);
int ret = nlsTokenRequest.applyNlsToken();
if (ret < 0) {
printf("generateToken Failed, error code:%d msg:%s\n", ret, nlsTokenRequest.getErrorMsg());
return ret;
}
*token = nlsTokenRequest.getToken();
*expireTime = nlsTokenRequest.getExpireTime();
return 0;
}
```
### Audio Data Sending Delay Calculation
The `getSendAudioSleepTime` function calculates the appropriate delay after sending audio data to ensure smooth processing by the server. The delay depends on the data size, sample rate, and compression rate.
```cpp
/**
* Calculates the delay time for sending audio data.
* @param dataSize Size of the data to be sent.
* @param sampleRate Sample rate (16k/8K).
* @param compressRate Data compression rate (1 for uncompressed).
* @return The sleep time in milliseconds after sending audio.
*/
unsigned int getSendAudioSleepTime(int dataSize, int sampleRate, int compressRate) {
const int sampleBytes = 16; // Assuming 16-bit sampling
const int soundChannel = 1; // Assuming mono channel
int bytes = (sampleRate * sampleBytes * soundChannel) / 8;
int bytesMs = bytes / 1000;
int sleepMs = (dataSize * compressRate) / bytesMs;
return sleepMs;
}
```
```
--------------------------------
### SDK Installation and Import
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-go-1
Instructions for downloading, installing, and importing the Aliyun NLS Go SDK.
```APIDOC
## SDK Installation and Import
### Download and Install SDK
Use the following command to download and install the SDK:
```go
go get github.com/aliyun/alibabacloud-nls-go-sdk
```
### Import SDK
Import the SDK in your code as follows:
```go
import ("github.com/aliyun/alibabacloud-nls-go-sdk")
```
```
--------------------------------
### Go SDK Installation
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-go
Instructions for downloading and installing the Go SDK for Aliyun's Intelligent Speech Service.
```APIDOC
## Go SDK Installation
### Description
This section covers the download and installation process for the Go SDK.
### 1. Download and Install SDK
Use the following command to download and install the SDK:
```bash
go get github.com/aliyun/alibabacloud-nls-go-sdk
```
### 2. Import SDK
Import the SDK in your Go code using the following statement:
```go
import ("github.com/aliyun/alibabacloud-nls-go-sdk")
```
```
--------------------------------
### Node.js: Make Aliyun TTS GET Request
Source: https://help.aliyun.com/zh/isi/developer-reference/restful-api-3
This snippet shows how to make a GET request to the Aliyun TTS API to convert text to speech. It requires the 'request' and 'fs' modules. The function constructs the URL with necessary parameters and handles the response, saving the audio if successful. Ensure 'request' is installed via 'npm install request --save'.
```javascript
const request = require('request');
const fs = require('fs');
function processGETRequest(appkey, token, text, audioSaveFile, format, sampleRate) {
var url = 'https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts';
/**
* 设置URL请求参数。
*/
url = url + '?appkey=' + appkey;
url = url + '&token=' + token;
url = url + '&text=' + text;
url = url + '&format=' + format;
url = url + '&sample_rate=' + sampleRate;
// voice 发音人,可选,默认是xiaoyun。
// url = url + "&voice=" + "xiaoyun";
// volume 音量,范围是0~100,可选,默认50。
// url = url + "&volume=" + 50;
// speech_rate 语速,范围是-500~500,可选,默认是0。
// url = url + "&speech_rate=" + 0;
// pitch_rate 语调,范围是-500~500,可选,默认是0。
// url = url + "&pitch_rate=" + 0;
console.log(url);
/**
* 设置HTTPS GET请求。
* encoding必须设置为null,HTTPS响应的Body为二进制Buffer类型。
*/
var options = {
url: url,
method: 'GET',
encoding: null
};
request(options, function (error, response, body) {
/**
* 处理服务端的响应。
*/
if (error != null) {
console.log(error);
}
else {
var contentType = response.headers['content-type'];
if (contentType === undefined || contentType != 'audio/mpeg') {
console.log(body.toString());
console.log('The GET request failed!');
}
else {
fs.writeFileSync(audioSaveFile, body);
console.log('The GET request is succeed!');
}
}
});
}
var appkey = '您的appkey';
var token = '您的token';
var text = '今天是周一,天气挺好的。';
var textUrlEncode = encodeURIComponent(text)
.replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
console.log(textUrlEncode);
var audioSaveFile = 'syAudio.wav';
var format = 'wav';
var sampleRate = 16000;
processGETRequest(appkey, token, textUrlEncode, audioSaveFile, format, sampleRate);
```
--------------------------------
### SDK Download and Installation
Source: https://help.aliyun.com/zh/isi/developer-reference/c-sdk
Provides instructions on how to download and install the C++ SDK, including options for cloning from GitHub or downloading pre-compiled packages for different platforms.
```APIDOC
## SDK Download and Installation
### SDK Download
You can obtain the SDK using the following two methods:
* **Method 1: From GitHub**
Clone the latest source code from GitHub. Detailed compilation and running instructions can be found in the `readme.md` file within the repository.
```
git clone --depth 1 https://github.com/aliyun/alibabacloud-nls-cpp-sdk
```
* **Method 2: Download SDK Packages**
Download the required SDK package directly from the table below. The SDK source package contains the original code and needs to be compiled using the methods described later. Other platform-specific SDK packages include the necessary library and header files, and do not require compilation.
**Latest SDK Package** | **Platform** | **MD5**
---|---|---
alibabacloud-nls-cpp-sdk-3.3.0b-master_cbcac53.zip | SDK Source | 7257c0998654e611cf2e8ca9867670ef
NlsCppSdk_Linux-x86_64_3.3.0b_cbcac53.tar.gz | Linux x86_64 | 9a93df607f26f1558bc1043a425af6d1
**Note:** The Linux x86_64 version above is compiled using gcc 8.4.0 with _GLIBCXX_USE_CXX11_ABI=0. You can recompile using the source package according to the instructions in `readme.md`.
* `alibabacloud-nls-cpp-sdk-master_.zip`: SDK source package.
* `NlsCppSdk___.tar.gz`: SDK package for development on the corresponding platform, refer to the internal `readme.md` for details.
### SDK Package File Description
* `scripts/build_linux.sh`: Example compilation script for the Linux platform within the SDK source code.
* `CMakeLists.txt`: Example `CMakeList` file for Linux/Android platform code projects within the SDK source code.
* `demo` directory: Contains integration example code within the SDK package. For the Linux platform, see the table below:
**Filename** | **Description**
---|---
speechRecognizerDemo.cpp | One-shot speech recognition example.
speechSynthesizerDemo.cpp | Speech synthesis example.
speechTranscriberDemo.cpp | Real-time speech recognition example.
flowingSynthesizerDemo.cpp | Streaming text-to-speech synthesis example.
fileTransferDemo.cpp | Audio file recognition example.
* `resource` directory: Contains sample audio files for speech services within the SDK source code, usable for functional testing.
**Filename** | **Description**
---|---
* test0.wav
* test1.wav
* test2.wav
* test3.wav | Test audio files (16k sampling rate, 16-bit sample format).
* `include`: Contains SDK header files within the SDK source code.
**Filename** | **Description**
---|---
nlsClient.h | SDK instance.
nlsEvent.h | Callback event descriptions.
nlsGlobal.h | SDK global header file.
nlsToken.h | SDK Access Token instance.
iNlsRequest.h | Base NLS request header file.
speechRecognizerRequest.h | One-shot speech recognition.
speechSynthesizerRequest.h | Speech synthesis, long text speech synthesis.
speechTranscriberRequest.h | Real-time audio stream recognition.
flowingSynthesizerRequest.h | Streaming text-to-speech synthesis.
FileTrans.h | Audio file recognition.
* `lib`: SDK library files.
* `readme.md`: SDK description.
* `release.log`: Version update notes.
* `version`: Version number.
```
--------------------------------
### C++ Aliyun SDK: Handle Transcription Started Callback
Source: https://help.aliyun.com/zh/isi/developer-reference/sdk-for-c-5
This C++ function `onTranscriptionStarted` is a placeholder for handling the event when the real-time transcription task starts. It's intended to log or process information related to the start of the recognition process. The provided code includes commented-out examples for printing custom parameters and the full server response.
```cpp
void onTranscriptionStarted(NlsEvent* cbEvent, void* cbParam) {
ParamCallBack* tmpParam = (ParamCallBack*)cbParam;
// 演示如何打印/使用用户自定义参数示例。
printf("onTranscriptionStarted: %d\n", tmpParam->userId);
printf("onTranscriptionStarted: status code=%d, task id=%s\n",
cbEvent->getStatusCode(),
cbEvent->getTaskId());
// 获取服务端返回的全部信息
//printf("onTranscriptionStarted: all response=%s\n", cbEvent->getAllResponse());
}
```
--------------------------------
### Start HTTP Server Command
Source: https://help.aliyun.com/zh/isi/developer-reference/streaming-text-tts-wss
This Python command starts a simple HTTP server in the current directory, typically used to serve the JavaScript example for the Aliyun streaming speech synthesis. The server runs on port 8000.
```bash
python -m http.server 8000
```
--------------------------------
### C++ SDK Download and Installation
Source: https://help.aliyun.com/zh/isi/developer-reference/c-sdk-3
Instructions for downloading and installing the C++ SDK, including options for cloning from GitHub or downloading pre-compiled packages.
```APIDOC
## C++ SDK Download and Installation
### Description
This section details how to obtain the C++ SDK for Aliyun's Intelligent Speech service.
### Methods
1. **Clone from GitHub:**
```bash
git clone --depth 1 https://github.com/aliyun/alibabacloud-nls-cpp-sdk
```
Refer to the `readme.md` file within the repository for compilation and running instructions.
2. **Download Pre-compiled Packages:**
Download the appropriate SDK package for your platform from the table below. Some packages include source code requiring compilation, while others contain the necessary library and header files.
**Latest SDK Packages:**
| Package Name | Platform | MD5 |
| :----------------------------------------------- | :--------------- | :----------------------------------- |
| `alibabacloud-nls-cpp-sdk3.2.1b-master_1e2e874.zip` | SDK Source Code | `e1d4f71f3db2e24fccd43f103573bea0` |
| `NlsCppSdk_Linux-x86_64_3.2.1b_1e2e874.tar.gz` | Linux x86_64 | `31f118233bd5b0fa2908989746aad284` |
**Note:** The Linux x86_64 version was compiled with `gcc 8.4.0` and `_GLIBCXX_USE_CXX11_ABI=0`. You can recompile using the source package and instructions in `readme.md`.
### SDK Package File Structure
- `scripts/build_linux.sh`: Example build script for Linux platforms (within SDK source).
- `CMakeLists.txt`: Example `CMakeList` file for Linux/Android platforms (within SDK source).
- `demo/`: Contains integration example code (e.g., `speechRecognizerDemo.cpp`, `speechSynthesizerDemo.cpp`, `flowingSynthesizerDemo.cpp`).
- `resource/`: Sample audio files for testing (16kHz, 16-bit).
- `include/`: SDK header files (e.g., `nlsClient.h`, `nlsEvent.h`, `speechSynthesizerRequest.h`).
- `lib/`: SDK library files.
- `readme.md`: SDK documentation.
- `release.log`: Version update log.
- `version`: SDK version number.
```
--------------------------------
### Python: HTTP Request Example
Source: https://help.aliyun.com/zh/isi/getting-started/use-http-or-https-to-obtain-an-access-token
A basic Python script template for sending HTTP requests. It assumes Python 3.4+ and requires the 'requests' library to be installed.
```python
#!/usr/bin/env python
```