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

📄 mmwavin.pas

📁 一套及时通讯的原码
💻 PAS
📖 第 1 页 / 共 5 页
字号:
         {$IFDEF _MMDEBUG}
         DebugStr(0,'Try to open device...');
         {$ENDIF}

         { some fullduplex drivers require that WaveIn is opened before WaveOut  }
         { so we open the WaveIn device first and then open all other components }

         { create critical section object }
         InitCritical;

         {$IFDEF WIN32}
         if (FCallBackMode = cmThread) then InitThread;
         {$ENDIF}

         { now open Wave input device. }
         if _Win9x_ or _WinNT4_ then
         begin
            FError := WaveInOpen(@FHWaveIn,
                                 FDeviceId,
                                 Pointer(PWaveFormat),
                                 Longint(@WaveInFunc),
                                 Longint(Self),
                                 CALLBACK_FUNCTION);
         end
         else
         begin
            FError := WaveInOpen(@FHWaveIn,
                                 FDeviceId,
                                 Pointer(PWaveFormat),
                                 FHandle,
                                 0,
                                 CALLBACK_WINDOW);
         end;

         if (FError <> 0) then
         begin
            Error('WaveInOpen:'#10#13+WaveInErrorString(FError));
         end;

         aState := FState;
         FState := [wisClose];

         inherited Opened;

         FState := aState;

         { wait until the device returns its status }
         repeat
             if _Win9x_ or _WinNT4_ then
                Delay(10,False)
             else
                Delay(10,True);
             dec(TimeOut);
         until (wisOpen in FState) or (TimeOut <= 0);

         if (TimeOut <= 0) then
	     Error('WaveInOpen:'#10#13+LoadResStr(IDS_CANTOPENDEVICE));

         { create the waveIn headers and buffers }
         AllocWaveHeaders;

         { Prepare waveform headers for recording }
         PrepareWaveHeaders;

         Opened;

      except
         if assigned(FOnError) then FOnError(Self);
         FState := [wisOpen];
         Close;
         FState := [wisClose];
         raise;
      end;
   end;
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
Procedure TMMCustomWaveIn.Close;
var
   TimeOut: integer;

begin
   if (wisOpen in FState) and (not FClosing or FCloseIt) then
   try
      FClosing := True;

      { stop recording }
      if (wisRecord in FState) OR (wisPause in FState) then Stop;

      TimeOut := 100;

      { Close the device (finally!) }
      if FStopIt then FCloseIt := True
      else
      begin
         FCloseIt := False;
         if (FHWaveIn <> 0) then
         begin
            WaveInStop(FHWaveIn);
            WaveInReset(FHWaveIn);

            { unprepare wave headers }
            UnPrepareWaveHeaders;

            {$IFDEF _MMDEBUG}
            if (FInHandler > 0) then
               DebugStr(0,'Try to close device (while in Handler)...')
            else
               DebugStr(0,'Try to close device...');
            {$ENDIF}

            FError := WaveInClose(FHWaveIn);
            if FError <> 0 then
               Error('WaveInClose:'#10#13+WaveInErrorString(FError));

            {$IFDEF _MMDEBUG}
            DebugStr(0,'Waiting for state...');
            {$ENDIF}

            { wait until the device returns its status }
            repeat
               if _Win9x_ or _WinNT4_ then
                  Delay(10,False)
               else
                  Delay(10,True);
               dec(TimeOut);
            until (wisClose in FState) or (TimeOut <= 0);

            FWrapArrounds   := 0;
            FWrapSize       := 0;
         end
         else
         begin
            FState := [wisClose];
         end;

         { now notify all other components }
         inherited Closed;

         {$IFDEF WIN32}
         if (FCallBackMode = cmThread) then
             DoneThread
         else
             DoneCritical;
         {$ENDIF}

         Closed;

          if (TimeOut <= 0) then
      	     Error('WaveInOpen:'#10#13+LoadResStr(IDS_CANTCLOSEDEVICE));
      end;

   except
      FClosing := False;
   end;
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
Procedure TMMCustomWaveIn.Start;
Var
   i: integer;
begin
   try
      if not (wisOpen in FState) then Open;

      if (wisOpen in FState) and (not (wisRecord in FState)) then
      begin
         { reset the total bytes recorded counter }
         FBytesRecorded := 0;
         FBufferIndex := 0;
         FInHandler := 0;
         FStopIt := False;
         FStopping := False;
         FReseting := False;
         FPosted := False;
         FBufferCounter := 0;
         FLastPosition := 0;
         FWrapArrounds := 0;
         FWrapSize := 0;

         CalcMaxRecBytes;

         { To start recording, send the buffers to driver }
         for i := 0 to FNumBuffers-1 do
         begin
            {$IFDEF NUMERATE}
            FWaveInHdrs[i]^.dwUser := i;
            {$ENDIF}
            AddWaveHeader(FWaveInHdrs[i]);
         end;

         Started;
      end;

   except
      if assigned(FOnError) then FOnError(Self);
      FState := FState + [wisRecord];
      Close;
      FState := [wisClose];
      raise;
   end;
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
Procedure TMMCustomWaveIn.Pause;
begin
   try
      if not (wisOpen in FState) then Open;

      if (wisOpen in FState) and (not (wisPause in FState)) then
      begin
         if (wisRecord in FState) then
         begin
            {$IFDEF _MMDEBUG}
            DebugStr(0,'Try to pause device...');
            {$ENDIF}

            FError := WaveInStop(FHWaveIn);
            if FError <> 0 then
               Error('WaveInPause:'#10#13+WaveInErrorString(FError));
         end;

         Paused;
      end;

   except
      if assigned(FOnError) then FOnError(Self);
      Close;
      raise;
   end;
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
Procedure TMMCustomWaveIn.Restart;
begin
   try
      if (wisRecord in FState) and (wisPause in FState) then
      begin
         {$IFDEF _MMDEBUG}
         DebugStr(0,'Try to restart device...');
         {$ENDIF}

         FError := WaveInStart(FHWaveIn);
         if FError <> 0 then
            Error('WaveInRestart:'#10#13+WaveInErrorString(FError));

         Restarted;
      end;

   except
      if assigned(FOnError) then FOnError(Self);
      Close;
      raise;
   end;
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
procedure TMMCustomWaveIn.Stop;
var
   TimeOut: Longint;
begin
   if (wisRecord in FState) or (wisPause in FState) then
   begin
      try
         EnterCritical;
         try
            FStopping := True;

            {$IFDEF _MMDEBUG}
            if (FInHandler > 0) then
               DebugStr(0,'Try to stop device (while in Handler)...')
            else
               DebugStr(0,'Try to stop device...');
            {$ENDIF}

            FError := WaveInStop(FHWaveIn);
            if FError <> 0 then
               Error('WaveInStop:'#10#13+WaveInErrorString(FError));

            {$IFDEF _MMDEBUG}
            DebugStr(0,'WaveInStop Done...');
            {$ENDIF}

            FWrapArrounds := 0;
            FWrapSize     := 0;

            FReseting := True;
            FError := WaveInReset(FHWaveIn);
            if FError <> 0 then
               Error('WaveInReset:'#10#13+WaveInErrorString(FError));

            {$IFDEF _MMDEBUG}
            DebugStr(0,'WaveInReset Done...');
            {$ENDIF}

         finally
            LeaveCritical;
         end;

         if (FInHandler = 0) then
         begin
            TimeOut := 100;
            repeat
               if (FCallBackMode <> cmWindow) then
                   Delay(10,False)
               else
                   Delay(10,True);
               dec(TimeOut);
            until (FBufferCounter = 0) or (TimeOut <= 0);
         end;

         Stopped;

      except
         if assigned(FOnError) then FOnError(Self);
         Close;
         raise;
      end;
   end;
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
procedure TMMCustomWaveIn.Opened;
begin
   {$IFDEF _MMDEBUG}
   DebugStr(0,'Device is now open...');
   {$ENDIF}

   if Assigned(FOnOpen) then FOnOpen(Self);
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
procedure TMMCustomWaveIn.Closed;
begin
   FHWaveIn := 0;

   { free memory and remove }
   FreeWaveHeaders;

   {$IFDEF _MMDEBUG}
   DebugStr(0,'Device is now closed...');
   {$ENDIF}

   FClosing := False;

   if not (csDestroying in ComponentState) then
      if Assigned(FOnClose) then FOnClose(Self);
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
procedure TMMCustomWaveIn.Started;
begin
   Include(FState, wisRecord);

   if not (wisPause in FState) then
   begin
      { start the buffers recording (unpause) }
      FError := WaveInStart(FHWaveIn);
      if FError <> 0 then
        Error('WaveInStart:'#10#13+WaveInErrorString(FError));
   end;

   { now notify all other components }
   inherited Started;

   {$IFDEF _MMDEBUG}
   DebugStr(0,'Device is now started...');
   {$ENDIF}

   InitDSPMeter;

   if assigned(FOnStart) then FOnStart(Self);
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
procedure TMMCustomWaveIn.Paused;
begin
   Include(FState, wisPause);

   inherited Paused;

   {$IFDEF _MMDEBUG}
   DebugStr(0,'Device is now paused...');
   {$ENDIF}

   if assigned(FOnPause) then FOnPause(Self);
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}
procedure TMMCustomWaveIn.Restarted;
begin
   FState := FState - [wisPause];

   inherited Restarted;

   {$IFDEF _MMDEBUG}
   DebugStr(0,'Device is now restarted...');
   {$ENDIF}

   if assigned(FOnRestart) then FOnRestart(Self);
end;

{-- TMMCustomWaveIn ------------------------------------------------------------}

⌨️ 快捷键说明

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