快速入門:建立即時 diarization (預覽)

參考文件 | 套件 (NuGet) | GitHub 上的其他範例

在本快速入門中,您會使用即時自動分段標記來執行語音轉換文字謄寫的應用程式。 Diarization 區分參與交談的不同演講者。 語音服務提供有關哪位說話者正在講出所謄寫語音之特定部分的資訊。

注意

即時自動分段標記目前處於公開預覽狀態。

說話者資訊包含在說話者識別碼欄位內的結果中。 說話者識別碼是服務在所提供的音訊內容中識別不同說話者時指派給每位交談參與者的通用識別碼。

提示

您可以在 Speech Studio 中嘗試即時語音轉換文字,而不需要註冊或撰寫任何程式碼。 不過,Speech Studio 尚不支援自動分段標記。

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶
  • 在 Azure 入口網站上建立語音資源
  • 您的語音資源金鑰和區域。 部署語音資源之後,請選取 [移至資源 ] 以檢視和管理密鑰。 如需 Azure AI 服務資源的詳細資訊,請參閱取得資源的金鑰

設定環境

語音 SDK 可作為 NuGet 套件使用,並實作 .NET Standard 2.0。 您稍後會在本指南中安裝語音 SDK,但請先檢查 SDK 安裝指南 以取得任何更多需求。

設定環境變數

您的應用程式必須經過驗證,才能存取 Azure AI 服務資源。 針對生產環境,請使用安全的方式來儲存和存取您的認證。 例如,取得 語音資源的金鑰 之後,請將它寫入執行應用程式的本機計算機上新的環境變數。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項,請參閱 Azure AI 服務安全性,例如 Azure 金鑰保存庫

若要設定語音資源密鑰的環境變數,請開啟控制台視窗,並遵循作業系統和開發環境的指示。

  • 若要設定SPEECH_KEY環境變數,請將your-key取代為資源的其中一個密鑰。
  • 若要設定SPEECH_REGION環境變數,請將您的區域取代為您資源的其中一個區域。
setx SPEECH_KEY your-key
setx SPEECH_REGION your-region

注意

如果您只需要存取目前控制台中的環境變數,您可以使用 來設定環境變數 set ,而不是 setx

新增環境變數之後,您可能需要重新啟動任何需要讀取環境變數的程式,包括主控台視窗。 例如,如果您使用 Visual Studio 作為編輯器,請在執行範例之前重新啟動 Visual Studio。

使用交談轉譯從檔案實作 diarization

請遵循下列步驟來建立主控台應用程式並安裝語音 SDK。

  1. 在您想要新項目的資料夾中開啟命令提示字元視窗。 執行此命令以使用 .NET CLI 建立主控台應用程式。

    dotnet new console
    

    此命令會在 您的項目目錄中建立Program.cs 檔案。

  2. 使用 .NET CLI 在您的新專案中安裝語音 SDK。

    dotnet add package Microsoft.CognitiveServices.Speech
    
  3. 以下列程式碼取代 Program.cs 的內容。

    using Microsoft.CognitiveServices.Speech;
    using Microsoft.CognitiveServices.Speech.Audio;
    using Microsoft.CognitiveServices.Speech.Transcription;
    
    class Program 
    {
        // This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
        static string speechKey = Environment.GetEnvironmentVariable("SPEECH_KEY");
        static string speechRegion = Environment.GetEnvironmentVariable("SPEECH_REGION");
    
        async static Task Main(string[] args)
        {
            var filepath = "katiesteve.wav";
            var speechConfig = SpeechConfig.FromSubscription(speechKey, speechRegion);        
            speechConfig.SpeechRecognitionLanguage = "en-US";
    
            var stopRecognition = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
    
            // Create an audio stream from a wav file or from the default microphone
            using (var audioConfig = AudioConfig.FromWavFileInput(filepath))
            {
                // Create a conversation transcriber using audio stream input
                using (var conversationTranscriber = new ConversationTranscriber(speechConfig, audioConfig))
                {
                    conversationTranscriber.Transcribing += (s, e) =>
                    {
                        Console.WriteLine($"TRANSCRIBING: Text={e.Result.Text}");
                    };
    
                    conversationTranscriber.Transcribed += (s, e) =>
                    {
                        if (e.Result.Reason == ResultReason.RecognizedSpeech)
                        {
                            Console.WriteLine($"TRANSCRIBED: Text={e.Result.Text} Speaker ID={e.Result.SpeakerId}");
                        }
                        else if (e.Result.Reason == ResultReason.NoMatch)
                        {
                            Console.WriteLine($"NOMATCH: Speech could not be transcribed.");
                        }
                    };
    
                    conversationTranscriber.Canceled += (s, e) =>
                    {
                        Console.WriteLine($"CANCELED: Reason={e.Reason}");
    
                        if (e.Reason == CancellationReason.Error)
                        {
                            Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
                            Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
                            Console.WriteLine($"CANCELED: Did you set the speech resource key and region values?");
                            stopRecognition.TrySetResult(0);
                        }
    
                        stopRecognition.TrySetResult(0);
                    };
    
                    conversationTranscriber.SessionStopped += (s, e) =>
                    {
                        Console.WriteLine("\n    Session stopped event.");
                        stopRecognition.TrySetResult(0);
                    };
    
                    await conversationTranscriber.StartTranscribingAsync();
    
                    // Waits for completion. Use Task.WaitAny to keep the task rooted.
                    Task.WaitAny(new[] { stopRecognition.Task });
    
                    await conversationTranscriber.StopTranscribingAsync();
                }
            }
        }
    }
    
  4. 取得範例音訊檔案或使用您自己的.wav檔案。 將取代 katiesteve.wav 為您檔案的路徑和名稱 .wav

    應用程式會辨識來自交談中多個參與者的語音。 您的音訊檔案應該包含多個說話者。

    注意

    該服務在單一說話者輸出連續 7 秒的語音時效果最佳。 這可讓系統正確區分說話者。 否則,說話者識別碼會以 Unknown 傳回。

  5. 若要變更語音辨識語言,請將 取代 en-US 為另一種 支持的語言。 例如, es-ES 適用於西班牙文(西班牙)。 如果您未指定語言,則預設語言為 en-US。 如需如何識別其中一種可能說出的多種語言的詳細資訊,請參閱 語言識別

  6. 執行主控台應用程式以開始交談轉譯:

    dotnet run
    

