Code viewer for World: AI music

 
// program to construct random "tune" from small "notes"


  
// === Edit this section to define notes, tune ==============================================================

// the note files:
const notefiles = 
    [
        // 1 second or less:
        "/uploads/mathias/perc.wav",        // wooden beat          // notes[0]
        "/uploads/mathias/snap.wav",        // click                // notes[1]
        "/uploads/mathias/snare.wav",       // tight like beat      // notes[2]
        "/uploads/mathias/kick.wav",        // bounce drum          // notes[3]
        "/uploads/mathias/hihat.wav",       // tight click          // notes[4]
        "/uploads/mathias/clap.wav",        // clap                 // notes[5]
        "/uploads/oneilf27/beepmult.mp3",   // Beep                 // notes[6]
        
        // 3 seconds:
        "/uploads/mathias/crash.wav",       // cymbals crash        // notes[7]
        
        // 5 minutes:
        // "/uploads/mathias/kalimba.mp3",
    ];
    
    
    
function playTune()
{
  
// background music    
const MUSICFILE = "uploads/midnightsky/Interstellar.mp3";
AB.backgroundMusic(MUSICFILE); 

// play a note n times with gap of this no. of milliseconds:
//    repeatNote ( randomNote(), 4, 500 );


/*
// design a tune:
    repeatNote ( notes[2], 100, 200 );
    repeatNote ( notes[6], 100, 300 );
   
// this kicks in in n millisec time: 
   delayRepeatNote ( 7000, notes[2], 100, 300 );
   delayRepeatNote ( 9000, notes[6], 100, 300 );
*/
 

 
// entirely random tune:  

   for ( var i = 1; i <= 10; i++ ) 
   {
       delayRepeatNote (  AB.randomIntAtoB(0,20000),
                        randomNote(), 
                        AB.randomIntAtoB(50,200), 
                        AB.randomIntAtoB(50,500)       );
   }
   
}

// ==== End of edit section =================================================================================================





  
  
// === Can probably ignore below here ==============================================================
  
  
var NUM = notefiles.length; 
  
// going to set up an array of Audio objects:   
var notes = [];   
   
   
function initNotes()
{
        for ( var i = 0; i < NUM; i++ ) 
        {
           notes[i] = new Audio ( notefiles[i] );
		   // notes[i].play();	
		   // notes[i].pause();		   
        } 	
}

function randomNote()
{
  var i = AB.randomIntAtoB ( 0, NUM - 1 );
  return ( notes[i] );
}
     
     
     var p = [];     // the notes that are playing 
                    // an array of arrays: 
                    // ( note, num, current num, millisec )
     
  
function repeatNote ( note, n, millisec )
// repeat this note n times, with delays of millisec in between 
{
    // add one to end:
    var i = p.length;
    p[i] = [ note, n, 1, millisec ];
     
   
  p[i][0].onended = function() 
  {
    if ( p[i][2] < p[i][1] )
    {
      p[i][2] ++;
      setTimeout ( function() { p[i][0].play(); }, p[i][3] );
    }
    else
     console.log ( "finished " + p[i][0].src );
  };
  
  console.log ( "starting " + p[i][0].src ); 
  p[i][0].play();
}

   
  function delayRepeatNote ( delay, note, n, millisec ) 
  // delayed start of repeatNote, in "delay" milliseconds time 
  {
      setTimeout ( 
        function() {   repeatNote ( note, n, millisec  );   },
        delay );
  }
 
        
// display splash screen  
	
	AB.newSplash ( "Click to start audio" );	
	

// touch on splash screen button will mark audio objects as good for JS to call without further user interaction  

	AB.splashClick ( function ()        
	{	   
		AB.removeSplash();			  
		splashClicked = true;
		AB.runReady = true;  		 
 
	    initNotes();
        setTimeout ( playTune, 0 );     // play the tune, maybe delayed 
	});
		
		

// not sure if will use 3D world  

const skycolor          = 'LightBlue';          // sky color

const startRadius       = 1000;                 // distance from centre we start the camera at
const maxRadius         = startRadius * 10;     // maximum distance from camera we render things 

	
AB.world.newRun = function() 
{
    ABWorld.init3d ( startRadius, maxRadius, skycolor ); 
};