"sign" program (Amiga)

Deven T. Corzine deven at rpi.edu
Fri Jun 1 02:33:47 AEST 1990


This is a utility which simply converts between signed and unsigned
data by flipping the sign bit on each byte.  I specifically wrote it
when I noticed loading the binary data for the Simpsons (mac) sound
samples came out heavily distorted because unsigned data was
interpreted as signed.  When the sign bit is flipped and then the raw
data is read in, the sample sounds as it should.  [once you get the
sample playback rate right, of course.]  Clearly, this program is
intended to be run on raw sample data, not an IFF 8SVX sample; it
would scramble the IFF header information.

I'll offer credit to ronf at polya.stanford.edu (Ron Frederick) for the
idea of XORing with 0x80 instead of subtracting and truncating as the
prototype version originally did, although that was mainly because I
was too lazy to try to think through *p^=0x80 (which seemed right) at
the time, since I just wanted to try fixing the sample data.  But
whatever....  [Happy Ron? ;-)]

This is an incredibly trivial algorithm, but the implementation is
specific to the Amiga.  If someone wants to run it on Unix or a PC or
something, they can damn well write the code themselves.  After all,
you just read a buffer, XOR each byte with 0x80, and write the buffer
back out.  I wanted a small, efficient implementation for the Amiga,
and that's what this is.

Usage is simple: it reads from its standard input and writes to its
standard output.  Use shell I/O redirection to define files to read
from and write to, as in:

sign <sound.data >newsound.data

It is dynamic, using either half of total available memory or the
largest block of free memory.  After each Read() or Write(), it checks
for a Control-C break.  [Note if the buffer size is large, the single
Read() or Write() may take a noticable amount of time.]

These are the files in this posting:

-rw-r--r--  1 deven    wheel         304 May 31 12:01 sign
-rw-r--r--  1 deven    wheel        1095 May 31 12:01 sign.c

I'm not bothering to try to shar or zoo them; it's not hard to
extract.  Just uudecode the posting to get the sign binary
(executable) and edit the posting to save the source portion.

Here are the files:

-rw-r--r--  1 deven    wheel         304 May 31 12:01 sign

begin 644 sign
M   #\P         !              !#   #Z0   $-.5?_H2.<S,$/Z /9P
M "QX  1.KOW8*T#_Z&<  -I.KO]\<@!.KO\HXH N '("2$%.KO\H+ "^AF\"
M+ at 9*AV<  )8@!W( 3J[_.B9 ($L@"&<  (1.KO]V+&W_Z$ZN_\HB "0+)@=.
MKO_6+ !*AF]8<  B "QX  1.KO[."   #&9&)$(@0M'&*TC_]+7M__1D#G  
M$!(*0 " %(!2BF#L+&W_Z$ZN_\0B "0+)@9.KO_04H!G$G  (@ L>  $3J[^
MS@@   QGD")+( <L>  $3J[_+F *+'@ !$ZN_W9\_R)M_^A.KOYB</^\@&8$
B< I@!G  8 )P%$S?#,Q.74YU9&]S+FQI8G)A<GD    #\NA.
end

-rw-r--r--  1 deven    wheel        1095 May 31 12:01 sign.c

--------
/* sign.c - change data from unsigned to signed or vice versa */

/* by Deven Thomas Corzine 5/19/90 */

#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <proto/exec.h>
#include <proto/dos.h>

sign()
{
   register char *buf,*p,*end;
   register int bufsize,n;
   register struct DosLibrary *DOSBase;

   if (DOSBase=(struct DosLibrary *) OpenLibrary(DOSNAME,0)) {
      Forbid();
      bufsize=AvailMem(0)>>1;
      n=AvailMem(MEMF_LARGEST);
      if (bufsize>n) bufsize=n;
      if (bufsize && (buf=AllocMem(bufsize,0))) {
         Permit();
         while ((n=Read(Input(),buf,bufsize))>0) {
            if (SetSignal(0,0)&SIGBREAKF_CTRL_C) break;
            for (p=buf,end=buf+n;p<end;p++) *p^=0x80;
            if (Write(Output(),buf,n)==-1) break;
            if (SetSignal(0,0)&SIGBREAKF_CTRL_C) break;
         }
         FreeMem(buf,bufsize);
      } else {
         Permit();
         n= -1;
      }
      CloseLibrary((struct Library *) DOSBase);
      if (n==-1) return(10);
      return(0);
   }
   return(20);
}
--------

The source was compiled using Lattice C V5.04, using the following
command lines:

lc -cuist -v -ms sign.c
blink sign.o to sign sc sd nd

Final executable size was (as shown in the ls listing above) 304
bytes.  The executable is pure and reentrant, so it can safely be made
resident.  Operation speed is fairly decent.  [depends on your
processor and I/O speed, but it's not much slower than copying the
file...]

Have fun.

Deven


-- 
Deven T. Corzine        Internet:  deven at rpi.edu, shadow at pawl.rpi.edu
Snail:  2214 12th St. Apt. 2, Troy, NY 12180   Phone:  (518) 271-0750
Bitnet:  deven at rpitsmts, userfxb6 at rpitsmts     UUCP:  uunet!rpi!deven
Simple things should be simple and complex things should be possible.



More information about the Alt.sources mailing list