重要

請確定您已設定 SPEECH_KEYSPEECH_REGION環境變數。 如果您未設定這些變數,範例會失敗並顯示錯誤訊息。

謄寫的交談應輸出為文字:

TRANSCRIBED: Text=Good morning, Steve. Speaker ID=Unknown
TRANSCRIBED: Text=Good morning. Katie. Speaker ID=Unknown
TRANSCRIBED: Text=Have you tried the latest real time diarization in Microsoft Speech Service which can tell you who said what in real time? Speaker ID=Guest-1
TRANSCRIBED: Text=Not yet. I've been using the batch transcription with diarization functionality, but it produces diarization result until whole audio get processed. Speaker ID=Guest-2
TRANSRIBED: Text=Is the new feature can diarize in real time? Speaker ID=Guest-2
TRANSCRIBED: Text=Absolutely. Speaker ID=GUEST-1
TRANSCRIBED: Text=That's exciting. Let me try it right now. Speaker ID=GUEST-2
CANCELED: Reason=EndOfStream

根據交談中的說話者數目,說話者會被識別為 Guest-1、Guest-2 等等。

清除資源

您可以使用 Azure 入口網站Azure 命令行介面 (CLI) 來移除您所建立的語音資源。

參考文件 | 套件 (NuGet) | GitHub 上的其他範例

在本快速入門中,您會使用即時自動分段標記來執行語音轉換文字謄寫的應用程式。 Diarization 區分參與交談的不同演講者。 語音服務提供有關哪位說話者正在講出所謄寫語音之特定部分的資訊。

注意

即時自動分段標記目前處於公開預覽狀態。

說話者資訊包含在說話者識別碼欄位內的結果中。 說話者識別碼是服務在所提供的音訊內容中識別不同說話者時指派給每位交談參與者的通用識別碼。

提示

您可以在 Speech Studio 中嘗試即時語音轉換文字,而不需要註冊或撰寫任何程式碼。 不過,Speech Studio 尚不支援自動分段標記。

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶
  • 在 Azure 入口網站上建立語音資源
  • 您的語音資源金鑰和區域。 部署語音資源之後,請選取 [移至資源 ] 以檢視和管理密鑰。 如需 Azure AI 服務資源的詳細資訊,請參閱取得資源的金鑰

設定環境

語音 SDK 可作為 NuGet 套件使用,並實作 .NET Standard 2.0。 您稍後會在本指南中安裝語音 SDK,但請先檢查 SDK 安裝指南 以取得任何更多需求。

設定環境變數

您的應用程式必須經過驗證,才能存取 Azure AI 服務資源。 針對生產環境,請使用安全的方式來儲存和存取您的認證。 例如,取得 語音資源的金鑰 之後,請將它寫入執行應用程式的本機計算機上新的環境變數。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項,請參閱 Azure AI 服務安全性,例如 Azure 金鑰保存庫

若要設定語音資源密鑰的環境變數,請開啟控制台視窗,並遵循作業系統和開發環境的指示。

  • 若要設定SPEECH_KEY環境變數,請將your-key取代為資源的其中一個密鑰。
  • 若要設定SPEECH_REGION環境變數,請將您的區域取代為您資源的其中一個區域。
setx SPEECH_KEY your-key
setx SPEECH_REGION your-region

注意

如果您只需要存取目前控制台中的環境變數,您可以使用 來設定環境變數 set ,而不是 setx

新增環境變數之後,您可能需要重新啟動任何需要讀取環境變數的程式,包括主控台視窗。 例如,如果您使用 Visual Studio 作為編輯器,請在執行範例之前重新啟動 Visual Studio。

使用交談轉譯從檔案實作 diarization

