home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / DELPHI / WAVESHOW / READ.ME next >
Text File  |  1995-04-27  |  4KB  |  97 lines

  1.  
  2.  
  3. WavShow - A Simple 8-bit .WAV file displayer in Borland's Delphi.
  4. Written by Pete Cervasio on April 8, 1995.
  5. Released to the public domain.
  6.  
  7.  
  8.  
  9. Okay, here's a simple 8-bit .WAV file displayer.  It's not much, and it
  10. could use some more work, but it shows how to do it.  I haven't bothered to
  11. read the wave file to make sure it's 8-bit, mono.  I'm also making the
  12. assumption that the wave header is 44 bytes long, even though some programs
  13. (like GoldWave) put extra info in there.
  14.  
  15. This software is hereby placed into the public domain.  I sure don't want
  16. it. <grin>  If you really feel that you need to give someone money for it,
  17. then send it to a charity that helps needy children.  On second thought, do
  18. that even if you don't.  There are no warranties that this will do anything
  19. but take up room on your disk drive.  If it fails to do that, you need to
  20. download another copy.
  21.  
  22. One part of the code that I like is in the Open button OnClick method:
  23.  
  24.     WaveBytes := FileSize (WaveFile) - WaveHdrSize;
  25.     BytesPerSBPosition := 1;
  26.  
  27. First, we find out how large the data portion of the file is, and set the
  28. default value of how many bytes to move in the file for a change of 1 in the
  29. scrollbar.
  30.  
  31.     while (WaveBytes div BytesPerSBPosition) > 32767 do
  32.         inc(BytesPerSBPosition);
  33.  
  34. Here, we want to make sure that we will be able to use the scrollbar to
  35. reach the far end of the file.  Since the maximum we can use is $FFFF, we
  36. keep dividing the file size by increasingly larger numbers until it gets
  37. down to something we can use.
  38.  
  39.     if WaveBytes div BytesPerSBPosition > PaintBox1.Width then
  40.     begin
  41.         ScrollBar1.Visible := True;
  42.         ScrollBar1.Max := WaveBytes div BytesPerSBPosition
  43.                         - PaintBox1.Width;
  44.     end
  45.     else
  46.         ScrollBar1.Visible := False;
  47.  
  48. Now we are doing some fudging.  If the size of the file is less than the
  49. width of the paintbox, we don't need to see the scrollbar.  If it is larger,
  50. then we show the scrollbar and set the max.  We're subtracting the width of
  51. the paintbox to keep the last byte of the file on the right hand side of the
  52. paintbox when the scrollbar is at max.  Comment out the subtraction and
  53. you'll see what I mean.  Finally:
  54.  
  55.     if ScrollBar1.Position <> 0 then
  56.         ScrollBar1.Position := 0
  57.     else
  58.         PaintBox1.Refresh;
  59.  
  60. If we're opening a second file, we may have a scrollbar that is scrolled off
  61. the 0 mark.  Setting it to 0 forces a refresh of the paintbox (it's magic!).
  62. Otherwise, we just refresh it manually.
  63.  
  64. Over in the OnPaint method for the paintbox component, we're doing this:
  65.  
  66.     CurrPos := (ScrollBar1.Position * BytesPerSBPosition) + WaveHdrSize;
  67.     Seek (WaveFile, CurrPos);
  68.  
  69. First, we figure out where in the file we should be.  This is why we needed
  70. to calculate the BytesPerSBPosition thingie.
  71.  
  72.     with PaintBox1.Canvas do
  73.     begin
  74.         FillChar (ReadBuffer, SizeOf(ReadBuffer), $80);
  75.         BlockRead (WaveFile, ReadBuffer, ClipRect.Right, NumRead);
  76.         Pen.Color := clLime;
  77.         MoveTo (0, ReadBuffer[0] shr 1);  { 128 bit high picture }
  78.         for X := 1 to NumRead - 1 do
  79.             LineTo (X, ReadBuffer[X] shr 1);
  80.     end;
  81.  
  82. This part fills the buffer with $80, which is what 8-bit .wav files use for
  83. no audio.  Then we read in as many bytes as the canvas is wide.  I use
  84. clLime for the pen, since it shows up nice on black.  Finally, we move to
  85. the first position, and draw lines to the rest.  The code above is based on
  86. the picture box being just about 128 pixels high, hence the division of the
  87. values read from the .wav file by 2.
  88.  
  89. The rest is all easier than this was.  Have fun and enjoy!
  90.  
  91. Oh, I guess I ought to include the standard email junk:
  92.  
  93. 73443,1426 on Compuserve     (73443.1426@compuserve.com)
  94. 1:130/209.0 on Fidonet       (pete.cervasio@p0.f209.n130.z1.fidonet.org)
  95.                               or something like that...
  96.  
  97.