package com.haxademic.core.media.audio.playback;

import java.util.ArrayList;
import java.util.HashMap;

import com.haxademic.core.app.P;
import com.haxademic.core.file.FileUtil;
import com.haxademic.core.math.MathUtil;
import com.haxademic.core.media.audio.interphase.Scales;

public class DroneSampler {
	
	protected String audioDir;
	protected String[] audioFiles;
	protected WavPlayer activePlayer;
	protected HashMap<String, DroneSamplerLoop> droneLoops = new HashMap<String, DroneSamplerLoop>();
	protected int soundIndex = -1;
	protected int loopInterval = 15000;
	protected int loopLastStartTime = -loopInterval;
	
	
	public DroneSampler(String audioDir, float loopIntervalSeconds) {
		this.audioDir = audioDir;
		this.loopInterval = P.round(loopIntervalSeconds * 1000); // ms
		P.out("DroneSampler loading sounds from:", this.audioDir);
		loadSounds();
	}
	
	protected void loadSounds() {
		// load audio directory
		ArrayList<String> sounds = FileUtil.getFilesInDirOfTypes(FileUtil.getPath(audioDir), "wav,aif");
		audioFiles = new String[sounds.size()];
		for (int i = 0; i < sounds.size(); i++) {
			audioFiles[i] = sounds.get(i);
			P.out("Loading...", audioFiles[i]);
		}
	}
	
	protected void startNextSound() {
		// kill old players
		killOldPlayers();
		// go to next index & play next sound!
		soundIndex = (soundIndex < audioFiles.length - 1) ? soundIndex + 1 : 0;	
		String nextSoundId = audioFiles[soundIndex];
		startPlayer(nextSoundId);
	}
	
	protected void killOldPlayers() {
		for (HashMap.Entry<String, DroneSamplerLoop> entry : droneLoops.entrySet()) {
			// ramp down old players halfway through interval
			DroneSamplerLoop synthLoop = entry.getValue();
			if(synthLoop.active() && P.p.millis() - synthLoop.startTime() > loopInterval/2) {
				synthLoop.release();
			}
		}
	}
	
	protected void startPlayer(String id) {
		// lazy-init SynthLoop
		if(droneLoops.containsKey(id) == false) {
			droneLoops.put(id, new DroneSamplerLoop(id));
		}
		// get pitch
		int newPitch = Scales.SCALES[0][MathUtil.randRange(0, Scales.SCALES[0].length - 1)];
		// play!
		droneLoops.get(id).start(newPitch);
	}
	
	protected void checkNextSoundInterval() {
		if(P.p.millis() > loopLastStartTime + loopInterval) {
			loopLastStartTime = P.p.millis();
			startNextSound();
		}
	}
	
	public void update() {
		checkNextSoundInterval();
		for (HashMap.Entry<String, DroneSamplerLoop> entry : droneLoops.entrySet()) {
//			String id = entry.getKey();
			DroneSamplerLoop synthLoop = entry.getValue();
			synthLoop.update();
		}
	}
}
@@ -1223,50 +1223,8 @@
 ) %7B%0A
-%09%09// kill old players%0A%09%09killOldPlayers();%0A
 %09%09//
@@ -1260,16 +1260,16 @@
  sound!%0A
+
 %09%09soundI
@@ -1435,12 +1435,15 @@
 oid 
-kill
+release
 OldP
@@ -1820,17 +1820,20 @@
 nit 
-SynthLoop
+DroneSampler
 %0A%09%09i
@@ -1939,13 +1939,29 @@
 get 
-pitch
+random pitch and play
 %0A%09%09i
@@ -2048,19 +2048,8 @@
 )%5D;%0A
-%09%09// play!%0A
 %09%09dr
@@ -2253,16 +2253,210 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09protected void updateLoops() %7B%0A%09%09for (HashMap.Entry%3CString, DroneSamplerLoop%3E entry : droneLoops.entrySet()) %7B%0A%09%09%09DroneSamplerLoop droneLoop = entry.getValue();%0A%09%09%09droneLoop.update();%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09public 
@@ -2471,16 +2471,39 @@
 ate() %7B%0A
+%09%09releaseOldPlayers();%0A
 %09%09checkN
@@ -2528,195 +2528,22 @@
 ;%0A%09%09
-for (HashMap.Entry%3CString, DroneSamplerLoop%3E entry : droneLoops.entrySet()) %7B%0A//%09%09%09String id = entry.getKey();%0A%09%09%09DroneSamplerLoop synthLoop = entry.getValue();%0A%09%09%09synthLoop.update();%0A%09%09%7D
+updateLoops();
 %0A%09%7D%0A

Update DroneSampler.java