Browse Source

* Created some helper functions for vorbis and a52
* Working madopenal samples with mp3, ogg and ac3 support

git-svn-id: trunk@4685 -

ivost 19 years ago
parent
commit
1870c41c40

+ 19 - 11
packages/extra/a52/a52.pas

@@ -492,18 +492,20 @@ var
 begin
   ofs := 0;
   num := length;
+
   while num > 0 do
   begin
     len := PtrInt(decoder^.inbuf_ptr) - PtrInt(@decoder^.inbuf);
+
     if decoder^.frame_size = 0 then
     begin
       (* no header seen : find one. We need at least 7 bytes to parse it *)
-      len := HEADER_SIZE - len;
-      {if len > buf_size then
-        len := buf_size;}
+      //WriteLn('no header seen (', len, ')');
 
+      len := HEADER_SIZE - len;
       if decoder^.read(decoder^.inbuf_ptr, 1, len, decoder^.datasource) <> len then
         Exit(ofs);
+
       Inc(decoder^.inbuf_ptr, len);
 
       if PtrInt(decoder^.inbuf_ptr) - PtrInt(@decoder^.inbuf) = HEADER_SIZE then
@@ -516,6 +518,7 @@ begin
           Dec(decoder^.inbuf_ptr);
         end else begin
           decoder^.frame_size := len;
+
           (* update codec info *)
           decoder^.sample_rate := sample_rate;
           decoder^.bit_rate := bit_rate;
@@ -523,28 +526,32 @@ begin
           if decoder^.flags and A52_LFE <> 0 then
             Inc(decoder^.channels);
 
+         {WriteLn('  frame_size  : ', decoder^.frame_size);
+          WriteLn('  sample_rate : ', sample_rate);
+          WriteLn('  bit_rate    : ', bit_rate);
+          WriteLn('  channels    : ', decoder^.channels);}
+
           // test against user channel settings (wrong here)
           {if decoder^.req_chan = 0 then
             decoder^.req_chan := decoder^.channels else
             if decoder^.channels < decoder^.req_chan then
               decoder^.req_chan := decoder^.channels;}
         end;
-      end;
-    end else
+      end else
+        WriteLn('BUG!!!!');
+    end else begin
       if len < decoder^.frame_size then
       begin
         len := decoder^.frame_size - len;
-        {if len > buf_size then
-          len := buf_size;}
-
         if decoder^.read(decoder^.inbuf_ptr, 1, len, decoder^.datasource) <> len then
           Exit(ofs);
+
         Inc(decoder^.inbuf_ptr, len);
       end else begin
         flags := A52_STEREO;//decoder^.flags;
-        level := 1;
+        level := High(Smallint)-30;
 
-        if a52_frame(decoder^.state, @decoder^.inbuf, flags, level, 384) <> 0 then
+        if a52_frame(decoder^.state, @decoder^.inbuf, flags, level, 0) <> 0 then
         begin
           decoder^.inbuf_ptr := @decoder^.inbuf;
           decoder^.frame_size := 0;
@@ -556,7 +563,7 @@ begin
           if a52_block(decoder^.state) <> 0 then
             Exit(-1);
 
-          float_to_int(decoder^.samples, pointer(PtrInt(buffer) + Ofs + 2{channels}*i*256*2{sample_size}), 2{channels});
+          float_to_int(decoder^.samples, pointer(PtrInt(buffer) + ofs + 2{channels}*i*256*2{sample_size}), 2{channels});
         end;
 
         decoder^.inbuf_ptr := @decoder^.inbuf;
@@ -565,6 +572,7 @@ begin
         ofs := ofs + 2{channels}*(6*256){samples}*2{sample_size};
         num := num - 2{channels}*(6*256){samples}*2{sample_size};
       end;
+    end;
   end;
 
   Result := ofs;

+ 36 - 0
packages/extra/oggvorbis/vorbis.pas

@@ -374,6 +374,14 @@ function ov_halfrate(var vf: OggVorbis_File; flag: cint): cint; cdecl; external
 function ov_halfrate_p(var vf: OggVorbis_File): cint; cdecl; external {$IFDEF DYNLINK}vorbisfilelib{$ENDIF};
 
 
