# timeToNrOfWindows function returns number of windows which correspond to a given time and crtl-object.
# INPUT: 	time: time in seconds
#			ctrl: ctrl-object
# OUTPUT:	nr of windows

# At the moment the method of Boeck et al. (2012) is used by default. However, this method is can allow to consider more time, than given by time parameter

#@inproceedings{Boeck2012Onset,
#   author = {Sebastian B\"{o}ck and Florian Krebs and Markus Schedl},
#   title = {Evaluating the online capabilities of onset detection methods},
#   booktitle = {Proceedings of the 13th International Society for Music Information Retrieval Conference (ISMIR'12)},
#   year = {2012},
#   pages = {49--54}
#}

# More correct method is "correct", time specification will not be exceeded.

timeToNrOfWindows=function(time, ctrl){
  
  methode="boeck"
  
  if(methode=="correct"){
    offset = seq(1, 44100 - ctrl$window.size, by = ctrl$hop.size)
    win.start.sample = offset
    win.start.time = win.start.sample / ctrl$sampling.rate
    w = which((win.start.time+ctrl$window.size/ctrl$sampling.rate)<time) 

    if(length(w) == 0)
      return(0)
    else
      return(max(w))
  }
  
  if(methode=="boeck"){
    fps = ctrl$sampling.rate/(ctrl$hop.size)
    w = round(fps * time)
    return(w)
  }
  
}




fromTimeToWindow=function(time.vec, ctrl)
{
  N = ctrl$window.size
  h = ctrl$hop.size
  sample.vec = time.vec*ctrl$sampling.rate+1
  starts = seq(1,(max(sample.vec)+N),h)
  ends = starts+(N-1)
  # a list: each element is a tone onset
  win.nr.list = sapply(sample.vec, function(x){which(starts <=x & ends >= x)})  
  win.nr = unlist(win.nr.list)
  return(win.nr)
}