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

📄 mmwave.pas

📁 P2P即时通讯源码(DELPHI编写)
💻 PAS
📖 第 1 页 / 共 5 页
字号:
         if FPWAVEIOCB^.hmmio <> 0 then
            if wioSetIOBufferSize(FPWAVEIOCB,FIOBufSize) <> 0 then
               raise EMMWaveError.Create(LoadResStr(IDS_WFIOBUFERROR));
   end;
end;

{-- TMMWave --------------------------------------------------------------}
Procedure TMMWave.OpenFile;
begin
   if (FPWAVEIOCB <> Nil) then
   begin
      if wioWaveOpen(FPWAVEIOCB) <> 0 then
         raise EMMWaveError.Create(LoadResStr(IDS_WFOPENERROR));

      if wioSetIOBufferSize(FPWAVEIOCB,FIOBufSize) <> 0 then
         raise EMMWaveError.Create(LoadResStr(IDS_WFIOBUFERROR));

      { build the region list }
      InitRegionList;

      { build the fade list }
      InitPlayFadeList(False);

      FOpen := True;

      { update the position }
      Position := GetPosition;
   end;
end;

{-- TMMWave --------------------------------------------------------------}
procedure TMMWave.CloseFile;
begin
   if (FPWAVEIOCB <> Nil) then wioWaveClose(FPWAVEIOCB);
   FOpen := False;
   FPosition := 0;
   FBytesLeft := 0;
end;

{-- TMMWave --------------------------------------------------------------}
procedure TMMWave.ResetFile;
begin
end;

{-- TMMWave --------------------------------------------------------------}
function TMMWave.GetBytesLeft: LongInt;
begin
   Result := FBytesLeft;
end;

{$IFDEF BCB}
{-- TMMWave --------------------------------------------------------------}
procedure TMMWave.CreateFileC(const FileName: TFileName; pwfx: PWaveFormatEx);
begin
   { bug fix for C++ Builder }
   CreateFile(FileName,pwfx);
end;
{$ENDIF}

{-- TMMWave --------------------------------------------------------------}
{ TODO: Exceptions und bessere Fehlermeldungen, FreeSpace testen !!!!     }
procedure TMMWave.CreateFile(const FileName: TFileName; pwfx: PWaveFormatEx);
Label ERROR_CREATE;
Var
   aResult : Boolean;
   DestFile: PChar;
   lpwioDst: PWaveIOCB;

begin
   aResult := False;

   if (FMemoryWave <> nil) then
      raise EMMWaveError.Create(LoadResStr(IDS_WFMEMFILEERROR));

   if (pwfx = Nil) or (FileName = '') then exit;

   DestFile := StrAlloc(MAX_PATH+1);
   StrPCopy(DestFile, ExpandUNCFileName(FileName));

   if not wioGetFullPathName(DestFile) then
      goto ERROR_CREATE;

   { delete the real destination file (if exits) }
   if wioFileExists(DestFile) then wioFileDelete(DestFile);

   { create the header for the new file }
   if wioCreateFileInfo(lpwioDst, pwfx) <> 0 then
      goto ERROR_CREATE;

   { write the new Header to disc }
   if wioWriteFileInfo(lpwioDst, DestFile) <> 0 then
      goto ERROR_CREATE;

   { set the structure }
   PWaveIOInfo := lpwioDst;

   if (FMemoryWave = nil) then
      { set the FileName }
      FFileName := StrPas(DestFile);

   if wioSetIOBufferSize(FPWAVEIOCB,FIOBufSize) <> 0 then
      raise EMMWaveError.Create(LoadResStr(IDS_WFIOBUFERROR));

   aResult := True;

ERROR_CREATE:

   StrDispose(DestFile);
   if not aResult then
   begin
      wioWaveClose(lpwioDst);
      wioFreeFileInfo(lpwioDst);
      raise EMMWaveError.Create(LoadResStr(IDS_WFCREATEERROR));
   end;
end;

{-- TMMWave --------------------------------------------------------------}
procedure TMMWave.SaveToFile(const Filename: TFileName);
Label ERROR_SAVE;
Var
   Result : Boolean;
   Cancel: Boolean;
   TmpFile, DestFile: PChar;
   lpwioDst: PWAVEIOCB;
   pData: PChar;
   BufSize, CurByte, NumBytes, NumRead: DWORD;

