// minim is required to generate audio
import ddf.minim.*; 
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

import processing.video.*;


// import XYscope
import xyscope.*;

import themidibus.*;
MidiBus bus;

int    master_control = 0;
boolean accueil_loaded = false;
boolean decollage_loaded = false;
boolean dimension_warp_loaded = false;
boolean pause_loaded = false;
boolean radar_loaded = false;
boolean fin_loaded = false;
float myTime;

Minim minim;
AudioPlayer player;
AudioPlayer player2;

int bruitage_count = 0;
boolean intro = true;

Movie movie;

void setup()
{
    size(1280, 720, P3D);
    bus.list();
    bus = new MidiBus(this, 1, 0);
    minim = new Minim(this);
    player = minim.loadFile("accueil.mp3");
    player.loop();
    player2 = minim.loadFile("decollage_bruitage.mp3");

    setup_terrain_generation();
    setup_polyphonic_synth();
    setup_monophonic_synth();
    setup_newton_fractal();
    setup_radar();
    
    movie = new Movie(this, "video_terre.m4v");
}

void movieEvent(Movie m) {
  m.read();
}

void draw()
{
    background(0);
    if (intro)
    {
        if (master_control == 0)
        {
            if (!accueil_loaded)
            {
                player.pause();
                player = minim.loadFile("accueil.mp3");
                player.loop();
                player.setGain(-3);
                accueil_loaded = true;
                decollage_loaded = false;
                dimension_warp_loaded = false;
                pause_loaded = false;
                radar_loaded = false;
                fin_loaded = false;
            }
            draw_terrain_generation();
        }
        if (master_control == 1)
        {
            if (!decollage_loaded)
            {
                player.pause();
                player = minim.loadFile("decollage.mp3");
                player.loop();
                accueil_loaded = false;
                decollage_loaded = true;
                dimension_warp_loaded = false;
                pause_loaded = false;
                radar_loaded = false;
                fin_loaded = false;
            }
            draw_terrain_generation();
        }
        if (master_control == 2)
        {
            if (!decollage_loaded)
            {
                player.pause();
                player = minim.loadFile("decollage.mp3");
                player.loop();
                accueil_loaded = false;
                decollage_loaded = true;
                dimension_warp_loaded = false;
                pause_loaded = false;
                radar_loaded = false;
                fin_loaded = false;
            }
            draw_polyphonic_synth();
        }
        if (master_control == 3)
        {
            if (!pause_loaded)
            {
                player.pause();
                accueil_loaded = false;
                decollage_loaded = false;
                dimension_warp_loaded = false;
                pause_loaded = true;
                radar_loaded = false;
                fin_loaded = false;
            }
            draw_monophonic_synth();
        }
        if (master_control == 4)
        {
            if (!dimension_warp_loaded)
            {
                player.pause();
                player = minim.loadFile("dimension_warp.mp3");
                player.play();
                player.setGain(-9);
                accueil_loaded = false;
                decollage_loaded = false;
                dimension_warp_loaded = true;
                pause_loaded = false;
                radar_loaded = false;
                fin_loaded = false;
                myTime = millis() / float(1000);
            }
            draw_newton_fractal();
        }
        if (master_control == 5)
        {
            if (!radar_loaded)
            {
                player.pause();
                player = minim.loadFile("radar_bruitage.mp3");
                player.play();
                accueil_loaded = false;
                decollage_loaded = false;
                dimension_warp_loaded = false;
                pause_loaded = false;
                radar_loaded = true;
                fin_loaded = false;
                myTime = millis();
            }
            draw_radar();
        }
    }
    else
    {
        if (master_control == 0)
        {
            if (!decollage_loaded)
            {
                player.pause();
                player = minim.loadFile("decollage.mp3");
                player.loop();
                accueil_loaded = false;
                decollage_loaded = true;
                dimension_warp_loaded = false;
                pause_loaded = false;
                radar_loaded = false;
                fin_loaded = false;
            }
            draw_terrain_generation();
        }
        if (master_control == 1)
        {
            if (!fin_loaded)
            {
                player.pause();
                player = minim.loadFile("video_terre.mp3");
                movie.play();
                player.play();
                player.setGain(0);
                accueil_loaded = false;
                decollage_loaded = false;
                dimension_warp_loaded = false;
                pause_loaded = false;
                radar_loaded = false;
                fin_loaded = true;
            }
            draw_end();
        }
    }
}
