package net.mkonrad.synes.util;

import net.mkonrad.synes.gui.SceneCore;
import net.mkonrad.synes.gui.elements.GroundShape;

public class MusicTools
{
  /**
   * Convert crotchet to milliseconds
   * @param beats crochet
   * @return milliseconds
   */
  public static long beats2ms(float beats)
  {
    return (long)((Math.ceil((beats * SceneCore.getBpm() / (/*4 * */ 60.0f)) * 100) / 100) * 1000);
  }

  public static float getPanFromXCoord(float x)
  {
    return 2.0f * x / GroundShape.GROUND_SIZE_X; 
  }
  
  public static float getPitchFromYCoord(float y)
  {
    return 2.0f * y / GroundShape.GROUND_SIZE_Y; 
  }

  public static float getMidiFromYCoord(float y, boolean roundDown)
  {
    return getMidiFromYCoord(y, 1, roundDown);
  }
  
  public static float getMidiFromYCoord(float y, int startOctave, boolean roundDown)
  {
    float yMax = GroundShape.GROUND_SIZE_Y;
    
    float midi = 12 * startOctave + ((y + yMax / 2.0f) / yMax * SceneCore.DEFAULT_NUM_OCTAVES * 12);
    
    if (roundDown)
      return (float)Math.floor(midi);
    else
      return midi;
  }
}