請遵循下列步驟來建立主控台應用程式並安裝語音 SDK。

  1. 在名為ConversationTranscription的 Visual Studio Community 2022建立新的 C++ 控制台專案。

  2. 選取 [工具>Nuget 封裝管理員> 封裝管理員 控制台]。封裝管理員 主控台中,執行此命令:

    Install-Package Microsoft.CognitiveServices.Speech
    
  3. 以下列程式碼取代 ConversationTranscription.cpp 的內容。

    #include <iostream> 
    #include <stdlib.h>
    #include <speechapi_cxx.h>
    #include <future>
    
    using namespace Microsoft::CognitiveServices::Speech;
    using namespace Microsoft::CognitiveServices::Speech::Audio;
    using namespace Microsoft::CognitiveServices::Speech::Transcription;
    
    std::string GetEnvironmentVariable(const char* name);
    
    int main()
    {
        // This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
        auto speechKey = GetEnvironmentVariable("SPEECH_KEY");
        auto speechRegion = GetEnvironmentVariable("SPEECH_REGION");
    
        if ((size(speechKey) == 0) || (size(speechRegion) == 0)) {
            std::cout << "Please set both SPEECH_KEY and SPEECH_REGION environment variables." << std::endl;
            return -1;
        }
    
        auto speechConfig = SpeechConfig::FromSubscription(speechKey, speechRegion);
    
        speechConfig->SetSpeechRecognitionLanguage("en-US");
    
        auto audioConfig = AudioConfig::FromWavFileInput("katiesteve.wav");
        auto conversationTranscriber = ConversationTranscriber::FromConfig(speechConfig, audioConfig);
    
        // promise for synchronization of recognition end.
        std::promise<void> recognitionEnd;
    
        // Subscribes to events.
        conversationTranscriber->Transcribing.Connect([](const ConversationTranscriptionEventArgs& e)
            {
                std::cout << "TRANSCRIBING:" << e.Result->Text << std::endl;
            });
    
        conversationTranscriber->Transcribed.Connect([](const ConversationTranscriptionEventArgs& e)
            {
                if (e.Result->Reason == ResultReason::RecognizedSpeech)
                {
                    std::cout << "TRANSCRIBED: Text=" << e.Result->Text << std::endl;
                    std::cout << "Speaker ID=" << e.Result->SpeakerId << std::endl;
                }
                else if (e.Result->Reason == ResultReason::NoMatch)
                {
                    std::cout << "NOMATCH: Speech could not be transcribed." << std::endl;
                }
            });
    
        conversationTranscriber->Canceled.Connect([&recognitionEnd](const ConversationTranscriptionCanceledEventArgs& e)
            {
                auto cancellation = CancellationDetails::FromResult(e.Result);
                std::cout << "CANCELED: Reason=" << (int)cancellation->Reason << std::endl;
    
                if (cancellation->Reason == CancellationReason::Error)
                {
                    std::cout << "CANCELED: ErrorCode=" << (int)cancellation->ErrorCode << std::endl;
                    std::cout << "CANCELED: ErrorDetails=" << cancellation->ErrorDetails << std::endl;
                    std::cout << "CANCELED: Did you set the speech resource key and region values?" << std::endl;
                }
                else if (cancellation->Reason == CancellationReason::EndOfStream)
                {
                    std::cout << "CANCELED: Reach the end of the file." << std::endl;
                }
            });
    
        conversationTranscriber->SessionStopped.Connect([&recognitionEnd](const SessionEventArgs& e)
            {
                std::cout << "Session stopped.";
                recognitionEnd.set_value(); // Notify to stop recognition.
            });
    
        conversationTranscriber->StartTranscribingAsync().wait();
    
        // Waits for recognition end.
        recognitionEnd.get_future().wait();
    
        conversationTranscriber->StopTranscribingAsync().wait();
    }
    
    std::string GetEnvironmentVariable(const char* name)
    {
    #if defined(_MSC_VER)
        size_t requiredSize = 0;
        (void)getenv_s(&requiredSize, nullptr, 0, name);
        if (requiredSize == 0)
        {
            return "";
        }
        auto buffer = std::make_unique<char[]>(requiredSize);
        (void)getenv_s(&requiredSize, buffer.get(), requiredSize, name);
        return buffer.get();
    #else
        auto value = getenv(name);
        return value ? value : "";
    #endif
    }
    
  4. 取得範例音訊檔案或使用您自己的.wav檔案。 將取代 katiesteve.wav 為您檔案的路徑和名稱 .wav

    應用程式會辨識來自交談中多個參與者的語音。 您的音訊檔案應該包含多個說話者。

    注意

    該服務在單一說話者輸出連續 7 秒的語音時效果最佳。 這可讓系統正確區分說話者。 否則,說話者識別碼會以 Unknown 傳回。

  5. 若要變更語音辨識語言,請將 取代 en-US 為另一種 支持的語言。 例如, es-ES 適用於西班牙文(西班牙)。 如果您未指定語言,則預設語言為 en-US。 如需如何識別其中一種可能說出的多種語言的詳細資訊,請參閱 語言識別

  6. 執行並執行您的應用程式,以開始交談謄寫:

    重要

    請確定您已設定 SPEECH_KEYSPEECH_REGION環境變數。 如果您未設定這些變數,範例會失敗並顯示錯誤訊息。

