function [y, Fs, Nbit] = flacread(file_path, span, flac_exe_dir)
   // Reads sound data from a flac file
   //
   // Usage:
   //       [y, Fs, Nbit] = flacread(file_path, span, flac_exe_dir)
   //       info = flacread(file_path, "info", flac_exe_dir)
   // where
   //       file_path:    Full path of file to read
   //       span:         Two component vector indicating the
   //                     first and last sample to read, or 
   //                     the string "info" to retrieve 
   //                     the information listed below  
   //       flac_exe_dir: Directory containing the reference 
   //                     executable codec (flac.exe)
   //       y:            Audio samples in +/- 1 range
   //       Fs:           Sample rate in samples per second
   //       Nbit:         Number of bits per sample
   //       info:         Vector containing the following 
   //                     metadata: 
   //                     Type
   //                     Channels
   //                     Sample rate in Hz
   //                     Bytes per second
   //                     Total bytes per sample 
   //                     Bits per sample per channel
   //                     bytes per sample per channel
   //                     Length of sound data per channel
   // 
   // NOTE: file_exe_path 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-08

   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-211a.flac"
      span = [5000 10000]
      span = "info"
   end

   if rhs<3
      if isfile(SCI + "tools\flac\flac.exe")
         flac_exe_dir = SCI + "tools\flac\";
      elseif isfile(pwd() + "\flac.exe")
         flac_exe_dir = pwd() + "\";
      elseif isfile(pwd() + "\flac\flac.exe")
         flac_exe_dir = pwd() + "\flac\";
      else
         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

   // Temporary file selected at random
   folder = fileparts(file_path);
   already = 0;
   while already==0; 
      out_path = folder + msprintf("%.0f", floor(rand()*1e15)) + ".wav";
      // Checks if the name is already in use
      if ~isfile(out_path)
         already = 1;
      end
   end

   // Position of the colon symbol within the path
   k = strindex(flac_exe_dir, ":");

   // DOS command to choose drive
   com1 = part(flac_exe_dir,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
   // Decode
   com3 = "flac -d ";
   if isdef("span")
      if type(span)==1 & length(span)==2
         // Initial and final sample in the requested range
         i1 = span(1);
         i2 = span(2)+1;
         // Only read samples in the requested range
         com3 = com3 + "--skip=" + string(i1) + " --until=" + string(i2);
      elseif span=="info"
         i1 = 0;
         i2 = 1;
         // Only read samples in the requested range
         com3 = com3 + "--skip=" + string(i1) + " --until=" + string(i2);
      else
         // Read all samples
      end
   end
   
   // 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; 

   // Run commands in a DOS window 
   [output, bOK, exitcode] = dos(commands);

   // Read temporary wave file
   if isdef("span")
      if span=="info"
         // Return metadata of the associated wav file
         y = wavread(out_path, "info");
         // Read the first 26 bytes from flac file to 
         // retrieve audio data size. Information is
         // contained in the lesat significant half 
         // of byte 22 and bytes 23 to 26.
         fid = mopen(file_path,"rb");
         mseek(0);
         meta = mget(26, "uc", fid);
         mclose(fid);
         // Remove most significant half of byte 22
         meta(22) = modulo(meta(22), 16);
         // Convert bytes 22 to 26 to decimal (the
         // hexadecimal is big endian) 
         audiosize = sum(meta(22:26).*(256.^[4:-1:0]));
         // Get number of samples
         y(8) = audiosize/y(2)/y(7);
      else
         // Return the signal, sample rate and bits 
         // per sample
         [y, Fs, Nbit] = wavread(out_path);
      end
   else
      // Return the signal, sample rate and bits 
      // per sample
      [y, Fs, Nbit] = wavread(out_path);
   end

   // Delete temporary wave file
   deletefile(out_path);

endfunction
