
'use strict'

let clock = function(start) {
  if ( !start ) return process.hrtime()
  var end = process.hrtime(start)
  return Math.round((end[0]*1000) + (end[1]/1000000))
}

let _ = require('./gen_new.js')
let Gibberish = require('./gibberish.min.js' )

let add = _.add, 
    gen = _.gen,
    mul = _.mul,
    bus = _.bus,
    abs = _.abs,
    sin = _.sin,
    PI  = _.PI,
    twoPI = PI * 2,
    param = _.param,
    accum = _.accum,
    table = _.table,
    sampleRate = _.SR

let samplesToTest = 44100 * 60 // 60 seconds

let start, duration

/* HERE ARE THE IMPORTANT LINES */
let frequency = param( 440 )

// gen.graph = sin( mul( accum( mul( 1 / sampleRate, frequency ) ), twoPI ) )
//let graph = gen.graph = bus( mul( table( 440 ), 1), mul( table( 220 ), 1), mul( table( 330 ),1), mul( table( 550 ), 1) )
//let graph = gen.graph = bus( table( 440 ), table( 220 ), table( 330 ), table( 550 ) )

let table1 = new table(440),
    table2 = new table(220),
    table3 = new table(330),
    table4 = new table(550)

// let wavetable1 = new wavetable(),
//     wavetable2 = new wavetable(),
//     wavetable3 = new wavetable(),
//     wavetable4 = new wavetable()

Gibberish.context = { sampleRate: 44100 }

let s1 = new Gibberish.Sine( 440 ),
    s2 = new Gibberish.Sine( 220 ),
    s3 = new Gibberish.Sine( 330 ),
    s4 = new Gibberish.Sine( 550 ),
    gbus = new Gibberish.Bus()

start = clock()

for( let i = 0; i < samplesToTest; i++ ) {
  //let out = gbus.callback( s1.callback( 440, 1 ), s2.callback( 220, 1 ), s3.callback( 330, 1 ), s4.callback( 550, 1 ), 1 )
  let out = s1.callback(440,1) + s2.callback(440,1) + s3.callback(440,1) + s4.callback(440,1)
}

duration = clock( start )

console.log( 'gibberish took ' + duration + ' ms.' )

start = clock()

let mygraph = mul( bus( table1, table2, table3, table4 ), .5 )
//console.log( mygraph )

for( let i = 0; i < samplesToTest; i++ ) {
  //let out = graph.out()
  // let out = table1.sample() + table2.sample() + table3.sample() + table4.sample()
  let out = mygraph.sample()
  //let out = table1() + table2() + table3() + table4()
  //let out = wavetable1.callback(440,1) + wavetable2.callback(330,1) + wavetable3.callback(220,1) + wavetable4.callback(550,1)

}

duration = clock( start )

console.log( 'gen~ took ' + duration + ' ms.' )