/**
 * Written by SaEeD
 * Description : Changes the Valume of sound devices
 * 
 * #### NOTE:  THIS APPLICATION HAS BEEN TESTED UNDER WINDOWS XP ONLY ########
 */


import javax.sound.sampled.*;
import java.io.*;
//Complete Details: http://forum.java.sun.com/thread.jspa?threadID=576721&start=15
public class Volume 
{
    static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    static float x;
   public static void main(String [] args)
   {
		System.out.println("Please enter volume value: [1-100]: " );
       Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); //Bedast ovordan Mixer ha
       Mixer myMixer; // more details: http://java.sun.com/docs/books/tutorial/sound/controls.html
       String input;
       try{
       input = stdin.readLine();
       int l = Integer.parseInt(input);
       x = l/100f;
       System.out.println("X= " + x);
       }catch(IOException e){System.out.println(e.getMessage());}
       
       for(int i=0;i< mixerInfo.length;i++) //match kardan mixer be etelalate bedast ovordeye mixer ha
       {
           System.out.println("Mixer Info: " + mixerInfo[i]);
           myMixer = AudioSystem.getMixer(mixerInfo[i]);
           /*Control[] controls = myMixer.getControls();
           System.out.println("--->" + controls.length + "<---");
           
           if(controls.length != 0)
           {
               System.out.println("[+]" + controls[0]);
           }
           */
           Line.Info[] targetLineInfo = myMixer.getTargetLineInfo(); //bedast ovordan line haye ghabele estefade
           System.out.println("[+]Target Lines: " + targetLineInfo.length);
           for(int j=0; j < targetLineInfo.length ; j++)
           {
               try{
               Line.Info lineInfo = targetLineInfo[j]; //Test kardan Line haye bedast omade baraye khoroji
               Line line = AudioSystem.getLine(lineInfo); //Conect kardan be Line
               line.open();
               System.out.println("Is Line Open? [True/False] :" + line.isOpen());
               FloatControl c = (FloatControl) line.getControl(FloatControl.Type.VOLUME);
               //Bedast ovordan Controller be khoroji line vasl shodeh
               c.setValue(x); 
               line.close();
           
               }
               catch(Exception e){
                   System.out.println("Error: " + e.getMessage());
               }
           }
       }
                  
   }
    
}