+{
+  Developer of the A52 helpers for FreePascal
+  Copyright (C) 2006 by Ivo Steinmann
+}
+
+function ov_read_ext(var vf: OggVorbis_File; buffer: pointer; length: cint; bigendianp: cbool; word: cint; sgned: cbool): clong;
+
+
 (***********************************************************************)
 (* Header : vorbisenc.h                                                *)
 (***********************************************************************)
@@ -416,5 +424,33 @@ function vorbis_encode_ctl(var vi: vorbis_info; number: cint; arg: pointer): cin
 
 implementation
 
+function ov_read_ext(var vf: OggVorbis_File; buffer: pointer; length: cint; bigendianp: cbool; word: cint; sgned: cbool): clong;
+var
+  ofs: cint;
+  Num: cint;
+  Res: cint;
+begin
+  {if length mod 4 <> 0 then
+    Exit(0);}
+
+  ofs := 0;
+  num := length;
+
+  while num > 0 do
+  begin
+    res := ov_read(vf, pointer(ptrint(buffer) + ofs), num, bigendianp, word, sgned, nil);
+    if res <= 0 then
+      Exit(res);
+
+    if res = 0 then
+      Break;
+
+    ofs := ofs + res;
+    num := num - res;
+  end;
+
+  Result := ofs;
+end;
+
 end.
 

+ 12 - 27
packages/extra/openal/examples/madopenal.pas

@@ -39,7 +39,7 @@ var
   Remaining : Integer;
   ReadStart : Pointer;
   ReadSize  : Integer;
-  Output    : PByte;
+  Output    : PSmallint;
 begin
   Ofs := 0;
   Num := Count;
@@ -130,7 +130,6 @@ end;
 
 function ogg_seek_func(datasource: pointer; offset: ogg_int64_t; whence: cint): cint; cdecl;
 begin
-  WriteLn('seek');
   case whence of
     {SEEK_SET} 0: TStream(datasource).Seek(offset, soFromBeginning);
     {SEEK_CUR} 1: TStream(datasource).Seek(offset, soFromCurrent);
@@ -157,27 +156,12 @@ end;}
 
 function ogg_read(const Buffer: Pointer; const Count: Longword): Longword;
 var
-  Ofs: Longword;
-  Num: Longword;
-  Res: Integer;
+  Res: clong;
 begin
-  Ofs := 0;
-  Num := Count;
-
-  while Num > 0 do
-  begin
-    Res := ov_read(ogg_vorbis, Pointer(PtrUInt(Buffer) + Ofs), Num, False, 2, True, nil);
-    if Res < 0 then
-      Exit(0);
-
-    if Res = 0 then
-      Break;
-
-    Ofs := Ofs + Longword(Res);
-    Num := Num - Longword(Res);
-  end;
-
-  Result := Ofs;
+  Res := ov_read_ext(ogg_vorbis, Buffer, Count, false, 2, true);
+  if Res < 0 then
+    Exit(0) else
+    Result := Res;
 end;
 
 
@@ -286,14 +270,15 @@ var
   ov: pvorbis_info;
 begin
 // define codec
-  {WriteLn('Define codec');
+  WriteLn('Define codec');
   Writeln('  (1) mp3');
   Writeln('  (2) ogg');
+  Writeln('  (3) ac3');
   Write('Enter: '); ReadLn(codec);
-  Write('File: '); ReadLn(Filename);}
+  Write('File: '); ReadLn(Filename);
 
-  codec := 1;
-  Filename := 'test.mp3';
+  {codec := 1;
+  Filename := 'test.mp3';}
 
 
 // load file
@@ -336,7 +321,7 @@ begin
         a52_decoder := a52_decoder_init(0, source, @ogg_read_func, @ogg_seek_func, @ogg_close_func, @ogg_tell_func);
         codec_bs   := 2{channels}*1536*2{sample_size};
         codec_read := @a52_read;
-        codec_rate := 44100;
+        codec_rate := 48000;
         codec_chan := 2;
       end;
   end;