# The main function for multivariate onset detection

# INPUT: 	path.wav: path to the wav which has to be analysied
#			path.time: path to the time vector of true onsets
#			ctrl: ctrl object (a list), putput of OnsetDetectionControl() function

# OUTPUT:	orig.odf: original odf feature
#			smooth.odf: smoothed odf feature
# 			thresh.odf: threshold values
#			est.onset.times: estimated onste times
#			est.onset.frames: estimated frames numbers where a tone onset occures
#			true.onset.times: true onset times
#			fMeasure: F-value of onset detection
#			c: number of correctly detected onsets
#			f.neg: number of undetected onsets
#			f.pos: numper of false detections
# 			dMeasure: F-value of onset detection

multiOnsetDetection=function(path.wav, path.time, type = "offline") {
    
	library(randomForest)
	
	
	if (type == "offline"){
	load("C:/Users/bauer/Desktop/CD_Diss/randomForestModels/top1_offline.RData")
	model = randForestModel1_offline
	features = rownames(model$importance)
	
	ctrl = list()
	ctrl$window.size = 2048L
    ctrl$hop.size = 1043L 
    ctrl$sampling.rate = 44100L
    ctrl$window.fun = "hann"
    ctrl$onset.time.left = 1.802107 * 10^(-4)
	ctrl$onset.time.right = 0.052
    ctrl$onset.time.min.dist = 0.036789
    ctrl$tolerance = 0.025
    ctrl$spec.filter = "yes"
    ctrl$spec.log = "yes"
    ctrl$log.lambda = 1.017
    ctrl$classif.threshold = 0.546
    ctrl$odfMatrix.time.left = 0.15
    ctrl$odfMatrix.time.right = 0.15
	ctrl$norm = TRUE

    n.onset.left = TimeToNrOfWindows(ctrl$onset.time.left, ctrl)
    n.onset.right = TimeToNrOfWindows(ctrl$onset.time.right, ctrl)
    onset.min.dist = TimeToNrOfWindows(ctrl$onset.time.min.dist,  ctrl)
	}

	
	if (type == "online"){
	load("C:/Users/bauer/Desktop/CD_Diss/randomForestModels/top1_online.RData")
	model = randForestModel1_online
	features = rownames(model$importance)
	
	ctrl = list()
	ctrl$window.size = 1024L
    ctrl$hop.size = 816L 
    ctrl$sampling.rate = 44100L
    ctrl$window.fun = "hann"
    ctrl$onset.time.left = 0.0266
	ctrl$onset.time.right = 0
    ctrl$onset.time.min.dist = 0.0245
    ctrl$tolerance = 0.025
    ctrl$spec.filter = "yes"
    ctrl$spec.log = "yes"
    ctrl$log.lambda = 19.25
    ctrl$classif.threshold = 0.310
    ctrl$odfMatrix.time.left = 0.15
    ctrl$odfMatrix.time.right = 0
	ctrl$norm = FALSE

    n.onset.left = TimeToNrOfWindows(ctrl$onset.time.left, ctrl)
    n.onset.right = TimeToNrOfWindows(ctrl$onset.time.right, ctrl)
    onset.min.dist = TimeToNrOfWindows(ctrl$onset.time.min.dist,  ctrl)
	}

	
	
	
	# get onset detection Matrix
    oM = getOdfMatrix(path.wav, path.time, ctrl, feats = features)
	
	oM.true = oM$oM.true
    time.end = oM$time.end 
    true.times = oM$true.times 
  
	# prediction for the odfMatrix
    pred = predict(model, newdata = oM.true,  type="prob")
 
    # compute onsets
    onsets = computeOnsetsClassif(n.onset.left, n.onset.right, onset.min.dist, pred[,2], ctrl$classif.threshold)
    
    estim.frames.nr=which(onsets=="1")
    onset.times=((1:length(pred[,2])*ctrl$hop.size)/ctrl$sampling.rate)[estim.frames.nr]
    
         
    fMeas = fMeasure(true.times, estim.times = onset.times, ctrl)
    dMeas = dMeasure(true.times, estim.times = onset.times, time.end)

    return(list(oM.true = oM.true, 
              pred = pred, 
              est.onset.times = onset.times, 
              est.onset.frames = estim.frames.nr,
              true.onset.times = true.times,
              fMeasure = fMeas$fMeasure,
              c = fMeas$c,
              f.neg = fMeas$fn,
              f.pos = fMeas$fp,
              dMeasure = dMeas))
}
			  
			  
