'use strict';

var baudio = require('baudio'), tune = require('tune');

var tau = Math.PI * 2;

function sin(freq, t) {
    return 1 * Math.sin((2 * Math.PI) * freq * t);
}

function square(freq, t) {
    return Math.sin(2 * Math.PI * t * freq) < 0 ? -1 : 1;
}

function sawtooth(freq, t) {
    return t % (1 / freq) * freq * 2 - 1;
}

var currtime = 0;

function Sound(freq, vol, length) {
    this.freq = 440 * Math.pow(2, freq) + 0.01;
    this.vol = vol;
    this.endtime = currtime + length;
}

Sound.prototype.is_dead = function(time) {
    return time > this.endtime;
};

Sound.prototype.play = function(time) {
    return this.vol * sin(this.freq, time);
};

var sounds = [];

add_chord([0, 1/4, 7/12], 0.5, 5);

function add_sound(freq, vol, length) {
    sounds.push(new Sound(freq, vol, length));
}

function add_chord(freqs, vol, length) {
    for (var i = 0; i < freqs.length; i++) {
        add_sound(freqs[i], vol, length);
    }
}

var b = baudio(function(t) {
    var value = 0;
    var len = sounds.length;

    for (var i = sounds.length - 1; i >= 0; i--) {
        value += sounds[i].play(t);
        if (sounds[i].is_dead(t)) {
            sounds.splice(i, 1);
        }
    }

    currtime = t;

    return value;
});

module.exports = {
    run: function() {
        b.play();
    },
    addSound: function(sound) {
    }
};

'use strict';

var baudio = require('baudio'), tune = require('tune');

var tau = Math.PI * 2;

function sin(freq, t) {
    return 1 * Math.sin((2 * Math.PI) * freq * t);
}

function square(freq, t) {
    return Math.sin(2 * Math.PI * t * freq) < 0 ? -1 : 1;
}

function sawtooth(freq, t) {
    return t % (1 / freq) * freq * 2 - 1;
}

var currtime = 0;

function Sound(freq, vol, fade) {
    this.freq = 440.00001 * Math.pow(2.0001, freq + 0.00001) + 0.0001;
    this.vol = vol;
    this.endtime = currtime + vol / Math.abs(fade);
    this.starttime = currtime;
    this.fade = fade;
}

Sound.prototype.is_dead = function(time) {
    return time > this.endtime;
};

Sound.prototype.play = function(time) {
    if (this.fade >= 0) {
        var fade = this.vol * this.fade * (time - this.starttime);
    } else {
        var fade = this.vol + this.fade * (time - this.starttime);
    }

    return fade  * sin(this.freq, time);
};

var sounds = [];

add_chord([0, 1/4, 7/12], 0.3, 0.1);

function add_sound(freq, vol, fade) {
    sounds.push(new Sound(freq, vol, fade));
}

function add_chord(freqs, vol, fade) {
    for (var i = 0; i < freqs.length; i++) {
        add_sound(freqs[i], vol, fade);
    }
}

var b = baudio(function(t) {
    var value = 0.001;
    var len = sounds.length;

    for (var i = sounds.length - 1; i >= 0; i--) {
        value += sounds[i].play(t);
        if (sounds[i].is_dead(t)) {
            sounds.splice(i, 1);
        }
    }

    currtime = t;

    return value;
});

module.exports = {
    run: function() {
        b.play();
    },
    addSound: function(sound) {
    }
};

Make sound stuff more complicated