Created
March 8, 2024 07:06
-
-
Save drmaniac/23a4faf462caabd57f0175f281739ada to your computer and use it in GitHub Desktop.
SpeechSDK arch working example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdlib.h> | |
#include <speechapi_cxx.h> | |
using namespace Microsoft::CognitiveServices::Speech; | |
using namespace Microsoft::CognitiveServices::Speech::Audio; | |
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->SetProperty(PropertyId::Speech_LogFilename, "/home/christian/speech_log.txt"); | |
speechConfig->SetSpeechRecognitionLanguage("de-DE"); | |
// auto audioConfig = AudioConfig::FromDefaultMicrophoneInput(); // Not working on Arch Linux | |
auto audioConfig = AudioConfig::FromMicrophoneInput("hw:2,0"); // Working on Arch Linux (used arecord -l to find the correct hardware id) | |
auto speechRecognizer = SpeechRecognizer::FromConfig(speechConfig, audioConfig); | |
std::cout << "Speak into your microphone.\n"; | |
auto result = speechRecognizer->RecognizeOnceAsync().get(); | |
if (result->Reason == ResultReason::RecognizedSpeech) | |
{ | |
std::cout << "RECOGNIZED: Text=" << result->Text << std::endl; | |
} | |
else if (result->Reason == ResultReason::NoMatch) | |
{ | |
std::cout << "NOMATCH: Speech could not be recognized." << std::endl; | |
} | |
else if (result->Reason == ResultReason::Canceled) | |
{ | |
auto cancellation = CancellationDetails::FromResult(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; | |
} | |
} | |
} | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment