From: Jerry
 
Subject: Dolby encoding  (1 of 2)
Originator: jerry@nick.csh.rit.edu
Nntp-Posting-Host: nick
Organization: Computer Science House @ RIT
Distribution: csh
Date: Fri, 5 Aug 1994 18:13:44 GMT
 
(I posted these to comp.sys.amiga.audio.  the document that i reference can
be found in /stuff/anarchy/textfiles/laserdiscs)
 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
 
I have recieved mail messages asking questions on Dolby Pro logic, and
encoding it and such. I have researched the Dolby encoding scheme in futile
attempts to build my own decoder.  (encoding is simple, decoding is a bitch.)
 
Some of the information below i got from Bob Niland's textfiles on laserdisc
information.  This stuff is covered in chapter 03 (Sound), which I also
have posted to this group in its entirety.
 
Encoding the surround signal
-------- --- -------- ------
 
First of all, Here's a little mixing diagram that shows how the audio tracks
are mixed...
 
             Source Sounds                                         As Encoded

Lt
  Left ----> + -------------------------------------------------+---> Left
 Source      ^                                                  ^ Total
             |                                         5 dB     |
             |                              100-7K     Dolby  +90 deg
  Ctr ---> -3 dB     Surround ---> -3dB ---> band ---> B NR --->|
 Source      |       Source                  pass      Encode -90 deg
             |                                                  |
             v                                                  v Rt
  Right ---> + -------------------------------------------------+---> Right
 Source Total
 
 
