source: branches/delphi/Sound.pas

Last change on this file was 6, checked in by chronos, 7 years ago
  • Modified: Formated all project source files using Delphi formatter as original indentation and other formatting was really bad.
File size: 3.2 KB
Line 
1unit Sound;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, MMSystem;
7
8function PrepareSound(FileName: string): integer;
9procedure PlaySound(FileName: string);
10
11type
12 TSoundPlayer = class(TForm)
13 private
14 procedure OnMCI(var m: TMessage); message MM_MCINOTIFY;
15 end;
16
17implementation
18
19{$R *.DFM}
20
21type
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;
31 end;
32
33constructor TSound.Create(const FileName: string);
34var
35 OpenParm: TMCI_Open_Parms;
36begin
37 FDeviceID := 0;
38 FFileName := FileName;
39 if FileExists(FFileName) then
40 begin
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;
47 end
48end;
49
50destructor TSound.Destroy;
51begin
52 if FDeviceID <> 0 then
53 mciSendCommand(FDeviceID, MCI_CLOSE, MCI_WAIT, 0);
54 inherited Destroy;
55end;
56
57procedure TSound.Play(HWND: DWORD);
58var
59 PlayParm: TMCI_Play_Parms;
60begin
61 if FDeviceID <> 0 then
62 begin
63 PlayParm.dwCallback := HWND;
64 mciSendCommand(FDeviceID, MCI_PLAY, MCI_NOTIFY, integer(@PlayParm));
65 end
66end;
67
68procedure TSound.Stop;
69begin
70 mciSendCommand(FDeviceID, MCI_STOP, 0, 0);
71end;
72
73procedure TSound.Reset;
74begin
75 mciSendCommand(FDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
76end;
77
78type
79 TSoundList = array [0 .. 99999] of TSound;
80
81var
82 nSoundList: integer;
83 SoundPlayer: TSoundPlayer;
84 SoundList: ^TSoundList;
85 PlayingSound: TSound;
86
87procedure TSoundPlayer.OnMCI(var m: TMessage);
88begin
89 if (m.wParam = MCI_Notify_Successful) and (PlayingSound <> nil) then
90 begin
91 PlayingSound.Reset;
92 PlayingSound := nil;
93 end;
94end;
95
96function PrepareSound(FileName: string): integer;
97begin
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
104 begin // first time this sound is played
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);
111 end;
112end;
113
114procedure PlaySound(FileName: string);
115begin
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);
125end;
126
127var
128 i: integer;
129
130initialization
131
132nSoundList := 0;
133SoundList := nil;
134PlayingSound := nil;
135SoundPlayer := nil;
136
137finalization
138
139if PlayingSound <> nil then
140begin
141 PlayingSound.Stop;
142 Sleep(222);
143end;
144for i := 0 to nSoundList - 1 do
145 SoundList[i].Free;
146ReallocMem(SoundList, 0);
147
148end.
Note: See TracBrowser for help on using the repository browser.