謄寫的交談應輸出為文字:

TRANSCRIBED: Text=Good morning, Steve. Speaker ID=Unknown
TRANSCRIBED: Text=Good morning. Katie. Speaker ID=Unknown
TRANSCRIBED: Text=Have you tried the latest real time diarization in Microsoft Speech Service which can tell you who said what in real time? Speaker ID=Guest-1
TRANSCRIBED: Text=Not yet. I've been using the batch transcription with diarization functionality, but it produces diarization result until whole audio get processed. Speaker ID=Guest-2
TRANSRIBED: Text=Is the new feature can diarize in real time? Speaker ID=Guest-2
TRANSCRIBED: Text=Absolutely. Speaker ID=GUEST-1
TRANSCRIBED: Text=That's exciting. Let me try it right now. Speaker ID=GUEST-2 
CANCELED: Reason=EndOfStream

根據交談中的說話者數目,說話者會被識別為 Guest-1、Guest-2 等等。

清除資源

您可以使用 Azure 入口網站Azure 命令行介面 (CLI) 來移除您所建立的語音資源。

參考文件 | 套件 (Go) | GitHub 上的其他範例

語音 SDK for Go 不支援交談謄寫。 請選取其他程式設計語言,或本文開頭的 Go 參考和樣本連結。

參考文件 | GitHub 上的其他範例

在本快速入門中,您會使用即時自動分段標記來執行語音轉換文字謄寫的應用程式。 Diarization 區分參與交談的不同演講者。 語音服務提供有關哪位說話者正在講出所謄寫語音之特定部分的資訊。

注意

即時自動分段標記目前處於公開預覽狀態。

說話者資訊包含在說話者識別碼欄位內的結果中。 說話者識別碼是服務在所提供的音訊內容中識別不同說話者時指派給每位交談參與者的通用識別碼。

提示

您可以在 Speech Studio 中嘗試即時語音轉換文字,而不需要註冊或撰寫任何程式碼。 不過,Speech Studio 尚不支援自動分段標記。

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶
  • 在 Azure 入口網站上建立語音資源
  • 您的語音資源金鑰和區域。 部署語音資源之後,請選取 [移至資源 ] 以檢視和管理密鑰。 如需 Azure AI 服務資源的詳細資訊,請參閱取得資源的金鑰

設定環境

若要設定環境, 請安裝語音 SDK。 本快速入門中的範例適用於 Java 運行時間

  1. 安裝 Apache Maven。 然後執行 mvn -v 以確認安裝成功。

  2. 在專案的根目錄中建立新的 pom.xml 檔案,並將下列內容複製到其中:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.microsoft.cognitiveservices.speech.samples</groupId>
        <artifactId>quickstart-eclipse</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                <source>1.8</source>
                <target>1.8</target>
                </configuration>
            </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
            <groupId>com.microsoft.cognitiveservices.speech</groupId>
            <artifactId>client-sdk</artifactId>
            <version>1.36.0</version>
            </dependency>
        </dependencies>
    </project>
    
  3. 安裝語音 SDK 和相依性。

    mvn clean dependency:copy-dependencies
    

設定環境變數

您的應用程式必須經過驗證,才能存取 Azure AI 服務資源。 針對生產環境,請使用安全的方式來儲存和存取您的認證。 例如,取得 語音資源的金鑰 之後,請將它寫入執行應用程式的本機計算機上新的環境變數。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項,請參閱 Azure AI 服務安全性,例如 Azure 金鑰保存庫

若要設定語音資源密鑰的環境變數,請開啟控制台視窗,並遵循作業系統和開發環境的指示。

  • 若要設定SPEECH_KEY環境變數,請將your-key取代為資源的其中一個密鑰。
  • 若要設定SPEECH_REGION環境變數,請將您的區域取代為您資源的其中一個區域。
setx SPEECH_KEY your-key
setx SPEECH_REGION your-region

注意

如果您只需要存取目前控制台中的環境變數,您可以使用 來設定環境變數 set ,而不是 setx

新增環境變數之後,您可能需要重新啟動任何需要讀取環境變數的程式,包括主控台視窗。 例如,如果您使用 Visual Studio 作為編輯器,請在執行範例之前重新啟動 Visual Studio。

使用交談轉譯從檔案實作 diarization