As you can see, the Left goes straight through, as does the right.
The center source gets dropped by a few decibels, then sent to both left
and right. (That one's the bitch to decode cleanly..)  The surround source
is where it gets funky.  It gets dropped by 3 decibels, filtered (we can
skip this step, i'll explain below) gets a bit of noise reduction (skip
this one too) and then phase shifted by +90 and -90 degrees.
 
Okay.. the reason why we can forget about doing the band passs filter
ans the Dolby B encoding are as follows.  This is made to be used for
background sounds.  We are just arbitrarily throwing information there, or
at least i'm assuming we are. :)  We probably should do the Dolby encoding
though, as the rear (surround) channels go through a respective dolby B
deocoder in your surround decoder.  No biggie though..
 
A few quick notes, then i'll get into problems for coding this puppy...
 
This encoding scheme works with both regular Dolby encoding, as well as Pro
Logic Dolby encoding.  Here's why... the only difference between the two
is how they are decoded.  Regular dolby decoders just pull out that surround
channel, and let Left and Right pass through.  THere is no Center channel
decoding on regular Dolby decoders.  (there can be, but it is rare.)
 
Pro Logic deals with this, as well as decoding the center, and also trying to
cancel out signals that don't belong in each channel.  ie, it will try to
cancel sounds intended for the Left channel from the center, and surround
signals will be cancelled from the Left and Right Channels.  Pro logic is
definitely worth the extra money
 
See section 5 in the accompanying document for information on the differences
between the surround systems, as well as a way to add it yours?
just an extra speaker and some wire (see stage 1.)
 
 
Programming this thing...
----------- ---- --------
 
The Left/Right signals are easy enough to decode... they become the base for
which other samples can be added.
The center channel is rather simple also.  just subtract a constant
from the values of its samples, and add it onto the Left and Right
Channels.
 
The Surround channel is where it gets tricky...
The following is in the before mentioned document:
"Since several people have asked:  I do not know how the +90/-90 degree phase
 shift is handled.  For sounds not correlated to L&R front, +0/-180 probably
 works as well.  For correlated sounds, +0/-180 is probably safe for playback
 systems that have rear delay.  Optimally, a frequency dependent shift is
 probably used.  A pair of simple passive filter circuits might suffice."
 
For those of you who are scratch on this, a +0/-180 phase shift can also
be described as; take the sample, leave it the way it is, then send it 
to the Left.  Invert it and send it to the right.  Okay?  Okay..

There can be problems with this.  If the signale being encoded for the surround
channel is the same as the left or right, there is a possibility of it becoming
cancelled entirely, or having its volume doubled by this process.  Thats where
the +90/-90 phase shift can be employed.  You may be asking yourself..
"Now, how do we do this?" And I'm not going to tell you, because I have
no clue myself.   If I were to write an encoder, which i plan on doing,
(nothing fancy, just mixes premixed samples together..)  I would just offset
the surround signal by one or two bytes to eliminate this problem. (or make
it lesser.  If anyone out there has a clue on how to actually do a +90/-90
phase shift, please let us know... I'll be asking some of my math friends...
 
 
One question that was asked to me via mail was:
"Better yet, is there any "formula" so that
 you could encode any arbitrary mono sample into Pro Logic, with a 2-d
 "panner"?  It seems like something along those lines wouldn't be awful..
 then you could use the Pro Logic encoder to encode your mono signals into
 panned Pro Logic stereo signals, then mix an arbitrary number of those
 together (depending on how much signal loss you're willing to tolerate)."
 
okay, until i learn more, this will be a platform unspecific mixing program,
and will output raw unsigned sound samples.  (Unsigned is much easier to
work with than signed (VOC) or .AU (whichis just really screwed up to begin
with... don't get me started...)  luckilly the amiga is an
unsigned-understanding machine..  :)
 
this "panner" as you so call it can be accoplished by using the software
I'm
gonna write, and the following 4 mono sound files:
 
L-left, R-right, C-center, S-surround
   _____________                          ____________
L /             \________________________/            \________
            ______________
C _________/              \____________________________________
                       ________________
R ____________________/                \_______________________
                                  ________________
S________________________________/                \____________
 
 
Where each low part is no sound, and the higher parts are loud sounds.
 
This will appear to shift from Left to Center, to Right, to Surround, and
back to Left.
 
 
 
How's that?
 
Please post all further questions here... for those of you who have no clue,
please post and ask questions.... It's the only way to learn.  :)
 
 
        -Jerry
 
ps: Dolby, and Dolby Pro Logic are trademarks of some sort of company from
    which they originated.  Any other trademarks and copyrights are
    respective to their companies as well.
--
 
--
     _________________________________.
       /  jerry@mail.csh.rit.edu      : Amiga: We do more with 7.16 MhZ
  _   /  http:www.csh.rit.edu/~jerry/ ;        than most do all day.
  \__/       BalooLama on IRC         ,

Subject: Dolby encoding  (2 of 2)
Originator: jerry@nick.csh.rit.edu
Nntp-Posting-Host: nick
Organization: Computer Science House @ RIT
Distribution: csh
Date: Fri, 5 Aug 1994 18:16:13 GMT
 
(I posted this to comp.sys.amiga.audio)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .
.
 While at lunch, I realized how to do the +90/-90 degree Phase shift!!! It's
just so simple too!  I asked a friend how to do phase shifts, and i was being
really sketchy for no good reason, and i had said "I know how to do a 180'
phase shift -- just invert the sample." to which he responded "Yeah, but that's
just a specific example"... that got me thinking, and i now know how to do it.
It's all in my head, so bear with me... converting this to words and ascii
graphics  is gonna be interesting...
 
Okay.  This works on a sample-by-sample method.  No reason to look at ranges
of samples or anything like that, which is how i originally thought of it.
The 90' phase shift as described in the encoding process is not even a
frequency-related operation, as i thought it might be.  It's much simpler.
No fourier transforms, or anything... In fact a simple Look-up table would
work perfectly.
 
I'm not sure of an algorithm to do this yet, but here goes in English (go
figure!) with examples...
 
Say we have the following sample/waveform  let each vertical column be a
sample:NOTE: in these examples, I'll be using ranges from a unsigned 8-bit
sound sample. Extrapolate these numbers to be useful to you.
 
 
 127      ...
         .   .               ...
        .     .            ..   .
 0    ..       .       ....      .....
     .          .     .
                 .   .
-127              ...
 
Okay?  okay.
if we were to invert this, or do a 180 degree phase shift, we'd get the
following:
 
-127              ...
                 .   .
     .          .     .
 0    ..       .       ....      .....
        .     .            ..   .
         .   .               ...
 127      ...
 
which would cancel out the first.  however, if we were to do a 90' shift on
the first, we'd get:
 
 127  ..               ....      .....
     .  .             .    ..   .
         .           .       ...
 0        ...     ...
             .   .
              . .
-127           .
(this one may be wrong, but for illustration purposes, it's fine)
 
And a -90' would give the invers of that... (both together would cancel each
other out.)
 
 
Now, how do we do this?  It's easy. Think of a specific sample as being a
marble in a track with rubber bands at either end.. (+127 & -127, 0 in the
middle) so that if you were to throw the ball to one end, (up to 127) it would
bounce back to the other end.  Say the ball is smart.  If you give it a force
of 10, it will only move 10 units in the direction you pushed it to.  Let's
use Positive as being towards 127, and negative as being towards -127.  So If
you were to push this marble from 0  with a force of -30, it would be at -30.
By using the first two examples above, the force needed to convert a sample
to be it's inverse, or 180' phase shift would be 255.  Start the marble at 0,
push with a force of +255 , it would move +127 bounce off the end, then go
-127 back the other way, ending in 0.  if it were to start at 127, +255 would
make it bounce immediately, then go 255 back in the negative direction,
causing it to stop in -127.  Understad this?  good.  So, if we were to take
this one step further, a 90' phase shift would need half the fource than that
of a 180' phase shift, or a force of 127.  Use the same method with this
on consecutive samples, with a +90 shift, then -90 shift, add them together i
and they cancel.  Why would you want to do this?  I have no clue, but it's
that reasoning that keeps background sounds out of the center channel.
 
So that +90'/-90' phase shift is not as elusive as once i thought..
 
The only problem now is that i think sound samples may be logarithmic, as
that's how they are in .au format on this sparc... that may fix some problems,
like a perfect sine wave, witha 90' shift would, instead of being nice gentle
curves, would be a curve to a point at max volume, then a curve back down to
zero, and a curve down to min, etc...
 
 
 __      __      ___
/  \    /  \    /                           \__  __/\__  __/\
    \__/    \__/    etc...   == yields ==>     \/      \/     etc..
 
 
Not sure how to handle this... i was never good at logs...
 
 
Constants:
for a 180' phase shift, the constant is 255.
for a 90' phase shift, the constant is 127.
etc... it is linear.
 
 
 
Other notes:
 
Sound cleaning-up:
----- ------------
On a side note: to do dolby B noise reduction, a cheat to do, which will
permanently muffle the sound, or clean it up, however you want to look at it,
is to read in 3 samples, a, b, c, then average them, and throw that result in
b, and out to a file.. then move b to a, c to b and get the next c.
 
   a b c
     a b c
 
then do the same thing, over and over until you reach the end..
 

--
     _________________________________.
       /  jerry@mail.csh.rit.edu      : Amiga: We do more with 7.16 MhZ
  _   /  http:www.csh.rit.edu/~jerry/ ;        than most do all day.
  \__/       BalooLama on IRC         ,
