Note
這項功能目前處於公開預覽狀態。 此預覽版是在沒有服務等級協定的情況下提供的,不建議用於生產工作負載。 某些功能可能不被支援或功能受限。 欲了解更多資訊,請參閱 Microsoft Azure 預覽版補充使用條款。
即時多聲道轉錄處理立體聲(雙聲道)音訊檔案或串流,並回傳依聲道標記的識別結果。 當每個頻道攜帶你想獨立轉錄的獨立音訊來源時,例如客服通話的兩端,請使用它。 語音服務同時轉錄最多兩個頻道,並報告每個識別結果的來源頻道。
兩個通道間大量重疊的語音會增加結果處理延遲。
參考文件 | 套件 (PyPi) | GitHub 上的其他範例
在本指南中,你可以使用 Python 的語音 SDK 轉錄立體聲音訊來源,並為每個聲道讀取獨立的語音轉文字結果。
先決條件
- Python 版本 1.51.0 或更新版的語音 SDK。
- 雙聲道(立體聲)WAV 檔案或立體聲音訊串流。 語音服務最多可轉錄兩個頻道。
建立語音配置並啟用多頻道處理
建立一個 SpeechConfig 實例並將屬性設 Speech_EnableMultiChannelProcessing 為 true。 用你的語音資源端點和鍵替換 YourSpeechEndpoint 和 YourSpeechKey 。
import azure.cognitiveservices.speech as speechsdk
speech_config = speechsdk.SpeechConfig(
subscription="YourSpeechKey", endpoint="YourSpeechEndpoint")
speech_config.speech_recognition_language = "en-US"
# Enable per-channel transcription of up to two channels.
speech_config.set_property(speechsdk.PropertyId.Speech_EnableMultiChannelProcessing, "true")
提供立體聲音訊
多聲道轉錄可接受立體聲音訊檔案或立體聲串流。 要轉錄檔案,請從檔名建立一個 AudioConfig 實例:
audio_config = speechsdk.audio.AudioConfig(filename="stereo.wav")
對於即時來源,請從推送或拉取串流建立實 AudioConfig 例。 建立串流格式時,請將頻道數量設為 2:
# Match the format of your stereo source: sample rate, bits per sample, and 2 channels.
stream_format = speechsdk.audio.AudioStreamFormat(samples_per_second=16000, bits_per_sample=16, channels=2)
push_stream = speechsdk.audio.PushAudioInputStream(stream_format=stream_format)
audio_config = speechsdk.audio.AudioConfig(stream=push_stream)
# Write raw PCM audio (without the WAV header) to push_stream as it becomes available,
# and call push_stream.close() when the source ends.
識別並讀取每個通道的結果
建立一個 SpeechRecognizer 範例並使用持續識別。 每個最終結果都會包含該 channel 屬性中的來源通道。 多通道轉錄僅支援連續識別;不支援單發識別recognize_once()。
import time
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
done = False
def recognized_cb(evt):
if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("RECOGNIZED (channel {}): {}".format(evt.result.channel, evt.result.text))
def stop_cb(evt):
global done
done = True
speech_recognizer.recognized.connect(recognized_cb)
speech_recognizer.session_stopped.connect(stop_cb)
speech_recognizer.canceled.connect(stop_cb)
speech_recognizer.start_continuous_recognition()
while not done:
time.sleep(0.5)
speech_recognizer.stop_continuous_recognition()
這個 channel 特性會識別產生結果的零基通道,因此你可以將每個通道的逐字稿分開保存。
結合多通道轉錄與日記
若要識別每個聲道中的說話者,請使用 a ConversationTranscriber 代替 SpeechRecognizer。 轉錄員會報告事件的 transcribed 最終結果,每個結果都包含 與 channelspeaker_id。
import time
conversation_transcriber = speechsdk.transcription.ConversationTranscriber(
speech_config=speech_config, audio_config=audio_config)
done = False
def transcribed_cb(evt):
if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("TRANSCRIBED (channel {}, speaker {}): {}".format(
evt.result.channel, evt.result.speaker_id, evt.result.text))
def stop_cb(evt):
global done
done = True
conversation_transcriber.transcribed.connect(transcribed_cb)
conversation_transcriber.session_stopped.connect(stop_cb)
conversation_transcriber.canceled.connect(stop_cb)
conversation_transcriber.start_transcribing_async().get()
while not done:
time.sleep(0.5)
conversation_transcriber.stop_transcribing_async().get()
參考文件 | 套件 (NuGet) | GitHub 上的其他範例
在本指南中,你會使用 C# 的語音 SDK 來轉錄立體聲音訊來源,並為每個聲道讀取獨立的語音轉文字結果。
先決條件
- C# 版本 1.51.0 或更新版的 Speech SDK 。
- 雙聲道(立體聲)WAV 檔案或立體聲音訊串流。 語音服務最多可轉錄兩個頻道。
建立語音配置並啟用多頻道處理
建立一個 SpeechConfig 實例並將屬性設 Speech_EnableMultiChannelProcessing 為 true。 用你的語音資源端點和鍵替換 YourSpeechEndpoint 和 YourSpeechKey 。
using System;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using Microsoft.CognitiveServices.Speech.Transcription;
var speechConfig = SpeechConfig.FromEndpoint(
new Uri("YourSpeechEndpoint"), "YourSpeechKey");
speechConfig.SpeechRecognitionLanguage = "en-US";
// Enable per-channel transcription of up to two channels.
speechConfig.SetProperty(PropertyId.Speech_EnableMultiChannelProcessing, "true");
提供立體聲音訊
多聲道轉錄可接受立體聲音訊檔案或立體聲串流。 要轉錄檔案,請從檔名建立一個 AudioConfig 實例:
using var audioConfig = AudioConfig.FromWavFileInput("stereo.wav");
對於即時來源,請從推送或拉取串流建立實 AudioConfig 例。 建立串流格式時,請將頻道數量設為 2:
// Match the format of your stereo source: sample rate, bits per sample, and 2 channels.
var streamFormat = AudioStreamFormat.GetWaveFormatPCM(16000, 16, 2);
var pushStream = AudioInputStream.CreatePushStream(streamFormat);
using var audioConfig = AudioConfig.FromStreamInput(pushStream);
// Write raw PCM audio (without the WAV header) to pushStream as it becomes available,
// and call pushStream.Close() when the source ends.
識別並讀取每個通道的結果
建立一個 SpeechRecognizer 範例並使用持續識別。 每個最終結果都會包含該 Channel 屬性中的來源通道。 多通道轉錄僅支援連續識別;不支援單發識別RecognizeOnceAsync()。
using var recognizer = new SpeechRecognizer(speechConfig, audioConfig);
var stopRecognition = new TaskCompletionSource<int>();
recognizer.Recognized += (s, e) =>
{
if (e.Result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"RECOGNIZED (channel {e.Result.Channel}): {e.Result.Text}");
}
};
recognizer.Canceled += (s, e) => stopRecognition.TrySetResult(0);
recognizer.SessionStopped += (s, e) => stopRecognition.TrySetResult(0);
await recognizer.StartContinuousRecognitionAsync();
Task.WaitAny(new[] { stopRecognition.Task });
await recognizer.StopContinuousRecognitionAsync();
這個 Channel 特性會識別產生結果的零基通道,因此你可以將每個通道的逐字稿分開保存。
結合多通道轉錄與日記
若要識別每個聲道中的說話者,請使用 a ConversationTranscriber 代替 SpeechRecognizer。 轉錄員會報告活動的 Transcribed 最終結果,每個結果都包含頻道名稱和講者編號。
using var conversationTranscriber = new ConversationTranscriber(speechConfig, audioConfig);
var stopTranscription = new TaskCompletionSource<int>();
conversationTranscriber.Transcribed += (s, e) =>
{
if (e.Result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"TRANSCRIBED (channel {e.Result.Channel}, speaker {e.Result.SpeakerId}): {e.Result.Text}");
}
};
conversationTranscriber.Canceled += (s, e) => stopTranscription.TrySetResult(0);
conversationTranscriber.SessionStopped += (s, e) => stopTranscription.TrySetResult(0);
await conversationTranscriber.StartTranscribingAsync();
Task.WaitAny(new[] { stopTranscription.Task });
await conversationTranscriber.StopTranscribingAsync();
參考文件 | 套件 (NuGet) | GitHub 上的其他範例
在本指南中,你將使用 C++ 的語音 SDK 來轉錄立體聲音訊來源,並為每個聲道讀取獨立的語音轉文字結果。
先決條件
- C++ 1.51.0 或更新版本的 語音 SDK 。
- 雙聲道(立體聲)WAV 檔案或立體聲音訊串流。 語音服務最多可轉錄兩個頻道。
建立語音配置並啟用多頻道處理
建立一個 SpeechConfig 實例並將屬性設 Speech_EnableMultiChannelProcessing 為 true。 用你的語音資源端點和鍵替換 YourSpeechEndpoint 和 YourSpeechKey 。
#include <speechapi_cxx.h>
using namespace Microsoft::CognitiveServices::Speech;
using namespace Microsoft::CognitiveServices::Speech::Audio;
using namespace Microsoft::CognitiveServices::Speech::Transcription;
auto speechConfig = SpeechConfig::FromEndpoint(
"YourSpeechEndpoint", "YourSpeechKey");
speechConfig->SetSpeechRecognitionLanguage("en-US");
// Enable per-channel transcription of up to two channels.
speechConfig->SetProperty(PropertyId::Speech_EnableMultiChannelProcessing, "true");
提供立體聲音訊
多聲道轉錄可接受立體聲音訊檔案或立體聲串流。 要轉錄檔案,請從檔名建立一個 AudioConfig 實例:
auto audioConfig = AudioConfig::FromWavFileInput("stereo.wav");
對於即時來源,請從推送或拉取串流建立實 AudioConfig 例。 建立串流格式時,請將頻道數量設為 2:
// Match the format of your stereo source: sample rate, bits per sample, and 2 channels.
auto streamFormat = AudioStreamFormat::GetWaveFormatPCM(16000, 16, 2);
auto pushStream = AudioInputStream::CreatePushStream(streamFormat);
auto audioConfig = AudioConfig::FromStreamInput(pushStream);
// Write raw PCM audio (without the WAV header) to pushStream as it becomes available,
// and call pushStream->Close() when the source ends.
識別並讀取每個通道的結果
建立一個 SpeechRecognizer 範例並使用持續識別。 每個最終結果都包含該方法的 Channel() 來源通道。 多通道轉錄僅支援連續識別;不支援單發識別RecognizeOnceAsync()。
#include <atomic>
#include <future>
auto recognizer = SpeechRecognizer::FromConfig(speechConfig, audioConfig);
std::promise<void> recognitionEnd;
std::atomic<bool> recognitionEnded{false};
auto signalRecognitionEnd = [&recognitionEnd, &recognitionEnded]()
{
if (!recognitionEnded.exchange(true))
{
recognitionEnd.set_value();
}
};
recognizer->Recognized.Connect([](const SpeechRecognitionEventArgs& e)
{
if (e.Result->Reason == ResultReason::RecognizedSpeech)
{
printf("RECOGNIZED (channel %d): %s\n", e.Result->Channel(), e.Result->Text.c_str());
}
});
recognizer->Canceled.Connect([&signalRecognitionEnd](const SpeechRecognitionCanceledEventArgs& e)
{
if (e.Reason == CancellationReason::Error)
{
signalRecognitionEnd();
}
});
recognizer->SessionStopped.Connect([&signalRecognitionEnd](const SessionEventArgs& e)
{
signalRecognitionEnd();
});
recognizer->StartContinuousRecognitionAsync().get();
recognitionEnd.get_future().get();
recognizer->StopContinuousRecognitionAsync().get();
該 Channel() 方法會回傳產生結果的零基通道,因此你可以將每個通道的逐字稿分開保存。
結合多通道轉錄與日記
若要識別每個聲道中的說話者,請使用 a ConversationTranscriber 代替 SpeechRecognizer。 轉錄員會報告活動的 Transcribed 最終結果,每個結果都包含頻道名稱和講者編號。
auto conversationTranscriber = ConversationTranscriber::FromConfig(speechConfig, audioConfig);
std::promise<void> transcriptionEnd;
std::atomic<bool> transcriptionEnded{false};
auto signalTranscriptionEnd = [&transcriptionEnd, &transcriptionEnded]()
{
if (!transcriptionEnded.exchange(true))
{
transcriptionEnd.set_value();
}
};
conversationTranscriber->Transcribed.Connect([](const ConversationTranscriptionEventArgs& e)
{
if (e.Result->Reason == ResultReason::RecognizedSpeech)
{
printf("TRANSCRIBED (channel %d, speaker %s): %s\n",
e.Result->Channel(), e.Result->SpeakerId.c_str(), e.Result->Text.c_str());
}
});
conversationTranscriber->Canceled.Connect([&signalTranscriptionEnd](const ConversationTranscriptionCanceledEventArgs& e)
{
if (e.Reason == CancellationReason::Error)
{
signalTranscriptionEnd();
}
});
conversationTranscriber->SessionStopped.Connect([&signalTranscriptionEnd](const SessionEventArgs& e)
{
signalTranscriptionEnd();
});
conversationTranscriber->StartTranscribingAsync().get();
transcriptionEnd.get_future().get();
conversationTranscriber->StopTranscribingAsync().get();
在本指南中,你使用 Speech SDK for Java 來轉錄立體聲音訊來源,並為每個聲道分別讀取語音轉文字結果。
先決條件
- Java 版本 1.51.0 或更新版本的語音 SDK。
- 雙聲道(立體聲)WAV 檔案或立體聲音訊串流。 語音服務最多可轉錄兩個頻道。
建立語音配置並啟用多頻道處理
建立一個 SpeechConfig 實例並將屬性設 Speech_EnableMultiChannelProcessing 為 true。 用你的語音資源端點和鍵替換 YourSpeechEndpoint 和 YourSpeechKey 。
import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.*;
import com.microsoft.cognitiveservices.speech.transcription.*;
import java.net.URI;
SpeechConfig speechConfig = SpeechConfig.fromEndpoint(
new URI("YourSpeechEndpoint"), "YourSpeechKey");
speechConfig.setSpeechRecognitionLanguage("en-US");
// Enable per-channel transcription of up to two channels.
speechConfig.setProperty(PropertyId.Speech_EnableMultiChannelProcessing, "true");
提供立體聲音訊
多聲道轉錄可接受立體聲音訊檔案或立體聲串流。 要轉錄檔案,請從檔名建立一個 AudioConfig 實例:
AudioConfig audioConfig = AudioConfig.fromWavFileInput("stereo.wav");
對於即時來源,請從推送或拉取串流建立實 AudioConfig 例。 建立串流格式時,請將頻道數量設為 2:
// Match the format of your stereo source: sample rate, bits per sample, and 2 channels.
AudioStreamFormat streamFormat = AudioStreamFormat.getWaveFormatPCM(16000, (short)16, (short)2);
PushAudioInputStream pushStream = AudioInputStream.createPushStream(streamFormat);
AudioConfig audioConfig = AudioConfig.fromStreamInput(pushStream);
// Write raw PCM audio (without the WAV header) to pushStream as it becomes available,
// and call pushStream.close() when the source ends.
識別並讀取每個通道的結果
建立一個 SpeechRecognizer 範例並使用持續識別。 每個最終結果都包含該方法的 getChannel() 來源通道。 多通道轉錄僅支援連續識別;不支援單發識別recognizeOnceAsync()。
import java.util.concurrent.Semaphore;
SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, audioConfig);
Semaphore recognitionEnd = new Semaphore(0);
recognizer.recognized.addEventListener((s, e) -> {
if (e.getResult().getReason() == ResultReason.RecognizedSpeech) {
System.out.println("RECOGNIZED (channel " + e.getResult().getChannel() + "): " + e.getResult().getText());
}
});
recognizer.canceled.addEventListener((s, e) -> {
if (e.getReason() == CancellationReason.Error) {
recognitionEnd.release();
}
});
recognizer.sessionStopped.addEventListener((s, e) -> recognitionEnd.release());
recognizer.startContinuousRecognitionAsync().get();
recognitionEnd.acquire();
recognizer.stopContinuousRecognitionAsync().get();
recognizer.close();
該 getChannel() 方法會回傳產生結果的零基通道,因此你可以將每個通道的逐字稿分開保存。
結合多通道轉錄與日記
若要識別每個聲道中的說話者,請使用 a ConversationTranscriber 代替 SpeechRecognizer。 轉錄員會報告活動的 transcribed 最終結果,每個結果都包含頻道名稱和講者編號。
ConversationTranscriber conversationTranscriber = new ConversationTranscriber(speechConfig, audioConfig);
Semaphore transcriptionEnd = new Semaphore(0);
conversationTranscriber.transcribed.addEventListener((s, e) -> {
if (e.getResult().getReason() == ResultReason.RecognizedSpeech) {
System.out.println("TRANSCRIBED (channel " + e.getResult().getChannel()
+ ", speaker " + e.getResult().getSpeakerId() + "): " + e.getResult().getText());
}
});
conversationTranscriber.canceled.addEventListener((s, e) -> {
if (e.getReason() == CancellationReason.Error) {
transcriptionEnd.release();
}
});
conversationTranscriber.sessionStopped.addEventListener((s, e) -> transcriptionEnd.release());
conversationTranscriber.startTranscribingAsync().get();
transcriptionEnd.acquire();
conversationTranscriber.stopTranscribingAsync().get();
conversationTranscriber.close();
參考文件 | 套件 (npm) | GitHub 上的其他範例 | 程式庫原始程式碼
在本指南中,你要使用 JavaScript 的語音 SDK 來轉錄立體聲音訊來源,並為每個聲道讀取獨立的語音轉文字結果。
先決條件
- JavaScript 版本 1.51.0 或更新版本的 語音 SDK 。
- 雙聲道(立體聲)WAV 檔案或立體聲音訊串流。 語音服務最多可轉錄兩個頻道。
建立語音配置並啟用多頻道處理
建立一個 SpeechConfig 實例並將屬性設 Speech_EnableMultiChannelProcessing 為 true。 用你的語音資源端點和鍵替換 YourSpeechEndpoint 和 YourSpeechKey 。
const sdk = require("microsoft-cognitiveservices-speech-sdk");
const speechConfig = sdk.SpeechConfig.fromEndpoint(
new URL("YourSpeechEndpoint"), "YourSpeechKey");
speechConfig.speechRecognitionLanguage = "en-US";
// Enable per-channel transcription of up to two channels.
speechConfig.setProperty(sdk.PropertyId.Speech_EnableMultiChannelProcessing, "true");
提供立體聲音訊
多聲道轉錄可接受立體聲音訊檔案或立體聲串流。 要轉錄檔案,請從檔案建立一個 AudioConfig 實例:
const fs = require("fs");
const audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync("stereo.wav"));
對於即時來源,請從推送串流建立實 AudioConfig 例。 建立串流格式時,請將頻道數量設為 2:
// Match the format of your stereo source: sample rate, bits per sample, and 2 channels.
const streamFormat = sdk.AudioStreamFormat.getWaveFormatPCM(16000, 16, 2);
const pushStream = sdk.AudioInputStream.createPushStream(streamFormat);
const audioConfig = sdk.AudioConfig.fromStreamInput(pushStream);
// Write raw PCM audio (without the WAV header) to pushStream as it becomes available,
// and call pushStream.close() when the source ends.
識別並讀取每個通道的結果
建立一個 SpeechRecognizer 範例並使用持續識別。 多通道轉錄僅支援連續識別;不支援單發識別recognizeOnceAsync()。
const recognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);
recognizer.recognized = (s, e) => {
if (e.result.reason === sdk.ResultReason.RecognizedSpeech) {
// e.result.channel identifies the source channel (0 or 1).
console.log(`RECOGNIZED (channel ${e.result.channel}): ${e.result.text}`);
}
};
recognizer.sessionStopped = (s, e) => {
recognizer.stopContinuousRecognitionAsync();
};
recognizer.startContinuousRecognitionAsync();
使用每個結果的頻道值來保持每個頻道的逐字稿分開。
結合多通道轉錄與日記
要辨識說話者,請使用 a ConversationTranscriber 代替 SpeechRecognizer。 轉錄員會報告活動的最終結果 transcribed ,每個結果都包含講者編號。
Important
在 JavaScript 1.51.0 版本的 Speech SDK 中, ConversationTranscriber 結果無法可靠回報來源通道。 計畫在 1.52.0 版本推出修正。 如果你用的是版本 1.51.0,請用來 speakerId 辨識喇叭,但不要用 channel 來依來源聲道分開日變結果。 當你需要可靠的來源通道元資料時使用 SpeechRecognizer 。
const conversationTranscriber = new sdk.ConversationTranscriber(speechConfig, audioConfig);
conversationTranscriber.transcribed = (s, e) => {
if (e.result.reason === sdk.ResultReason.RecognizedSpeech) {
console.log(`TRANSCRIBED (speaker ${e.result.speakerId}): ${e.result.text}`);
}
};
conversationTranscriber.sessionStopped = (s, e) => {
conversationTranscriber.stopTranscribingAsync();
};
conversationTranscriber.startTranscribingAsync();
參考文件 | 套件(Go) | 在 GitHub 上的其他範例
在本指南中,你會使用 Go 的語音 SDK 來轉錄立體聲音訊來源,並為每個聲道讀取獨立的語音轉文字結果。
先決條件
- Speech SDK for Go 版本 1.52.0 或更新版本。
- 雙聲道(立體聲)WAV 檔案或立體聲音訊串流。 語音服務最多可轉錄兩個頻道。
Note
在 SDK 1.52.0 發布之前,請直接下載目前的 Speech SDK Go 原始碼 。
建立語音配置並啟用多頻道處理
建立一個 SpeechConfig 實例並啟用多通道處理。 用你的語音資源端點和鍵替換 YourSpeechEndpoint 和 YourSpeechKey 。
import (
"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
"github.com/Microsoft/cognitive-services-speech-sdk-go/speech"
)
speechConfig, err := speech.NewSpeechConfigFromEndpointWithSubscription(
"YourSpeechEndpoint", "YourSpeechKey")
if err != nil {
fmt.Println("Got an error: ", err)
return
}
defer speechConfig.Close()
speechConfig.SetSpeechRecognitionLanguage("en-US")
// Enable per-channel transcription of up to two channels.
speechConfig.SetProperty(common.EnableMultiChannelProcessing, "true")
提供立體聲音訊
多聲道轉錄可接受立體聲音訊檔案或立體聲串流。 要轉錄檔案,請從檔名建立一個 AudioConfig 實例:
import "github.com/Microsoft/cognitive-services-speech-sdk-go/audio"
audioConfig, err := audio.NewAudioConfigFromWavFileInput("stereo.wav")
if err != nil {
fmt.Println("Got an error: ", err)
return
}
defer audioConfig.Close()
對於即時來源,請從格式指定兩個通道的串流建立 AudioConfig 實例,並在串流可用時寫入原始 PCM 音訊(不含 WAV 標頭)。
識別並讀取每個通道的結果
建立一個 SpeechRecognizer 範例並使用持續識別。 多通道轉錄僅支援連續識別;不支援單發識別。
每個識別結果都 Channel 帶有一個欄位,用以識別來源音訊通道。 頻道編號從零開始。
import (
"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
"github.com/Microsoft/cognitive-services-speech-sdk-go/speech"
)
speechRecognizer, err := speech.NewSpeechRecognizerFromConfig(speechConfig, audioConfig)
if err != nil {
fmt.Println("Got an error: ", err)
return
}
defer speechRecognizer.Close()
speechRecognizer.Recognized(func(event speech.SpeechRecognitionEventArgs) {
defer event.Close()
// event.Result.Channel identifies the source channel (0 or 1).
fmt.Printf("RECOGNIZED (channel %d): %s\n", event.Result.Channel, event.Result.Text)
})
// The Start and Stop methods return a channel that reports the outcome
// of the operation. Read from it to wait for the operation to complete.
if err := <-speechRecognizer.StartContinuousRecognitionAsync(); err != nil {
fmt.Println("Got an error: ", err)
return
}
defer func() { <-speechRecognizer.StopContinuousRecognitionAsync() }()
使用每個結果的頻道值來保持每個頻道的逐字稿分開。
結合多通道轉錄與日記
若要識別每個聲道中的說話者,請使用 a ConversationTranscriber 代替 SpeechRecognizer。 轉錄員會報告活動的 Transcribed 最終結果,每個結果都包含頻道名稱和講者編號。
import "github.com/Microsoft/cognitive-services-speech-sdk-go/speech"
conversationTranscriber, err := speech.NewConversationTranscriberFromConfig(speechConfig, audioConfig)
if err != nil {
fmt.Println("Got an error: ", err)
return
}
defer conversationTranscriber.Close()
conversationTranscriber.Transcribed(func(event speech.ConversationTranscriptionEventArgs) {
defer event.Close()
fmt.Printf("TRANSCRIBED (channel %d, speaker %s): %s\n",
event.Result.Channel, event.Result.SpeakerID, event.Result.Text)
})
if err := <-conversationTranscriber.StartTranscribingAsync(); err != nil {
fmt.Println("Got an error: ", err)
return
}
defer func() { <-conversationTranscriber.StopTranscribingAsync() }()
參考文件 | 套件 (下載) | GitHub 上的其他範例
在本指南中,你使用 Speech SDK for Objective-C 來轉錄立體聲音訊來源,並為每個聲道讀取獨立的語音轉文字結果。
先決條件
- Objective-C 1.51.0 或更新版本的語音 SDK。
- 雙聲道(立體聲)WAV 檔案或立體聲音訊串流。 語音服務最多可轉錄兩個頻道。
建立語音配置並啟用多頻道處理
建立一個 SPXSpeechConfiguration 實例並將屬性設 SPXSpeechEnableMultiChannelProcessing 為 true。 用你的語音資源端點和鍵替換 YourSpeechEndpoint 和 YourSpeechKey 。
SPXSpeechConfiguration *speechConfig = [[SPXSpeechConfiguration alloc]
initWithEndpoint:@"YourSpeechEndpoint" subscription:@"YourSpeechKey"];
speechConfig.speechRecognitionLanguage = @"en-US";
// Enable per-channel transcription of up to two channels.
[speechConfig setPropertyTo:@"true" byId:SPXSpeechEnableMultiChannelProcessing];
提供立體聲音訊
多聲道轉錄可接受立體聲音訊檔案或立體聲串流。 要轉錄檔案,請從檔名建立一個 SPXAudioConfiguration 實例:
SPXAudioConfiguration *audioConfig = [[SPXAudioConfiguration alloc] initWithWavFileInput:@"stereo.wav"];
對於即時來源,請從格式指定兩個通道的串流建立 SPXAudioConfiguration 實例,並在串流可用時寫入原始 PCM 音訊(不含 WAV 標頭)。
識別並讀取每個通道的結果
建立一個 SPXSpeechRecognizer 實例並使用持續識別。 多通道轉錄僅支援連續識別;不支援單發識別recognizeOnce()。
SPXSpeechRecognizer *recognizer = [[SPXSpeechRecognizer alloc] initWithSpeechConfiguration:speechConfig audioConfiguration:audioConfig];
[recognizer addRecognizedEventHandler:^(SPXSpeechRecognizer *recognizer, SPXSpeechRecognitionEventArgs *evt) {
SPXSpeechRecognitionResult *result = evt.result;
if (result != nil && result.reason == SPXResultReason_RecognizedSpeech) {
// result.channel identifies the source channel (0 or 1).
NSLog(@"RECOGNIZED (channel %ld): %@", (long)result.channel, result.text);
}
}];
[recognizer startContinuousRecognition];
使用每個結果的頻道值來保持每個頻道的逐字稿分開。
結合多通道轉錄與日記
若要在每個頻道中識別說話者,請使用 a SPXConversationTranscriber 代替 SPXSpeechRecognizer。 轉錄器會回報轉錄事件的最終結果,每個結果都會包含頻道名稱和講者ID。
SPXConversationTranscriber *conversationTranscriber = [[SPXConversationTranscriber alloc] initWithSpeechConfiguration:speechConfig audioConfiguration:audioConfig];
[conversationTranscriber addTranscribedEventHandler:^(SPXConversationTranscriber *transcriber, SPXConversationTranscriptionEventArgs *evt) {
SPXConversationTranscriptionResult *result = evt.result;
if (result != nil && result.reason == SPXResultReason_RecognizedSpeech) {
NSLog(@"TRANSCRIBED (channel %ld, speaker %@): %@", (long)result.channel, result.speakerId, result.text);
}
}];
[conversationTranscriber startTranscribingAsync:^(BOOL started, NSError *error) {
if (!started || error != nil) {
NSLog(@"Could not start transcription: %@", error);
}
}];
轉錄完成後,停止轉錄器:
[conversationTranscriber stopTranscribingAsync:^(BOOL stopped, NSError *error) {
if (!stopped || error != nil) {
NSLog(@"Could not stop transcription: %@", error);
}
}];
參考文件 | 套件 (下載) | GitHub 上的其他範例
在本指南中,你會使用 Swift 的語音 SDK 來轉錄立體聲音訊來源,並為每個聲道讀取獨立的語音轉文字結果。
先決條件
- Swift 1.51.0 或更新版本的 語音 SDK 。
- 雙聲道(立體聲)WAV 檔案或立體聲音訊串流。 語音服務最多可轉錄兩個頻道。
建立語音配置並啟用多頻道處理
建立一個 SPXSpeechConfiguration 實例並啟用多通道處理。 用你的語音資源端點和鍵替換 YourSpeechEndpoint 和 YourSpeechKey 。
let speechConfig = try! SPXSpeechConfiguration(
endpoint: "YourSpeechEndpoint", subscription: "YourSpeechKey")
speechConfig.speechRecognitionLanguage = "en-US"
// Enable per-channel transcription of up to two channels.
speechConfig.setPropertyTo("true", by: SPXPropertyId.speechEnableMultiChannelProcessing)
提供立體聲音訊
多聲道轉錄可接受立體聲音訊檔案或立體聲串流。 要轉錄檔案,請從檔名建立一個 SPXAudioConfiguration 實例:
let audioConfig = SPXAudioConfiguration(wavFileInput: "stereo.wav")
對於即時來源,請從格式指定兩個通道的串流建立 SPXAudioConfiguration 實例,並在串流可用時寫入原始 PCM 音訊(不含 WAV 標頭)。
識別並讀取每個通道的結果
建立一個 SPXSpeechRecognizer 實例並使用持續識別。 多通道轉錄僅支援連續識別;不支援單發識別recognizeOnce()。
let recognizer = try! SPXSpeechRecognizer(speechConfiguration: speechConfig, audioConfiguration: audioConfig!)
recognizer.addRecognizedEventHandler { recognizer, evt in
guard let result = evt.result else {
return
}
if result.reason == SPXResultReason.recognizedSpeech {
// result.channel identifies the source channel (0 or 1).
print("RECOGNIZED (channel \(result.channel)): \(result.text ?? "")")
}
}
try! recognizer.startContinuousRecognition()
使用每個結果的頻道值來保持每個頻道的逐字稿分開。
結合多通道轉錄與日記
若要在每個頻道中識別說話者,請使用 a SPXConversationTranscriber 代替 SPXSpeechRecognizer。 轉錄器會回報轉錄事件的最終結果,每個結果都會包含頻道名稱和講者ID。
let conversationTranscriber = try! SPXConversationTranscriber(speechConfiguration: speechConfig, audioConfiguration: audioConfig!)
conversationTranscriber.addTranscribedEventHandler { transcriber, evt in
guard let result = evt.result else {
return
}
if result.reason == SPXResultReason.recognizedSpeech {
print("TRANSCRIBED (channel \(result.channel), speaker \(result.speakerId ?? "")): \(result.text ?? "")")
}
}
do {
try conversationTranscriber.startTranscribingAsync { started, error in
if let error = error {
print("Could not start transcription: \(error)")
} else if started {
print("Transcription started")
}
}
} catch {
print("Could not start transcription: \(error)")
}
轉錄完成後,停止轉錄器:
do {
try conversationTranscriber.stopTranscribingAsync { stopped, error in
if let error = error {
print("Could not stop transcription: \(error)")
} else if stopped {
print("Transcription stopped")
}
}
} catch {
print("Could not stop transcription: \(error)")
}
支援與不支援的功能
多頻道轉錄支援多項即時語音轉文字功能,且不支援其他功能。 下表總結了目前的支撐情況。
| Feature | 支援 |
|---|---|
| 自動分段標記 | ✅ |
| 自訂語音 | ✅ |
| 語意分割 | ✅ |
| 真實文本 | ✅ |
| 語言識別 | ❌ |
| 多語言模型 | ❌ |
| 串流後最佳化 | ❌ |
| 片語列表 | ❌ |
| 發音評量 | ❌ |
當你結合多頻道轉錄與日記化時,結果也會包含講者ID。 diarization 結果的來源通道元資料會因語音 SDK 而異。 在同時使用頻道和說話者元資料前,請先閱讀你程式語言的指引。
不同頻道的結果並不保證會完美時間順序送達,尤其是當語音在不同頻道重疊時。