uses dos;

type
        MAMEHeader = Record
          ID   : array[0..3] of char;
          len  : longint;
          freq : longint;
          res  : byte;
          vol  : byte;
          exp  : word;   (* expansion *)
        End;

var  f,g : file;
     i,nr,nw : word;
     header : MAMEHeader;
     unsignedflag : boolean;
     resolution,volume : word;
     samplerate : longint;
     idcheck,inputfile, outputfile : string;
     buffer : array[0..16383] of byte;


function upstr(s : string) : string;
var i : integer;
    tmp : string;
begin
 tmp := s;
 for i := 1 to length(s) do
  tmp[i] := upcase(s[i]);
 upstr := tmp;
end;

procedure parsecommandline;
 var
        i,total,code : integer;
        str,path,ext : string;
 begin
        total:= paramcount;
        i := 1;
        while (i <= total) do
          Begin
            str := Paramstr(i);
            if str[1] = '-' then
              Case Upcase(str[2]) of
               'F' : if (i+1 > total) then begin writeln('Expected a sample rate value after -F'); halt(3); end
                     else
                       Begin
                         inc(i);
                         str := paramstr(i);
                         val(str,samplerate,code);
                         if code <> 0 then
                           Begin
                             Writeln('Sample rate must be an integer value');
                             halt(3);
                           End;
                       End;
               'V' : if (i+1 > total) then begin writeln('Expected a volume value after -V'); halt(3); end
                     else
                       Begin
                         inc(i);
                         str := paramstr(i);
                         val(str,volume,code);
                         if code <> 0 then
                           Begin
                             Writeln('Volume must be an integer value');
                             halt(3);
                           End;
                       End;
               'R' : if (i+1 > total) then begin writeln('Expected a resolution after -R'); halt(3); end
                     else
                       Begin
                         inc(i);
                         str := paramstr(i);
                         val(str,resolution,code);
                         if code <> 0 then
                           Begin
                             Writeln('Bit resolution must be an integer value');
                             halt(3);
                           End;
                       End;
               'S' : unsignedflag := true;
              end
            else if inputfile <> '' then
              outputfile := str
            else
              inputfile := str;
            inc(i);
          End;
        if inputfile = '' then
          begin
            writeln('Input PCM file was not specified.');
            halt(3);
          end;
        if outputfile = '' then
          begin
            fsplit(inputfile,path,outputfile,ext);
            outputfile := path+outputfile+'.sam';
            if upstr(inputfile) = upstr(outputfile) then
              begin
                writeln('Please specify a different output filename.');
                halt(3);
              end;
          end;
 end;

begin
        If paramcount = 0 then
          Begin
            Writeln('MAME Sample Converter  v. 0.01b  -  (C) 1997 by Mirko Buffoni');
            writeln;
            writeln('USAGE:   MAMESAM  <input_pcm_file> <mame_sample_file> <switches>');
            writeln;
            Writeln('         switches:  -f #####   samplerate (def: 22050)');
            writeln('                    -v ###     volume     (def: 255)');
            writeln('                    -r ##      resolution (def: 8 bit)');
            writeln('                    -s         specify unsigned PCM (def: signed)');
            halt(1);
          End;
        inputfile := '';
        outputfile := '';
        volume := 255;
        resolution := 8;
        samplerate := 22050;
        unsignedflag := false;
        ParseCommandLine;
        Assign(f,inputfile);
        assign(g,outputfile);
        filemode := 0;
        reset(f,1);
        filemode := 2;
        rewrite(g,1);
        idcheck := '    ';
        blockread(f,IDCheck[1],4,nr);
        seek(f,0);
        if IDCheck = 'MAME' then
          Begin
            writeln('File is already in MAME sample format!  Changing values to the following:');
            writeln('Sample rate:  ',samplerate,'   Volume:  ',volume,'   Bit resolution:  ',resolution);
            blockread(f,header,sizeof(header),nr);
            Header.Freq := samplerate;
            Header.Res  := resolution;
            Header.vol  := volume;
            Header.Exp  := 0;
            close(f);
            filemode := 2;
            reset(f,1);
            blockwrite(f,header,sizeof(header),nw);
            halt(2);
          End;
        Header.ID := 'MAME';
        Header.Len := Filesize(f);
        Header.Freq := samplerate;
        Header.Res  := resolution;
        Header.vol  := volume;
        Header.Exp  := 0;
        blockwrite(g,header,sizeof(header),nw);
        while not(eof(f)) do
          Begin
            blockread(f,buffer,sizeof(buffer),nr);
            if unsignedflag and (nr > 0) then
              for i := 0 to nr-1 do
                buffer[i] := buffer[i] xor $80;
            blockwrite(g,buffer,nr,nw);
          End;
        close(f);
        close(g);
end.