begin
   Result := False;
   Cancel := False;
   if (FPWAVEIOCB = Nil) or (FileName = '') then
      raise EMMWaveError.Create(LoadResStr(IDS_WFINVALIDPARAMS));

   pData := nil;
   lpwioDst := nil;
   TmpFile := StrAlloc(MAX_PATH+1);
   DestFile := StrAlloc(MAX_PATH+1);

   { open the source file and set position to 0 }
   Position := 0;
   OpenFile;

   { how many bytes we have to save }
   NumBytes := BytesLeft;

   { create the dest. WAVEIOINFO }
   if wioCreateFileInfo(lpwioDst, PWaveFormat) <> 0 then
      goto ERROR_SAVE;

   { copy all known chunks to the destination }
   if wioCopyFileInfo(lpwioDst, FPWaveIOCB) <> 0 then
      goto ERROR_SAVE;

   {$IFDEF WIN32}
   {$IFDEF TRIAL}
   {$DEFINE _HACK3}
   {$I MMHACK.INC}
   {$ENDIF}
   {$ENDIF}

   { first create a temporary file for the destination file }
   StrPCopy(TmpFile, ExpandUNCFileName(FileName));
   if Not wioFileCreateTemp(TmpFile) then
      goto ERROR_SAVE;

   { write the new Header to disc }
   if wioWriteFileInfo(lpwioDst, TmpFile) <> 0 then
      goto ERROR_SAVE;

   { compute source bytes to read (round down to nearest }
   { block for one second of data)                       }
   with PWaveFormat^ do
   BufSize := nAvgBytesPerSec-(nAvgBytesPerSec mod nBlockAlign);

   { allocate the data buffer for reading/writing data }
   pData := GlobalAllocMem(BufSize);
   if (pData = Nil) then goto ERROR_SAVE;

   CurByte := 0;
   while CurByte < NumBytes do
   begin
      { read the data }
      NumRead := min(BufSize, NumBytes - CurByte);
      NumRead := ReadDataBytes(pData, NumRead);
      if (NumRead <= 0) then break;

      inc(CurByte, NumRead);

      { have we space on the drive ? }
      if not GetDiskFree(TmpFile, NumRead+DWORD(IOBufferSize)+10240) then
         goto ERROR_SAVE;

      { write the data out as we go... }
      if wioWaveWriteData(lpwioDst, pData, NumRead) <> NumRead then
         goto ERROR_SAVE;

      { let the user have some time }
      Application.ProcessMessages;
      Progress(CurByte, NumBytes, Cancel);
      if Cancel then goto ERROR_SAVE;
   end;

   { close source and temp file }
   CloseFile;
   wioWaveClose(lpwioDst);

   StrPCopy(DestFile, ExpandUNCFileName(FileName));

   { delete the real destination file (if exits) }
   if wioFileExists(DestFile) then wioFileDelete(DestFile);

   { rename the temp file to the destination file }
   if mmioRename(TmpFile, DestFile, Nil, 0) <> 0 then
      goto ERROR_SAVE;

   Result := True;

ERROR_SAVE:

   GlobalFreeMem(Pointer(pData));
   CloseFile;
   wioWaveClose(lpwioDst);
   wioFreeFileInfo(lpwioDst);
   { make sure we delete the temp file }
   if wioFileExists(TmpFile) then wioFileDelete(TmpFile);
   StrDispose(TmpFile);
   StrDispose(DestFile);
   if not Result then
      if not Cancel then
         raise EMMWaveError.Create(LoadResStr(IDS_WFSAVEERROR))
      else
         raise EMMWaveError.Create(LoadResStr(IDS_WFSAVEABORTED))
end;

{-- TMMWave --------------------------------------------------------------}
{ TODO: Exceptions und bessere Fehlermeldungen, FreeSpace testen !!!!     }
procedure TMMWave.ConvertFile(const FileName: TFileName; pwfxDst: PWaveFormatEx);
Label ERROR_CONVERT;
Var
   aResult: Boolean;
   Cancel: Boolean;
   TmpFile, DestFile: PChar;
   lpwioDst: PWAVEIOCB;
   pwfDst: PPCMWaveFormat;
   SrcBufSize, DstBufSize: Longint;
   pSrc, pDst: PChar;
   CurByte, NumBytes, NumRead: DWORD;

begin
   aResult := False;
   Cancel := False;

   if (FPWAVEIOCB = Nil) or (pwfxDst = Nil) or (FileName = '') then
      raise EMMWaveError.Create(LoadResStr(IDS_WFINVALIDPARAMS));

   pwfDst := PPCMWaveFormat(pwfxDst);

   if Not pcmIsValidFormat(PWaveFormatEx(PWaveFormat)) or
      Not pcmIsValidFormat(PWaveFormatEx(pwfDst)) then
      raise EMMWaveError.Create(LoadResStr(IDS_WFCONVERTINVALID));

   pSrc := nil;
   pDst := nil;
   lpwioDst := nil;
   TmpFile := StrAlloc(MAX_PATH+1);
   DestFile := StrAlloc(MAX_PATH+1);

   { open the source file and set position to 0 }
   Position := 0;
   OpenFile;

   { set the bytes we have to convert }
   NumBytes := BytesLeft;

   { create the dest. WAVEIOINFO }
   if wioCreateFileInfo(lpwioDst, PWaveFormatEx(pwfDst)) <> 0 then
      goto ERROR_CONVERT;

   { copy all known chunks to the destination }
   if wioCopyFileInfo(lpwioDst, FPWaveIOCB) <> 0 then
      goto ERROR_CONVERT;

   { first create a temporary file for the destination file }
   StrPCopy(TmpFile, ExpandUNCFileName(FileName));
   if Not wioFileCreateTemp(TmpFile) then
      goto ERROR_CONVERT;

   { write the new Header to disc }
   if wioWriteFileInfo(lpwioDst, TmpFile) <> 0 then
      goto ERROR_CONVERT;

