com.groovemanager.sampled
Class AudioManager

java.lang.Object
  extended by com.groovemanager.sampled.AudioManager
All Implemented Interfaces:
java.util.EventListener, org.eclipse.jface.util.IPropertyChangeListener

public class AudioManager
extends java.lang.Object
implements org.eclipse.jface.util.IPropertyChangeListener

This class can act as a central unit for using AudioRessources of different kinds. It provides some static methods as well as a default implementation, but it can also be subclassed. An AudioManager gets its settings for in and out device (Mixer) as well as the buffer size to use from a ConfigManager to which it is connected

Author:
Manu Robledo

Field Summary
protected  ConfigManager configManager
          The ConfigManager used by this instance.
static AudioManager DEFAULT_AUDIO_MANAGER
          The default AudioManager instance.
(package private)  java.util.ArrayList fileFormatProviders
          List of known AudioFileFormatProviders that will be queried for an AudioFileFormat when requested
protected  javax.sound.sampled.Mixer inMixer
          The input Mixer selected by the user or by the application
protected  javax.sound.sampled.Mixer outMixer
          The input Mixer selected by the user or by the application
(package private)  java.util.ArrayList outputStreamProviders
          List of known AudioFileOutputStreamProviders that will be queried for an AudioFileOutputStream when requested
 
Constructor Summary
protected AudioManager()
          Constructs a new Audiomanager using the default ConfigManager
  AudioManager(ConfigManager configManager)
          Constructs a new Audiomanager using the given ConfigManager
 
Method Summary
 boolean canWrite(javax.sound.sampled.AudioFileFormat format)
          Find out if a provider exists that can write files in the given AudioFileFormat
private static AudioManager createDefault()
          Create the default AudioManager instance
 javax.sound.sampled.AudioFileFormat getAudioFileFormat(java.io.File f, java.lang.String[] requiredProperties, java.lang.String[] desiredProperties)
          Get the AudioFileFormat out of a file.
 AudioFileOutputStream getAudioFileOutputStream(java.io.File f, javax.sound.sampled.AudioFormat format, javax.sound.sampled.AudioFileFormat.Type type, java.util.Map properties, java.lang.String[] requiredProperties, java.lang.String[] desiredProperties)
          Get an AudioFileOutputStream for writing the desired Audio data to the given file.
static AudioManager getDefault()
          Get the default Audio Manager.
 long getFrameLength(java.io.File source)
          Get the frame length in sample frames out of an audio file. this method is intended as a help, if the AudioInputStream provided for this file has AudioSystem.NOT_SPECIFIED length.
 javax.sound.sampled.Mixer getInMixer()
          Get the Input Mixer which is selected in the "audio_in" property of the given ConfigManager
static javax.sound.sampled.Mixer.Info[] getInputMixers()
          Get infos about all installed Mixers that are capable of recording Audio data via a TargetDataLine
static javax.sound.sampled.AudioInputStream getMonoInputStream(javax.sound.sampled.AudioInputStream in)
          Get a mono AudioInputStream out of the specified one´s left channel.
 javax.sound.sampled.Mixer getOutMixer()
          Get the Output Mixer which is selected in the "audio_out" property of the given ConfigManager
static javax.sound.sampled.Mixer.Info[] getOutputMixers()
          Get infos about all installed Mixers that are capable of playing back Audio data via a SourceDataLine
static java.util.Map getProperties(java.lang.Object o)
          Helper method for getting the properties out of an AudioFormat or AudioFileFormat object, if we cannot access it directly because of compatibility to JDK 1.4
 java.util.List getProviders(java.lang.Class c)
          Get the installed providers for the given type of service
 AudioFileFormatProvider[] getRegisteredFileFormatProviders()
          Get all AudioFileFormatProviders currently registered to this AudioManager
 AudioFileOutputStreamProvider[] getRegisteredOutputStreamProviders()
          Get all AudioFileOutputStreamProviders currently registered to this AudioManager
static int getRelation(javax.sound.sampled.AudioFormat source, javax.sound.sampled.AudioFormat target)
          Get a numeric value for the equalness of two AudioFormat objects.
static javax.sound.sampled.AudioInputStream getStereoInputStream(javax.sound.sampled.AudioInputStream in)
          Get a stereo AudioInputStream out of the specified one.
 void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event)
           
 void registerAudioFileFormatProvider(AudioFileFormatProvider provider)
          Add an AudioFileFormatProvider to the list of known AudioFileFormatProviders
 void registerAudioFileOutputStreamProvider(AudioFileOutputStreamProvider provider)
          Add an AudioFileOutputStreamProvider to the list of known AudioOutputStreamProviders
 void unregisterAudioFileFormatProvider(AudioFileFormatProvider provider)
          Remove an AudioFileFormatProvider from the list of known AudioFileFormatProviders
 void unregisterAudioFileOutputStreamProvider(AudioFileOutputStreamProvider provider)
          Remove an AudioFileOutputStreamProvider from the list of known AudioOutputStreamProviders
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

