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

📄 mmfxgen.pas

📁 一套及时通讯的原码
💻 PAS
📖 第 1 页 / 共 3 页
字号:
(*
{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.ModulateDataFM(Buffer: PChar; NumBytes: Cardinal);
var
   i,amp: integer;
   Time,Sample: Float;
   NumSamples: Cardinal;
   ReIndex: integer;
   pB: PByte;
   pS: PSMallInt;
   FreqFactor: Float;

begin
   { Frequency Modulation: y(t) = Ac * sin(Wct* Am * sin(Wmt)) }
   Sample := 1.0/FSampleRate;
   ReIndex := Ord(FChannel)-1;
   FreqFactor := 2*M_PI*FFrequency;

   if (FBits = b8bit) then
   begin
      pB := PByte(Buffer);
      Amp := Round(FAmplitude*(127/100));
      if (FMode = mMono) then
      begin
         NumSamples := NumBytes;
         Time := Sample * FBytesDone;
         for i := 0 to NumSamples-1 do
         begin
            pB^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+((pB^-128)/12.8))+128);
            inc(pB);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end
      else if (FChannel = chBoth) then
      begin
         NumBytes := NumBytes and $FFFE;
         NumSamples := NumBytes shr 1;
         Time := Sample * (FBytesDone shr 1);
         for i := 0 to NumSamples-1 do
         begin
            pB^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+((pB^-128)/12.8))+128);
            PByte(PChar(pB)+1)^ := pB^;
            inc(pB,2);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end
      else
      begin
         inc(pB,ReIndex);
         NumBytes := NumBytes and $FFFE;
         NumSamples := NumBytes shr 1;
         Time := Sample * (FBytesDone shr 1);
         for i := 0 to NumSamples-1 do
         begin
            pB^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+((pB^-128)/12.8))+128);
            inc(pB,2);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end;
   end
   else
   begin
      pS := pSmallInt(Buffer);
      Amp := Round(FAmplitude*(32767/100));
      if (FMode = mMono) then
      begin
         NumBytes := NumBytes and $FFFE;
         NumSamples := NumBytes shr 1;
         Time := Sample * (FBytesDone shr 1);
         for i := 0 to NumSamples-1 do
         begin
           { Frequency Modulation: y(t) = Ac * sin(Wct* Am * sin(Wmt)) }
            pS^ := Trunc(Amp*FWaveFunc(FreqFactor*Time*(pS^/3276800)));
            inc(pS);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end
      else if (FChannel = chBoth) then
      begin
         NumBytes := (NumBytes shr 2) shl 2;
         NumSamples := NumBytes shr 2;
         Time := Sample * (FBytesDone shr 2);
         for i := 0 to NumSamples-1 do
         begin
            pS^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+(pS^/3276.8)));
            PSmallInt(PChar(pS)+2)^ := pS^;
            inc(pS,2);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end
      else
      begin
         inc(pS,ReIndex);
         NumBytes := (NumBytes shr 2) shl 2;
         NumSamples := NumBytes shr 2;
         Time := Sample * (FBytesDone shr 2);
         for i := 0 to NumSamples-1 do
         begin
            pS^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+(pS^/3276.8)));
            inc(pS,2);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end;
   end;
end;
*)

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.ModulateDataPM(Buffer: PChar; NumBytes: Cardinal);
(*var
   i,amp: integer;
   Time,Sample: Float;
   NumSamples: Cardinal;
   ReIndex: integer;
   pB: PByte;
   pS: PSMallInt;
   FreqFactor: Float;
 *)

