Mega Code Archive

 
Categories / Delphi / Examples
 

Determine the bitrate of a wave file

Question: How can I determine the bitrate of a *.WAV file? Answer: Use the MCI and follow these steps: open the file: TForm1.OpenMedia using MCI_OPEN obtain the desired status information: TForm1.GetStatus using MCI_STATUS 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.