Delphi实现MP3 互相转换 WAV (无需任何第三方dll)

From: http://www.programbbs.com/doc/3152.htm

一:MP3转换为WAV

function acmDriverEnumCallback(hadid: HACMDRIVERID; dwInstance: DWORD; fdwSupport: DWORD): BOOL; stdcall;
var
  driver                                                    : HACMDRIVER;
  details                                                   : TACMDRIVERDETAILS;
  i                                                         : integer;
  fmtDetails                                                : TACMFORMATTAGDETAILS;
begin
  if (fdwSupport and ACMDRIVERDETAILS_SUPPORTF_CODEC) <> 0 then
  begin
    details.cbStruct := sizeof(TACMDRIVERDETAILS);
    acmDriverDetails(hadid, details, 0);
    acmDriverOpen(driver, hadid, 0);
    for i := 0 to details.cFormatTags - 1 do
    begin
      ZeroMemory(@fmtDetails, sizeof(fmtDetails));
      fmtDetails.cbStruct := sizeof(TACMFORMATTAGDETAILS);
      fmtDetails.dwFormatTagIndex := i;
      acmFormatTagDetails(driver, fmtDetails, ACM_FORMATTAGDETAILSF_INDEX);
      if (fmtDetails.dwFormatTag = WAVE_FORMAT_MPEGLAYER3) then inc(g_mp3Drivers);
    end;
    acmDriverClose(driver, 0);
  end;
  Result := true;
end;


function ConverMP3ToRaw(Mp3FileName: pChar; const Channels, SamplesPerSec, BitsPerSample: Integer): Boolean; stdcall;
var
  mmr                                                       : MMRESULT;
  maxFormatSize                                             : DWORD;
  waveFormat                                                : PWAVEFORMATEX;
  mp3format                                                 : PMPEGLAYER3WAVEFORMAT;
  g_mp3stream                                               : HACMSTREAM;
  fpIn, fpOut                                               : Integer;
  rawbufsize                                                : Cardinal;
  mp3buf, rawbuf                                            : PBYTE;
  mp3streamHead                                             : TACMSTREAMHEADER;
  count                                                     : Cardinal;
  RawFileName                                               : string;