begin
(*
   { Phase Modulation: y(t) = Ac * sin(Wct* Am + sin(Wmt)) }
   Sample := 1.0/FSampleRate;
   ReIndex := Ord(FChannel)-1;
   FreqFactor := 2*M_PI*FFrequency;

   if (FBits = b8bit) then
   begin
      pB := PByte(Buffer);
      Amp := Round(FAmplitude*(127/100));
      if (FMode = mMono) then
      begin
         NumSamples := NumBytes;
         Time := Sample * FBytesDone;
         for i := 0 to NumSamples-1 do
         begin
            pB^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+((pB^-128)/12.8))+128);
            inc(pB);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end
      else if (FChannel = chBoth) then
      begin
         NumBytes := NumBytes and $FFFE;
         NumSamples := NumBytes shr 1;
         Time := Sample * (FBytesDone shr 1);
         for i := 0 to NumSamples-1 do
         begin
            pB^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+((pB^-128)/12.8))+128);
            PByte(PChar(pB)+1)^ := pB^;
            inc(pB,2);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end
      else
      begin
         inc(pB,ReIndex);
         NumBytes := NumBytes and $FFFE;
         NumSamples := NumBytes shr 1;
         Time := Sample * (FBytesDone shr 1);
         for i := 0 to NumSamples-1 do
         begin
            pB^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+((pB^-128)/12.8))+128);
            inc(pB,2);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end;
   end
   else
   begin
      pS := pSmallInt(Buffer);
      Amp := Round(FAmplitude*(32767/100));
      if (FMode = mMono) then
      begin
         NumBytes := NumBytes and $FFFE;
         NumSamples := NumBytes shr 1;
         Time := Sample * (FBytesDone shr 1);
         for i := 0 to NumSamples-1 do
         begin
            pS^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+(pS^/3276.8)));
            inc(pS);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end
      else if (FChannel = chBoth) then
      begin
         NumBytes := (NumBytes shr 2) shl 2;
         NumSamples := NumBytes shr 2;
         Time := Sample * (FBytesDone shr 2);
         for i := 0 to NumSamples-1 do
         begin
            pS^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+(pS^/3276.8)));
            PSmallInt(PChar(pS)+2)^ := pS^;
            inc(pS,2);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end
      else
      begin
         inc(pS,ReIndex);
         NumBytes := (NumBytes shr 2) shl 2;
         NumSamples := NumBytes shr 2;
         Time := Sample * (FBytesDone shr 2);
         for i := 0 to NumSamples-1 do
         begin
            pS^ := Trunc(Amp*FWaveFunc(FreqFactor*Time+(pS^/3276.8)));
            inc(pS,2);
            Time := Time + Sample;
         end;
         inc(FBytesDone,NumBytes);
      end;
   end;  *)
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.Open;
begin
   if not FOpen then
   begin
      FOpen := True;
      FillWaveTable(TabLen);
   end;
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.Start;
begin
   FOffset := 0;
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.Stop;
begin
   ;
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.Close;
begin
   if FOpen then
   begin
      FOpen := False;
   end;
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.Opened;
begin
   inherited Opened;

   Open;
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.Closed;
begin
   Close;

   inherited Closed;
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.Started;
begin
   inherited Started;

   Start;
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.BufferReady(lpwh: PWaveHdr);
begin
   if Enabled then
   begin
      if FModulation = moAM then
         ModulateDataAM(lpwh^.lpData, lpwh^.dwBytesRecorded)
      else if FModulation = moFM then
         ModulateDataFM(lpwh^.lpData, lpwh^.dwBytesRecorded)
      else
         ModulateDataPM(lpwh^.lpData, lpwh^.dwBytesRecorded);
   end;

   inherited BufferReady(lpwh);
end;

{-- TMMGenerator --------------------------------------------------------}
procedure TMMGenerator.BufferLoad(lpwh: PWaveHdr; var MoreBuffers: Boolean);
var
   MustGenerate: Boolean;

begin
   MustGenerate := True;

   if (Input <> nil) then
   begin
      inherited BufferLoad(lpwh, MoreBuffers);

      if (lpwh^.dwBufferLength > 0) then
      begin
         MustGenerate := False;
         if Enabled then
         begin
            if FModulation = moAM then
               ModulateDataAM(lpwh^.lpData, lpwh^.dwBytesRecorded)
            else if FModulation = moFM then
               ModulateDataFM(lpwh^.lpData, lpwh^.dwBytesRecorded)
            else
               ModulateDataPM(lpwh^.lpData, lpwh^.dwBytesRecorded);
         end;
      end;
   end;

   if MustGenerate then
   begin
      if Enabled then
         GenerateData(lpwh^.lpData, lpwh^.dwBufferLength)
      else
         GlobalFillMem(lpwh^.lpData^, lpwh^.dwBufferLength, FSilence);

      lpwh^.dwBytesRecorded := lpwh^.dwBufferLength;
   end;

   MoreBuffers := True;
end;

(*
-----------------------------------------------------------
program SineMix; {$R-} {$S-} {$Q-}
    uses
        SBSample,
        CRT;
    const
        WaveLength = 32; {In samples, freq varies depending on CPU speed}
        Amplitude  = 8; {Keep it low to eliminate wave-top clipping}
    var
        Sines: array[0..WaveLength-1] of ShortInt;
        Sample: byte;
        x: word;
    procedure InitSines;
        var
            i: word;
        begin
            for i := 0 to WaveLength-1 do
                Sines[i] := Round(Sin((2*Pi) * (i/WaveLength))*Amplitude);
        end;
    procedure ClipSample(var Sample: integer);
        begin
            if Sample > 127 then Sample := 127;
            if Sample < -128 then Sample := -128;
        end;
    procedure ProcessSample(var Sample: byte);
      {Make sure that you clip the sample so it stays in the proper range}
        var
            Temp: integer;
        begin
            Temp := Sample-128;

            {Process the sample here}
            Temp := Temp*2+Sines[x];

            ClipSample(Temp);
            Sample := Temp+128;
        end;
    begin
        ResetDSP;
        InitSines;
        x := 0;
        repeat
            Sample := GetSample;
            ProcessSample(Sample);
            OutputSample(Sample);
            Inc(x);
            x := x mod WaveLength;
        until KeyPressed; ReadKey;
*)

end.

⌨️ 快捷键说明

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