I am using .Net's SpeechSynthesizer to generate WAV stream from a string. I then need to convert that stream from WaveStream to Opus.
I am using the following libraries:
- NAudio
- Opus .Net: https://github.com/JohnACarruthers/Opus.NET
I am synthesizing the speech using this function:
public static Stream Speak(string text)
{
SpeechSynthesizer s = new SpeechSynthesizer();
MemoryStream stream = new MemoryStream();
s.SetOutputToWaveStream(stream);
s.Speak(text);
s.SetOutputToNull();
return stream;
}
And in order to make the conversion I am using the following function:
public static Stream SpeakOgg(string text)
{
MemoryStream orgstream = Speak(text) as MemoryStream;
orgstream.Seek(0, SeekOrigin.Begin);
WaveFileReader reader = new WaveFileReader(orgstream);
WaveFormat newFormat = new WaveFormat(16000, reader.WaveFormat.Channels);
WaveFormatConversionStream newStream = new WaveFormatConversionStream(newFormat, reader);
WaveStream conv = WaveFormatConversionStream.CreatePcmStream(newStream);
byte[] bytes = new byte[conv.Length];
conv.Position = 0;
conv.Read(bytes, 0, (int)conv.Length);
OpusEncoder encoder = OpusEncoder.Create(newStream.WaveFormat.SampleRate, newStream.WaveFormat.Channels, Opus.Application.Voip);
int encodedLength = 0;
byte[] encoded = encoder.Encode(bytes, (int)conv.Length, out encodedLength);
MemoryStream finish = new MemoryStream();
finish.Write(encoded, 0, encodedLength);
return finish;
}
My problem is that the OpusEncoder throws an exception saying:
Encoding failed - BadArg
and it is thrown on the function call to "opus_encode". Can anyone help me track the problem?
Edit 1:
After looking around I found that the exception thrown is actually a define in Opus' API called: "OPUS_BAD_ARG", on which the documentation states:
One or more invalid/out of range arguments.
I still can't find the argument that is wrong...