configManager

protected ConfigManager configManager
The ConfigManager used by this instance.


DEFAULT_AUDIO_MANAGER

public static final AudioManager DEFAULT_AUDIO_MANAGER
The default AudioManager instance.


fileFormatProviders

java.util.ArrayList fileFormatProviders
List of known AudioFileFormatProviders that will be queried for an AudioFileFormat when requested


inMixer

protected javax.sound.sampled.Mixer inMixer
The input Mixer selected by the user or by the application


outMixer

protected javax.sound.sampled.Mixer outMixer
The input Mixer selected by the user or by the application


outputStreamProviders

java.util.ArrayList outputStreamProviders
List of known AudioFileOutputStreamProviders that will be queried for an AudioFileOutputStream when requested

Constructor Detail

AudioManager

protected AudioManager()
Constructs a new Audiomanager using the default ConfigManager


AudioManager

public AudioManager(ConfigManager configManager)
Constructs a new Audiomanager using the given ConfigManager

Parameters:
configManager - The ConfigManager to be used.
Method Detail

canWrite

public boolean canWrite(javax.sound.sampled.AudioFileFormat format)
Find out if a provider exists that can write files in the given AudioFileFormat

Parameters:
format - The deired AudioFileFormat in which the provider should be able to write files
Returns:
true, if such aprovider could be found, false otherwise

createDefault

private static AudioManager createDefault()
Create the default AudioManager instance

Returns:
The default AudioManager instance

getAudioFileFormat

public javax.sound.sampled.AudioFileFormat getAudioFileFormat(java.io.File f,
                                                              java.lang.String[] requiredProperties,
                                                              java.lang.String[] desiredProperties)
                                                       throws javax.sound.sampled.UnsupportedAudioFileException,
                                                              java.io.IOException
Get the AudioFileFormat out of a file. The default implementation will first look, if a known provider can provide the required properties from the specified file. Then if more than one provider is left, the remaining providers are asked about the desired properties. The one that can provide the most of them will be asked to provide the AudiofFileFormat. If no properties are required and no provider can be found, AudioSystem.getAudioFileFormat() will be used. Subclasses may provide a different behaviour for getting the AudioFileFormat, but it should be documented.

Parameters:
f - The Audio file to get the format from
requiredProperties - Array of property keys that the provider must understand. May also be null to indicate that no properties are required.
desiredProperties - Array of property keys that would be nice to have, but are not absolutely necessary. May also be null to indicate that no optional properties are desired.
Throws:
java.io.IOException - If an I/O Error occurs during file reading
javax.sound.sampled.UnsupportedAudioFileException - If either the file format of the file can not be determined or no provider knowing all required properties could be found.

getAudioFileOutputStream

public AudioFileOutputStream getAudioFileOutputStream(java.io.File f,
                                                      javax.sound.sampled.AudioFormat format,
                                                      javax.sound.sampled.AudioFileFormat.Type type,
                                                      java.util.Map properties,
                                                      java.lang.String[] requiredProperties,
                                                      java.lang.String[] desiredProperties)
                                               throws javax.sound.sampled.UnsupportedAudioFileException,
                                                      java.io.IOException
Get an AudioFileOutputStream for writing the desired Audio data to the given file. The default implementation will first search for a provider being able to write a file of the specified type in the specified format with the required properties. Then if more than one provider is left, the remaining providers are asked about the desired properties. The one that can provide the most of them will be asked to provide the AudioFileOutputStream. Subclasses may provide a different behaviour for getting the AudioFileFormat, but it should be documented.

Parameters:
f - The file to write the audio data to
format - The format in which the audio data should be written
type - The type of the audio file
properties - Map of properties of the source data
requiredProperties - Array of property keys that must be supported for writing
desiredProperties - Array of property keys that are nice to have but don´t need to be supported.
Returns:
An AudioFileOutputStream to which the data can be written. When this stream is being closed, the specified properties will have been written to the file too.
Throws:
java.io.IOException - If an I/O Error occurs during file reading
javax.sound.sampled.UnsupportedAudioFileException - If either the file format of the file can not be determined or no provider knowing all required properties could be found.
javax.sound.sampled.UnsupportedAudioFileException - If no provider can be found supporting the file type, the format and the required properties
java.io.IOException - If an I/O Error occurs during OutputStream creation

getDefault

public static AudioManager getDefault()
Get the default Audio Manager.

Returns:
The default Audio Manager. In fact it just returns DEFAULT_AUDIO_MANAGER

getFrameLength

public long getFrameLength(java.io.File source)
Get the frame length in sample frames out of an audio file. this method is intended as a help, if the AudioInputStream provided for this file has AudioSystem.NOT_SPECIFIED length. What it does is searching for the "duration" property in AudioFileFormat of this file and calculating the sample frame length out of it.

