DelphiFAQ Home Search:

Determine the Bitrate of a WAVE file

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

How can I determine the bitrate of a *.WAV file?

Answer:

Use the MCI and follow these steps:


  1. open the file: TForm1.OpenMedia using MCI_OPEN
  2. obtain the desired status information: TForm1.GetStatus using MCI_STATUS
  3. then close it: TForm1.CloseMedia using MCI_CLOSE


The unit below shows how it's done.

unit fMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
    procedure OpenMedia(WaveFile: string);
    function GetStatus(StatusRequested: DWord) : LongInt;
    procedure CloseMedia;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses
  MMSystem;

var
  MyError,
  dwFlags: LongInt;
  FDeviceID: Word;

procedure TForm1.OpenMedia(WaveFile: string);
var
  MyOpenParms: TMCI_Open_Parms;
begin { TForm1.OpenMedia }
  with MyOpenParms do
  begin
    dwCallback := Handle; // TForm1.Handle
    lpstrDeviceType := PChar('WaveAudio');
    lpstrElementName := PChar(WaveFile)
  end; { with MyOpenParms }

  dwFlags := MCI_WAIT or MCI_OPEN_ELEMENT or MCI_OPEN_TYPE;
  MyError := mciSendCommand(0, MCI_OPEN, dwFlags, LongInt(@MyOpenParms));

  // one could use mciSendCommand(DevId, here to specify a particular device
  if MyError=0 then
    FDeviceID := MyOpenParms.wDeviceID
  else
    raise Exception.Create('Open Failed')
end; { TForm1.OpenMedia }


function TForm1.GetStatus(StatusRequested: DWord) : LongInt;
var
  MyStatusParms: TMCI_Status_Parms;
begin { TForm1.GetStatus }
  dwFlags := MCI_WAIT or MCI_STATUS_ITEM;

  with MyStatusParms do
  begin
    dwCallback := Handle;
    dwItem := StatusRequested
  end; { with MyStatusParms }

  MyError := mciSendCommand(FDeviceID, MCI_STATUS, MCI_WAIT or MCI_STATUS_ITEM,
                            LongInt(@MyStatusParms));
  if MyError=0 then
    Result := MyStatusParms.dwReturn
  else
    raise Exception.Create('Status call to get status of ' + IntToStr(StatusRequested)
                           + ' Failed')
end; { TForm1.GetStatus }


procedure TForm1.CloseMedia;
var
  MyGenParms: TMCI_Generic_Parms;
begin { TForm1.CloseMedia }
  if FDeviceID>0 then
  begin
    dwFlags := 0;
    MyGenParms.dwCallback := Handle; // TForm1.Handle
    MyError := mciSendCommand(FDeviceID, MCI_CLOSE, dwFlags, LongInt(@MyGenParms));
    if MyError=0 then
      FDeviceID := 0
    else
    begin
      raise Exception.Create('Close Failed')
    end; { not (MyError=0) }
  end; { FDeviceID>0 }
end; { TForm1.CloseMedia }


procedure TForm1.Button1Click(Sender: TObject);
begin { TForm1.Button1Click }
  if OpenDialog1.Execute then
  begin
    OpenMedia(OpenDialog1.FileName);
    with ListBox1.Items do
    begin
      Clear;
      Add(OpenDialog1.FileName);
      Add('Average Bytes / Sec : ' + IntToStr(GetStatus(MCI_WAVE_STATUS_AVGBYTESPERSEC)));
      Add('Bits / Sample : ' + IntToStr(GetStatus(MCI_WAVE_STATUS_BITSPERSAMPLE)));
      Add('Samples / Sec : ' + IntToStr(GetStatus(MCI_WAVE_STATUS_SAMPLESPERSEC)));
      Add('Channels : ' + IntToStr(GetStatus(MCI_WAVE_STATUS_CHANNELS)))
    end; { with ListBox1.Items }
    CloseMedia
  end; { OpenDialog1.Execute }
end; { TForm1.Button1Click }


end.
You don't like the formatting? Check out SourceCoder then!

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: