Gaan_setaglA Gaan_setaglA - 29 days ago 17
C# Question

Sox returns some value but StandardOutput.ReadToEnd() returns empty

try
{
string filename = "E:\\sox-14-4-0\\mysamplevoice.wav";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "E:\\sox-14-4-0\\sox.exe ";
p.StartInfo.Arguments = filename + " -n stat";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
catch(Exception Ex)
{
Console.WriteLine(Ex.Message);
}


The output is always empty. When I run that
sox
command in command prompt I could get a response like:

E:\sox-14-4-0>sox mysamplevoice.wav -n stat
Samples read: 26640
Length (seconds): 3.330000
Scaled by: 2147483647.0
Maximum amplitude: 0.515625
Minimum amplitude: -0.734375
Midline amplitude: -0.109375
Mean norm: 0.058691
Mean amplitude: 0.000122
RMS amplitude: 0.101146
Maximum delta: 0.550781
Minimum delta: 0.000000
Mean delta: 0.021387
RMS delta: 0.041831
Rough frequency: 526
Volume adjustment: 1.362


When running the same command in C# I get the same result but value of "output" is empty.

Answer

Are you sure sox.exe writes to STDOUT and not to STDERR ?

you could try reading the data using the OutputDataReceived event instead.

string filename = "E:\\sox-14-4-0\\mysamplevoice.wav";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "E:\\sox-14-4-0\\sox.exe ";
p.StartInfo.Arguments = filename + " -n stat";

p.OutputDataReceived += process_OutputDataReceived;
p.ErrorDataReceived += process_ErrorDataReceived;

p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();


void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    string s = e.Data;
}

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    string s = e.Data;
}