請遵循下列步驟來建立用於交談轉譯的控制台應用程式。

  1. 在相同的專案根目錄中建立名為 ConversationTranscription.java 的新檔案。

  2. 將下列程式代碼 ConversationTranscription.java複製到 :

    import com.microsoft.cognitiveservices.speech.*;
    import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
    import com.microsoft.cognitiveservices.speech.transcription.*;
    
    import java.util.concurrent.Semaphore;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Future;
    
    public class ConversationTranscription {
        // This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
        private static String speechKey = System.getenv("SPEECH_KEY");
        private static String speechRegion = System.getenv("SPEECH_REGION");
    
        public static void main(String[] args) throws InterruptedException, ExecutionException {
    
            SpeechConfig speechConfig = SpeechConfig.fromSubscription(speechKey, speechRegion);
            speechConfig.setSpeechRecognitionLanguage("en-US");
            AudioConfig audioInput = AudioConfig.fromWavFileInput("katiesteve.wav");
    
            Semaphore stopRecognitionSemaphore = new Semaphore(0);
    
            ConversationTranscriber conversationTranscriber = new ConversationTranscriber(speechConfig, audioInput);
            {
                // Subscribes to events.
                conversationTranscriber.transcribing.addEventListener((s, e) -> {
                    System.out.println("TRANSCRIBING: Text=" + e.getResult().getText());
                });
    
                conversationTranscriber.transcribed.addEventListener((s, e) -> {
                    if (e.getResult().getReason() == ResultReason.RecognizedSpeech) {
                        System.out.println("TRANSCRIBED: Text=" + e.getResult().getText() + " Speaker ID=" + e.getResult().getSpeakerId() );
                    }
                    else if (e.getResult().getReason() == ResultReason.NoMatch) {
                        System.out.println("NOMATCH: Speech could not be transcribed.");
                    }
                });
    
                conversationTranscriber.canceled.addEventListener((s, e) -> {
                    System.out.println("CANCELED: Reason=" + e.getReason());
    
                    if (e.getReason() == CancellationReason.Error) {
                        System.out.println("CANCELED: ErrorCode=" + e.getErrorCode());
                        System.out.println("CANCELED: ErrorDetails=" + e.getErrorDetails());
                        System.out.println("CANCELED: Did you update the subscription info?");
                    }
    
                    stopRecognitionSemaphore.release();
                });
    
                conversationTranscriber.sessionStarted.addEventListener((s, e) -> {
                    System.out.println("\n    Session started event.");
                });
    
                conversationTranscriber.sessionStopped.addEventListener((s, e) -> {
                    System.out.println("\n    Session stopped event.");
                });
    
                conversationTranscriber.startTranscribingAsync().get();
    
                // Waits for completion.
                stopRecognitionSemaphore.acquire();
    
                conversationTranscriber.stopTranscribingAsync().get();
            }
    
            speechConfig.close();
            audioInput.close();
            conversationTranscriber.close();
    
            System.exit(0);
        }
    }
    
  3. 取得範例音訊檔案或使用您自己的.wav檔案。 將取代 katiesteve.wav 為您檔案的路徑和名稱 .wav

    應用程式會辨識來自交談中多個參與者的語音。 您的音訊檔案應該包含多個說話者。

    注意

    該服務在單一說話者輸出連續 7 秒的語音時效果最佳。 這可讓系統正確區分說話者。 否則,說話者識別碼會以 Unknown 傳回。

  4. 若要變更語音辨識語言,請將 取代 en-US 為另一種 支持的語言。 例如, es-ES 適用於西班牙文(西班牙)。 如果您未指定語言,則預設語言為 en-US。 如需如何識別其中一種可能說出的多種語言的詳細資訊,請參閱 語言識別

  5. 執行新的主控台應用程式,以開始交談謄寫:

    javac ConversationTranscription.java -cp ".;target\dependency\*"
    java -cp ".;target\dependency\*" ConversationTranscription
    

重要

請確定您已設定 SPEECH_KEYSPEECH_REGION環境變數。 如果您未設定這些變數,範例會失敗並顯示錯誤訊息。

謄寫的交談應輸出為文字:

TRANSCRIBED: Text=Good morning, Steve. Speaker ID=Unknown
TRANSCRIBED: Text=Good morning. Katie. Speaker ID=Unknown
TRANSCRIBED: Text=Have you tried the latest real time diarization in Microsoft Speech Service which can tell you who said what in real time? Speaker ID=Guest-1
TRANSCRIBED: Text=Not yet. I've been using the batch transcription with diarization functionality, but it produces diarization result until whole audio get processed. Speaker ID=Guest-2
TRANSRIBED: Text=Is the new feature can diarize in real time? Speaker ID=Guest-2
TRANSCRIBED: Text=Absolutely. Speaker ID=GUEST-1
TRANSCRIBED: Text=That's exciting. Let me try it right now. Speaker ID=GUEST-2
CANCELED: Reason=EndOfStream

根據交談中的說話者數目,說話者會被識別為 Guest-1、Guest-2 等等。

清除資源

您可以使用 Azure 入口網站Azure 命令行介面 (CLI) 來移除您所建立的語音資源。

參考文件 | 套件 (npm) | GitHub 上的其他範例 | 程式庫原始程式碼

在本快速入門中,您會使用即時自動分段標記來執行語音轉換文字謄寫的應用程式。 Diarization 區分參與交談的不同演講者。 語音服務提供有關哪位說話者正在講出所謄寫語音之特定部分的資訊。

注意

即時自動分段標記目前處於公開預覽狀態。

