function err = wav2flac(file_path, flac_exe_dir, comp_level)
   // Converts wav file to flac file
   //
   // Usage:
   //       err = wav2flac(file_path, flac_exe_dir, comp_level)
   // where
   //       file_path:    Full path of file to read
   //       flac_exe_dir: Directory containing the recerence 
   //                     executable codec (flac.exe)
   //       comp_level:   Compression level: 1 (faster) to
   //                     8 (best, default)    
   //       err:          error code
   //
   // The function requires the flac.exe codec installed
   // 
   // NOTE: flac_exe_dir is optional. If not provided, the 
   //       function will attempt to find the flac.exe codec
   //       in the following paths: 
   //       1) SCI + "tools\flac\flac.exe"
   //       2) pwd() + "\flac.exe"
   //       3) pwd() + "\flac\flac.exe" 
   //       If not found, a prompt will explicitly ask to
   //       enter it. The flac.exe can be downloaded from
   //       https://ftp.osuosl.org/pub/xiph/releases/flac/
   //       as a zip file. Once extracted it contains two
   //       files: flac.exe and metaflac.exe. Place the
   //       first in the desired directory and use it as
   //       flac_exe_dir.  
   //
   // ---------------------------------
   // Author: Federico Miyara
   // Date:   2021-08-02
   
   rhs = argn(2);
   
   // Testing data --DELETE--
   if 1==2
   flac_exe_dir = "H:\SOFTWARE\FLAC\FLAC frontend v2.1-portable\tools\"
   file_path = "H:\Grupo ruido\Aural\RECORDING210-211c.wav"
   rhs = 1
   end
   
   if rhs<3
      comp_level = 8;
   else
      if type(comp_level)==1
         comp_level = floor(real(comp_level(1)))
         if comp_level<1
            comp_level = 1
         elseif comp_level>8
            comp_level = 8
         end
      else
         comp_level = 8;
      end
   end
   
   if rhs<2
      if isfile(SCI + "tools\flac\flac.exe")
         flac_exe_dir = SCI + "tools\flac\flac.exe";
      elseif isfile(pwd() + "\flac.exe")
         flac_exe_dir = pwd() + "\flac.exe";
      elseif isfile(pwd() + "\flac\flac.exe")
         flac_exe_dir = pwd() + "\flac\flac.exe";
      else
         // Ask for the codec directory 
         msg = "Input full path to flac.exe: ";
         flac_exe_dir = input(msg, "s");   
         // If the directory does not end with "\",
         // append it 
         if ascii(ascii(flac_exe_dir)($))~="\"
            flac_exe_dir = flac_exe_dir + "\";
         end
      end
   end
   
   // Check if the default file name already exists
   [folder, filename, ext] = fileparts(file_path);
   default_filename = filename + ".flac";
   if isfile(folder + default_filename)
      // If it does exist a new name or confirmation
      // to overwrite is asked for
      mprintf("%s already exists\n", default_filename)
      msg = "Enter new name or press ENTER to overwrite: ";
      new_name = input(msg,"s");
      if new_name==""
         // Confirmation to overwrite
         out_path = folder + filename + ".flac";
      else
         // New name
         out_path = folder + new_name;
      end
   else
      out_path = folder + filename + ".flac";
   end
   
   // Position of the colon symbol within the codec path
   k = strindex(flac_exe_dir, ":");
   
   // DOS command to choose drive
   com1 = part(flac_exe_dir,k-1:k);
   
   // DOS command to change to the flac codec directory
   com2 = "cd " + flac_exe_dir;
   
   // Prepare a command line to run flac.exe with 
   // several options
   // Encode
   com3 = "flac -f -" + string(comp_level);
   // Keep foreign metadata (cues, etc.)
   com3 = com3 + " --keep-foreign-metadata";
   // Assign temporary file name to output
   com3 = com3 + " -o " + """" + out_path + """";    
   // Apply the options to the input flac file
   com3 = com3 + " """ + file_path + """";
   
   // Generate command string with the && separator
   commands = com1 + " && " + com2 + " && " + com3; 
   
   // Display conversion status
   mprintf("Converting...");
   
   // Run commands in a DOS window
   [output, bOK, err] = dos(commands);

endfunction
