# INPUT:
#   channels:     a vector of integer numbers. Should match with channels of the event.frame. Default is 0.
# 	instruments:  a string vector which provides a music instrument to each channel. 
#                 Possible instruments are: c("piano", "guitar", "flute", "clarinet", "trumpet", "violin"). Default is "piano".
#   sample.instrument: a string vector which determines for each channel whether the a special fixed instrument should be taken, instrument should be sampled randomly from the available data set (mostly 2-3 note data sets for an instrument), so that just tones from the samples data based are used in the channel or each tone should be sampled randomly from all available data sets for the interesting instrument. The associated allowed values are:  c("fix", "sample.instrument", "sample.each.tone").   
#   wave.to.channel: a logical parameter. If TRUE, the a Wave object is generated for ech channel, additionally to the final Wave object. Default is FALSE
#   fix.string:   a logical parameter which is important just for the string instrument where some tones can be produced by different strings (like guitar). If TRUE, always the highest possibly string is chosen, if FALSE, the string is sampled randomly. Default is FALSE. 
#   thresh.staccato: a time threshold in seconds for the decision whether a tone should be taken from the staccato data bank (short tones): if a duration of a tone is smaller than this threshold. Default: 0.15 seconds. Maximal value is 0.8 seconds.
#   data.path:    a path for the directory with tone data banks. Important, if instrument path's are not specified. Default is "//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/RealConverterTones"
#   notes.path.piano: a path for piano tones data bank. 
#                     If notes.path.piano = NULL (Defualt) and fix.string = "fix", ".../piano/tr011PF" tone library is taken. 
#                     If notes.path.piano = NULL and fix.string != "fix", notes.path.piano = c("./piano/tr011PF", "./piano/tr012PF", "./piano/tr013PF")
#   notes.path.guitar: a path for guitar tones data bank. 
#                     If notes.path.guitar = NULL (Defualt) and fix.string = "fix", ".../guitar/tr092CGRF" tone library is taken. 
#                       If notes.path.guitar = NULL and fix.string != "fix", notes.path.guitar = c("./guitar/tr092CGRF", "./ guitar/tr092CGRN", "./guitar/tr092CGAF", 
#                                                                                                "./guitar/tr092CGAN", "./guitar/tr093CGRF", "./guitar/tr093CGRN", "./guitar/tr093CGAF","./guitar/tr093CGAN","./guitar/tr091CGRF",
#                                                                                               "./guitar/tr091CGRN", "./guitar/ttr091CGAF", "./guitar/tr091CGAN)
#   notes.path.flute:   a path for flute tones data bank. 
#                       If notes.path.flute = NULL (Defualt) and fix.string = "fix", ".../flute/tr331FL" tone library is taken. 
#                       If notes.path.flute = NULL and fix.string != "fix", notes.path.flute = c("./flute/tr331FL", "./ flute/tr332FL").
#   notes.path.clarinet: a path for clarinet tones data bank. 
#                       If notes.path.clarinet = NULL (Defualt) and fix.string = "fix", ".../clarinet/tr311CL" tone library is taken. 
#                       If notes.path.clarinet = NULL and fix.string != "fix", notes.path.clarinet = c("./flute/tr331FL", "./ flute/tr332FL").
#   notes.path.trumpet: a path for trumpet tones data bank. 
#                       If notes.path.trumpet = NULL (Defualt) and fix.string = "fix", ".../trumpet/tr211TR" tone library is taken. 
#                       If notes.path.trumpet = NULL and fix.string != "fix", notes.path.trumpet = c("./trumpet/tr211TR", "./ trumpet/tr212TR").
#   notes.path.violin: a path for violin tones data bank. 
#                       If notes.path.violin = NULL (Defualt) and fix.string = "fix", ".../violin/tr151VN" tone library is taken. 
#                       If notes.path.violin = NULL and fix.string != "fix", notes.path.violin = c("./violin/tr151VN", "./ violin/tr152VN", "./violin/tr153VN",).

# OUTPUT: a list

