package net.paulhertz.pixelaudio.example;

import processing.core.PApplet;
import processing.core.PImage;
import net.paulhertz.pixelaudio.*;

public class Starter extends PApplet {
	PixelAudio pixelaudio;        // the library that we need to load in setup()
	HilbertGen hGen;              // a PixelMapGen child class for making Hilbert curves
	PixelAudioMapper mapper;      // a class that uses a PixelMapGen ("gen", for short) to map audio samples to RGB pixels
	PImage mapImage;              // a bitmap image for display
	int[] spectrum;               // an array of colors that will be written along the "signal path" in mapImage
	

	public static void main(String[] args) {
		PApplet.main(new String[] { Starter.class.getName() });
	}

	public void settings() {
		  size(512, 512);                          // Hilbert curves require dimensions that are powers of 2. 		
	}

	// PixelAudio sketches typically require these variables
	public void setup() {
	  pixelaudio = new PixelAudio(this);           // 1. initialize PixelAudio library
	  hGen = new HilbertGen(width, height);        // 2. create a PixelMapGen: here it's the Hilbert curve generator
	  mapper = new PixelAudioMapper(hGen);         // 3. initialize a PixelAudioMapper object with the gen
	  mapImage = createImage(width, height, RGB);  // 4. create an image for display
	  // At this point, we might load audio, but we'll leave that for later.
	  // Right now, let's just look at the pattern the Hilbert curve makes in the display image.
	  // We'll do this frequently in example code, since it shows how an audio signal maps to the image. 
	  mapImage.loadPixels();
	  spectrum = getColors(mapper.getSize());
	  // To show the colors, we call mapper.plantPixels(...). Mapper knows the geometry of the 
	  // signal path generated by hGen. PlantPixels(...) copies an array of RGB colors to the signal path
	  // of a supplied image pixels array: the colors array leaves a rainbow color trail.
	  mapper.plantPixels(spectrum, mapImage.pixels, 0, mapper.getSize());
	  mapImage.updatePixels();
	}

	/**
	 * Creates an array of colors in rainbow (spectrum) order.
	 * @param size    size of the desired array
	 * @return an array of colors in rainbow (spectrum) order
	 */
	public int[] getColors(int size) {
	  int[] colorWheel = new int[size];
	  pushStyle();
	  colorMode(HSB, colorWheel.length, 100, 100);
	  int h = 0;
	  for (int i = 0; i < colorWheel.length; i++) {
	    colorWheel[i] = color(h, 66, 66);
	    h++;
	  }
	  popStyle();
	  return colorWheel;
	}

	public void draw() {
	  image(mapImage, 0, 0);
	}

	public void keyPressed() {
	  switch(key) {
	  case 'h':
	    println("---- no help whatsoever ----");
	    break;
	  default:
	    break;
	  }
	}	
	
}