說話者資訊包含在說話者識別碼欄位內的結果中。 說話者識別碼是服務在所提供的音訊內容中識別不同說話者時指派給每位交談參與者的通用識別碼。

提示

您可以在 Speech Studio 中嘗試即時語音轉換文字,而不需要註冊或撰寫任何程式碼。 不過,Speech Studio 尚不支援自動分段標記。

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶
  • 在 Azure 入口網站上建立語音資源
  • 您的語音資源金鑰和區域。 部署語音資源之後,請選取 [移至資源 ] 以檢視和管理密鑰。 如需 Azure AI 服務資源的詳細資訊,請參閱取得資源的金鑰

設定環境

若要設定環境,請安裝適用於 JavaScript 的語音 SDK。 如果您只要安裝套件名稱,請執行 npm install microsoft-cognitiveservices-speech-sdk。 如需引導式安裝指示,請參閱 SDK 安裝指南

設定環境變數

您的應用程式必須經過驗證,才能存取 Azure AI 服務資源。 針對生產環境,請使用安全的方式來儲存和存取您的認證。 例如,取得 語音資源的金鑰 之後,請將它寫入執行應用程式的本機計算機上新的環境變數。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項,請參閱 Azure AI 服務安全性,例如 Azure 金鑰保存庫

若要設定語音資源密鑰的環境變數,請開啟控制台視窗,並遵循作業系統和開發環境的指示。

  • 若要設定SPEECH_KEY環境變數,請將your-key取代為資源的其中一個密鑰。
  • 若要設定SPEECH_REGION環境變數,請將您的區域取代為您資源的其中一個區域。
setx SPEECH_KEY your-key
setx SPEECH_REGION your-region

注意

如果您只需要存取目前控制台中的環境變數,您可以使用 來設定環境變數 set ,而不是 setx

新增環境變數之後,您可能需要重新啟動任何需要讀取環境變數的程式,包括主控台視窗。 例如,如果您使用 Visual Studio 作為編輯器,請在執行範例之前重新啟動 Visual Studio。

使用交談轉譯從檔案實作 diarization

遵循下列步驟來建立新的主控台應用程式進行交談謄寫。

  1. 開啟您想要新專案的命令提示字元視窗,並建立名為 ConversationTranscription.js的新檔案。

  2. 安裝適用於 JavaScript 的語音 SDK:

    npm install microsoft-cognitiveservices-speech-sdk
    
  3. 將下列程式代碼 ConversationTranscription.js複製到 :

    const fs = require("fs");
    const sdk = require("microsoft-cognitiveservices-speech-sdk");
    
    // This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
    const speechConfig = sdk.SpeechConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
    
    function fromFile() {
        const filename = "katiesteve.wav";
    
        let audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync(filename));
        let conversationTranscriber = new sdk.ConversationTranscriber(speechConfig, audioConfig);
    
        var pushStream = sdk.AudioInputStream.createPushStream();
    
        fs.createReadStream(filename).on('data', function(arrayBuffer) {
            pushStream.write(arrayBuffer.slice());
        }).on('end', function() {
            pushStream.close();
        });
    
        console.log("Transcribing from: " + filename);
    
        conversationTranscriber.sessionStarted = function(s, e) {
            console.log("SessionStarted event");
            console.log("SessionId:" + e.sessionId);
        };
        conversationTranscriber.sessionStopped = function(s, e) {
            console.log("SessionStopped event");
            console.log("SessionId:" + e.sessionId);
            conversationTranscriber.stopTranscribingAsync();
        };
        conversationTranscriber.canceled = function(s, e) {
            console.log("Canceled event");
            console.log(e.errorDetails);
            conversationTranscriber.stopTranscribingAsync();
        };
        conversationTranscriber.transcribed = function(s, e) {
            console.log("TRANSCRIBED: Text=" + e.result.text + " Speaker ID=" + e.result.speakerId);
        };
    
        // Start conversation transcription
        conversationTranscriber.startTranscribingAsync(
            function () {},
            function (err) {
                console.trace("err - starting transcription: " + err);
            }
        );
    
    }
    fromFile();
    
  4. 取得範例音訊檔案或使用您自己的.wav檔案。 將取代 katiesteve.wav 為您檔案的路徑和名稱 .wav

    應用程式會辨識來自交談中多個參與者的語音。 您的音訊檔案應該包含多個說話者。

    注意

    該服務在單一說話者輸出連續 7 秒的語音時效果最佳。 這可讓系統正確區分說話者。 否則,說話者識別碼會以 Unknown 傳回。

  5. 若要變更語音辨識語言,請將 取代 en-US 為另一種 支持的語言。 例如, es-ES 適用於西班牙文(西班牙)。 如果您未指定語言,則預設語言為 en-US。 如需如何識別其中一種可能說出的多種語言的詳細資訊,請參閱 語言識別

  6. 執行新的主控台應用程式,從檔案啟動語音辨識:

    node.exe ConversationTranscription.js
    

重要

請確定您已設定 SPEECH_KEYSPEECH_REGION環境變數。 如果您未設定這些變數,範例會失敗並顯示錯誤訊息。

