/***************************************************************************
                          GNUIO.cpp  -  description
                             -------------------
    begin                : Fre Nov 28 2003
    copyright            : (C) 2003 by klaus frieler
    email                : kgf@omniversum.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "types.h"
#include "FileReader.h"
#include "Melody.h"

bool GNUIO::read(const string& inFile, Melody** pMelody )
{
   return true;
}

bool GNUIO::write(const string& outFile, const Melody* pMelody)
{
   char buf[1024] = "";
   bool bRet = false;
   if(!pMelody) return false;
   FILE * fp;

   fp = fopen(outFile.c_str(), "w");
   if(!fp){
      setErrorText("Could not open file");
      return bRet;
   }

   for(uint i = 0; !feof(fp) && i < pMelody->size()-1; ++i){
      Tone tone1 = pMelody->at(i);
      double onset = tone1.getTime();
      uint pitch =   tone1.getPitch();
      if(pitch == 0 || (pitch>127 && pitch != 250)) continue;
      if(pitch == 250){
         if (i != 0) pitch =  pMelody->at(i-1).getPitch();
         else pitch = pMelody->getModusPitch();
      }
      sprintf(buf,"%.2f\t%d\n", onset, pitch);
      fputs(buf, fp);
      if(ferror(fp)) goto out;

   }
   bRet = true;

out:
   if(!bRet)
      setErrorText("I/O error during write");
   fclose(fp);
   return bRet;

}
