-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMADXDllinterface.pas
375 lines (317 loc) · 8.75 KB
/
MADXDllinterface.pas
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
unit MADXDllInterface;
interface
uses
Windows,sysutils,typestuff,winsock2;
const
MADX_INPUT_BUFFER_SIZE = (5*1152*8);
MADX_OUTPUT_BUFFER_SIZE = (1152*8);
MAD_BUFFER_GUARD =8;
type
madx_sig = (
ERROR_OCCURED,
MORE_INPUT,
FLUSH_BUFFER,
EOF_REACHED,
CALL_AGAIN );
type
size_t=longint;
madx_stat = record
msg: Array[0..256-1] of Char;
write_size: SIZE_T;
is_eof: Integer;
readsize: SIZE_T;
remaining: SIZE_T;
{/// Will reference some }
{/// "middle part" of in_buffer: }
buffstart: PByte;
end;
function madx_init(out_buffer: PByte;
mxhouse: pointer): Integer cdecl {$IFDEF WIN32} stdcall {$ENDIF};
function madx_read( in_buffer: PByte;
out_buffer: PByte;
mxhouse: pointer;
var mxstat: MADX_STAT): MADX_SIG cdecl {$IFDEF WIN32} stdcall {$ENDIF};
procedure madx_deinit(mxhouse: pointer) cdecl {$IFDEF WIN32} stdcall {$ENDIF};
type
TMp3File=class(Tobject)
private
fil:file of byte;
in_buffer: array [0..MADX_INPUT_BUFFER_SIZE -1] of byte ;
out_buffer: array [0..MADX_OUTPUT_BUFFER_SIZE+ MAD_BUFFER_GUARD-1] of byte ;
mxhouse:pointer;
pnagybuf:Pointer;
mxstat:madx_stat;
public
iseof:boolean;
samplerate:integer;
constructor create(filnam:string);
destructor destroy; override;
procedure read(var hova:Tsmallintdynarr);
end;
TMp3Stream=class(Tobject)
private
in_buffer: array [0..MADX_INPUT_BUFFER_SIZE -1] of byte ;
out_buffer: array [0..MADX_OUTPUT_BUFFER_SIZE+ MAD_BUFFER_GUARD-1] of byte ;
mxhouse:pointer;
pnagybuf:Pointer;
mxstat:madx_stat;
sck:dword;
mxsig:madx_sig;
public
error:integer; //0 ha minen oké.
samplerate:integer;//url:port, ha lehet. 8000-re defaultol
nev:string;
constructor create(url:string);
destructor destroy; override;
procedure read(var hova:Tsmallintdynarr);
end;
type
Pstaticintarr = ^Tstaticintarr;
Tstaticintarr = array [0..500] of integer;
implementation
procedure madx_deinit; external 'madmp3.DLL' name '_madx_deinit@4';
function madx_read; external 'madmp3.DLL' name '_madx_read@16';
function madx_init; external 'madmp3.dll' name '_madx_init@8';
//////////////////////////////////////////////////////
////////--------MP3FILE----------////////////////////
//////////////////////////////////////////////////////
constructor Tmp3File.create(filnam:string);
begin
inherited create;
assignfile(fil,filnam);
reset(fil);
getmem(mxhouse,128*1024); //Ez se lehet több fél megánál
zeromemory(mxhouse,128*1024);
zeromemory(@out_buffer[0],sizeof(out_buffer));
zeromemory(@in_buffer[0],sizeof(in_buffer));
madx_init(@out_buffer[0],mxhouse);
zeromemory(@mxstat,sizeof(mxstat));
end;
procedure Tmp3file.read(var hova:Tsmallintdynarr);
var
a:integer;
mxsig:madx_sig;
label
again;
begin
setlength(hova,0);
again:
if eof(fil) then
begin
iseof:=true;
exit;
end;
mxsig := madx_read (@in_buffer[0], @out_buffer[0], mxhouse, mxstat );
if (mxsig = ERROR_OCCURED) then
begin
iseof:=true;
exit;
end;
if (mxsig = MORE_INPUT) then // Fill buffer
begin
if (mxstat.buffstart<>nil) then
begin
blockread(fil,mxstat.buffstart^,mxstat.readsize,a);
if a<>mxstat.readsize then
// if eof(fil) then
begin
mxstat.is_eof := 1;
mxstat.readsize := a;
iseof:=true;
end
// else
// exit
end
else
begin
blockread(fil,in_buffer[0],mxstat.readsize,a);
if a<>mxstat.readsize then
// if eof(fil) then
begin
mxstat.is_eof := 1;
mxstat.readsize := a;
iseof:=true;
end
// else
// exit
end;
end;
if (mxsig = FLUSH_BUFFER) or (mxsig = EOF_REACHED) then // Output to file
begin
samplerate:=(Pinteger(Dword(mxhouse)+84))^; //hát igen, ez a lusta megoldás
setlength(hova,mxstat.write_size shr 1);
copymemory(@hova[0], @out_buffer[0],mxstat.write_size);
if (mxsig = EOF_REACHED) then iseof:=true;
exit;
end;
goto again
end;
destructor Tmp3file.destroy;
begin
closefile(fil);
madx_deinit(mxhouse);
freemem(mxhouse);
freemem(pnagybuf);
inherited;
end;
//////////////////////////////////////////////////////
////////--------MP3STREAM------------////////////////
//////////////////////////////////////////////////////
constructor TMP3Stream.create(url:string);
const
maxwait = 3000;
maxheadsiz = 4*1024;
var
strhost,strport,strfil:string;
incim:Tincim;
srvc:sockaddr_in;
mit:shortstring;
tmp:dword;
tmpchr:char;
recvd:integer;
elsostr:boolean;
headsiz:integer;
headwait:integer;
begin
inherited create;
//writeln(logfile,'madx crt1');flush(logfile);
error:=1;
getmem(mxhouse,256*1024); //Ez se lehet több fél megánál
zeromemory(mxhouse,256*1024);
zeromemory(@out_buffer[0],sizeof(out_buffer));
zeromemory(@in_buffer[0],sizeof(in_buffer));
madx_init(@out_buffer[0],mxhouse);
zeromemory(@mxstat,sizeof(mxstat));
mxsig:=CALL_AGAIN;
strhost:=copy(url,pos('://',url)+3,1000);
strport:=copy(strhost,pos(':',strhost)+1,1000);
if pos('/',strport)>0 then
strport:=copy(strport,1,pos('/',strport)-1);
if strport='' then strport:='8000';
if pos('/',strhost)>0 then
strfil:=copy(strhost,pos('/',strhost),1000)
else
strfil:='/';
strhost:=copy(strhost,1,pos(':',strhost)-1);
gethostbynamewrap2(strhost,@(incim.sin_addr),false);
incim.sin_port:=htons(strtoint(strport));
sck:=socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
srvc:=incimtosockaddr(incim);
// writeln(logfile,'madx crt1a');flush(logfile);
tmp:=gettickcount;
if SOCKET_ERROR=connectwithtimeout(sck,@srvc,sizeof(srvc),3) then
begin
// writeln(logfile,'navégre ',gettickcount-tmp); flush(logfile);
exit;
end;
// writeln(logfile,'madx crt1b');flush(logfile);
tmp:=0;
ioctlsocket(sck, FIONBIO, tmp);
// writeln(logfile,'madx crt1c');flush(logfile);
mit:='GET '+strfil+' HTTP/1.0'#13#10;
send(sck,mit[1],length(mit),0);
//0d 0a
mit:='Host: '+strhost+#13#10;
send(sck,mit[1],length(mit),0);
mit:='Accept: */*'#13#10;
send(sck,mit[1],length(mit),0);
mit:='User-Agent: Stickman Warfare Radio'#13#10;
send(sck,mit[1],length(mit),0);
mit:='Icy-MetaData:0'#13#10;
send(sck,mit[1],length(mit),0);
mit:='Connection: close'#13#10;
send(sck,mit[1],length(mit),0);
mit:=#13#10;
send(sck,mit[1],length(mit),0);
tmp:=0;
ioctlsocket(sck, FIONBIO, tmp);
elsostr:=true;
mit:='';
headwait:=gettickcount;
headsiz:=0;
nev:='Unnamed radio';
repeat
recvd:=recv(sck,tmpchr,1,0);
inc(headsiz);
mit:=mit+tmpchr;
if (mit[length(mit)-1]=#13) and (mit[length(mit)]=#10) then
begin
if elsostr and (pos('200',mit)=0) then exit;
if length(mit)=2 then break;
if lowercase(copy(mit,1,8))='icy-name' then
nev:=copy(mit,10,length(mit)-11);
mit:='';
elsostr:=false;
end;
if headwait+maxwait<integer(gettickcount) then begin recvd:=0; break; end;
if headsiz>maxheadsiz then break;
until recvd<=0;
if recvd<=0 then
error:=1
else
error:=0;
// writeln(logfile,'madx crt2');flush(logfile);
end;
procedure TMP3Stream.read(var hova:Tsmallintdynarr);
var
a:integer;
label
again;
begin
setlength(hova,0);
again:
if error>0 then exit;
// if mxsig=CALL_AGAIN then
mxsig := madx_read (@in_buffer[0], @out_buffer[0], mxhouse, mxstat );
if (mxsig = ERROR_OCCURED) then
begin
error:=5;
exit;
end;
if (mxsig = MORE_INPUT) then // Fill buffer
begin
// a:=mxstat.readsize;
if (mxstat.buffstart<>nil) then
begin
a:=recvall(sck,mxstat.buffstart^,mxstat.readsize,3000);
if a<>mxstat.readsize then
begin
mxstat.is_eof := 1;
mxstat.readsize := a;
error:=2;
end
end
else
begin
a:=recvall(sck,in_buffer[0],mxstat.readsize,3000);
if a<>mxstat.readsize then
begin
mxstat.is_eof := 1;
mxstat.readsize := a;
error:=2;
end
end;
mxsig:=CALL_AGAIN;
end;
if (mxsig = FLUSH_BUFFER) or (mxsig = EOF_REACHED) then // Output to file
begin
samplerate:=(Pinteger(Dword(mxhouse)+84))^; //hát igen, ez a lusta megoldás
setlength(hova,mxstat.write_size shr 1);
copymemory(@hova[0], @out_buffer[0],mxstat.write_size);
if (mxsig = EOF_REACHED) then error:=3;
mxsig:=CALL_AGAIN;
exit;
end;
goto again;
end;
destructor TMP3Stream.destroy;
begin
//writeln(logfile,'madx dest1');flush(logfile);
closesocket(sck);
madx_deinit(mxhouse);
freemem(mxhouse);
freemem(pnagybuf);
//writeln(logfile,'madx dest2');flush(logfile);
inherited;
end;
end.