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('Processing',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('Bit reduction','Bit reduction. Enter desired bit depth (for normalized input).');
Form.AddInputKnob('Bit depth',16,1,24);
if Form.Execute then BR(Round(Form.GetInputValue('Bit depth')));
Form.Free;
End.
