script "Bit reduction" language "pascal";

procedure BR(Value:Integer);
var   n,x,c,x1,x2:Integer;
      s,v1,v2:Single;
Begin
Editor.GetSelectionS(x1,x2);

if Value<1 then Value:=1;
v1:=1 shl (Value-1);
v2:=1/v1;

for n:=x1 to x2 do 
  Begin
  x:=n-x1;
  if x mod 10000=0 then ProgressMsg('正在处理',x,x2-x1);
  
  for c:=0 to EditorSample.NumChans-1 do
    Begin
    s:=EditorSample.GetSampleAt(n,c);
    s:=Round(s*v1)*v2;
    EditorSample.SetSampleAt(n,c,s);
    End;
  End;
End;


var Form:TScriptDialog;

Begin
Form:=CreateScriptDialog('比特缩减','比特缩减, 输入需要的比特深度(用于规格化输入)。');
Form.AddInputKnob('比特深度',16,1,24);
if Form.Execute then BR(Round(Form.GetInputValue('比特深度')));
Form.Free;
End.
