Ben Biddington

Whatever it is, it's not about "coding"

.NET Process — working with binary output

with one comment

Lately we discovered an issue while encoding Mp3 files with LameOur client reported encoded files we garbled; playable but watery — and full of pops and clicks.

We found this was due to interpreting the binary output from Lame as text — we had mistakenly employed Process.BeginOutputReadLine and its companion event OutputDataReceived.

Process.OutputDataReceived

By observing a Process using its OutputDataReceived event, clients can make asynchronous reads on a process’s StandardOutput.

Process.StandardOutput is a TextReader: it represents a reader that can read a sequential series of characters, i.e., it interprets its underlying stream as text.

When StandardOutput is being read asynchronously, the Process class monitors it, collecting characters into a string. Once it encounters a line ending, it notifies observers (handlers of its OutputDataReceived event), with the line of text it’s been collecting.

In short, the Process‘s underlying byte stream is converted to lines of text, and clients are notified one line at a time.

In doing so, some bytes are discarded: any bytes that (in the current encoding) represent line endings.

As a result of these missing bytes, our output Mp3s were playable, but sounded terrible.

Solution

Bypass StandardOutput. Use its underlying Stream instead.

Advertisements

Written by benbiddington

7 September, 2009 at 08:00

One Response

Subscribe to comments with RSS.

  1. Why not just stitch the lines back together? From memory this was because there is no way to tell which characters were missing — may have been \r\n, may have been just \n.

    Perhap both’re interpreted as new lines and because they’re discarded we can never tell which it was.

    But is there a way to influence line ending behaviour? If there is, then we could just add the missing character(s).

    Still, maybe for binary its better to avoid conversion to characters anyway.

    Ben

    13 February, 2010 at 12:52


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: