{--------------------------------------------------------}
{    Conversor para Microsoft ADPCM e outros formatos
{    Por José Antonio Borges
{    Em 29/05/98
{--------------------------------------------------------}

{$M 16384, 8192}
program convadpc;
uses winprocs, wintypes, dvcrt, dvwin, dvexec, dvmacro, strings, win31;

var
    janAnterior, minhaJan, janGrav: hwnd;
    nomeDir: string;
    arq: file;
    narq: integer;
    tipoConv: integer;

label fim;

{--------------------------------------------------------}

procedure altSeq (c1, c2: char; comEnter: boolean);
begin
    playInit (300);
    playAltKey (c1 , 1);
    playKey (c2, 100);
    if comEnter then
        playKey (ENTER, 1);
    playStart;
    while  playIsActive do;
end;

{--------------------------------------------------------}

procedure inicializa;
begin
    janAnterior := getActiveWindow;
    screenSize.y := 5;

    clrscr;
    minhaJan := getActiveWindow;

    sintInic (0, '');
    sintFalaPont := false;
    sintWriteln ('Conversor de formatos de SOM');
    writeln;
    while sintFalando do;

    sintWriteln ('Informe nome do diretório a converter');
    sintReadln (nomeDir);
    if nomeDir = '' then
        begin
            sintFim;
            doneWinCrt;
        end;

    sintWriteln ('Número de arquivos WAV que contém este diretório');
    sintReadInt (narq);

    sintWriteln ('Informe a qualidade desejada');
    sintWriteln ('   0  micro');
    sintWriteln ('   1  compacto');
    sintWriteln ('   2  telefone');
    sintWriteln ('   3  rádio');
    sintReadint (tipoConv);

    if (tipoConv < 0) or (tipoConv > 3) then
         begin
              sintWriteln ('Formato inválido');
              sintFim;
              doneWinCrt;
         end;

    if executaProg ('sndrec32', '.', '') <= 32 then
    begin
         sintWriteln ('Erro ao carregar o gravador do Windows');
         sintFim;
         doneWinCrt;
    end;

    repeat keypressed until getActiveWindow <> minhaJan;
    janGrav := getActiveWindow;
    setActiveWindow (minhaJan);
end;

{--------------------------------------------------------}

procedure converteArquivo (n: integer);
var nomePrinc, nomeExec, nomeTrab: array [0..80] of char;
    i: integer;
begin
    setActiveWindow (janGrav);
    repeat keypressed until getActiveWindow = janGrav;

    altSeq ('A', 'A', false);   { Arquivo abrir como }
    delay (1000);

    playInit (300);
    playString (nomeDir, 1);
    playKey (ENTER, 100);
    playStart;
    while  playIsActive do;

    playInit (300);
    playKey (TAB, 1);
    playKey (TAB, 1);
    playKey (TAB, 1);
    playKey (TAB, 1);
    playKey (TAB, 1);
    playKey (' ', 1);
    playStart;
    while  playIsActive do;

    for i := 2 to n do
        begin
            playInit (30);
            playVirtKey (VK_DOWN, false, false, false, 20);
            playStart;
            while  playIsActive do;
        end;

    delay (1000);
    playInit (30);
    playKey (ENTER, 1);
    playStart;

    delay (1000);

    getWindowText (getActiveWindow, nomePrinc, 80);

    altSeq ('A', 'C', false);   { Arquivo salvar como }
    delay (1000);

    getWindowText (getActiveWindow, nomeExec, 80);
    playInit (30);
    playAltKey ('A', 1);
    playStart;
    while  playIsActive do;

    repeat
        getWindowText (getActiveWindow, nomeTrab, 80);
    until keypressed or (strComp (nomeExec, nomeTrab) <> 0);
    delay (500);

    playInit (100);

    case tipoConv of
        0: begin    {micro}
                playAltKey ('F', 1);
                playKey ('D', 1);
                playKey (ENTER, 100);
            end;

        1: begin    {compacto}
                playAltKey ('F', 1);
                playKey ('M', 1);
                playAltKey ('A', 1);
                playVirtKey (VK_DOWN, false, false, false, 1);
                playVirtKey (VK_DOWN, false, false, false, 1);
                playKey (ENTER, 100);
            end;

        2: begin    {telefone}
               for i := 1 to 4 do
                   playVirtKey (VK_UP, false, false, false, 1);
               for i := 1 to 3 do
                   playVirtKey (VK_DOWN, false, false, false, 1);
               playKey (ENTER, 100);
           end;

        3: begin    {rádio}
               for i := 1 to 4 do
                   playVirtKey (VK_UP, false, false, false, 1);
               for i := 1 to 2 do
                   playVirtKey (VK_DOWN, false, false, false, 1);
               playKey (ENTER, 100);
           end;
    end;

    playStart;
    while  playIsActive do;

    delay (1000);
    playInit (30);
    playKey (ENTER, 1);
    playStart;
    while  playIsActive do;

    delay (1000);
    playInit (30);
    playKey (ENTER, 1);
    playStart;
    while  playIsActive do;

    repeat
        delay (1000);
        getWindowText (getActiveWindow, nomeTrab, 80);
    until strComp (nomeTrab, 'Convertendo') <> 0;

    setActiveWindow (minhaJan);
    repeat keypressed until getActiveWindow = minhaJan;
end;

{--------------------------------------------------------}

procedure finaliza;
begin
    setActiveWindow (janGrav);
    repeat keypressed until getActiveWindow = janGrav;

    playInit (300);
    playVirtKey (VK_F4, false, false, true, 1);
    playStart;
    while  playIsActive do;

    setActiveWindow (minhaJan);
    repeat
        if keypressed then readkey;
    until getActiveWindow = minhaJan;

    sintWriteln ('OK');
    sintFim;
    doneWinCrt;
end;

{--------------------------------------------------------}

var i: integer;
begin
    inicializa;
    for i := 1 to narq do
        begin
            sintWriteInt (i);
            write (#$0d);
            converteArquivo (i);
        end;
    writeln;
    finaliza;
end.