Parameters:
source - The audio file in question
Returns:
The number of sample frames of the audio data contained in the given file or AudioSystem.NOT_SPECIFIED, if the length cannot be determined.

getInMixer

public javax.sound.sampled.Mixer getInMixer()
Get the Input Mixer which is selected in the "audio_in" property of the given ConfigManager

Returns:
The selected input Mixer

getInputMixers

public static javax.sound.sampled.Mixer.Info[] getInputMixers()
Get infos about all installed Mixers that are capable of recording Audio data via a TargetDataLine

Returns:
An array of Mixer.Info Objects describing the found Mixers.

getMonoInputStream

public static javax.sound.sampled.AudioInputStream getMonoInputStream(javax.sound.sampled.AudioInputStream in)
Get a mono AudioInputStream out of the specified one´s left channel. If the given AudioInputStream is already mono, it will be returned itself. Otherwise an AudioInputStream reading from its left channel will be returned.

Parameters:
in - The AudioInputStream to convert to mono
Returns:
A mono AudioInputStream
Throws:
java.lang.IllegalArgumentException - If the given AudioInputStream has more than 2 channels

getOutMixer

public javax.sound.sampled.Mixer getOutMixer()
Get the Output Mixer which is selected in the "audio_out" property of the given ConfigManager

Returns:
The selected output Mixer

getOutputMixers

public static javax.sound.sampled.Mixer.Info[] getOutputMixers()
Get infos about all installed Mixers that are capable of playing back Audio data via a SourceDataLine

Returns:
An array of Mixer.Info Objects describing the found Mixers.

getProperties

public static java.util.Map getProperties(java.lang.Object o)
Helper method for getting the properties out of an AudioFormat or AudioFileFormat object, if we cannot access it directly because of compatibility to JDK 1.4

Parameters:
o - The AudioFormat or AudioFileFormat object to query for its properties.
Returns:
The map of properties returned by the object´s properties() method or null

getProviders

public java.util.List getProviders(java.lang.Class c)
Get the installed providers for the given type of service

Parameters:
c - The class representing the service
Returns:
A List of all found providers

getRegisteredFileFormatProviders

public AudioFileFormatProvider[] getRegisteredFileFormatProviders()
Get all AudioFileFormatProviders currently registered to this AudioManager

Returns:
An array of all AudioFileFormatProviders currently registered to this AudioManager

getRegisteredOutputStreamProviders

public AudioFileOutputStreamProvider[] getRegisteredOutputStreamProviders()
Get all AudioFileOutputStreamProviders currently registered to this AudioManager

Returns:
An array of all AudioFileOutputStreamProviders currently registered to this AudioManager

getRelation

public static int getRelation(javax.sound.sampled.AudioFormat source,
                              javax.sound.sampled.AudioFormat target)
Get a numeric value for the equalness of two AudioFormat objects. The number of channels must be the same and conversion from source to target must be supported, otherwise 0 will be returned.

Parameters:
source - The source Format
target - The target Format
Returns:
A number between 0 (no match) and 100 (total match)

getStereoInputStream

public static javax.sound.sampled.AudioInputStream getStereoInputStream(javax.sound.sampled.AudioInputStream in)
Get a stereo AudioInputStream out of the specified one. If the given AudioInputStream is already stereo, it will be returned itself. Otherwise an AudioInputStream reading from using its mono channel for its left as well as for its right channel will be returned.

Parameters:
in - The AudioInputStream to convert to stereo
Returns:
A stereo AudioInputStream
Throws:
java.lang.IllegalArgumentException - If the given AudioInputStream has more than 2 channels

propertyChange

public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event)
Specified by:
propertyChange in interface org.eclipse.jface.util.IPropertyChangeListener
See Also:
IPropertyChangeListener.propertyChange(org.eclipse.jface.util.PropertyChangeEvent)

registerAudioFileFormatProvider

public void registerAudioFileFormatProvider(AudioFileFormatProvider provider)
Add an AudioFileFormatProvider to the list of known AudioFileFormatProviders

Parameters:
provider - The AudioFileFormatProvider to add

registerAudioFileOutputStreamProvider

public void registerAudioFileOutputStreamProvider(AudioFileOutputStreamProvider provider)
Add an AudioFileOutputStreamProvider to the list of known AudioOutputStreamProviders

Parameters:
provider - The AudioFileOutputStreamProvider to add

unregisterAudioFileFormatProvider

public void unregisterAudioFileFormatProvider(AudioFileFormatProvider provider)
Remove an AudioFileFormatProvider from the list of known AudioFileFormatProviders

Parameters:
provider - The AudioFileFormatProvider to remove

unregisterAudioFileOutputStreamProvider

public void unregisterAudioFileOutputStreamProvider(AudioFileOutputStreamProvider provider)
Remove an AudioFileOutputStreamProvider from the list of known AudioOutputStreamProviders

Parameters:
provider - The AudioFileOutputStreamProvider to remove