謄寫的交談應輸出為文字:

SessionStarted event
SessionId:E87AFBA483C2481985F6C9AF719F616B
TRANSCRIBED: Text=Good morning, Steve. Speaker ID=Unknown
TRANSCRIBED: Text=Good morning, Katie. Speaker ID=Unknown
TRANSCRIBED: Text=Have you tried the latest real time diarization in Microsoft Speech Service which can tell you who said what in real time? Speaker ID=Guest-1
TRANSCRIBED: Text=Not yet. I've been using the batch transcription with diarization functionality, but it produces diarization result until whole audio get processed. Speaker ID=Guest-2
TRANSCRIBED: Text=Is the new feature can diarize in real time? Speaker ID=Guest-2
TRANSCRIBED: Text=Absolutely. Speaker ID=Guest-1
TRANSCRIBED: Text=That's exciting. Let me try it right now. Speaker ID=Guest-2
Canceled event
undefined
SessionStopped event
SessionId:E87AFBA483C2481985F6C9AF719F616B

根據交談中的說話者數目,說話者會被識別為 Guest-1、Guest-2 等等。

清除資源

您可以使用 Azure 入口網站Azure 命令行介面 (CLI) 來移除您所建立的語音資源。

參考文件 | 套件 (下載) | GitHub 上的其他範例

適用於 Objective-C 的語音 SDK 支援交談謄寫,但目前尚未提供相對應的指南。 請選取另一種程式設計語言來開始使用並了解概念,或參閱本文開頭連結的 Objective-C 參考和範例。

參考文件 | 套件 (下載) | GitHub 上的其他範例

適用於 Swift-C 的語音 SDK 支援交談謄寫,但目前尚未提供相對應的指南。 請選取另一種程式設計語言來開始使用並了解概念,或參閱本文開頭連結的 Swift 參考和範例。

參考文件 | 套件 (PyPi) | GitHub 上的其他範例

在本快速入門中,您會使用即時自動分段標記來執行語音轉換文字謄寫的應用程式。 Diarization 區分參與交談的不同演講者。 語音服務提供有關哪位說話者正在講出所謄寫語音之特定部分的資訊。

注意

即時自動分段標記目前處於公開預覽狀態。

說話者資訊包含在說話者識別碼欄位內的結果中。 說話者識別碼是服務在所提供的音訊內容中識別不同說話者時指派給每位交談參與者的通用識別碼。

提示

您可以在 Speech Studio 中嘗試即時語音轉換文字,而不需要註冊或撰寫任何程式碼。 不過,Speech Studio 尚不支援自動分段標記。

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶
  • 在 Azure 入口網站上建立語音資源
  • 您的語音資源金鑰和區域。 部署語音資源之後,請選取 [移至資源 ] 以檢視和管理密鑰。 如需 Azure AI 服務資源的詳細資訊,請參閱取得資源的金鑰

設定環境

適用於 Python 的語音 SDK 可作為 Python 套件索引 (PyPI) 模組。 適用於 Python 的語音 SDK 與 Windows、Linux 和 macOS 相容。

安裝 Python 從 3.7 或更新的版本。 請先檢查 SDK 安裝指南 ,以取得任何更多需求。

設定環境變數

您的應用程式必須經過驗證,才能存取 Azure AI 服務資源。 針對生產環境,請使用安全的方式來儲存和存取您的認證。 例如,取得 語音資源的金鑰 之後,請將它寫入執行應用程式的本機計算機上新的環境變數。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項,請參閱 Azure AI 服務安全性,例如 Azure 金鑰保存庫

若要設定語音資源密鑰的環境變數,請開啟控制台視窗,並遵循作業系統和開發環境的指示。

  • 若要設定SPEECH_KEY環境變數,請將your-key取代為資源的其中一個密鑰。
  • 若要設定SPEECH_REGION環境變數,請將您的區域取代為您資源的其中一個區域。
setx SPEECH_KEY your-key
setx SPEECH_REGION your-region

注意

如果您只需要存取目前控制台中的環境變數,您可以使用 來設定環境變數 set ,而不是 setx

新增環境變數之後,您可能需要重新啟動任何需要讀取環境變數的程式,包括主控台視窗。 例如,如果您使用 Visual Studio 作為編輯器,請在執行範例之前重新啟動 Visual Studio。

使用交談轉譯從檔案實作 diarization

