### Install and Start React Client Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-web/react-demo/README.md Use these npm commands to install project dependencies and start the development server. ```bash npm install ``` ```bash npm start ``` -------------------------------- ### Install Protobuf and gRPC Go plugins Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-go/protogen/README.md Installs the latest versions of the protoc-gen-go and protoc-gen-go-grpc plugins using the go install command. Ensure your Go environment is configured correctly. ```shell go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest ``` -------------------------------- ### Install Dependencies Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-python/proto/README.md Install the required protobuf and gRPC tools via pip. ```shell pip install protobuf pip install grpcio-tools ``` -------------------------------- ### Initialize AsrClient Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-python/README.md Example of initializing the AsrClient with specific host, port, and flush data configuration. ```python client = AsrClient("127.0.0.1", "8051", enable_flush_data=False) ``` -------------------------------- ### Build and Run Audio Streaming Client Demo Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-cpp/README.md Instructions for building the audio streaming client library and its demo application. Ensure you have GCC 4.8.2 or higher installed. The demo execution requires specific parameters. ```bash make ``` ```bash ./demo/client_demo 1903 baiduai.cloud:8443 user pwd 2019-06-25T12:41:16Z 0 ``` -------------------------------- ### Configure Go SDK for File or Microphone Recognition Source: https://context7.com/baidubce/pie/llms.txt Uses command-line flags to switch between file and microphone input modes. Microphone mode requires portaudio installed on macOS. ```go package main import ( "flag" "log" "strings" client "github.com/baidubce/pie/audio-streaming-client-go/baiduasr" flagUtil "github.com/baidubce/pie/audio-streaming-client-go/flag" "github.com/baidubce/pie/audio-streaming-client-go/util" ) // 命令行参数说明: // -server_addr 服务器地址(格式: host:port) // -username 用户名 // -password 密码 // -product_id 产品 ID(默认: 1912) // -sample_rate 采样率(默认: 16000) // -audio_file 音频文件路径 // -enable_flush_data 是否返回中间结果(默认: false) // -sleep_ratio 识别速率(默认: 1) // -ssl_path SSL 证书路径(可选) // -run_type 运行模式: file 或 microphone func main() { flag.Parse() runType := strings.ToLower(flagUtil.RunType) if runType == "file" { // 文件识别模式 client.ReadFile(util.GenerateInitRequest()) } else if runType == "microphone" { // 麦克风识别模式(需要 Mac: brew install portaudio) client.ReadMicrophone(util.GenerateInitRequest()) } } // 运行示例: // go run main.go \ // -server_addr=127.0.0.1:8051 \ // -username=username \ // -password=password \ // -product_id=1912 \ // -audio_file=testaudio/speech.wav \ // -enable_flush_data=true ``` -------------------------------- ### Install SDK Offline Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-python/README.md Commands to install the SDK on local machines without internet access, including pip installation and platform-specific dependency installation. ```bash ## 如果本地机器不存在pip tar zxvf pip-19.0.3.tar.gz cd pip-19.0.3 python setup.py install ## 安装,根据机器类型进相应的目录,目前支持mac和centos cd [os]-install for file in `ls`;do pip install --no-deps $file;done ``` -------------------------------- ### Synchronous File Recognition in Java Source: https://context7.com/baidubce/pie/llms.txt This example shows how to perform synchronous audio file recognition using AsrClient. It involves building the AsrConfig, creating an AsrClient, configuring request metadata, and processing the results. Ensure the audio file path is correct and the client is shut down after use. ```java import com.baidu.acu.pie.client.AsrClient; import com.baidu.acu.pie.client.AsrClientFactory; import com.baidu.acu.pie.model.AsrConfig; import com.baidu.acu.pie.model.AsrProduct; import com.baidu.acu.pie.model.RecognitionResult; import com.baidu.acu.pie.model.RequestMetaData; import java.io.File; import java.util.List; public class SyncFileRecognition { public static void main(String[] args) { // 构建配置 AsrConfig asrConfig = AsrConfig.builder() .serverIp("127.0.0.1") .serverPort(8051) .appName("syncDemo") .product(new AsrProduct("1912", 16000)) .userName("username") .password("password") .build(); // 创建客户端 AsrClient asrClient = AsrClientFactory.buildClient(asrConfig); // 配置请求参数 RequestMetaData requestMetaData = new RequestMetaData(); requestMetaData.setSendPackageRatio(1); requestMetaData.setSleepRatio(0); requestMetaData.setTimeoutMinutes(120); requestMetaData.setEnableFlushData(false); // 只返回完整句子 // 识别文件 File audioFile = new File("testaudio/speech.wav"); List results = asrClient.syncRecognize(audioFile, requestMetaData); // 输出结果 for (RecognitionResult result : results) { System.out.println("序列号: " + result.getSerialNum()); System.out.println("开始时间: " + result.getStartTime()); System.out.println("结束时间: " + result.getEndTime()); System.out.println("识别结果: " + result.getResult()); System.out.println("是否完成: " + result.isCompleted()); System.out.println("---"); } // 关闭客户端 asrClient.shutdown(); } } ``` -------------------------------- ### Install macOS Dependencies Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-go/README.md Required system packages for running the project on macOS. ```shell brew install pkg-config brew install portaudio ``` -------------------------------- ### Configure AsrClient with ChannelConfig Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Starting from version 1.1.0, you can pass a ChannelConfig instance when building an AsrClient to control keep-alive timeout parameters. ```java ChannelConfig channelConfig = new ChannelConfig(); channelConfig.setKeepaliveTimeout(30); AsrClient client = new AsrClient(channelConfig); ``` -------------------------------- ### Add traceId to AsrException Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Starting from version 1.1.8, AsrException includes a traceId field for better tracking of recognition requests. ```java public class AsrException extends Exception { private String traceId; public AsrException(String message, String traceId) { super(message); this.traceId = traceId; } public String getTraceId() { return traceId; } } ``` -------------------------------- ### Initialize AsrClient in Python Source: https://context7.com/baidubce/pie/llms.txt Initializes the AsrClient for connecting to the ASR server. Configuration includes server address, authentication, and audio parameters. It can be initialized using product type or product ID. ```python from baidu_acu_asr.asr_client import AsrClient from baidu_acu_asr.asr_product import AsrProduct # Initialize ASR client client = AsrClient( server_ip="127.0.0.1", # ASR server address port="8051", # Service port product=AsrProduct.SPEECH_SERVICE, # Product type (optional, mutually exclusive with product_id) enable_flush_data=True, # Whether to continuously output intermediate results enable_chunk=True, # Whether to allow chunked transmission enable_long_speech=True, # Whether to allow long speech sample_point_bytes=2, # Sample point bytes send_per_seconds=0.16, # Packet interval (seconds) sleep_ratio=1, # Recognition rate (1=real-time, 0.5=double speed) app_name='my_app', # Application name (for log tracing) log_level=4, # Log level (0=Trace, 4=Error, 6=Off) user_name="username", # Username (pre-allocated by server) password="password" # Password ) # Initialize using product_id (recommended) client = AsrClient( server_ip="127.0.0.1", port="8051", product=None, enable_flush_data=True, product_id="1912", # Product ID sample_rate=16000, # Sample rate (8000 or 16000) user_name="username", password="password" ) ``` -------------------------------- ### Initialize C++ AsrClient and Perform File Recognition Source: https://context7.com/baidubce/pie/llms.txt Uses AsrClient to manage connections and audio streaming. Requires picosha2 for token generation and a valid audio file path. ```cpp #include "audio_streaming_client.h" #include #include #include #include "picosha2.h" typedef com::baidu::acu::pie::AsrClient AsrClient; typedef com::baidu::acu::pie::AsrStream AsrStream; typedef com::baidu::acu::pie::AudioFragmentResponse AudioFragmentResponse; typedef com::baidu::acu::pie::AudioFragmentResult AudioFragmentResult; // 识别结果回调函数 void recognition_callback(AudioFragmentResponse& resp, void* data) { if (resp.type() == com::baidu::acu::pie::FRAGMENT_DATA) { AudioFragmentResult* fragment = resp.mutable_audio_fragment(); std::cout << "序列号: " << fragment->serial_num() << std::endl; std::cout << "开始时间: " << fragment->start_time() << std::endl; std::cout << "结束时间: " << fragment->end_time() << std::endl; std::cout << "识别结果: " << fragment->result() << std::endl; std::cout << "是否完成: " << (fragment->completed() ? "是" : "否") << std::endl; } else { std::cerr << "错误类型: " << resp.type() << std::endl; } } int main(int argc, char* argv[]) { // 创建 ASR 客户端 AsrClient client; client.set_app_name("cpp_client"); client.set_enable_flush_data(true); client.set_product_id("1912"); // 初始化连接(地址:端口, SSL标志) client.init("127.0.0.1:8051", 0); // 0=不使用SSL // 设置认证信息 std::string user_name = "username"; std::string password = "password"; std::string expire_time = "2025-12-31T23:59:59Z"; client.set_user_name(user_name.c_str()); client.set_expire_time(expire_time.c_str()); // 生成 token std::string token_str = user_name + password + expire_time; std::string token = picosha2::hash256_hex_string(token_str); client.set_token(token.c_str()); // 获取识别流 AsrStream* stream = client.get_stream(); // 打开音频文件 std::string audio_file = "testaudio/speech.wav"; FILE* fp = fopen(audio_file.c_str(), "rb"); if (!fp) { std::cerr << "无法打开文件: " << audio_file << std::endl; return -1; } // 启动写入线程 int package_size = client.get_send_package_size(); std::thread writer([&stream, fp, package_size]() { char buffer[package_size]; while (!feof(fp)) { size_t count = fread(buffer, 1, package_size, fp); if (stream->write(buffer, count, false) != 0) { std::cerr << "写入失败" << std::endl; break; } } stream->write(nullptr, 0, true); // 发送结束标志 }); // 读取识别结果 while (stream->read(recognition_callback, nullptr) == 0) { // 继续读取 } writer.join(); client.destroy_stream(stream); fclose(fp); return 0; } ``` -------------------------------- ### Async Stream Recognition in Java Source: https://context7.com/baidubce/pie/llms.txt Demonstrates setting up and using the AsrClient for asynchronous streaming recognition. Configure the client, set request metadata, and stream audio data for real-time results. Ensure to handle errors and shut down the client properly. ```java import com.baidu.acu.pie.client.AsrClient; import com.baidu.acu.pie.client.AsrClientFactory; import com.baidu.acu.pie.client.Consumer; import com.baidu.acu.pie.exception.GlobalException; import com.baidu.acu.pie.model.AsrConfig; import com.baidu.acu.pie.model.AsrProduct; import com.baidu.acu.pie.model.RecognitionResult; import com.baidu.acu.pie.model.RequestMetaData; import com.baidu.acu.pie.model.StreamContext; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; public class AsyncStreamRecognition { public static void main(String[] args) { // 构建配置 AsrConfig asrConfig = AsrConfig.builder() .serverIp("127.0.0.1") .serverPort(8051) .appName("asyncDemo") .product(new AsrProduct("1912", 16000)) .userName("username") .password("password") .build(); // 创建客户端 AsrClient asrClient = AsrClientFactory.buildClient(asrConfig); // 配置请求参数 RequestMetaData requestMetaData = new RequestMetaData(); requestMetaData.setSendPackageRatio(1); requestMetaData.setSleepRatio(0); requestMetaData.setTimeoutMinutes(120); requestMetaData.setEnableFlushData(true); // 返回中间结果 // 创建异步识别流 StreamContext streamContext = asrClient.asyncRecognize( new Consumer() { @Override public void accept(RecognitionResult result) { System.out.println("收到结果: " + result.getResult()); System.out.println("是否完成: " + result.isCompleted()); } }, requestMetaData ); // 设置错误回调 streamContext.enableCallback(new Consumer() { @Override public void accept(GlobalException e) { if (e != null) { System.err.println("发生错误: " + e.getMessage()); } } }); // 发送音频数据 String audioFilePath = "testaudio/speech.wav"; try (InputStream audioStream = Files.newInputStream(Paths.get(audioFilePath))) { byte[] data = new byte[asrClient.getFragmentSize(requestMetaData)]; while (audioStream.read(data) != -1 && !streamContext.getFinishLatch().finished()) { streamContext.send(data); Thread.sleep(20); // 模拟实时音频速率 } streamContext.complete(); streamContext.getFinishLatch().await(); } catch (Exception e) { e.printStackTrace(); } finally { asrClient.shutdown(); } } } ``` -------------------------------- ### Batch Recognition Demo Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Version 0.9.2 includes a simple demo for batch recognition of audio files in a folder and outputting results to a file. This does not support recursive directory traversal. ```java // Refer to JavaDemo for the batch recognition example. ``` -------------------------------- ### Execute the Java application Source: https://github.com/baidubce/pie/blob/master/java-demo/README.md Run the packaged JAR file with the required configuration parameters for the ASR service. ```shell java -jar java-demo-1.2.1-SNAPSHOT-shaded.jar --ip=127.0.0.1 --port=8050 --pid=888 --username=admin --password=admin --audio-path=16k.wav --enable-flush-data=false # java -jar java-demo-1.0-SNAPSHOT-jar-with-dependencies.jar -h 查看帮助 ``` -------------------------------- ### Configure AsrConfig for Java SDK Source: https://context7.com/baidubce/pie/llms.txt Use AsrConfig.builder() to set connection parameters like server IP, port, application name, product ID, sampling rate, username, and password. SSL configuration is optional. The constructed AsrConfig object is immutable. ```java import com.baidu.acu.pie.model.AsrConfig; import com.baidu.acu.pie.model.AsrProduct; // 构建 ASR 配置 AsrConfig asrConfig = AsrConfig.builder() .serverIp("127.0.0.1") // 服务器地址 .serverPort(8051) // 服务端口 .appName("myJavaApp") // 应用名称 .product(new AsrProduct("1912", 16000)) // 产品 ID 和采样率 .userName("username") // 用户名 .password("password") // 密码 // SSL 配置(可选) // .sslUseFlag(true) // .sslPath("cert/server.crt") .build(); // 使用预定义产品类型 AsrConfig configWithProduct = AsrConfig.builder() .serverIp("127.0.0.1") .serverPort(8051) .appName("myJavaApp") .product(AsrProduct.SPEECH_SERVICE) .userName("username") .password("password") .build(); ``` -------------------------------- ### Package the Java project Source: https://github.com/baidubce/pie/blob/master/java-demo/README.md Use Maven to package the project using either the shade or assembly plugin. ```shell # 使用shade插件,新版本使用此插件 mvn clean package # 使用assembly插件 mvn clean package assembly:single ``` -------------------------------- ### AsrClient Initialization Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-python/README.md Configuration parameters for initializing the AsrClient instance. ```APIDOC ## Initialization Parameters ### Description Parameters used when initializing the AsrClient instance (e.g., AsrClient("127.0.0.1", "8051", ...)). ### Parameters - **enable_chunk** (bool) - Optional - Default: True - Whether to allow chunking. - **enable_long_speech** (bool) - Optional - Default: True - Whether to allow long speech. - **enable_flush_data** (bool) - Optional - Default: True - Whether to output continuously. - **product_id** (string) - Optional - Default: 1903 - Model ID for the decoder. - **sample_point_bytes** (int) - Optional - Default: 2 - Sample point bytes. - **send_per_seconds** (double) - Optional - Default: 0.16 - Packet sending interval. - **sleep_ratio** (double) - Optional - Default: 1 - ASR recognition duration interval. - **app_name** (String) - Optional - Default: python - Application name for logging. - **log_level** (int) - Optional - Default: 4 - Log level (0:Trace to 6:Off). - **user_name** (String) - Optional - Default: python - Username assigned by the server. - **expire_time** (String) - Optional - Default: python - Expiration time in UTC format. - **token** (String) - Optional - Default: python - SHA256 token generated from user_name, password, and expire_time. ``` -------------------------------- ### Run PIE Recognition Commands Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-go/README.md Execute file or microphone recognition tasks using Go run commands with specific server and authentication parameters. ```shell # 可通过 go run main.go -h 查看可输入的命令 # mac 文件识别 go run main_darwin.go -server_addr=127.0.0.1:8051 -username 123 -password 123 # mac 麦克风识别 go run main_microphone_darwin.go -server_addr=127.0.0.1:8051 -username 123 -password 123 -run_type microphone # linux 文件识别 go run main_linux.go -server_addr=127.0.0.1:8051 -username 123 -password 123 # 编译二进制 GOOS=darwin GOARCH=arm64 go build ``` -------------------------------- ### Deploy to Bintray Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/README.md Command to clean and deploy the project. ```bash mvn clean deploy ``` -------------------------------- ### Generate Proto Code (Old Version) Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-go/protogen/README.md Generates Go code from an audio_streaming.proto file using an older protoc command syntax. This command includes the include path and specifies the output directory. ```shell script protoc -I ./ audio_streaming.proto --go_out=plugins=grpc:. ``` -------------------------------- ### Generate Proto Code (New Version) Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-go/protogen/README.md Generates Go code from an audio_streaming.proto file using the new protoc command structure. It specifies output paths for both Go and gRPC plugins. ```shell script protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative audio_streaming.proto ``` -------------------------------- ### Build and Generate gRPC Code Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/README.md Commands for compiling the project and generating gRPC-related code. ```bash mvn clean compile ``` ```bash mvn protobuf:compile ``` ```bash mvn protobuf:compile-custom ``` -------------------------------- ### Web Client Configuration Parameters Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-web/README.md Configuration options for the web client, including WebSocket URL and default parameters. ```APIDOC ## Web Client Configuration ### Description Configuration parameters for the web client. ### Parameters #### Query Parameters - **WS_URL** (string) - Required - The WebSocket URL for establishing a connection. - **DEFAULT_PARAMS** (object) - Optional - Default parameters to be used in requests. - **onMessage** (function) - Required - Callback function to receive real-time recognition results. Supports visualization of character changes. ### Notes 1. **Audio Device Access**: Requires a trusted environment. This includes: - Using `localhost` as the domain for local addresses. - Using `127.0.0.1` for the local loopback address. - For other environments, HTTPS access is required. 2. **Audio Stream Upload Interval**: Sending audio stream data with an interval of 0-18ms can lead to recognition delays (results appearing after 2-3 minutes). Intervals of 19ms or more do not cause such delays. This can be configured using the `INTERVAL` parameter. ``` -------------------------------- ### Asynchronous File Recognition with C# AsrClient Source: https://context7.com/baidubce/pie/llms.txt Demonstrates asynchronous file recognition using the AsrClient. This method reads an audio file in chunks, sends it to the stream, and processes the recognition results asynchronously. Ensure the AsrStream is properly initialized and the file path is correct. ```csharp using System; using System.IO; using System.Threading.Tasks; using Com.Baidu.Acu.Pie; class AsrDemo { // 异步文件识别 public async Task FileAsrAsync(AsrStream stream, string fileName, int packageSize) { Console.WriteLine($"开始识别文件: {fileName}"); FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(file); // 启动异步读取任务 var responseReaderTask = Task.Run(async () => { while (await stream.MoveNext()) { var response = stream.Current(); if (response.ErrorCode == 0) { if (response.Type == ResponseType.FragmentData) { var fragment = response.AudioFragment; Console.WriteLine($"序列号: {fragment.SerialNum}"); Console.WriteLine($"开始时间: {fragment.StartTime}"); Console.WriteLine($"结束时间: {fragment.EndTime}"); Console.WriteLine($"识别结果: {fragment.Result}"); } } else { Console.WriteLine($"错误码: {response.ErrorCode}, 错误信息: {response.ErrorMessage}"); } } }); // 发送音频数据 while (true) { var bytes = reader.ReadBytes(packageSize); if (bytes.Length == 0) { await stream.WriteComplete(); break; } await stream.Write(bytes); } await responseReaderTask; reader.Close(); file.Close(); } static void Main(string[] args) { // 创建客户端 var client = new AsrClient("127.0.0.1:8051", "1912"); client.LogLevel = 4; client.Flush = true; // 返回中间结果 client.SendPerSeconds = 0.02; client.SleepRatio = 1; client.AppName = "csharp_demo"; AsrDemo demo = new AsrDemo(); // 创建带认证的流 var streamToken = new StreamToken( "username", new DateTime(2025, 12, 31, 23, 59, 59), "password" ); // 并行识别多个文件 var stream1 = client.NewStream(streamToken); var task1 = demo.FileAsrAsync(stream1, "audio1.wav", client.RecommendPacketSize); var stream2 = client.NewStream(streamToken); var task2 = demo.FileAsrAsync(stream2, "audio2.wav", client.RecommendPacketSize); Task.WaitAll(task1, task2); Console.WriteLine("所有任务完成"); } } ``` -------------------------------- ### Capture Audio Stream and Handle WebSocket Events Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-web/js-demo/index.html Initializes audio recording from the user's microphone and manages WebSocket lifecycle events. ```javascript handleAudioprocess; } startButton.addEventListener('click', () => { startButton.disabled = true; endButton.disabled = false; navigator.mediaDevices.getUserMedia({ video: false, audio: { sampleRate: SAMPLE_RATE, sampleSize: SAMPLE_SIZE, channelCount: INPUT_CHANNELS } }).then(stream => { startWebSocket(); initAudio(stream); }).catch(error => { let errorMessage; switch (error.name) { // 用户拒绝 case 'NotAllowedError': case 'PermissionDeniedError': errorMessage = '用户已禁止网页调用录音设备'; break; // 未接入录音设备 case 'NotFoundError': case 'DevicesNotFoundError': errorMessage = '录音设备未找到'; break; // 其它错误 case 'NotSupportedError': errorMessage = '不支持录音功能'; break; default: errorMessage = '录音调用错误'; } throw new Error(errorMessage); }); }); endButton.addEventListener('click', () => { audioContent && audioContent.close(); doSend(FINISH_PARAMS); }) ``` -------------------------------- ### 实现 WebSocket 实时语音识别与文件处理 Source: https://context7.com/baidubce/pie/llms.txt 包含 WebSocket 连接初始化、音频数据发送、PCM 格式转换以及麦克风流式识别和文件上传识别的完整逻辑。 ```javascript // WebSocket 配置 const WS_URL = 'ws://ai-speech.baidu.com/api/v1/asr/stream'; const SAMPLE_RATE = 16000; const BUFFER_SIZE = 512; const INTERVAL = 20; // 发送间隔(毫秒) let websocket; let audioContext; // 初始化 WebSocket 连接 function initWebSocket(onMessage) { websocket = new WebSocket(WS_URL); websocket.onopen = function(evt) { console.log('WebSocket 连接成功'); // 发送初始化参数 const initParams = JSON.stringify({ enableFlushData: true, productId: '1912', samplePointBytes: 1, sendPerSeconds: INTERVAL / 1000, sleepRatio: 0, appName: 'web_demo', userName: 'username', password: 'password' }); websocket.send(initParams); }; websocket.onmessage = function(evt) { const data = JSON.parse(evt.data); console.log('识别结果:', data.result); console.log('是否完成:', data.completed); onMessage(data); }; websocket.onerror = function(evt) { console.error('WebSocket 错误:', evt); }; websocket.onclose = function(evt) { console.log('WebSocket 连接关闭'); }; } // 发送音频数据 function sendAudioData(audioData) { if (websocket && websocket.readyState === 1) { websocket.send(audioData); } } // 结束识别 function finishRecognition() { if (websocket && websocket.readyState === 1) { websocket.send(JSON.stringify({ status: 'finish' })); } } // PCM 转换函数 function convertToPCM(float32Array) { const int16Array = new Int16Array(float32Array.length); for (let i = 0; i < float32Array.length; i++) { const s = Math.max(-1, Math.min(1, float32Array[i])); int16Array[i] = s < 0 ? s * 0x8000 : s * 0x7FFF; } return int16Array; } // 麦克风实时识别 async function startMicrophoneRecognition() { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: { sampleRate: SAMPLE_RATE, sampleSize: 16, channelCount: 1 } }); initWebSocket((data) => { document.getElementById('result').textContent += data.result; }); const AudioContext = window.AudioContext || window.webkitAudioContext; audioContext = new AudioContext({ sampleRate: SAMPLE_RATE }); const source = audioContext.createMediaStreamSource(stream); const processor = audioContext.createScriptProcessor(BUFFER_SIZE, 1, 1); processor.onaudioprocess = function(e) { const pcmData = convertToPCM(e.inputBuffer.getChannelData(0)); sendAudioData(pcmData); }; source.connect(processor); processor.connect(audioContext.destination); } catch (error) { console.error('麦克风访问失败:', error); } } // 停止识别 function stopRecognition() { if (audioContext) { audioContext.close(); } finishRecognition(); } // 文件上传识别 async function recognizeFile(file) { const reader = new FileReader(); reader.onload = async function() { initWebSocket((data) => { console.log('文件识别结果:', data.result); }); const byteArray = new Uint8Array(reader.result); const chunkSize = 512; const totalChunks = Math.ceil(byteArray.length / chunkSize); for (let i = 0; i < totalChunks; i++) { const start = i * chunkSize; const end = Math.min(start + chunkSize, byteArray.length); const chunk = new Uint8Array(reader.result, start, end - start); await new Promise(resolve => setTimeout(resolve, INTERVAL)); sendAudioData(chunk); } finishRecognition(); }; reader.readAsArrayBuffer(file); } ``` -------------------------------- ### Perform Real-time Microphone Recognition in Java Source: https://context7.com/baidubce/pie/llms.txt Uses Java Sound API to capture PCM audio and stream it to the AsrClient. Requires proper configuration of AsrConfig and AudioFormat to match the ASR service requirements. ```java import com.baidu.acu.pie.client.AsrClient; import com.baidu.acu.pie.client.AsrClientFactory; import com.baidu.acu.pie.client.Consumer; import com.baidu.acu.pie.model.AsrConfig; import com.baidu.acu.pie.model.AsrProduct; import com.baidu.acu.pie.model.RecognitionResult; import com.baidu.acu.pie.model.RequestMetaData; import com.baidu.acu.pie.model.StreamContext; import javax.sound.sampled.*; public class MicrophoneRecognition { public static void main(String[] args) { AsrConfig asrConfig = AsrConfig.builder() .serverIp("127.0.0.1") .serverPort(8051) .appName("micDemo") .product(new AsrProduct("1912", 16000)) .userName("username") .password("password") .build(); AsrClient asrClient = AsrClientFactory.buildClient(asrConfig); RequestMetaData requestMetaData = new RequestMetaData(); requestMetaData.setEnableFlushData(true); StreamContext streamContext = asrClient.asyncRecognize( result -> System.out.println("识别结果: " + result.getResult()), requestMetaData ); // 配置麦克风音频格式 AudioFormat audioFormat = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, 16000, // 采样率 16, // 位深 1, // 单声道 2, // 帧大小 16000, // 帧率 false // 小端序 ); DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat); try { TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info); line.open(audioFormat, line.getBufferSize()); line.start(); int bufferSize = asrClient.getFragmentSize(); byte[] data = new byte[bufferSize]; System.out.println("开始录音..."); while (line.read(data, 0, bufferSize) != -1 && !streamContext.getFinishLatch().finished()) { streamContext.send(data); } streamContext.complete(); streamContext.getFinishLatch().await(); line.close(); } catch (Exception e) { e.printStackTrace(); } finally { asrClient.shutdown(); } } } ``` -------------------------------- ### Introduce SLF4J for Logging Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Version 0.9.3 introduces SLF4J to ensure logging output, preventing issues caused by the upper layer forgetting to include the logging dependency. ```java import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyService { private static final Logger logger = LoggerFactory.getLogger(MyService.class); public void doSomething() { logger.info("Performing an action."); } } ``` -------------------------------- ### Sync Recognition with byte[] Input Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Version 1.1.3 introduced an overloaded syncRecognition method that accepts a byte array as input, providing an alternative for synchronous recognition. ```java public RecognitionResult syncRecognition(byte[] audioData) throws AsrException; ``` -------------------------------- ### Add Gradle Dependency Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/README.md Include the library in a Gradle project's dependencies block. ```gradle dependencies { compile "com.github.xiashuai-mr:audio-streaming-client-java:1.3.0.100-SNAPSHOTS" } ``` -------------------------------- ### Enable sendPerSeconds Parameter Setting Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Version 0.9.2 allows configuration of the sendPerSeconds parameter and enables sleepRatio to be less than 1. ```java AsrConfig config = new AsrConfig.Builder() .setSendPerSeconds(5) .setSleepRatio(0.8) .build(); ``` -------------------------------- ### AsrConfig Builder Pattern Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md AsrConfig in version 1.0.0 supports the builder pattern for easier configuration, along with standard getters and setters. Older methods are deprecated. ```java AsrConfig config = new AsrConfig.Builder() .setProductId("your_product_id") .build(); ``` -------------------------------- ### Recognize Audio File in Python Source: https://context7.com/baidubce/pie/llms.txt Performs offline audio file recognition using the file path. Requires the `baidu_acu_asr` library and returns an iterator for recognition results. Handles different response types, including audio fragments and errors. ```python from baidu_acu_asr.asr_client import AsrClient import baidu_acu_asr.audio_streaming_pb2 import logging logging.basicConfig(level=logging.INFO) # Initialize client client = AsrClient( server_ip="127.0.0.1", port="8051", product=None, enable_flush_data=True, product_id="1912", sample_rate=16000, user_name="username", password="password" ) # Recognize audio file audio_file_path = "testaudio/speech.wav" responses = client.get_result(audio_file_path) # Process recognition results for response in responses: if response.type == baidu_acu_asr.audio_streaming_pb2.FRAGMENT_DATA: fragment = response.audio_fragment print(f"Start time: {fragment.start_time}") print(f"End time: {fragment.end_time}") print(f"Recognition result: {fragment.result}") print(f"Serial number: {fragment.serial_num}") print(f"Is completed: {fragment.completed}") else: print(f"Error type: {response.type}, Error code: {response.error_code}") ``` -------------------------------- ### Generate gRPC Python Code Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-python/proto/README.md Use the protoc compiler to generate Python gRPC stubs from the audio_streaming.proto definition file. ```shell python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. audio_streaming.proto ``` -------------------------------- ### New Product Types: Far-field and Far-field Robot Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Version 0.9.1 introduced two new product types: 'far-field model' and 'far-field model-robot domain'. ```java // No specific code example provided, refers to new product types. ``` -------------------------------- ### Process and Stream Audio Files Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-web/js-demo/index.html Reads a local file as an ArrayBuffer and streams it in chunks via WebSocket. ```javascript const UNIT = 512; const file = document.getElementById('file'); file.onchange = function () { const newFile = file.files[0]; //读取为ArrayBuffer const reader = new FileReader(); reader.readAsArrayBuffer(newFile); //显示进度 const progress = document.getElementById('progress'); progress.max = newFile.size; progress.value = 0; reader.onprogress = function (e) { progress.value = e.loaded; } reader.onload = function () { const byteArray = new Uint8Array(reader.result); const newLength = byteArray.byteLength / UNIT; const afterOpen = async () => { // 使用setTimeout而不使用setInterval提高性能 const sleep = interval => new Promise(resolve => setTimeout(resolve, interval)); for (let i = 0; i < newLength; i++) { const newArray = (i === Math.floor(newLength) && Math.floor(newLength) !== newLength) ? new Uint8Array(reader.result, UNIT * i, byteArray.byteLength % UNIT) : new Uint8Array(reader.result, UNIT * i, UNIT); await sleep(INTERVAL); doSend(newArray); } doSend(FINISH_PARAMS); }; startWebSocket('upload', afterOpen); } } ``` -------------------------------- ### Authenticate AsrClient with Username and Password Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md In version 1.0.0, you can provide a username and password when building an AsrClient for authentication purposes. ```java AsrClient client = new AsrClient("your_username", "your_password"); ``` -------------------------------- ### Add Maven Dependency Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/README.md Include the library in a Maven project's dependencies section. ```xml com.github.xiashuai-mr audio-streaming-client-java 1.3.0.100-SNAPSHOTS ``` -------------------------------- ### WebSocket /api/v1/asr/stream Source: https://context7.com/baidubce/pie/llms.txt Establishes a WebSocket connection for real-time audio streaming and speech recognition. ```APIDOC ## WS ws://ai-speech.baidu.com/api/v1/asr/stream ### Description Establishes a persistent WebSocket connection to stream audio data for real-time speech recognition. ### Method WebSocket ### Endpoint ws://ai-speech.baidu.com/api/v1/asr/stream ### Request Body - **enableFlushData** (bool) - Required - Enable flushing data - **productId** (string) - Required - Product identifier - **samplePointBytes** (int) - Required - Sample point bytes - **sendPerSeconds** (float) - Required - Sending interval in seconds - **sleepRatio** (float) - Required - Sleep ratio for optimization - **appName** (string) - Required - Application name - **userName** (string) - Required - Authentication username - **password** (string) - Required - Authentication password ### Response #### Success Response (200) - **error_code** (int) - Error code, 0 indicates success - **error_message** (string) - Error details - **start_time** (string) - Start time of the recognized audio segment - **end_time** (string) - End time of the recognized audio segment - **result** (string) - Recognized text content - **completed** (bool) - Indicates if the sentence recognition is finished - **serial_num** (string) - Unique identifier for the sentence #### Response Example { "error_code": 0, "error_message": "", "start_time": "00:00:01.500", "end_time": "00:00:03.200", "result": "这是识别出的文字内容", "completed": true, "serial_num": "uuid-12345-67890" } ``` -------------------------------- ### Adjust Recognition with RequestMeta Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Version 1.0.0 allows passing a RequestMeta object during recognition calls to adjust parameters like packet sending speed or intermediate result display. ```java RequestMeta meta = new RequestMeta(); meta.setSendSpeed(10); client.recognize(audioData, meta); ``` -------------------------------- ### Set Product ID for Flexible Model Selection Source: https://github.com/baidubce/pie/blob/master/audio-streaming-client-java/CHANGES.md Version 0.9.4 allows direct setting of a productId for more flexible model selection, especially for model types not present in AsrProduct. Refer to JavaDemo line 43 for usage. ```java String productId = "your_custom_product_id"; AsrConfig config = new AsrConfig.Builder().setProductId(productId).build(); ```