@Juan Morales Marañon Welcome to Microsoft Q&A Forum, Thank you for posting your query here!
Plan 1:
Could you please test with the below sample and check ?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
namespace SpeechToTextStream
{
class Program
{
static async Task Main(string[] args)
{
var autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig.FromLanguages(new string[] { "en-US" });
var config = SpeechConfig.FromSubscription("965XXXXXXXXX0c8c8c2", "eastus");
config.OutputFormat = OutputFormat.Detailed;
config.SetProperty(PropertyId.Speech_LogFilename, "logFile.txt");
byte channels = 1;
byte bitsPerSample = 16;
uint samplesPerSecond = 8000;
var audioFormat = AudioStreamFormat.GetWaveFormatPCM(samplesPerSecond, bitsPerSample, channels);
Stream myAudioStream = File.OpenRead(@"C:\myfile.wav");
var callback = new MyPushAudioInputStreamCallback(myAudioStream);
var pullStream = AudioInputStream.CreatePullStream(callback, audioFormat);
var audioInput = AudioConfig.FromStreamInput(pullStream);
var recognizer = new SpeechRecognizer(config, autoDetectSourceLanguageConfig, audioInput);
Console.WriteLine("Processing the audio file...");
var result = await recognizer.RecognizeOnceAsync();
if (result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"We recognized: {result.Text}");
}
else if (result.Reason == ResultReason.NoMatch)
{
Console.WriteLine($"NOMATCH: Speech could not be recognized.");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = CancellationDetails.FromResult(result);
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 update the subscription info?");
}
}
Console.ReadLine();
}
}
public class MyPushAudioInputStreamCallback : PullAudioInputStreamCallback
{
private Stream audioStream;
public MyPushAudioInputStreamCallback(Stream audioStream)
{
this.audioStream = audioStream;
}
public override int Read(byte[] dataBuffer, uint size)
{
try
{
return audioStream.Read(dataBuffer, 0, (int)size);
}
catch (Exception ex)
{
Console.WriteLine($"Error in Read: {ex.Message}");
return -1;
}
}
public override void Close()
{
audioStream.Close();
}
}
}
.
Plan 2:
Check your Audio Format: The Speech SDK expects the audio data to be in a specific format: single-channel (mono) PCM audio data with a sample rate of 16 kHz and 16 bits per sample. If your audio data is in a different format, you’ll need to convert it to this format before using it with the Speech SDK.
.**
Plan 3:**
Check your Audio Quality: The quality of the audio data can significantly affect speech recognition results. Background noise, low volume, or low bitrate can make the speech in the audio data difficult to recognize. You might want to check the quality of your audio data and, if necessary, use audio editing software to improve it.
.
Hope this helps. If you have any follow-up questions, please let me know. I would be happy to help.