# load required libraries
library(BBmisc)
library(tuneR)
library(R.matlab)


source("makeTrack.R")
source("HelpFunctions.R")
source("extractNote.R")
source("midi2wave.R")
source("readwave2.R")
source("MakeRealConverterControl.R")
source("readMidiMatlab.R")




# Example 1:  monophonic flute


event.frame=readMidiMatlab("Flute1", "//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/Datenbank/Klassik")

ctrl=RealConverterControl(channels=unique(event.frame$channel.number),
                                instruments="flute",
                                sample.instrument="sample.instrument",
                                data.path="//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/RealConverterTones")

w=midi2wave(event.frame,ctrl)

w.o=w$wave.object
writeWave(wo,file="//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/Datenbank/Klassik/wav/trueFlute1.wav")
play(w.o)



# Example 2: flute and guitar

event.frame=readMidiMatlab("GuitarFlute1", "//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/Datenbank/Klassik")

# ckeck the channels
unique(event.frame$channel.number)
table(event.frame$channel.number)
#0   1 
#112 108 
# 0 is guitar 
# 1 is flute

ctrl=RealConverterControl(channels=unique(event.frame$channel.number),
                          instruments=c("guitar","flute"),
                          wave.to.channel = TRUE,
                          notes.path.flute = "//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/RealConverterTones/flute/tr331FL",
                          sample.instrument="sample.instrument",
                          data.path="//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/RealConverterTones")

w=midi2wave(event.frame,ctrl)
wo=w$wave.object
writeWave(wo,file="//STORE/share/LSWeihs/LSWeihsServer/DissBauer2015/Datenbank/Klassik/wav/trueGuitarFlute1.wav")


play(wo[[1]]$channel.wave) # flute
play(wo[[2]]$channel.wave) # guitare
play(wo[[3]]) # composite wave
