Controlling the audio volume of a music computer from OpenRemote

I have been controlling the volume of of my music computer using an OpenRemote slider for some time.

I have OpenRemote commands for getting and setting the volume, an OpenRemote sensor for the volume, and a slider for setting the volume.

I send the commands over TCP/IP to ORControl running on localhost. ORControl then sends them to the music computer.

The ORMusicControl Java program on the music computer sets the volume by:


private void setVolume(int vol) {	
    Mixer.Info[] infos = AudioSystem.getMixerInfo();  
    for (Mixer.Info info: infos)  
    {  
        Mixer mixer = AudioSystem.getMixer(info);  
        if (mixer.isLineSupported(Port.Info.SPEAKER))  
        {  
            Port port;
	    try {
		port = (Port)mixer.getLine(Port.Info.SPEAKER);
                port.open();
                if (port.isControlSupported(FloatControl.Type.VOLUME))  
                {  
                    FloatControl volume = 
                               (FloatControl)port.getControl(FloatControl.Type.VOLUME);  
                    volume.setValue(vol/ 100f);
                }  
                port.close();
	    } catch (LineUnavailableException e) {
		e.printStackTrace();
	    }     
        }  
    }
}

This code should be portable, but it does not seem to run on the latest versions on Windows. Luckily, my music computer runs Windows XP, so it is not a problem. I will need to write JNI or JNA code if ever want to run this on a recent version of Windows, unless Java gets fixed.

The code for getting the volume is:


private int getVolume() {	
    Mixer.Info[] infos = AudioSystem.getMixerInfo();  
    for (Mixer.Info info: infos)  
    {  
        Mixer mixer = AudioSystem.getMixer(info);  
        if (mixer.isLineSupported(Port.Info.SPEAKER))  
        {  
            Port port;
            try {
		port = (Port)mixer.getLine(Port.Info.SPEAKER);
	        port.open();
	        if (port.isControlSupported(FloatControl.Type.VOLUME))  
	        {  
	            FloatControl volume = 
                        (FloatControl)port.getControl(FloatControl.Type.VOLUME);  
	            return (int) (volume.getValue() * 100f) ;
	        }  
	        port.close();
	    } catch (LineUnavailableException e) {
		e.printStackTrace();
            }     
        }  
    }
    return 0;
}
Advertisements
This entry was posted in Electronics, Home automation. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s