/***************************************************************************
                          Pitch.h  -  description
                             -------------------
    begin                : Die Nov 25 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.                                   *
 *                                                                         *
 ***************************************************************************/
#ifndef _LIBMELODY_PITCH_H
#define _LIBMELODY_PITCH_H
#include "types.h"

class Pitch{

public:
   static   const char* stdToneNames[];
   static   const char* genToneNamesSharp[];
   static   const char* genToneNamesFlat[];
   static   uint        major[12];
   static   uint        minor[12];
   
   static const char* getToneName(uint idx)
   {
      idx = idx % 12;
      return Pitch::stdToneNames[idx];
   }
   static uint getPitchNumber(const char* pitchName)
   {
      for(uint i = 0; i<12; ++i){
         if(0==strcmp(Pitch::genToneNamesSharp[i], pitchName))
            return i;
      }
      for(uint i = 0; i<12; ++i){
         if(0==strcmp(Pitch::genToneNamesFlat[i], pitchName))
            return i;
      }
      return 12;
   }
   static uint getMIDIPitchNumber(const char* pitchName, int octave)
   {
      uint pitch = getPitchNumber(pitchName);
      if( pitch == 12) return (uint)128;
      return  (C0 + pitch) + octave*12;
   }
   
   static uint getMIDIPitchByKey(uint step, uint key, int sign, int octave, bool useMinor = false)
   {
      uint pitch = useMinor ? minor[step-1] : major[step-1];
      return C0 + key + pitch + sign + (12*octave);      
   }
   
   static uint getMIDIPitchByKey(uint step, const char* keyName, int sign, int octave, bool useMinor = false)
   {
      uint key = getPitchNumber(keyName);
      if(key == 12) return 128;
      return getMIDIPitchByKey(step, key, sign, octave, useMinor);
   }
   
   static int getTonalityIdx(const int pitchClass, const int trans)
   {
      if(pitchClass>= trans) return (pitchClass-trans)%12;
      return 12+(pitchClass-trans)%12;
   }
protected:
};

#endif
