function flacwrite(y, Fs, Nbit, comp_level, file_path, flac_exe_dir)
   // Writes a signal to a flac file
   //
   // Usage:
   //       flacwrite(y, Fs, Nbit, comp_level, file_path, flac_exe_dir)
   //       flacwrite(y, Fs, Nbit, file_path, flac_exe_dir)
   //       flacwrite(y, Fs, file_path, flac_exe_dir)
   //       flacwrite(y, file_path, flac_exe_dir)
   //       flacwrite(y, file_path)
   // where
   //       y:            signal vector or matrix, each column 
   //                     is a channel
   //       Fs:           Sample rate in Hz
   //       Nbit:         Number of bits per sample per channel
   //       comp_level:   Compression level: 1 (faster) to
   //                     8 (best, default) 
   //       file_path:    Full path to the output flac file
   //       flac_exe_dir: Directory where the flac.exe code is
   //
   // NOTE: If the flac_exe_dir is 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-03
   
   rhs = argn(2);
   
   select rhs
   case 5
      flac_exe_dir = file_path;
      file_path = comp_level;
      comp_level = 8;
   case 4
      flac_exe_dir = comp_level
      file_path = Nbit;
      comp_level = 8;
      Nbit = 16;
   case 3
      flac_exe_dir = Nbit
      file_path = Fs;
      comp_level = 8;
      Nbit = 16;
      Fs = 44100;
   case 2
      flac_exe_dir = getflacpath();
      file_path = Fs;
      comp_level = 8;
      Nbit = 16;
      Fs = 44100;
   end
   
   if 1==2
      flac_exe_dir = "H:\SOFTWARE\FLAC\FLAC frontend v2.1-portable\tools\";
      file_path = "H:\Grupo ruido\Aural\prueba.flac";
      comp_level = 8;
      Nbit = 16;
      Fs = 44100;
      y = 0.5*sin(2*%pi*10*(0:Fs)/Fs);
   end
   
   if fileext(file_path)==".flac"
      [dir, filename, ext] = fileparts(file_path);
      wav_path = dir + filename + ".wav";
   end
   
   // Write temporary wav file
   wavwrite(y, Fs, Nbit, wav_path)
   
   // Generate DOS command line
   
   // 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 wav file
   com3 = com3 + " """ + wav_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);   
   
   // Delete temporary wav file
   deletefile(wav_path)
       
endfunction

function flac_exe_dir = getflacpath();
   // Search or ask for a valid flac.exe path
   // --------------------------
   // Federico Miyara
   // 2021-08-03
   
   // Check some possible standard locations 
   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
endfunction
