/**
 * Copyright (C) 2015 Marian Tietz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact:
 * vqvad@nemo.ikkoku.de
 */
package VQVAD;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;

import Utils.Player;

import Frontend.VQVoiceActivityDetector;
import Frontend.VoiceActivityDetector;

public class PlaybackTest {

	private static AudioInputStream copyAis(AudioInputStream source, AudioFormat format) {
		boolean run = true;
		int buffer_size = 4000;
		byte tempBuffer[] = new byte[buffer_size];
		ByteArrayOutputStream debugStream = new ByteArrayOutputStream();

		while (run) {
			int cnt = -1;
			try {//read data from the audio input stream
				cnt = source.read(tempBuffer, 0, buffer_size);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (cnt > 0) {//if there is data
				try {
					debugStream.write(tempBuffer);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				run = false;
			}
		}

		return new AudioInputStream (new ByteArrayInputStream(debugStream.toByteArray()), format, debugStream.size());
	}

	/**
	 * @param args
	 * @throws MalformedURLException
	 */
	public static void main(String[] args) throws MalformedURLException {

		//URL path = new URL("file:///home/nemo/Documents/Studium/Master/study/code/VQVAD/foo.wav");
		//URL path = new URL("file:///home/nemo/Documents/Studium/Master/study/code/VQVAD/trainset/noise_only.wav");

		//URL path = new URL("file:///home/nemo/Documents/Studium/Master/study/code/VQVAD/trainset/noizeus_train/train_10dB/sp12_train_sn10.wav");
		//URL path = new URL("file:///home/nemo/Documents/Studium/Master/study/code/VQVAD/trainset/noizeus_train/train_5dB/sp12_train_sn5.wav");
		//URL path = new URL("file:///home/nemo/Documents/Studium/Master/study/code/VQVAD/trainset/noizeus_train/car_10dB/sp12_car_sn10.wav");
		//URL path = new URL("file:///home/nemo/Documents/Studium/Master/study/code/VQVAD/trainset/sp12_train_sn10_then_noise_only.wav");
		//URL path = new URL("file:///home/nemo/Documents/Studium/Master/study/code/VQVAD/trainset/sp12_train_sn10_preceeding_noise.wav");
		URL path = new URL("file:///home/nemo/Documents/Studium/Master/study/code/VQVAD/trainset/sp12_train_sn10_preceeding_noise_sp12_train_sn10_again.wav");

		VQVoiceActivityDetector vac = new VQVoiceActivityDetector(path, 8000, "foo");
		//VoiceActivityDetector vac = new VoiceActivityDetector(path, 8000, "foo");

		AudioInputStream debugAis = copyAis(vac, vac.getFormat());

		Player.playStream(debugAis);
	}

}

@@ -828,32 +828,103 @@
 ayOutputStream;%0A
+import java.io.FileNotFoundException;%0Aimport java.io.FileOutputStream;%0A
 import java.io.I
@@ -996,16 +996,60 @@
 t.URL;%0A%0A
+import javax.sound.sampled.AudioFileFormat;%0A
 import j
@@ -1126,16 +1126,60 @@
 tream;%0A%0A
+import com.sun.media.sound.WaveFileWriter;%0A%0A
 import U
@@ -2152,36 +2152,26 @@
 %09 * @throws 
-MalformedURL
+IO
 Exception%0A%09 
@@ -2220,28 +2220,18 @@
  throws 
-MalformedURL
+IO
 Exceptio
@@ -2985,34 +2985,32 @@
 e_only.wav%22);%0A%09%09
-//
 URL path = new U
@@ -3116,32 +3116,34 @@
 _noise.wav%22);%0A%09%09
+//
 URL path = new U
@@ -3499,16 +3499,237 @@
 at());%0A%0A
+%09%09WaveFileWriter w = new WaveFileWriter();%0A%09%09FileOutputStream of = new FileOutputStream(%22/home/nemo/Documents/Studium/Master/study/test_out.wav%22);%0A%0A%09%09w.write(debugAis, AudioFileFormat.Type.WAVE, of);%0A%09%09debugAis.reset();%0A%0A
 %09%09Player

Write test output to some location as well