如何将字节数组发送到 Azure 语音服务(语音转文本)

问题描述 投票:0回答:0

我想使用Azure的语音服务发送语音文件进行翻译

Azure 提供了有关如何将 FileStream 发送到其语音服务的示例。但我希望能够发送一个Byte Array.

但我不知道该怎么做。

在此页面上,它向您展示了如何发送 StreamFile

https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/how-to-recognize-speech?pivots=programming-language-csharp

Stream 示例对我不起作用。
File 示例对我有用(所以这证明问题不是我的音频文件)。

我会在下面举例说明我尝试过的事情。

我能够让这个例子工作,发送一个File

private static string _speechKey = "your_key";
private static string _speechRegion = "your_region";
private static string _filePath = "PathToFile.wav";

public async Task FromFile()
{
    var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion);

    using var audioConfig = AudioConfig.FromWavFileInput(_filePath);
    using var speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);

    var result = await speechRecognizer.RecognizeOnceAsync();
    OutputSpeechRecognitionResult(result);
}


private static void OutputSpeechRecognitionResult(SpeechRecognitionResult speechRecognitionResult)
{
switch (speechRecognitionResult.Reason)
{
    case ResultReason.RecognizedSpeech:
        Console.WriteLine($"RECOGNIZED: Text={speechRecognitionResult.Text}");
        break;
    case ResultReason.NoMatch:
        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
        break;
    case ResultReason.Canceled:
        var cancellation = CancellationDetails.FromResult(speechRecognitionResult);
        Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

        if (cancellation.Reason == CancellationReason.Error)
        {
            Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
            Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
            Console.WriteLine($"CANCELED: Did you set the speech resource key and region values?");
        }
        break;
    }
}

但是我无法从 Azure 中得到这个发送 Stream 的示例:

public async Task FromStream()
{
var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion);

var reader = new BinaryReader(File.OpenRead(_filePath));
using var audioConfigStream = AudioInputStream.CreatePushStream();
using var audioConfig = AudioConfig.FromStreamInput(audioConfigStream);
using var speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);

byte[] readBytes;
do
{
    readBytes = reader.ReadBytes(1024);
    audioConfigStream.Write(readBytes, readBytes.Length);
} while (readBytes.Length > 0);

var result = await speechRecognizer.RecognizeOnceAsync();
OutputSpeechRecognitionResult(result);
}

我从这里得到上面的例子(第三个例子):

https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/how-to-recognize-speech?pivots=programming-language-csharp

但更重要的是,我需要向 Azure 发送一个 Byte Array。 我尝试了以下方法,但都不起作用:

我尝试先将 Byte Array 转换为 Stream,然后将 Stream 发送到 Azure:

public async Task FromByteArray1()
{
var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion);

byte[] byteArray = File.ReadAllBytes(_filePath);
Stream stream = new MemoryStream(byteArray);

var reader = new BinaryReader(stream);
using var audioConfigStream = AudioInputStream.CreatePushStream();
using var audioConfig = AudioConfig.FromStreamInput(audioConfigStream);
using var speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);

byte[] readBytes;
do
{
    readBytes = reader.ReadBytes(1024);
    audioConfigStream.Write(readBytes, readBytes.Length);
} while (readBytes.Length > 0);

var result = await speechRecognizer.RecognizeOnceAsync();
OutputSpeechRecognitionResult(result);
}

我试过这种发送字节数组的方法......我从这个页面得到的这个例子: https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/91

public async Task FromByteArray2()
{
var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion);

byte[] byteArray = File.ReadAllBytes(_filePath);

using var pushStream = AudioInputStream.CreatePushStream();
pushStream.Write(byteArray);
//pushStream.Close();
AudioConfig audioConfig = AudioConfig.FromStreamInput(pushStream);
using var speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);
pushStream.Close();

var result = await speechRecognizer.RecognizeOnceAsync();

OutputSpeechRecognitionResult(result);
}

我尝试了这种发送Byte Array的方法,我想出了自己的方法。

public async Task FromByteArray3()
{
var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion);

byte[] byteArray = File.ReadAllBytes(_filePath);

using var audioConfigStream = AudioInputStream.CreatePushStream();
using var audioConfig = AudioConfig.FromStreamInput(audioConfigStream);
using var speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);

foreach (var item in byteArray)
{
    audioConfigStream.Write(byteArray, 1);
}

var result = await speechRecognizer.RecognizeOnceAsync();
OutputSpeechRecognitionResult(result);
}

我试过这种发送字节数组的方法,我从Azure的页面得到:

https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/how-to-use-conversation-transcription?pivots=programming-language-csharp

public async Task FromByteArray4()
{
byte[] fileBytes = File.ReadAllBytes(_filePath);
var content = new ByteArrayContent(fileBytes);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _speechKey);
var response = await client.PostAsync($"https://signature.{_speechRegion}.cts.speech.microsoft.com/api/v1/Signature/GenerateVoiceSignatureFromByteArray", content);

var jsonData = await response.Content.ReadAsStringAsync();
Console.WriteLine($"JSON Data: Text={jsonData}");
}

上面的例子都没有用……要么我得到一个空响应,要么我得到一个说它无法识别文本的响应。

File 示例确实对我有用,所以这证明问题不是我的测试音频文件......当我将它作为 File 发送时它可以识别文本......只是不是StreamByte Array.

我在这里测试了我的演讲音频文件:

https://www.pacdv.com/sounds/voices-4.html

c# asp.net azure api speech
© www.soinside.com 2019 - 2024. All rights reserved.