PitchShift again..

Started by Ingolf2,

Ingolf2

Can anyone help me with this? This code works while (SourceLength / DestLength) < 2, so it works when pitch shifting 1.8 or so, but when I try to shift it twice the speed, the sound is completely distorted, much too loud and clipped, very weird. (Dest is always 32 bits, TAudioSample is of type Single)

Maybe someone knows a better way to handle different bitdepths too.

procedure PitchShift(SourcePtr, DestPtr: Pointer; SourceLength, DestLength: Integer; SourceBitDepth: TBitDepth);
var
  S8: ^ShortInt;
  S16: ^SmallInt;
  S32: ^TAudioSample;
  D: ^TAudioSample;
  F: Single;
  I: Integer;
begin
  D := DestPtr;
  case SourceBitDepth of
    bd8: F := (SourceLength * 4) / DestLength;
    bd16: F := (SourceLength * 2) / DestLength;
    bd32: F := SourceLength / DestLength;
  end;
  I := 0;
  case SourceBitDepth of
    bd8: begin
      while DestLength > 0 do begin
        S8 := SourcePtr;
        Inc(S8, Trunc(I*F)*2);
        D^ := S8^ / 128;
        Inc(D);
        Inc(S8);
        D^ := S8^ / 128;
        Inc(D);
        Inc(I);
        DestLength := DestLength - SampleSize;
      end;
    end;
    bd16: begin
      while DestLength > 0 do begin
        S16 := SourcePtr;
        Inc(S16, Trunc(I*F)*2);
        D^ := S16^ / 32768;
        Inc(D);
        Inc(S16);
        D^ := S16^ / 32768;
        Inc(D);
        Inc(I);
        DestLength := DestLength - SampleSize;
      end;
    end;
    bd32: begin
      while DestLength > 0 do begin
        S32 := SourcePtr;
        Inc(S32, Trunc(I*F)*2);
        D^ := S32^;
        Inc(D);
        Inc(S32);
        D^ := S32^;
        Inc(D);
        Inc(I);
        DestLength := DestLength - SampleSize;
      end;
    end;
  end;
end;

Ingolf2

Sorry, it's not that procedure, but I still would like to know about the bitdepths... (I've commented it out, and it was still distorted when playing twice the speed)

When I comment my MixChannels proc, the distortion is gone, so I guess my problem lies there, while it is a simple procedure.

procedure MixChannels(BasePtr, MixPtr: Pointer; Length: Integer);
var
  B, M: ^TAudioSample;
begin
  B := BasePtr;
  M := MixPtr;
  while Length > 0 do begin
    B^ := B^ + M^;
    Inc(B);
    Inc(M);
    B^ := B^ + M^;
    Inc(B);
    Inc(M);
    Length := Length - SampleSize;
  end;
end;

Ingolf2


Ingolf2

Here I am again, noticed that only the right channel was distorted. Looks again like the procedure is trying to read beyond the buffer, only I would expect clicks every end of the buffer, so... beats me