I'm trying to encode and decode opus data using opusdec in c#.
`using NAudio.Wave;
using System.Diagnostics;
List<byte[]> samples = new List<byte[]>();
WaveInEvent waveIn = new WaveInEvent();
WaveOutEvent waveOut = new WaveOutEvent();
waveIn.WaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(48000, 2);
waveIn.BufferMilliseconds = 50;
var writer = new WaveFileWriter("output.wav", waveIn.WaveFormat);
waveIn.DataAvailable += (sender, e) =>
{
using (MemoryStream m4 = new MemoryStream())
using (var reader = new RawSourceWaveStream(new MemoryStream(e.Buffer, 0, e.BytesRecorded), waveIn.WaveFormat))
//using (var conversionStream = new WaveFormatConversionProvider(waveIn.WaveFormat, reader))
{
writer.Write(e.Buffer, 0, e.BytesRecorded);
WaveFileWriter.WriteWavFileToStream(m4, reader);
m4.Seek(0, SeekOrigin.Begin);
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "opusenc.exe",
Arguments = "--quiet --speech --bitrate 160 - -",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
MemoryStream opusBytes = new MemoryStream();
using (Process process = Process.Start(startInfo))
{
process.StandardInput.BaseStream.Write(m4.ToArray());
process.StandardInput.Flush();
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
{
opusBytes.Write(buffer, 0, bytes);
}
}
Console.WriteLine(m4.Length);
Console.WriteLine(opusBytes.Length);
samples.Add(opusBytes.ToArray());
}
Console.WriteLine("Recording");
};
waveIn.StartRecording();
Console.ReadLine();
waveIn.StopRecording();
Thread.Sleep(1000);
writer.Close();
foreach(var sample in samples)
{
//File.WriteAllBytes("temp.opus", sample);
MemoryStream wavBytes = new MemoryStream();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "opusdec.exe",
Arguments = "- -",
RedirectStandardInput = true, // Rediriger l'entrée standard
RedirectStandardOutput = true,
UseShellExecute = false
};
using (Process process = Process.Start(startInfo))
{
process.StandardInput.BaseStream.Write(sample);
process.StandardInput.Close();
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
{
wavBytes.Write(buffer, 0, bytes);
}
}
wavBytes.Position = 0;
waveOut.Init(new RawSourceWaveStream(wavBytes, new WaveFormat(48000,16,2)));
waveOut.Play();
//Console.ReadLine();
while(waveOut.PlaybackState == PlaybackState.Playing)
{ }
}`
but opusdec throws
Failed to open '-'.
If i unquote //File.WriteAllBytes("temp.opus", sample); and try to decode the file using cmd and opusdec it works fine.
If i replace Arguments = "- -", by Arguments = "temp.opus -", it works fine.
If i type
ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "opusdec.exe", Arguments = "- test.wav", RedirectStandardInput = true, // Rediriger l'entrée standard RedirectStandardOutput = true, UseShellExecute = false }; using (Process process = Process.Start(startInfo)) { process.StandardInput.BaseStream.Write(File.ReadAllBytes("temp.opus")); process.StandardInput.Close(); byte[] buffer = new byte[1024]; int bytes = 0; while ((bytes = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0) { wavBytes.Write(buffer, 0, bytes); } } it also works fine.
All of this leads me to thing there is an issue when using stdin and stdout with opusdec.exe
I'm trying to encode and decode opus data using opusdec in c#.
`using NAudio.Wave;
using System.Diagnostics;
List<byte[]> samples = new List<byte[]>();
WaveInEvent waveIn = new WaveInEvent();
WaveOutEvent waveOut = new WaveOutEvent();
waveIn.WaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(48000, 2);
waveIn.BufferMilliseconds = 50;
var writer = new WaveFileWriter("output.wav", waveIn.WaveFormat);
waveIn.DataAvailable += (sender, e) =>
{
using (MemoryStream m4 = new MemoryStream())
using (var reader = new RawSourceWaveStream(new MemoryStream(e.Buffer, 0, e.BytesRecorded), waveIn.WaveFormat))
//using (var conversionStream = new WaveFormatConversionProvider(waveIn.WaveFormat, reader))
{
writer.Write(e.Buffer, 0, e.BytesRecorded);
WaveFileWriter.WriteWavFileToStream(m4, reader);
m4.Seek(0, SeekOrigin.Begin);
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "opusenc.exe",
Arguments = "--quiet --speech --bitrate 160 - -",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
MemoryStream opusBytes = new MemoryStream();
using (Process process = Process.Start(startInfo))
{
process.StandardInput.BaseStream.Write(m4.ToArray());
process.StandardInput.Flush();
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
{
opusBytes.Write(buffer, 0, bytes);
}
}
Console.WriteLine(m4.Length);
Console.WriteLine(opusBytes.Length);
samples.Add(opusBytes.ToArray());
}
Console.WriteLine("Recording");
};
waveIn.StartRecording();
Console.ReadLine();
waveIn.StopRecording();
Thread.Sleep(1000);
writer.Close();
foreach(var sample in samples)
{
//File.WriteAllBytes("temp.opus", sample);
MemoryStream wavBytes = new MemoryStream();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "opusdec.exe",
Arguments = "- -",
RedirectStandardInput = true, // Rediriger l'entrée standard
RedirectStandardOutput = true,
UseShellExecute = false
};
using (Process process = Process.Start(startInfo))
{
process.StandardInput.BaseStream.Write(sample);
process.StandardInput.Close();
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
{
wavBytes.Write(buffer, 0, bytes);
}
}
wavBytes.Position = 0;
waveOut.Init(new RawSourceWaveStream(wavBytes, new WaveFormat(48000,16,2)));
waveOut.Play();
//Console.ReadLine();
while(waveOut.PlaybackState == PlaybackState.Playing)
{ }
}`
but opusdec throws
Failed to open '-'.If i unquote
//File.WriteAllBytes("temp.opus", sample);and try to decode the file using cmd and opusdec it works fine.If i replace
Arguments = "- -",byArguments = "temp.opus -",it works fine.If i type
ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "opusdec.exe", Arguments = "- test.wav", RedirectStandardInput = true, // Rediriger l'entrée standard RedirectStandardOutput = true, UseShellExecute = false }; using (Process process = Process.Start(startInfo)) { process.StandardInput.BaseStream.Write(File.ReadAllBytes("temp.opus")); process.StandardInput.Close(); byte[] buffer = new byte[1024]; int bytes = 0; while ((bytes = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0) { wavBytes.Write(buffer, 0, bytes); } }it also works fine.All of this leads me to thing there is an issue when using stdin and stdout with opusdec.exe