home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d56 / WAVFILE.ZIP / WAVFile.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2001-08-02  |  6.4 KB  |  178 lines

  1. { *************************************************************************** }
  2. {                                                                             }
  3. { Audio Tools Library (Freeware)                                              }
  4. { Class TWAVFile - for extracting information from WAV file header            }
  5. {                                                                             }
  6. { Copyright (c) 2001 by Jurgen Faul                                           }
  7. { E-mail: jfaul@gmx.de                                                        }
  8. { http://jfaul.de/atl                                                         }
  9. {                                                                             }
  10. { Version 1.0 (31 July 2001)                                                  }
  11. {   - Info: channel mode, sample rate, bits per sample, file size, duration   }
  12. {                                                                             }
  13. { *************************************************************************** }
  14.  
  15. unit WAVFile;
  16.  
  17. interface
  18.  
  19. uses
  20.   Classes, SysUtils;
  21.  
  22. const
  23.   { Used with ChannelModeID property }
  24.   WAV_CM_MONO = 1;                                      { Index for mono mode }
  25.   WAV_CM_STEREO = 2;                                  { Index for stereo mode }
  26.  
  27.   { Channel mode names }
  28.   WAV_MODE: array [0..2] of string = ('Unknown', 'Mono', 'Stereo');
  29.  
  30. type
  31.   { Class TWAVFile }
  32.   TWAVFile = class(TObject)
  33.     private
  34.       { Private declarations }
  35.       FValid: Boolean;
  36.       FChannelModeID: Byte;
  37.       FSampleRate: Word;
  38.       FBitsPerSample: Byte;
  39.       FFileSize: Cardinal;
  40.       procedure FResetData;
  41.       function FGetChannelMode: string;
  42.       function FGetDuration: Double;
  43.     public
  44.       { Public declarations }
  45.       constructor Create;                                     { Create object }
  46.       function ReadFromFile(const FileName: string): Boolean;   { Load header }
  47.       property Valid: Boolean read FValid;             { True if header valid }
  48.       property ChannelModeID: Byte read FChannelModeID;   { Channel mode code }
  49.       property ChannelMode: string read FGetChannelMode;  { Channel mode name }
  50.       property SampleRate: Word read FSampleRate;          { Sample rate (hz) }
  51.       property BitsPerSample: Byte read FBitsPerSample;     { Bits per sample }
  52.       property FileSize: Cardinal read FFileSize;         { File size (bytes) }
  53.       property Duration: Double read FGetDuration;       { Duration (seconds) }
  54.   end;
  55.  
  56. implementation
  57.  
  58. type
  59.   { Real structure of WAV file header }
  60.   WAVRecord = record
  61.     { RIFF file header }
  62.     RIFFHeader: array [1..4] of Char;                        { Must be "RIFF" }
  63.     FileSize: Integer;                           { Must be "RealFileSize - 8" }
  64.     WAVEHeader: array [1..4] of Char;                        { Must be "WAVE" }
  65.     { Format information }
  66.     FormatHeader: array [1..4] of Char;                      { Must be "fmt " }
  67.     FormatSize: Integer;                               { Must be 16 (decimal) }
  68.     FormatCode: Word;                                             { Must be 1 }
  69.     ChannelNumber: Word;                                 { Number of channels }
  70.     SampleRate: Integer;                                   { Sample rate (hz) }
  71.     BytesPerSecond: Integer;                               { Bytes per second }
  72.     BytesPerSample: Word;                                  { Bytes per Sample }
  73.     BitsPerSample: Word;                                    { Bits per sample }
  74.     { Data area }
  75.     DataHeader: array [1..4] of Char;                        { Must be "data" }
  76.     DataSize: Integer;                                            { Data size }
  77.   end;
  78.  
  79. { ********************* Auxiliary functions & procedures ******************** }
  80.  
  81. function ReadWAV(const FileName: string; var WAVData: WAVRecord): Boolean;
  82. var
  83.   SourceFile: file;
  84.   Transferred: Integer;
  85. begin
  86.   try
  87.     Result := true;
  88.     { Set read-access and open file }
  89.     AssignFile(SourceFile, FileName);
  90.     FileMode := 0;
  91.     Reset(SourceFile, 1);
  92.     { Read header }
  93.     BlockRead(SourceFile, WAVData, 44, Transferred);
  94.     CloseFile(SourceFile);
  95.     { if transfer is not complete }
  96.     if Transferred < 44 then Result := false;
  97.   except
  98.     { Error }
  99.     Result := false;
  100.   end;
  101. end;
  102.  
  103. { --------------------------------------------------------------------------- }
  104.  
  105. function HeaderIsValid(const WAVData: WAVRecord): Boolean;
  106. begin
  107.   Result := true;
  108.   { Validation }
  109.   if WAVData.RIFFHeader <> 'RIFF' then Result := false;
  110.   if WAVData.WAVEHeader <> 'WAVE' then Result := false;
  111.   if WAVData.FormatHeader <> 'fmt ' then Result := false;
  112.   if WAVData.FormatSize <> 16 then Result := false;
  113.   if WAVData.FormatCode <> 1 then Result := false;
  114.   if WAVData.DataHeader <> 'data' then Result := false;
  115.   if (WAVData.ChannelNumber <> WAV_CM_MONO) and
  116.     (WAVData.ChannelNumber <> WAV_CM_STEREO) then Result := false;
  117. end;
  118.  
  119. { ********************** Private functions & procedures ********************* }
  120.  
  121. procedure TWAVFile.FResetData;
  122. begin
  123.   FValid := false;
  124.   FChannelModeID := 0;
  125.   FSampleRate := 0;
  126.   FBitsPerSample := 0;
  127.   FFileSize := 0;
  128. end;
  129.  
  130. { --------------------------------------------------------------------------- }
  131.  
  132. function TWAVFile.FGetChannelMode: string;
  133. begin
  134.   Result := WAV_MODE[FChannelModeID];
  135. end;
  136.  
  137. { --------------------------------------------------------------------------- }
  138.  
  139. function TWAVFile.FGetDuration: Double;
  140. begin
  141.   if FValid then
  142.     Result := (FFileSize - 44) * 8 /
  143.       FSampleRate / FBitsPerSample / FChannelModeID
  144.   else
  145.     Result := 0;
  146. end;
  147.  
  148. { ********************** Public functions & procedures ********************** }
  149.  
  150. constructor TWAVFile.Create;
  151. begin
  152.   inherited;
  153.   FResetData;
  154. end;
  155.  
  156. { --------------------------------------------------------------------------- }
  157.  
  158. function TWAVFile.ReadFromFile(const FileName: string): Boolean;
  159. var
  160.   WAVData: WAVRecord;
  161. begin
  162.   { Reset and load header data from file to variable }
  163.   FResetData;
  164.   Result := ReadWAV(FileName, WAVData);
  165.   { Process data if loaded and header valid }
  166.   if (Result) and (HeaderIsValid(WAVData)) then
  167.   begin
  168.     FValid := true;
  169.     { Fill properties with header data }
  170.     FChannelModeID := WAVData.ChannelNumber;
  171.     FSampleRate := WAVData.SampleRate;
  172.     FBitsPerSample := WAVData.BitsPerSample;
  173.     FFileSize := WAVData.FileSize + 8;
  174.   end;
  175. end;
  176.  
  177. end.
  178.