As you know, the Java Sound API includes two packages,
javax.sound.sampled.spiandjavax.sound.midi.spi, that define abstract classes to be used by developers of sound services. By implementing and installing a subclass of one of these abstract classes, a service provider registers the new service, extending the functionality of the runtime system. This page tells you how to go about using thejavax.sound.sampled.spipackage to provide new services for handling sampled audio.There are four abstract classes in the
javax.sound.sampled.spipackage, representing four different types of services that you can provide for the sampled-audio system:To recapitulate earlier discussions, service providers can extend the functionality of the runtime system. A typical SPI class has two types of methods: ones that respond to queries about the types of services available from a particular provider, and ones that either perform the new service directly, or return instances of objects that actually provide the service. The runtime environment's service-provider mechanism provides registration of installed services with the audio system, and management of the new service provider classes.
AudioFileWriterprovides sound file-writing services. These services make it possible for an application program to write a stream of audio data to a file of a particular type.
AudioFileReaderprovides file-reading services. These services enable an application program to ascertain a sound file's characteristics, and to obtain a stream from which the file's audio data can be read.
FormatConversionProviderprovides services for converting audio data formats. These services allow an application program to translate audio streams from one data format to another.
MixerProviderprovides management of a particular kind of mixer. This mechanism allows an application program to obtain information about, and access instances of, a given kind of mixer.In essence there is a double isolation of the service instances from the application developer. An application program never directly creates instances of the service objects, such as mixers or format converters, that it needs for its audio processing tasks. Nor does the program even directly request these objects from the SPI classes that administer them. The application program makes requests to the
AudioSystemobject in thejavax.sound.sampledpackage, andAudioSystemin turn uses the SPI objects to process these queries and service requests.The existence of new audio services might be completely transparent to both the user and the application programmer. All application references are through standard objects of the
javax.sound.sampledpackage, primarilyAudioSystem, and the special handling that new services might be providing is often completely hidden.In this discussion, we'll continue the previous convention of referring to new SPI subclasses by names like
AcmeMixerandAcmeMixerProvider.Providing Audio File-Writing Services
Let's start with
AudioFileWriter, one of the simpler SPI classes.A subclass that implements the methods of
AudioFileWritermust provide implementations of a set of methods to handle queries about the file formats and file types supported by the class, as well as provide methods that actually write out a supplied audio data stream to aFileorOutputStream.
AudioFileWriterincludes two methods that have concrete implementations in the base class:The first of these methods informs the caller whether this file writer can write sound files of the specified type. This method is a general inquiry, it will returnboolean isFileTypeSupported(AudioFileFormat.Type fileType) boolean isFileTypeSupported(AudioFileFormat.Type fileType, AudioInputStream stream)trueif the file writer can write that kind of file, assuming the file writer is handed appropriate audio data. However, the ability to write a file can depend on the format of the specific audio data that's handed to the file writer. A file writer might not support every audio data format, or the constraint might be imposed by the file format itself. (Not all kinds of audio data can be written to all kinds of sound files.) The second method is more specific, then, asking whether a particularAudioInputStreamcan be written to a particular type of file.Generally, you won't need to override these two concrete methods. Each is simply a wrapper that invokes one of two other query methods and iterates over the results returned. These other two query methods are abstract and therefore need to be implemented in the subclass:
These methods correspond directly to the previous two. Each returns an array of all the supported file types-all that are supported in general, in the case of the first method, and all that are supported for a specific audio stream, in the case of the second method. A typical implementation of the first method might simply return an array that the file writer's constructor initializes. An implementation of the second method might test the stream'sabstract AudioFileFormat.Type[] getAudioFileTypes() abstract AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream)AudioFormatobject to see whether it's a data format that the requested type of file supports.The final two methods of
AudioFileWriterdo the actual file-writing work:These methods write a stream of bytes representing the audio data to the stream or file specified by the third argument. The details of how this is done depend on the structure of the specified type of file. Theabstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, java.io.File out) abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, java.io.OutputStream out)writemethod must write the file's header and the audio data in the manner prescribed for sound files of this format (whether it's a standard type of sound file or a new, possibly proprietary one).Providing Audio File-Reading Services
The
AudioFileReaderclass consists of six abstract methods that your subclass needs to implement-actually, two different overloaded methods, each of which can take aFile,URL, orInputStreamargument. The first of these overloaded methods accepts queries about the file format of a specified file:A typical implementation ofabstract AudioFileFormat getAudioFileFormat(java.io.File file) abstract AudioFileFormat getAudioFileFormat(java.io.InputStream stream) abstract AudioFileFormat getAudioFileFormat(java.net.URL url)getAudioFileFormatmethod reads and parses the sound file's header to ascertain its file format. See the description of the AudioFileFormat class to see what fields need to be read from the header, and refer to the specification for the particular file type to figure out how to parse the header.Because the caller providing a stream as an argument to this method expects the stream to be unaltered by the method, the file reader should generally start by marking the stream. After reading to the end of the header, it should reset the stream to its original position.
The other overloaded
AudioFileReadermethod provides file-reading services, by returning an AudioInputStream from which the file's audio data can be read:Typically, an implementation ofabstract AudioInputStream getAudioInputStream(java.io.File file) abstract AudioInputStream getAudioInputStream(java.io.InputStream stream) abstract AudioInputStream getAudioInputStream(java.net.URL url)getAudioInputStreamreturns anAudioInputStreamwound to the beginning of the file's data chunk (after the header), ready for reading. It would be conceivable, though, for a file reader to return anAudioInputStreamwhose audio format represents a stream of data that is in some way decoded from what is contained in the file. The important thing is that the method return a formatted stream from which the audio data contained in the file can be read. TheAudioFormatencapsulated in the returnedAudioInputStreamobject will inform the caller about the stream's data format, which is usually, but not necessarily, the same as the data format in the file itself.Generally, the returned stream is an instance of
AudioInputStream; it's unlikely you would ever need to subclassAudioInputStream.Providing Format-Conversion Services
A
FormatConversionProvidersubclass transforms anAudioInputStreamthat has one audio data format into one that has another format. The former (input) stream is referred to as the source stream, and the latter (output) stream is referred to as the target stream. Recall that anAudioInputStreamcontains anAudioFormat, and theAudioFormatin turn contains a particular type of data encoding, represented by anAudioFormat.Encodingobject. The format and encoding in the source stream are called the source format and source encoding, and those in the target stream are likewise called the target format and target encoding.The work of conversion is performed in the overloaded abstract method of
FormatConversionProvidercalledgetAudioInputStream. The class also has abstract query methods for learning about all the supported target and source formats and encodings. There are concrete wrapper methods for querying about a specific conversion.The two variants of
getAudioInputStreamare:andabstract AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream)These differ in the first argument, according to whether the caller is specifying a complete target format or just the format's encoding.abstract AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream)A typical implementation of
getAudioInputStreamworks by returning a new subclass ofAudioInputStreamthat wraps around the original (source)AudioInputStreamand applies a data format conversion to its data whenever areadmethod is invoked. For example, consider the case of a newFormatConversionProvidersubclass calledAcmeCodec, which works with a newAudioInputStreamsubclass calledAcmeCodecStream.The implementation of
AcmeCodec'ssecondgetAudioInputStreammethod might be:The actual format conversion takes place in newpublic AudioInputStream getAudioInputStream (AudioFormat outputFormat, AudioInputStream stream) { AudioInputStream cs = null; AudioFormat inputFormat = stream.getFormat(); if (inputFormat.matches(outputFormat) ) { cs = stream; } else { cs = (AudioInputStream) (new AcmeCodecStream(stream, outputFormat)); tempBuffer = new byte[tempBufferSize]; } return cs; }readmethods of the returnedAcmeCodecStream, a subclass ofAudioInputStream. Again, application programs that access this returnedAcmeCodecStreamsimply operate on it as anAudioInputStream, and don't need to know the details of its implementation.The other methods of a
FormatConversionProviderall permit queries about the input and output encodings and formats that the object supports. The following four methods, being abstract, need to be implemented:abstract AudioFormat.Encoding[] getSourceEncodings() abstract AudioFormat.Encoding[] getTargetEncodings() abstract AudioFormat.Encoding[] getTargetEncodings( AudioFormat sourceFormat) abstract AudioFormat[] getTargetFormats( AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat)As in the query methods of the
AudioFileReaderclass discussed above, these queries are typically handled by checking private data of the object and, for the latter two methods, comparing them against the argument(s).The remaining four
FormatConversionProvidermethods are concrete and generally don't need to be overridden:As withboolean isConversionSupported( AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) boolean isConversionSupported(AudioFormat targetFormat, AudioFormat sourceFormat) boolean isSourceEncodingSupported( AudioFormat.Encoding sourceEncoding) boolean isTargetEncodingSupported( AudioFormat.Encoding targetEncoding)AudioFileWriter.isFileTypeSupported(), the default implementation of each of these methods is essentially a wrapper that invokes one of the other query methods and iterates over the results returned.Providing New Types of Mixers
As its name implies, a
MixerProvidersupplies instances of mixers. Each concreteMixerProvidersubclass acts as a factory for theMixerobjects used by an application program. Of course, defining a newMixerProvideronly makes sense if one or more new implementations of theMixerinterface are also defined. As in theFormatConversionProviderexample above, where ourgetAudioInputStreammethod returned a subclass ofAudioInputStreamthat performed the conversion, our new classAcmeMixerProviderhas a methodgetMixerthat returns an instance of another new class that implements theMixerinterface. We'll call the latter classAcmeMixer. Particularly if the mixer is implemented in hardware, the provider might support only one static instance of the requested device. If so, it should return this static instance in response to each invocation ofgetMixer.Since
AcmeMixersupports theMixerinterface, application programs don't require any additional information to access its basic functionality. However, ifAcmeMixersupports functionality not defined in theMixerinterface, and the vendor wants to make this extended functionality accessible to application programs, the mixer should of course be defined as a public class with additional, well-documented public methods, so that a program that wishes to make use of this extended functionality can importAcmeMixerand cast the object returned bygetMixerto this type.The other two methods of
MixerProviderare:andabstract Mixer.Info[] getMixerInfo()These methods allow the audio system to determine whether this particular provider class can produce a device that an application program needs. In other words, theboolean isMixerSupported(Mixer.Info info)AudioSystemobject can iterate over all the installedMixerProvidersto see which ones, if any, can supply the device that the application program has requested of theAudioSystem. ThegetMixerInfomethod returns an array of objects containing information about the kinds of mixer available from this provider object. The system can pass these information objects, along with those from other providers, to an application program.A single
MixerProvidercan provide more than one kind of mixer. When the system invokes theMixerProvider's getMixerInfomethod, it gets a list of information objects identifying the different kinds of mixer that this provider supports. The system can then invokeMixerProvider.getMixer(Mixer.Info)to obtain each mixer of interest.Your subclass needs to implement
getMixerInfo, as it's abstract. TheisMixerSupportedmethod is concrete and doesn't generally need to be overridden. The default implementation simply compares the providedMixer.Infoto each one in the array returned bygetMixerInfo.