RealConverterControl = function(channels = 0,
                                instruments = "piano",
                                sample.instrument = "sample.instument",
                                wave.to.channel = FALSE,
                                fix.string = TRUE,
                                thresh.staccato = 0.15,
                                notes.path.piano = NULL,
                                notes.path.guitar = NULL,
                                notes.path.flute = NULL,
                                notes.path.clarinet = NULL,
                                notes.path.trumpet = NULL,
                                notes.path.violin = NULL,
                                data.path="//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/RealConverterTones"
                                ) {
  
  
  if (!is.numeric(channels)) {
    stop("parameter 'channels' has to be numeric")
  }
  
  if (length(instruments)==0){
    stop("at least one instrument has to be assigned")
  } else {
    for (i in 1:length(instruments)){
      if (!any(instruments[i]==c("piano","guitar","flute","clarinet","trumpet","violin"))) {
        stop("only 'piano', 'guitar', 'flute', 'clarinet', 'trumpet' or 'violin' can be set in 'instruments' ")
      }
      
    }
  }

  if (!is.logical(wave.to.channel)) 
    stop("parameter 'wave.to.channel' has to be logical")
  
  
   
  if (!any(sample.instrument==c("fix", "sample.instrument", "sample.each.tone"))) 
    stop("parameter 'sample.instrument' has three values: 'fix', 'sample.instrument', 'sample.each.tone'")
  

  if (!is.logical(fix.string)) 
    stop("parameter 'fix.string' has to be logical")
  
  
  if (!is.numeric(thresh.staccato)) 
    stop("parameter 'thresh.staccato' has to be numeric")
  
  if (thresh.staccato > 0.8) {
    warning("parameter 'thresh.staccato' should be smaller than 0.8. It will be set to 0.8")
    thresh.staccato = 0.8
  }
  
  # notes.path.piano
  if (!file.exists(data.path))
    stop(" 'data.path' direktory does not exist")
  
  
  if (is.null(notes.path.piano)){
    if(sample.instrument!="fix"){
      notes.path.piano=c(sprintf("%s/piano/tr011PF",data.path), sprintf("%s/piano/tr012PF",data.path),
                         sprintf("%s/piano/tr013PF",data.path))
    } 
    else {
      notes.path.piano=sprintf("%s/piano/tr011PF",data.path)
    }
  }


  
  # notes.path.guitar 
  if (is.null(notes.path.guitar)){
    if(sample.instrument!="fix"){
      notes.path.guitar=c(sprintf("%s/guitar/tr092CGRF",data.path), sprintf("%s/guitar/tr092CGRN",data.path),
                          sprintf("%s/guitar/tr092CGAF",data.path),sprintf("%s/guitar/tr092CGAN",data.path),
                          sprintf("%s/guitar/tr093CGRF",data.path), sprintf("%s/guitar/tr093CGRN",data.path),
                          sprintf("%s/guitar/tr093CGAF",data.path),sprintf("%s/guitar/tr093CGAN",data.path),
                          sprintf("%s/guitar/tr091CGRF",data.path), sprintf("%s/guitar/tr091CGRN",data.path),
                          sprintf("%s/guitar/tr091CGAF",data.path),sprintf("%s/guitar/tr091CGAN",data.path))
    } 
    else {
      notes.path.guitar=sprintf("%s/guitar/tr092CGRF",data.path)
    }
  }

  
  # notes.path.flute
  if (is.null(notes.path.flute)){
    if(sample.instrument!="fix"){
      notes.path.flute=c(sprintf("%s/flute/tr331FL",data.path), sprintf("%s/flute/tr332FL",data.path))
    } 
    else {
      notes.path.flute=sprintf("%s/flute/tr331FL",data.path)
    }
  }

  
  # notes.path.clarinet  
  if (is.null(notes.path.clarinet)){
    if(sample.instrument!="fix"){
      notes.path.clarinet=c(sprintf("%s/clarinet/tr311CL",data.path), sprintf("%s/clarinet/tr312CL",data.path),
                         sprintf("%s/clarinet/tr313CL",data.path))
    } 
    else {
      notes.path.clarinet=sprintf("%s/clarinet/tr311CL",data.path)
    }
  }


  # notes.path.trumpet
  if (is.null(notes.path.trumpet)){
    if(sample.instrument!="fix"){
      notes.path.trumpet=c(sprintf("%s/trumpet/tr211TR",data.path), sprintf("%s/trumpet/tr212TR",data.path))
    } 
    else {
      notes.path.trumpet=sprintf("%s/trumpet/tr211TR",data.path)
    }
  }

  
  # notes.path.violin
  if (is.null(notes.path.violin)){
    if(sample.instrument!="fix"){
      notes.path.violin=c(sprintf("%s/violin/tr151VN",data.path), sprintf("%s/violin/tr152VN",data.path),
                          sprintf("%s/violin/tr153VN",data.path))
    } 
    else {
      notes.path.violin=sprintf("%s/violin/tr151VN",data.path)
    }
  }

  
  
  control = list(channels = channels,
                      instruments = instruments,
                      sample.instrument = sample.instrument,
                      wave.to.channel = wave.to.channel,
                      fix.string = fix.string,
                      thresh.staccato = thresh.staccato,
                      notes.path.piano = notes.path.piano,
                      notes.path.guitar = notes.path.guitar,
                      notes.path.flute = notes.path.flute,
                      notes.path.clarinet = notes.path.clarinet,
                      notes.path.trumpet = notes.path.trumpet,
                      notes.path.violin = notes.path.violin,
                      data.path = data.path
  )
  
  return(control)
}
