home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1398 < prev    next >
Text File  |  1990-12-28  |  5KB  |  134 lines

  1. Newsgroups: comp.sys.amiga,comp.sys.amiga.tech,alt.sources,alt.tv.simpsons
  2. From: deven@rpi.edu (Deven T. Corzine)
  3. Subject: "sign" program (Amiga)
  4. Message-ID: <DEVEN.90May31123518@netserv2.rpi.edu>
  5. Date: 31 May 90 16:33:47 GMT
  6.  
  7.  
  8. This is a utility which simply converts between signed and unsigned
  9. data by flipping the sign bit on each byte.  I specifically wrote it
  10. when I noticed loading the binary data for the Simpsons (mac) sound
  11. samples came out heavily distorted because unsigned data was
  12. interpreted as signed.  When the sign bit is flipped and then the raw
  13. data is read in, the sample sounds as it should.  [once you get the
  14. sample playback rate right, of course.]  Clearly, this program is
  15. intended to be run on raw sample data, not an IFF 8SVX sample; it
  16. would scramble the IFF header information.
  17.  
  18. I'll offer credit to ronf@polya.stanford.edu (Ron Frederick) for the
  19. idea of XORing with 0x80 instead of subtracting and truncating as the
  20. prototype version originally did, although that was mainly because I
  21. was too lazy to try to think through *p^=0x80 (which seemed right) at
  22. the time, since I just wanted to try fixing the sample data.  But
  23. whatever....  [Happy Ron? ;-)]
  24.  
  25. This is an incredibly trivial algorithm, but the implementation is
  26. specific to the Amiga.  If someone wants to run it on Unix or a PC or
  27. something, they can damn well write the code themselves.  After all,
  28. you just read a buffer, XOR each byte with 0x80, and write the buffer
  29. back out.  I wanted a small, efficient implementation for the Amiga,
  30. and that's what this is.
  31.  
  32. Usage is simple: it reads from its standard input and writes to its
  33. standard output.  Use shell I/O redirection to define files to read
  34. from and write to, as in:
  35.  
  36. sign <sound.data >newsound.data
  37.  
  38. It is dynamic, using either half of total available memory or the
  39. largest block of free memory.  After each Read() or Write(), it checks
  40. for a Control-C break.  [Note if the buffer size is large, the single
  41. Read() or Write() may take a noticable amount of time.]
  42.  
  43. These are the files in this posting:
  44.  
  45. -rw-r--r--  1 deven    wheel         304 May 31 12:01 sign
  46. -rw-r--r--  1 deven    wheel        1095 May 31 12:01 sign.c
  47.  
  48. I'm not bothering to try to shar or zoo them; it's not hard to
  49. extract.  Just uudecode the posting to get the sign binary
  50. (executable) and edit the posting to save the source portion.
  51.  
  52. Here are the files:
  53.  
  54. -rw-r--r--  1 deven    wheel         304 May 31 12:01 sign
  55.  
  56. begin 644 sign
  57. M   #\P         !              !#   #Z0   $-.5?_H2.<S,$/Z /9P
  58. M "QX  1.KOW8*T#_Z&<  -I.KO]\<@!.KO\HXH N '("2$%.KO\H+ "^AF\"
  59. M+@9*AV<  )8@!W( 3J[_.B9 ($L@"&<  (1.KO]V+&W_Z$ZN_\HB "0+)@=.
  60. MKO_6+ !*AF]8<  B "QX  1.KO[."   #&9&)$(@0M'&*TC_]+7M__1D#G  
  61. M$!(*0 " %(!2BF#L+&W_Z$ZN_\0B "0+)@9.KO_04H!G$G  (@ L>  $3J[^
  62. MS@@   QGD")+( <L>  $3J[_+F *+'@ !$ZN_W9\_R)M_^A.KOYB</^\@&8$
  63. B< I@!G  8 )P%$S?#,Q.74YU9&]S+FQI8G)A<GD    #\NA.
  64. end
  65.  
  66. -rw-r--r--  1 deven    wheel        1095 May 31 12:01 sign.c
  67.  
  68. --------
  69. /* sign.c - change data from unsigned to signed or vice versa */
  70.  
  71. /* by Deven Thomas Corzine 5/19/90 */
  72.  
  73. #include <exec/types.h>
  74. #include <exec/memory.h>
  75. #include <libraries/dos.h>
  76. #include <libraries/dosextens.h>
  77. #include <proto/exec.h>
  78. #include <proto/dos.h>
  79.  
  80. sign()
  81. {
  82.    register char *buf,*p,*end;
  83.    register int bufsize,n;
  84.    register struct DosLibrary *DOSBase;
  85.  
  86.    if (DOSBase=(struct DosLibrary *) OpenLibrary(DOSNAME,0)) {
  87.       Forbid();
  88.       bufsize=AvailMem(0)>>1;
  89.       n=AvailMem(MEMF_LARGEST);
  90.       if (bufsize>n) bufsize=n;
  91.       if (bufsize && (buf=AllocMem(bufsize,0))) {
  92.          Permit();
  93.          while ((n=Read(Input(),buf,bufsize))>0) {
  94.             if (SetSignal(0,0)&SIGBREAKF_CTRL_C) break;
  95.             for (p=buf,end=buf+n;p<end;p++) *p^=0x80;
  96.             if (Write(Output(),buf,n)==-1) break;
  97.             if (SetSignal(0,0)&SIGBREAKF_CTRL_C) break;
  98.          }
  99.          FreeMem(buf,bufsize);
  100.       } else {
  101.          Permit();
  102.          n= -1;
  103.       }
  104.       CloseLibrary((struct Library *) DOSBase);
  105.       if (n==-1) return(10);
  106.       return(0);
  107.    }
  108.    return(20);
  109. }
  110. --------
  111.  
  112. The source was compiled using Lattice C V5.04, using the following
  113. command lines:
  114.  
  115. lc -cuist -v -ms sign.c
  116. blink sign.o to sign sc sd nd
  117.  
  118. Final executable size was (as shown in the ls listing above) 304
  119. bytes.  The executable is pure and reentrant, so it can safely be made
  120. resident.  Operation speed is fairly decent.  [depends on your
  121. processor and I/O speed, but it's not much slower than copying the
  122. file...]
  123.  
  124. Have fun.
  125.  
  126. Deven
  127.  
  128.  
  129. -- 
  130. Deven T. Corzine        Internet:  deven@rpi.edu, shadow@pawl.rpi.edu
  131. Snail:  2214 12th St. Apt. 2, Troy, NY 12180   Phone:  (518) 271-0750
  132. Bitnet:  deven@rpitsmts, userfxb6@rpitsmts     UUCP:  uunet!rpi!deven
  133. Simple things should be simple and complex things should be possible.
  134.