/*
* rhythmspace (c) 2008 Alex McLean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import flash.Sound;
import flash.Mouse;
class Audio {
static var bpm = 400;
static var start = 0.0;
static var ticks = 0;
static var buffer = 0.2;
static var patterns = [[0], [0]];
static var playbuf = [[], []];
static var down = false;
static var startx = 0.0;
static var starty = 0.0;
static function repeat(sounda, soundb) {
var now;
if (playbuf[0].length == 0) {
playbuf[0] = patterns[0].copy();
playbuf[1] = patterns[1].copy();
}
if (start == 0) {
start = haxe.Timer.stamp();
}
if (playbuf[0].shift() == 1) {
now = start + (ticks * (60 / bpm));
sounda.start(0.5 - (now - haxe.Timer.stamp()) - buffer, 1.0);
}
if (playbuf[1].shift() == 1) {
now = start + (ticks * (60 / bpm));
soundb.start(0.5 - (now - haxe.Timer.stamp()) - buffer, 1.0);
}
ticks++;
now = start + (ticks * (60 / bpm));
var delay : Float;
delay = now - haxe.Timer.stamp();
delay = delay * 1000;
haxe.Timer.queue(function(){repeat(sounda, soundb);}, Std.int(delay));
}
static function load_and_play_sound(urla, urlb) {
var sounda = new Sound(flash.Lib.current);
var soundb = new Sound(flash.Lib.current);
// should check if both are loaded really,
// but how to deal with the race condition?
soundb.onLoad = function(success) {
repeat(sounda, soundb);
}
sounda.loadSound(urla, false);
soundb.loadSound(urlb, false);
}
static function mouseTrack() {
var mc : flash.MovieClip = flash.Lib.current;
var tracker = {
onMouseMove : function() {
if (down) {
var x : Float = mc._xmouse;
var y : Float = mc._ymouse;
mc.clear();
mc.lineStyle(3, 0xFF0000);
mc.moveTo(startx,starty);
mc.lineTo(x,y);
var x : Float = mc._xmouse / 400;
var y : Float = mc._ymouse / 300;
var z = 0;
patterns[1] = get_rhythm(8, x, y, z);
}
},
onMouseDown : function() {
startx = mc._xmouse;
starty = mc._ymouse;
mc.clear();
mc.lineStyle(3, 0xFF0000);
mc.moveTo(startx,starty);
mc.lineTo(startx,starty);
var x : Float = mc._xmouse / 400;
var y : Float = mc._ymouse / 300;
var z = 0;
down = true;
patterns[0] = get_rhythm(8, x, y, z);
},
onMouseUp : function() {
var x : Float = mc._xmouse / 400;
var y : Float = mc._ymouse / 300;
var z = 0;
down = false;
patterns[1] = get_rhythm(8, x, y, z);
}
};
flash.Mouse.addListener(tracker);
}
static function mask(rhythm : Array, n : Float) {
var toggle = 1;
if (n < 0) {
toggle = 0;
n = 0 - n;
}
while(n > 0) {
var count = 0;
for (i in 0 ... rhythm.length) {
var val : Int = rhythm[i];
if (val == toggle) {
count++;
}
}
var point = Std.int(count / 2);
var j;
for (j in 0 ... rhythm.length) {
if(rhythm[j] == toggle) {
if (point == 0) {
rhythm[j] = (toggle == 1) ? 0 : 1;
break;
}
point--;
}
}
--n;
}
return(rhythm);
}
static function tri(n) {
var result = 0;
var i;
for (i in 0 ... (n + 1)) {
result += i;
}
return(result);
}
static function jumble(len : Int, disorder : Float) {
var half = Std.int(len / 2);
var i;
var rhythm : Array = [];
for (i in 0 ... len) {
rhythm.push(i % 2 == 0 ? 1 : 0);
}
var steps = tri(half - 1) + 1;
var strength = steps * disorder;
var offset = 1;
while(offset < half && strength > 0) {
var p = offset;
while (p < (len - 1) && strength > 0) {
var tmp = rhythm[p];
rhythm[p] = rhythm[p + 1];
rhythm[p + 1] = tmp;
p = p + 2;
strength--;
}
offset++;
}
return(rhythm);
}
static function get_rhythm(len : Int, intensity : Float, disorder : Float, phase : Float) {
intensity = intensity * len;
var rhythm = jumble(len, disorder);
if (Std.int(intensity) != Std.int(len / 2)) {
rhythm = mask(rhythm, intensity - (len / 2));
}
return(rhythm);
}
static function main() {
var root = flash.Lib._root;
mouseTrack();
load_and_play_sound(root.soundfilea, root.soundfileb);
}
}