var carrier; // this is the carrierillator we will hear
var modulator; // this carrierillator will modulate the amplitude of the carrier
var fft; // we'll visualize the waveform

var opac;
var carrier;



function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}




function setup() {
	

var myCanvas = createCanvas(innerWidth,100);
    myCanvas.parent("canvasp5");
	
  
  noFill();
 

	//SETING FIRST OSCILLATOR: CARRIER
// EXPLANATION FROM WEBSITE: The carrier is typically set at an audible frequency (i.e. 440 Hz) and connected to master output by default. The carrier.amp is set to zero because we will have the modulator control its amplitude.
 
	var randomnumb = getRandomArbitrary(0, 20);

	console.log(randomnumb);
	carrier = new p5.Oscillator(); // connects to master output by default
  carrier.freq(200 + randomnumb); // it sets the frequency of the carrier. AN AUDIBLE ONE.
  carrier.amp(1);
  // carrier's amp is 0 by default, giving our modulator total control

  carrier.start();


	

  // create an fft to analyze the audio
	//an FFT (fast Fourier transform) converts a signal from its original domain (often time or space) to a representation in the frequency domain and vice versa
	
  fft = new p5.FFT(); 
}




function draw() {
	

	
	
var modAmp= 1;
	
	// si el mouseY és igual o el mateix que la meitat de l'altura, fes aixo. ((DEFINEIX LA AMPLITUD.))
	/*	if(mouseY <= height) {
			
  			var modAmp = map(mouseY, 0, height, 0.01, 2.02);
			
		}else{
		
			var modAmp = map(mouseY, height, 0, 0.01, 2.02);			

		
		}
	*/
	carrier.amp(modAmp, 0.01); // fade time of 0.1 for smooth fading


  // analyze the waveform
  waveform = fft.waveform();


			background(255,255,255,50); // alpha

		  // draw the shape of the waveform
		drawWaveform();

	}
	
	
	




function drawWaveform() {
 

 	stroke(0,0,0,100);
	strokeWeight(0.5);
  beginShape();
  for (var i = 0; i<waveform.length; i++){
    var x = map(i, 0, (waveform.length)/8, 0, width);
    var y = map((waveform[i])/8, -1, 1, -height/2, height/2);
    vertex(x, y + 100);
  }
  endShape();

}