請遵循下列步驟來建立新的控制台應用程式。

  1. 開啟您想要新專案的命令提示字元視窗,並建立名為 conversation_transcription.py的新檔案。

  2. 執行此指令以安裝語音 SDK:

    pip install azure-cognitiveservices-speech
    
  3. 將下列程式代碼 conversation_transcription.py複製到 :

    import os
    import time
    import azure.cognitiveservices.speech as speechsdk
    
    def conversation_transcriber_recognition_canceled_cb(evt: speechsdk.SessionEventArgs):
        print('Canceled event')
    
    def conversation_transcriber_session_stopped_cb(evt: speechsdk.SessionEventArgs):
        print('SessionStopped event')
    
    def conversation_transcriber_transcribed_cb(evt: speechsdk.SpeechRecognitionEventArgs):
        print('TRANSCRIBED:')
        if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
            print('\tText={}'.format(evt.result.text))
            print('\tSpeaker ID={}'.format(evt.result.speaker_id))
        elif evt.result.reason == speechsdk.ResultReason.NoMatch:
            print('\tNOMATCH: Speech could not be TRANSCRIBED: {}'.format(evt.result.no_match_details))
    
    def conversation_transcriber_session_started_cb(evt: speechsdk.SessionEventArgs):
        print('SessionStarted event')
    
    def recognize_from_file():
        # This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
        speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
        speech_config.speech_recognition_language="en-US"
    
        audio_config = speechsdk.audio.AudioConfig(filename="katiesteve.wav")
        conversation_transcriber = speechsdk.transcription.ConversationTranscriber(speech_config=speech_config, audio_config=audio_config)
    
        transcribing_stop = False
    
        def stop_cb(evt: speechsdk.SessionEventArgs):
            #"""callback that signals to stop continuous recognition upon receiving an event `evt`"""
            print('CLOSING on {}'.format(evt))
            nonlocal transcribing_stop
            transcribing_stop = True
    
        # Connect callbacks to the events fired by the conversation transcriber
        conversation_transcriber.transcribed.connect(conversation_transcriber_transcribed_cb)
        conversation_transcriber.session_started.connect(conversation_transcriber_session_started_cb)
        conversation_transcriber.session_stopped.connect(conversation_transcriber_session_stopped_cb)
        conversation_transcriber.canceled.connect(conversation_transcriber_recognition_canceled_cb)
        # stop transcribing on either session stopped or canceled events
        conversation_transcriber.session_stopped.connect(stop_cb)
        conversation_transcriber.canceled.connect(stop_cb)
    
        conversation_transcriber.start_transcribing_async()
    
        # Waits for completion.
        while not transcribing_stop:
            time.sleep(.5)
    
        conversation_transcriber.stop_transcribing_async()
    
    # Main
    
    try:
        recognize_from_file()
    except Exception as err:
        print("Encountered exception. {}".format(err))
    
  4. 取得範例音訊檔案或使用您自己的.wav檔案。 將取代 katiesteve.wav 為您檔案的路徑和名稱 .wav

    應用程式會辨識來自交談中多個參與者的語音。 您的音訊檔案應該包含多個說話者。

    注意

    該服務在單一說話者輸出連續 7 秒的語音時效果最佳。 這可讓系統正確區分說話者。 否則,說話者識別碼會以 Unknown 傳回。

  5. 若要變更語音辨識語言,請將 取代 en-US 為另一種 支持的語言。 例如, es-ES 適用於西班牙文(西班牙)。 如果您未指定語言,則預設語言為 en-US。 如需如何識別其中一種可能說出的多種語言的詳細資訊,請參閱 語言識別

  6. 執行新的主控台應用程式,以開始交談謄寫:

    python conversation_transcription.py
    

重要

請確定您已設定 SPEECH_KEYSPEECH_REGION環境變數。 如果您未設定這些變數,範例會失敗並顯示錯誤訊息。

謄寫的交談應輸出為文字:

SessionStarted event
TRANSCRIBED:
        Text=Good morning, Steve.
        Speaker ID=Unknown
TRANSCRIBED:
        Text=Good morning, Katie.
        Speaker ID=Unknown
TRANSCRIBED:
        Text=Have you tried the latest real time diarization in Microsoft Speech Service which can tell you who said what in real time?
        Speaker ID=Guest-1
TRANSCRIBED:
        Text=Not yet. I've been using the batch transcription with diarization functionality, but it produces diarization result until whole audio get processed.
        Speaker ID=Guest-2
TRANSCRIBED:
        Text=Is the new feature can diarize in real time?
        Speaker ID=Guest-2
TRANSCRIBED:
        Text=Absolutely.
        Speaker ID=Guest-1
TRANSCRIBED:
        Text=That's exciting. Let me try it right now.
        Speaker ID=Guest-2
Canceled event
CLOSING on ConversationTranscriptionCanceledEventArgs(session_id=92a0abb68636471dac07041b335d9be3, result=ConversationTranscriptionResult(result_id=ad1b1d83b5c742fcacca0692baa8df74, speaker_id=, text=, reason=ResultReason.Canceled))
SessionStopped event
CLOSING on SessionEventArgs(session_id=92a0abb68636471dac07041b335d9be3)

根據交談中的說話者數目,說話者會被識別為 Guest-1、Guest-2 等等。

清除資源

您可以使用 Azure 入口網站Azure 命令行介面 (CLI) 來移除您所建立的語音資源。

語音轉換文字 REST API 參考 | 適用於簡短音訊的語音轉換文字 REST API 參考 | GitHub 上的其他範例

REST API 不支援交談謄寫。 請從此頁面頂端選取另一種程式設計語言或工具。

語音 CLI 不支援交談謄寫。 請從此頁面頂端選取另一種程式設計語言或工具。

後續步驟