import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.tritonus.share.sampled.file.TAudioFileFormat;

/**
 * Demonstrates some problems with opening the weird .wav of Ollies.
 * 
 * @author ben
 *
 */


public class WavOpener {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnsupportedAudioFileException 
	 */
	public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
		// File file = new File("D:/audio/Jyske Rugkiks - Svartbag Remix M01.wav");
		// File file = new File("D:/audio/Jyske Rugkiks - Svartbag Remix M01convert.wav");
		File file = new File("audio/gammaBrosTheme.mp3");
		// File file = new File("audio/minimal.ogg"); // OGG NOT WORKING YET...
		AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file);		
		System.out.println(audioFileFormat);
		
		// check if .wav ending and detected as .mp3, if so it is bad!		
		if (!file.getName().endsWith(".mp3") && audioFileFormat instanceof TAudioFileFormat)
		{
			System.out.println("Ooops");
			System.exit(1);
			
			/*
		    Map properties = ((TAudioFileFormat)baseFileFormat).properties();
		    String key = "author";
		    String val = (String) properties.get(key);
		    key = "mp3.id3tag.v2";
		    InputStream tag= (InputStream) properties.get(key);
		    */
		}
		
		
//		System.out.printf("File size: %d\n",file.getTotalSpace());
		
		// continue 
		
		AudioInputStream encodedStream = null, decodedStream = null;
		AudioFormat encodedFormat, decodedFormat;		
		if (file.exists()) encodedStream = AudioSystem.getAudioInputStream(file);
		encodedFormat = encodedStream.getFormat();
		decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
				encodedFormat.getSampleRate(),
				16,
				encodedFormat.getChannels(),
				encodedFormat.getChannels()*2, // 2*8 = 16-bits per sample per channel
				44100,
				encodedFormat.isBigEndian());

		Map<String,Object> audioInfo = decodedFormat.properties();
		
		if (AudioSystem.isConversionSupported(decodedFormat, encodedFormat))
		{
			decodedStream = AudioSystem.getAudioInputStream(decodedFormat, encodedStream);
			System.out.println("Decoding...");	
			
			int lengthInBytes = audioFileFormat.getByteLength();
			audioInfo = audioFileFormat.properties();
			/*
			double length = getTimeLengthEstimation(audioInfo);
			if (length<0)
			{
				System.out.println("Beads cannot determine the duration of the file " + file.getAbsolutePath() + " - is it missing the duration tag?\n");
				System.exit(1);
			}
			else
			{
				nFrames = (long)(decodedFormat.getSampleRate() * (length/1000.));
			}			
			*/
		}
		else			
		{
			System.out.println("Input not encoded, or cannot decode!");	
		}
	}
}
