1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
| unit BassComp;
interface
uses
System.SysUtils, System.Classes, System.SysConst, System.Types,
Dynamic_Bass;
type
TEventInitProc = procedure(Sender: TObject; Initialized: Boolean) of object;
TEventPlayingProc = procedure(Sender: TObject; Position: Int64) of object;
TEventDonePlayingProc = procedure(Sedner: TObject) of object;
TEventDoneDownload = procedure(Sender: TObject; Downloaded: Boolean)
of object;
TBass = class(TComponent)
private
FLibrary: string;
FInitialized: Boolean;
// Track
FLength: Int64;
FPosition: Int64;
FLoop: Boolean;
// Stream
FStream: HSTREAM;
// Volume, Mute and etc.
FVolume: Single;
FMute: Boolean;
FReservedVolume: Single;
// Event Procedures
FOnInitialized: TEventInitProc;
FOnPlaying: TEventPlayingProc;
FOnDonePlaying: TEventDonePlayingProc;
FOnStreamDownloaded: TEventDoneDownload;
procedure DoInitialized(Sender: TObject; Initialized: Boolean);
procedure DoPlaying(Sender: TObject; Position: Int64);
procedure DoDonePlaying(Sender: TObject);
procedure DoStreamDownloaded(Sender: TObject; Downloaded: Boolean);
protected
public
procedure Initialize(const BASSLibrary: string; const Device: integer;
Freq: integer; Flags: Cardinal; WND: Cardinal; BGUID: PGUID);
procedure SetVolume(const NewVolume: integer);
function _getlength(): Int64;
procedure SetMute(const Mute: Boolean);
procedure PlayFile(const FileName: string);
procedure PlayURL(const URL: string);
published
property OnInitialized: TEventInitProc read FOnInitialized
write FOnInitialized;
property OnPlaying: TEventPlayingProc read FOnPlaying write FOnPlaying;
property OnDone: TEventDonePlayingProc read FOnDonePlaying
write FOnDonePlaying;
property OnStreamDownloaded: TEventDoneDownload read FOnStreamDownloaded
write FOnStreamDownloaded;
property Volume: Single read FVolume;
property Loop: Boolean read FLoop write FLoop;
end;
procedure register;
var
b: TBass;
implementation
procedure register;
begin
RegisterComponents('HunterComponents', [TBass]);
end;
{ Sync Procs }
procedure sync_playing(HSYNC: Cardinal; Channel: DWORD; Data: DWORD;
User: Pointer);
begin
b.DoPlaying(b, BASS_ChannelGetPosition(Channel, BASS_POS_BYTE));
end;
procedure sync_doneplaying(HSYNC: Cardinal; Channel: DWORD; Data: DWORD;
User: Pointer);
begin
BASS_ChannelRemoveSync(Channel, HSYNC);
BASS_StreamFree(Channel);
b.DoDonePlaying(b);
end;
procedure sync_downloaded(HSYNC: Cardinal; Channel: DWORD; Data: DWORD;
User: Pointer);
begin
b.DoStreamDownloaded(b, true);
end;
{ TBass }
procedure TBass.DoDonePlaying(Sender: TObject);
begin
FOnDonePlaying(Sender);
end;
procedure TBass.DoInitialized(Sender: TObject; Initialized: Boolean);
begin
FInitialized := Initialized;
FOnInitialized(Sender, Initialized);
end;
procedure TBass.DoPlaying(Sender: TObject; Position: Int64);
begin
FOnPlaying(Sender, Position);
end;
procedure TBass.DoStreamDownloaded(Sender: TObject; Downloaded: Boolean);
begin
FOnStreamDownloaded(Sender, Downloaded);
end;
procedure TBass.Initialize(const BASSLibrary: string; const Device: integer;
Freq: integer; Flags, WND: Cardinal; BGUID: PGUID);
begin
if BASSLibrary <> '' then
begin
if Load_BASSDLL(BASSLibrary) = true then
begin
if not BASS_Init(Device, Freq, Flags, WND, BGUID) then
begin
DoInitialized(Self, False);
end
else
begin
DoInitialized(Self, true);
b := Self;
end;
end;
end;
end;
procedure TBass.PlayFile(const FileName: string);
begin
if FInitialized = true then
begin
if FileName <> '' then
begin
if BASS_ChannelIsActive(FStream) = BASS_ACTIVE_PLAYING then
begin
BASS_ChannelStop(FStream);
end;
FStream := BASS_StreamCreateFile(False, PChar(AnsiString(FileName)
), 0, 0, BASS);
BASS_ChannelGetAttribute(FStream, BASS_ATTRIB_VOL, FVolume);
FLength := BASS_ChannelGetLength(FStream, BASS_POS_BYTE);
BASS_ChannelSetSync(FStream, BASS_SYNC_POS, 0,
@sync_playing, nil);
BASS_ChannelSetSync(FStream, BASS_SYNC_DOWNLOAD, 0,
@sync_downloaded, nil);
BASS_ChannelPlay(FStream, FLoop);
BASS_ChannelSetSync(FStream, BASS_SYNC_END, 0,
@sync_doneplaying, nil);
end;
end;
end;
procedure TBass.PlayURL(const URL: string);
begin
if FInitialized = true then
begin
if URL <> '' then
begin
if BASS_ChannelIsActive(FStream) = BASS_ACTIVE_PLAYING then
begin
BASS_ChannelStop(FStream);
end;
FStream := BASS_StreamCreateURL(PAnsiChar(AnsiString(URL)), 0, 0,
nil, nil);
BASS_ChannelSetSync(FStream, BASS_SYNC_POS, 0,
@sync_playing, nil);
BASS_ChannelSetSync(FStream, BASS_SYNC_END, 0,
@sync_doneplaying, nil);
BASS_ChannelSetSync(FStream, BASS_SYNC_DOWNLOAD, 0,
@sync_downloaded, nil);
FLength := BASS_ChannelGetLength(FStream, BASS_POS_BYTE);
BASS_ChannelPlay(FStream, FLoop);
end;
end;
end;
procedure TBass.SetMute(const Mute: Boolean);
begin
if Mute = true then
begin
BASS_ChannelGetAttribute(FStream, BASS_ATTRIB_VOL, FReservedVolume);
BASS_ChannelSetAttribute(FStream, BASS_ATTRIB_VOL, 0);
FVolume := 0;
Exit;
end;
if Mute = False then
begin
BASS_ChannelSetAttribute(FStream, BASS_ATTRIB_VOL, FReservedVolume);
FVolume := FReservedVolume;
Exit;
end;
end;
procedure TBass.SetVolume(const NewVolume: integer);
begin
BASS_ChannelSetAttribute(FStream, BASS_ATTRIB_VOL, NewVolume);
FVolume := NewVolume;
end;
function TBass._getlength: Int64;
begin
Result := BASS_ChannelGetLength(FStream, BASS_POS_BYTE);
end;
begin
end. |