Changeset 6 for trunk/Sound.pas


Ignore:
Timestamp:
Jan 7, 2017, 11:32:14 AM (7 years ago)
Author:
chronos
Message:
  • Modified: Formated all project source files using Delphi formatter as original indentation and other formatting was really bad.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Sound.pas

    r2 r6  
    44
    55uses
    6 Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,MMSystem;
    7 
     6  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, MMSystem;
    87
    98function PrepareSound(FileName: string): integer;
    109procedure PlaySound(FileName: string);
    1110
    12 
    1311type
    14 TSoundPlayer = class(TForm)
    15 private
    16   procedure OnMCI(var m:TMessage); message MM_MCINOTIFY;
     12  TSoundPlayer = class(TForm)
     13  private
     14    procedure OnMCI(var m: TMessage); message MM_MCINOTIFY;
    1715  end;
    18 
    1916
    2017implementation
     
    2219{$R *.DFM}
    2320
    24 
    2521type
    26 TSound = class
    27 public
    28   FDeviceID: word;
    29   FFileName: string;
    30   constructor Create(const FileName : string);
    31   destructor Destroy; override;
    32   procedure Play(HWND: DWORD);
    33   procedure Stop;
    34   procedure Reset;
     22  TSound = class
     23  public
     24    FDeviceID: word;
     25    FFileName: string;
     26    constructor Create(const FileName: string);
     27    destructor Destroy; override;
     28    procedure Play(HWND: DWORD);
     29    procedure Stop;
     30    procedure Reset;
    3531  end;
    36 
    3732
    3833constructor TSound.Create(const FileName: string);
    3934var
    40 OpenParm: TMCI_Open_Parms;
     35  OpenParm: TMCI_Open_Parms;
    4136begin
    42 FDeviceID:=0;
    43 FFileName:=FileName;
    44 if FileExists(FFileName) then
     37  FDeviceID := 0;
     38  FFileName := FileName;
     39  if FileExists(FFileName) then
    4540  begin
    46   OpenParm.dwCallback:=0;
    47   OpenParm.lpstrDeviceType:='WaveAudio';
    48   OpenParm.lpstrElementName:=PChar(FFileName);
    49   mciSendCommand(0, MCI_Open,
    50     MCI_WAIT or MCI_OPEN_ELEMENT or MCI_OPEN_SHAREABLE, integer(@OpenParm));
    51   FDeviceID:=OpenParm.wDeviceID;
     41    OpenParm.dwCallback := 0;
     42    OpenParm.lpstrDeviceType := 'WaveAudio';
     43    OpenParm.lpstrElementName := PChar(FFileName);
     44    mciSendCommand(0, MCI_Open, MCI_WAIT or MCI_OPEN_ELEMENT or
     45      MCI_OPEN_SHAREABLE, integer(@OpenParm));
     46    FDeviceID := OpenParm.wDeviceID;
    5247  end
    5348end;
     
    5550destructor TSound.Destroy;
    5651begin
    57 if FDeviceID<>0 then
    58   mciSendCommand(FDeviceID, MCI_CLOSE, MCI_WAIT, 0);
    59 inherited Destroy;
     52  if FDeviceID <> 0 then
     53    mciSendCommand(FDeviceID, MCI_CLOSE, MCI_WAIT, 0);
     54  inherited Destroy;
    6055end;
    6156
    62 procedure TSound.Play(HWND: dword);
     57procedure TSound.Play(HWND: DWORD);
    6358var
    64 PlayParm: TMCI_Play_Parms;
     59  PlayParm: TMCI_Play_Parms;
    6560begin
    66 if FDeviceID<>0 then
     61  if FDeviceID <> 0 then
    6762  begin
    68   PlayParm.dwCallback:=HWND;
    69   mciSendCommand(FDeviceID, MCI_PLAY, MCI_NOTIFY, integer(@PlayParm));
     63    PlayParm.dwCallback := HWND;
     64    mciSendCommand(FDeviceID, MCI_PLAY, MCI_NOTIFY, integer(@PlayParm));
    7065  end
    7166end;
     
    7368procedure TSound.Stop;
    7469begin
    75 mciSendCommand(FDeviceID, MCI_STOP, 0, 0);
     70  mciSendCommand(FDeviceID, MCI_STOP, 0, 0);
    7671end;
    7772
    7873procedure TSound.Reset;
    7974begin
    80 mciSendCommand(FDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
     75  mciSendCommand(FDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
    8176end;
    8277
    83 
    8478type
    85 TSoundList=array[0..99999] of TSound;
     79  TSoundList = array [0 .. 99999] of TSound;
    8680
    8781var
    88 nSoundList: integer;
    89 SoundPlayer: TSoundPlayer;
    90 SoundList: ^TSoundList;
    91 PlayingSound: TSound;
    92 
     82  nSoundList: integer;
     83  SoundPlayer: TSoundPlayer;
     84  SoundList: ^TSoundList;
     85  PlayingSound: TSound;
    9386
    9487procedure TSoundPlayer.OnMCI(var m: TMessage);
    9588begin
    96 if (m.wParam=MCI_Notify_Successful) and (PlayingSound<>nil) then
     89  if (m.wParam = MCI_Notify_Successful) and (PlayingSound <> nil) then
    9790  begin
    98   PlayingSound.Reset;
    99   PlayingSound:=nil;
     91    PlayingSound.Reset;
     92    PlayingSound := nil;
    10093  end;
    10194end;
    10295
    103 
    10496function PrepareSound(FileName: string): integer;
    10597begin
    106 for result:=1 to Length(FileName) do
    107   FileName[result]:=upcase(FileName[result]);
    108 result:=0;
    109 while (result<nSoundList) and (SoundList[result].FFileName<>FileName) do
    110   inc(result);
    111 if result=nSoundList then
     98  for result := 1 to Length(FileName) do
     99    FileName[result] := upcase(FileName[result]);
     100  result := 0;
     101  while (result < nSoundList) and (SoundList[result].FFileName <> FileName) do
     102    inc(result);
     103  if result = nSoundList then
    112104  begin // first time this sound is played
    113   if nSoundList=0 then
    114     ReallocMem(SoundList, 16*4)
    115   else if (nSoundList>=16) and (nSoundList and (nSoundList-1)=0) then
    116     ReallocMem(SoundList, nSoundList*(2*4));
    117   inc(nSoundList);
    118   SoundList[result]:=TSound.Create(FileName);
     105    if nSoundList = 0 then
     106      ReallocMem(SoundList, 16 * 4)
     107    else if (nSoundList >= 16) and (nSoundList and (nSoundList - 1) = 0) then
     108      ReallocMem(SoundList, nSoundList * (2 * 4));
     109    inc(nSoundList);
     110    SoundList[result] := TSound.Create(FileName);
    119111  end;
    120112end;
     
    122114procedure PlaySound(FileName: string);
    123115begin
    124 if PlayingSound<>nil then
    125   exit;
    126 if SoundPlayer=nil then
    127   Application.CreateForm(TSoundPlayer, SoundPlayer);
    128 PlayingSound:=SoundList[PrepareSound(FileName)];
    129 if PlayingSound.FDeviceID=0 then PlayingSound:=nil
    130 else PlayingSound.Play(SoundPlayer.Handle);
     116  if PlayingSound <> nil then
     117    exit;
     118  if SoundPlayer = nil then
     119    Application.CreateForm(TSoundPlayer, SoundPlayer);
     120  PlayingSound := SoundList[PrepareSound(FileName)];
     121  if PlayingSound.FDeviceID = 0 then
     122    PlayingSound := nil
     123  else
     124    PlayingSound.Play(SoundPlayer.Handle);
    131125end;
    132126
    133127var
    134 i: integer;
     128  i: integer;
    135129
    136130initialization
    137 nSoundList:=0;
    138 SoundList:=nil;
    139 PlayingSound:=nil;
    140 SoundPlayer:=nil;
     131
     132nSoundList := 0;
     133SoundList := nil;
     134PlayingSound := nil;
     135SoundPlayer := nil;
    141136
    142137finalization
    143 if PlayingSound<>nil then
    144   begin
     138
     139if PlayingSound <> nil then
     140begin
    145141  PlayingSound.Stop;
    146142  Sleep(222);
    147   end;
    148 for i:=0 to nSoundList-1 do
     143end;
     144for i := 0 to nSoundList - 1 do
    149145  SoundList[i].Free;
    150 ReallocMem(SoundList,0);
     146ReallocMem(SoundList, 0);
    151147
    152148end.
    153 
Note: See TracChangeset for help on using the changeset viewer.