begin
  Result := False;
  acmDriverEnum(acmDriverEnumCallback, 0, 0);                                                       // try to find an MP3 codec
  if (g_mp3Drivers = 0) then Exit;

  maxFormatSize := 0;                                                                               // find the biggest format size
  acmMetrics(nil, ACM_METRIC_MAX_SIZE_FORMAT, maxFormatSize);

  waveFormat := PWAVEFORMATEX(Pointer(LocalAlloc(LPTR, maxFormatSize)));                            // define desired output format
  waveFormat^.wFormatTag := WAVE_FORMAT_PCM;
  if Channels = 1 then waveFormat^.nChannels := 1;
  if Channels = 2 then waveFormat^.nChannels := 2;
  waveFormat^.nSamplesPerSec := SamplesPerSec;                                                      // 44.1kHz
  waveFormat^.wBitsPerSample := BitsPerSample;                                                      // 16 bits
  waveFormat^.nBlockAlign := waveFormat^.nChannels * waveFormat^.wBitsperSample div 8;              // 4;                                                                     // 4 bytes of data at a time are useful (1 sample)
  waveFormat^.nAvgBytesPerSec := waveFormat^.nBlockAlign * SamplesPerSec;                           // byte-rate
  waveFormat^.cbSize := 0;                                                                          // no more data to follow

  mp3format := PMPEGLAYER3WAVEFORMAT(Pointer(LocalAlloc(LPTR, maxFormatSize)));                     // define MP3 input format
  mp3format^.wfx.cbSize := MPEGLAYER3_WFX_EXTRA_BYTES;
  mp3format^.wfx.wFormatTag := WAVE_FORMAT_MPEGLAYER3;
  mp3format^.wfx.nChannels := 2;
  mp3format^.wfx.nAvgBytesPerSec := 128 * (1024 div 8);                                             // not really used but must be one of 64, 96, 112, 128, 160kbps
  mp3format^.wfx.wBitsPerSample := 0;                                                               // MUST BE ZERO
  mp3format^.wfx.nBlockAlign := 1;                                                                  // MUST BE ONE
  mp3format^.wfx.nSamplesPerSec := 44100;                                                           // 44.1kHz
  mp3format^.fdwFlags := MPEGLAYER3_FLAG_PADDING_OFF;
  mp3format^.nBlockSize := MP3_BLOCK_SIZE;                                                          // voodoo value #1
  mp3format^.nFramesPerBlock := 1;                                                                  // MUST BE ONE
  mp3format^.nCodecDelay := 1393;                                                                   // voodoo value #2
  mp3format^.wID := MPEGLAYER3_ID_MPEG;

  g_mp3stream := nil;
  mmr := acmStreamOpen(g_mp3stream,                                                                 // open an ACM conversion stream
    nil,                                                                                            // querying all ACM drivers
    TWAVEFORMATEX(Pointer(mp3format)^),                                                             //TWAVEFORMATEX(Pointer(LongInt(@mp3format))),                                                   // converting from MP3
    waveFormat^,                                                                                    // to WAV
    nil,                                                                                            // with no filter
    0,                                                                                              // or async callbacks
    0,                                                                                              // (and no data for the callback)
    0                                                                                               // and no flags
    );

  LocalFree(LongInt(mp3format));
  LocalFree(LongInt(waveFormat));
  if mmr <> 0 then Exit;

  // MP3 stream converter opened correctly
  // now, let's open a file, read in a bunch of MP3 data, and convert it!

  // open file
  fpIn := FileOpen(MP3FileName, fmOpenRead);
  if (fpIn = 0) then Exit;

  // find out how big the decompressed buffer will be
  rawbufsize := 0;
  acmStreamSize(g_mp3stream, MP3_BLOCK_SIZE, rawbufsize, ACM_STREAMSIZEF_SOURCE);

  // allocate our I/O buffers
  mp3Buf := PByte(LocalAlloc(LPTR, MP3_BLOCK_SIZE));
  rawbuf := pByte(LocalAlloc(LPTR, rawbufsize));

  // prepare the decoder
  ZeroMemory(@mp3streamHead, sizeof(TACMSTREAMHEADER));
  mp3streamHead.cbStruct := sizeof(TACMSTREAMHEADER);
  mp3streamHead.pbSrc := mp3buf;
  mp3streamHead.cbSrcLength := MP3_BLOCK_SIZE;
  mp3streamHead.pbDst := rawbuf;
  mp3streamHead.cbDstLength := rawbufsize;
  acmStreamPrepareHeader(g_mp3stream, mp3streamHead, 0);

  // let's dump this data off to disk (for debug checking!)
  RawFileName := ChangeFileExt(MP3FileName, '.raw');
  fpOut := FileCreate(RawFileName);
  if (fpOut = 0) then Exit;

  FileSeek(fpIn, 0, 0);

  while True do
  begin
    //suck in some MP3 data
    Count := FileRead(fpIn, mp3buf^, MP3_BLOCK_SIZE);
    if (Count <> MP3_BLOCK_SIZE) then break;

    // convert the data
    mmr := acmStreamConvert(g_mp3stream, mp3streamHead, ACM_STREAMCONVERTF_BLOCKALIGN);
    if mmr <> 0 then
    begin
      MessageBox(0, '输入参数错误。请检查你的输入参数。', '系统提示: ', 64);
      Exit;
    end;

    //write the decoded PCM to disk
    Count := FileWrite(fpOut, rawbuf^, mp3streamHead.cbDstLengthUsed);
    if Count <> mp3streamHead.cbDstLengthUsed then
    begin
      MessageBox(0, '输入参数错误。请检查你的输入参数。', '系统提示: ', 64);
      Exit;
    end;
  end; ;

//  clean up after yourself like a good little boy
  FileClose(fpIn);
  FileClose(fpOut);
  acmStreamUnprepareHeader(g_mp3stream, mp3streamHead, 0);
  LocalFree(LongInt(rawbuf));
  LocalFree(LongInt(mp3buf));
  acmStreamClose(g_mp3stream, 0);
  Result := True;
end;

 ConverMP3ToRaw函数是将MP3转换成RAW(RIFF格式),添加WAV文件头就是WAV文件了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值