{$IFDEF WIN32}
   { compute source bytes to read (round down to nearest }
   { block for one second of data)                       }
   with PWaveFormat^ do
   SrcBufSize := nAvgBytesPerSec-(nAvgBytesPerSec mod nBlockAlign);
{$ELSE}
   { make sure the DstBufSize can not exceed 64 K ! }
   SrcBufSize := Min($FF0, NumBytes * 4 div 4);
{$ENDIF}
   DstBufSize := pcmConvertSizeOutputData(pwfDst, PPCMWaveFormat(PWaveFormat), SrcBufSize);

   { allocate the src and dst buffers for reading/converting data }
   pSrc := GlobalAllocMem(SrcBufSize);
   if (pSrc = Nil) then goto ERROR_CONVERT;
   pDst := GlobalAllocMem(DstBufSize);
   if (pDst = Nil) then goto ERROR_CONVERT;

   CurByte := 0;
   while CurByte < NumBytes do
   begin
      { read the data to convert }
      NumRead := min(SrcBufSize, NumBytes - CurByte);
      NumRead := ReadDataBytes(pSrc, NumRead);
      if (NumRead <= 0) then break;

      inc(CurByte, NumRead);

      { convert data }
      NumRead := pcmConvert(pwfDst, pDst, PPCMWaveFormat(PWaveFormat), pSrc, NumRead);
      if (NumRead <= 0) then break;

      { have we space on the drive ? }
      if not GetDiskFree(TmpFile, NumRead+DWORD(IOBufferSize)+10240) then
         goto ERROR_CONVERT;

      { write the data out as we go... }
      if wioWaveWriteData(lpwioDst, pDst, NumRead) <> NumRead then
         goto ERROR_CONVERT;

      { let the user have some time }
      Application.ProcessMessages;
      Progress(CurByte, NumBytes, Cancel);
      if Cancel then goto ERROR_CONVERT;
   end;

   { close source and temp file }
   CloseFile;
   wioWaveClose(lpwioDst);

   StrPCopy(DestFile, ExpandUNCFileName(FileName));

   { delete the real destination file (if exits) }
   if wioFileExists(DestFile) then wioFileDelete(DestFile);

   { rename the temp file to the destination file }
   if mmioRename(TmpFile, DestFile, Nil, 0) <> 0 then
      goto ERROR_CONVERT;

   aResult := True;

ERROR_CONVERT:

   CloseFile;
   wioWaveClose(lpwioDst);
   wioFreeFileInfo(lpwioDst);
   GlobalFreeMem(Pointer(pSrc));
   GlobalFreeMem(Pointer(pDst));
   { make sure we delete the temp file }
   if wioFileExists(TmpFile) then wioFileDelete(TmpFile);
   StrDispose(TmpFile);
   StrDispose(DestFile);
   if not aResult then
      if not Cancel then
         raise EMMWaveError.Create(LoadResStr(IDS_WFCONVERTERROR))
      else
         raise EMMWaveError.Create(LoadResStr(IDS_WFCONVERTABORTED));
end;

{-- TMMWave --------------------------------------------------------------}
procedure TMMWave.SetTimeFormat(aValue: TMMTimeFormats);
begin
   if (aValue <> FTimeFormat) then
   begin
      FTimeFormat := aValue;
      InitRegionList;
   end;
end;

{-- TMMWave --------------------------------------------------------------}
procedure TMMWave.SetStartPos(aPosition: Longint);
begin
   if (FPWAVEIOCB <> Nil) then
   begin
      FStartPos := MinMax(TimeFormatToSamples(aPosition),0,FEndPos);
      InitRegionList;
   end;
end;

{-- TMMWave --------------------------------------------------------------}
function TMMWave.GetStartPos: Longint;
begin
   if (FPWAVEIOCB <> Nil) then
       Result := SamplesToTimeFormat(FStartPos)
   else
       Result := -1;
end;

{-- TMMWave --------------------------------------------------------------}
procedure TMMWave.SetEndPos(aPosition: Longint);
begin
   if (FPWAVEIOCB <> Nil) then
   with FPWAVEIOCB^ do
   begin
      FEndPos := MinMax(TimeFormatToSamples(aPosition),FStartPos,dwDataSamples);
      InitRegionList;
   end;
end;

{-- TMMWave --------------------------------------------------------------}
function TMMWave.GetEndPos: Longint;
begin
   if (FPWAVEIOCB <> Nil) then
      Result := SamplesToTimeFormat(FEndPos)
   else
      Result := -1;
end;

{-- TMMWave --------------------------------------------------------------}
procedure TMMWave.SetPosition(aPosition: Longint);
var
   SampleCount,aPos: Longint;
   i: integer;
begin
   if (FPWAVEIOCB <> Nil) then
   with FPWAVEIOCB^ do
   begin
      FPosition := TimeFormatToSamples(aPosition);
      if FOpen then
      begin
         aPos := MinMax(RealSamplesToSamples(FPosition),0,LengthSamples);

⌨️ 快捷键说明

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