⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unampglibapi.pas

📁 Voice Commnucation Components for Delphi
💻 PAS
📖 第 1 页 / 共 2 页
字号:


(*
 * Mpeg Layer-3 audio decoder
 * --------------------------
 * copyright (c) 1995,1996,1997 by Michael Hipp.
 * All rights reserved. See also 'README'
 *)


(*
  mpglib.dll (Win32) with source (LGPL)
  Version 0.92, November 2001
  Adapted from mpglib by Martin Pesch
  (http://www.rz.uni-frankfurt.de/~pesch)
*)


(*
	----------------------------------------------

	  unaMpgLibAPI.pas - Delphi API for MpgLib.DLL

	----------------------------------------------
	  This source code cannot be used without
	  proper permission granted to you as a private
	  person or an entity by the Lake of Soft, Ltd

	  Visit http://lakeofsoft.com/ for details.

	  Copyright (c) 2001, 2004 Lake of Soft, Ltd
		     All rights reserved
	----------------------------------------------

	  created by:
		Lake, 10 Feb 2004

	  modified by:
		Lake, Feb-Mar 2004

	----------------------------------------------
*)

{$I unaDef.inc}

unit
  unaMpgLibAPI;

interface

uses
  Windows, unaTypes, unaClasses;

// ------ common.c ------------

const
  tabsel_123: array[0..1, 0..2, 0..15] of unsigned = (
     (
       (0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0),
       (0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384, 0),
       (0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 0)
     ),

     (
       (0, 32,48,56,64,80,96,112,128,144,160,176,192,224,256, 0),
       (0, 8, 16,24,32,40,48, 56, 64, 80, 96,112,128,144,160, 0),
       (0, 8, 16,24,32,40,48, 56, 64, 80, 96,112,128,144,160, 0)
     )
  );

  //
  freqs: array[0..8] of unsigned =
    (
      44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000
    );



// -------------- mpg123.h --------------

const
  M_PI 		= 3.14159265358979323846;
  M_SQRT2 	= 1.41421356237309504880;

//* AUDIOBUFSIZE = n*64 with n=1,2,3 ...  */
  AUDIOBUFSIZE	= 16384;

  SBLIMIT	= 32;
  SSLIMIT       = 18;

  SCALE_BLOCK	= 12; //* Layer 2 */

  MPG_MD_STEREO           = 0;
  MPG_MD_JOINT_STEREO     = 1;
  MPG_MD_DUAL_CHANNEL     = 2;
  MPG_MD_MONO             = 3;

  MAXFRAMESIZE 		  = 1792;


type
  // --  --
  pfloat = ^double;


  // --  --
  pmpglib_frame = ^mpglib_frame;
  mpglib_frame  = packed record
    //
    stereo              : int;
    jsbound             : int;
    single              : int;
    lsf                 : int;
    mpeg25              : int;
    header_change       : int;
    lay                 : int;
    error_protection    : int;
    bitrate_index       : int;
    sampling_frequency	: int;
    padding             : int;
    extension           : int;
    mode                : int;
    mode_ext            : int;
    copyright           : int;
    original            : int;
    emphasis            : int;
    framesize  		: int;	//* computed framesize */

    //* AF: ADDED FOR LAYER1/LAYER2 */
    //#if defined(USE_LAYER_2) || defined(USE_LAYER_1)
    //
    II_sblimit:	int;
    //
    alloc: pointer;    // ^al_table2
    down_sample_sblimit: int;
    down_sample: int;
  end;


  // --  --
  pmpglib_gr_info_s = ^mpglib_gr_info_s;
  mpglib_gr_info_s = packed record
      //
      scfsi: int;
      part2_3_length:     unsigned;
      big_values:         unsigned;
      scalefac_compress:  unsigned;
      block_type:         unsigned;
      mixed_block_flag:   unsigned;
      table_select: array[0..2] of unsigned;
      subblock_gain: array[0..2] of unsigned;
      maxband: array[0..2] of unsigned;
      maxbandl:           unsigned;
      maxb:               unsigned;
      region1start:       unsigned;
      region2start:       unsigned;
      preflag:            unsigned;
      scalefac_scale:     unsigned;
      count1table_select: unsigned;
      //
      full_gain: array[0..2] of pfloat;
      pow2gain: pfloat;
  end;

  // --  --
  mpglib_gr_info_s2 = packed record
    gr: array[0..1] of mpglib_gr_info_s;
  end;

  // --  --
  pmpglib_III_sideinfo = ^mpglib_III_sideinfo;
  mpglib_III_sideinfo = packed record
    //
    main_data_begin: unsigned;
    private_bits: unsigned;

    //  struct {
    //    struct gr_info_s gr[2];
    //  } ch[2];
    //
    ch: array[0..1] of mpglib_gr_info_s2;
  end;




// --------- mpglib.h ---------------

//#include "lame-analysis.h"

//#ifndef NOANALYSIS
//extern plotting_data *mpg123_pinfo;
//#endif

type
  // -- buf --
  pmpglib_buf = ^mpglib_buf;
  mpglib_buf = packed record
    //
    pnt: pChar;
    size: int;
    pos: long;
    next: pmpglib_buf;
    prev: pmpglib_buf;
  end;


  // -- framebuf --
  pmpglib_framebuf = ^mpglib_framebuf;
  mpglib_framebuf = packed record
    //
    buf: pmpglib_buf;
    pos: int;
    next: pmpglib_frame;
    prev: pmpglib_frame;
  end;

  // --  --
  PMPSTR = ^MPSTR;
  MPSTR = packed record
    //
    head: pmpglib_buf;
    tail: pmpglib_buf;
    //
    //vbr_header: int;               //* 1 if valid Xing vbr header detected */
    //num_frames: int;               //* set if vbr header present */
    //enc_delay: int;                //* set if vbr header present */
    //enc_padding: int;              //* set if vbr header present */
    //header_parsed: int;
    //side_parsed: int;
    //data_parsed: int;
    //free_format: int;              //* 1 = free format frame */
    //old_free_format: int;          //* 1 = last frame was free format */
    bsize: int;
    framesize: int;
    //ssize: int;
    //dsize: int;
    fsizeold: int;
    //fsizeold_nopadding: int;
    fr: mpglib_frame;
    //
    //-- unsigned char bsspace[2][MAXFRAMESIZE+512]; //* MAXFRAMESIZE */
    bsspace: array[0..1, 0..MAXFRAMESIZE + 511] of byte;
    //
    //-- real hybrid_block[2][2][SBLIMIT*SSLIMIT];
    hybrid_block: array[0..1, 0..1, 0..SBLIMIT * SSLIMIT - 1] of double;
    //
    hybrid_blc: array[0..1] of int;
    header: unsigned;
    bsnum: int;
    //-- real synth_buffs[2][2][0x110];
    synth_buffs: array[0..1, 0..1, 0..$110-1] of double;
    //
    synth_bo: int;
    //sync_bitstream: int;
  end;


const
  // error codes

  MP3_OK	= 0;
  MP3_NEED_MORE	= 1;
  MP3_ERR	= -1;

  // additional

  mpglib_error_OK	= MP3_OK;
  mpglib_error_more	= MP3_NEED_MORE;
  mpglib_error_general	= MP3_ERR;
  //
  mpglib_error_noLib	= -10;
  mpglib_error_noProc	= -11;
  mpglib_error_initFail	= -12;


type

  // -- prototypes --
  mpglib_proto = record
    //
    r_libName: string;
    r_refCount: int;
    r_module: hModule;
    //
    r_initMP3: function(mp: PMPSTR): bool; cdecl;
    r_decodeMP3: function(mp: PMPSTR; inmemory: pointer; inmemsize: unsigned; outmemory: pointer; outmemsize: int; var done: int): int; cdecl;
    r_exitMP3: procedure(mp: PMPSTR); cdecl;
  end;


  // --  --
  mpglibDataAvailEvent = procedure(sender: tObject; data: pointer; size: unsigned; var copyToStream: bool) of object;	// copyToStream is not used
  mpglibApplySamplingEvent = procedure(sender: tObject; rate, bits, channels: unsigned) of object;


  {DP:CLASS
    //
  }
  unaMpgLibDecoder = class(unaThread)
  private
    f_proto: mpglib_proto;
    f_errorCode: int;
    f_MPSTR: PMPSTR;
    f_inStream: unaMemoryStream;
    f_oldPCM_rate: unsigned;
    f_oldPCM_channels: unsigned;
    f_enterFails: unsigned;
    f_ensureLayer: int;
    //
    f_onDataAvail: mpgLibDataAvailEvent;
    f_onApplySampling: mpglibApplySamplingEvent;
    //
    function getInDataSize(): unsigned;
  protected
    function execute(threadIndex: unsigned): int; override;
    procedure startIn(); override;
    procedure startOut(); override;
    procedure doWrite(data: pointer; len: unsigned); virtual;
    //
    procedure notifyData(data: pointer; size: unsigned; var copyToStream: bool); virtual;
    procedure notifySamplingChange(rate, bits, channels: unsigned); virtual;
  public
    constructor create(const libName: string = '');
    procedure AfterConstruction(); override;
    procedure BeforeDestruction(); override;
    //
    function open(): int;
    procedure close();
    procedure write(data: pointer; len: unsigned);
    //
    procedure unloadLibrary();
    procedure loadLibrary(const libName: string = '');
    //
    property errorCode: int read f_errorCode;	// 0 for OK
    property MPSTR: PMPSTR read f_MPSTR;
    property ensureLayer: int read f_ensureLayer write f_ensureLayer default -1;	// set to 1, 2, or 3 to better protection against broken MP3 files
    property inDataSize: unsigned read getInDataSize;
    //
    property onDataAvail: mpgLibDataAvailEvent read f_onDataAvail write f_onDataAvail;
    property onApplySampling: mpglibApplySamplingEvent read f_onApplySampling write f_onApplySampling;
  end;


//
function loadLib(var proto: mpglib_proto; const libName: string = ''): int;

//
function unloadLib(var proto: mpglib_proto): int;


implementation


uses
  unaUtils;

{ utility }

const
  cdef_libName	= 'mpglib.dll';


// --  --
function loadLib(var proto: mpglib_proto; const libName: string = ''): int;
begin
  if ((1 > proto.r_refCount) and (0 = proto.r_module)) then begin
    //
    proto.r_refCount := 1;
    //
    proto.r_libName := choice('' = trim(libName), cdef_libName, libName);
    proto.r_module := LoadLibrary(pChar(proto.r_libName));
    //
    if (0 <> proto.r_module) then begin
      //
      proto.r_initMP3 := GetProcAddress(proto.r_module, '_InitMP3');
      proto.r_decodeMP3 := GetProcAddress(proto.r_module, '_decodeMP3');
      proto.r_exitMP3 := GetProcAddress(proto.r_module, '_ExitMP3');
      //
      if (
	   not assigned(proto.r_initMP3) or
	   not assigned(proto.r_decodeMP3) or
	   not assigned(proto.r_exitMP3)
	 ) then begin
	//
	unloadLib(proto);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -