# Function makeTrack: generate a Wave to each channel of the MIDI Event Frame

# INPUT: 	event.frame.chan: event frame of a channel
#			ctrl: control object
#			nr.smaples: nr of samples of the composite wave (sample number of the last offset)

# OUTPUT: 	channel.wave: wave of the channel
#			new.notes.path: note paths of the generated wav

makeTrack=function(event.frame.chan,ctrl,nr.samples)
{
  new.notes.path=NULL #will contain information about path's of notes in each step
  
  channel.wave.amplit=numeric(nr.samples)
  
  # nr.onsets.channel
  nr.onsets.channel=dim(event.frame.chan)[1] 
  
  # channel id number 
  chan=event.frame.chan$channel.number[1]
  w.ch=which(ctrl$channels==chan)
  
  # which instrument is playing in this channel
  INSTR=ctrl$instruments[w.ch]
  
  # determine the notes.path and instrumnet kind and others
  if (INSTR == "clarinet"){
    instr.kind = "WIND"
    notes.path = ctrl$notes.path.clarinet
    extractNote = extractNoteSimple
  }
  if (INSTR == "flute"){
    instr.kind = "WIND"
    notes.path = ctrl$notes.path.flute
    extractNote = extractNoteSimple
  }
  if (INSTR == "trumpet"){
    instr.kind = "WIND"
    notes.path = ctrl$notes.path.trumpet
    extractNote = extractNoteSimple
  }
  if (INSTR == "violin"){
    instr.kind = "WIND"
    notes.path = ctrl$notes.path.violin
    if (ctrl$fix.string) 
      extractNote = extractNoteViolinFix
    else
      extractNote = extractNoteViolinSample
  }
  if (INSTR == "piano"){
    instr.kind = "STRING"
    notes.path = ctrl$notes.path.piano
    extractNote = extractNoteSimple
  }
  if (INSTR == "guitar"){
    instr.kind = "STRING"
    notes.path = ctrl$notes.path.guitar
    if (ctrl$fix.string) 
      extractNote = extractNoteGuitarFix
    else
      extractNote = extractNoteGuitarSample
  }
  

  print(c("do track for the instrument:", as.character(INSTR)))
  
  # sample one instrument if ctrl$sample.instrument = "sample.instrument"
  if (ctrl$sample.instrument=="sample.instrument")
    notes.path.one = sample(notes.path, 1)
  
  if (ctrl$sample.instrument=="fix")
   notes.path.one = notes.path # for "fix"
  
  
  
  
  # do for every tone onset
  for (i in 1:nr.onsets.channel)
  {
    note.start = as.integer(event.frame.chan$start.time[i] * 44100 + 1)
    note.end = as.integer(event.frame.chan$end.time[i] * 44100)
    note.duration.sec = (note.end-note.start)/44100
    note.duration.samples = (note.end-note.start)
    
    # detrmine the path ot the note
    if (ctrl$sample.instrument == "TRUE") {
      s = sample(length(notes.path), 1)
      path = notes.path[s]
    } 
    else 
      path = notes.path[1]
      
    # sample instrument for this tone if ctrl$sample.instrument = "sample.each.tone"
    if (ctrl$sample.instrument=="sample.each.tone")
      notes.path.one = sample(notes.path, 1)

    new.note = extractNote(event.frame.chan$note.number[i], event.frame.chan$velocity[i], 
                          note.duration.sec, notes.path.one, 
                          ctrl)
    new.note.wav = new.note$wav
    new.notes.path = c(new.notes.path, new.note$path)
    
    
    
    l = length(new.note.wav)
    if(3 * note.duration.samples < l)
      note.length = 3 * note.duration.samples 
    else 
      note.length = l
    
    # note.length is the maximal length in samples for the note. 
    # It is a sum of note.duration.samples and note.off.samples (number of samples for quiting the note)
    note.off.samples = ((note.duration.samples + 1):note.length)
    
    # FIXME: do it better
    new.note.wav = noteLoudness(new.note.wav, event.frame.chan$velocity[i]*100)
    
    if(instr.kind == "STRING") {
      smoothing.coeff = -10^(-5)*1.5 
      new.note.wav@left=c(new.note.wav@left[1:note.duration.samples], 
                          new.note.wav@left[note.off.samples] * 2^(smoothing.coeff * note.off.samples))
    }
    
    if(instr.kind=="WIND") {
      new.note.wav@left=c(new.note.wav@left[1:note.duration.samples],
                          new.note.wav@left[note.off.samples]*(chi(1:length(note.off.samples),2,500)))}
    
    
    
    if((note.start+note.length-1) <= nr.samples) 
      channel.wave.amplit[note.start: (note.start+note.length-1)] = 
      plus(channel.wave.amplit[note.start:(note.start+note.length-1)], new.note.wav@left[1:note.length]) 
    else{
      y=note.start:nr.samples 
      channel.wave.amplit[y] = plus(channel.wave.amplit[y],new.note.wav@left[1:length(y)])
    }
    
  } # end of for


  channel.wave.amplit[which(is.na(channel.wave.amplit) == TRUE)] = 0
  channel.wave <- Wave(left = channel.wave.amplit, samp.rate = 44100, bit=16)

return(list(channel.wave=channel.wave, new.notes.path=new.notes.path))
}