반응형


JavaSound API 는 자바 플랫폼에서 audio 재생을 지원하기 위해 만들어졌다.
이 API는 J2SE 1.3 버전에서 처음 추가되었고, wav, au, aiff, midi 오디오 포멧만을 지원한
다.

mp3 나 ogg 파일 포멧을 사용하고 싶을때는 어떻게 해야 할까?
이러한 문제점을 해결하기 위해 JavaSound API  에서는 JavaSound Service Provider Interfaces (SPIs) 를 이용한 확장을 지원한다. 이 인터페이스를 통해서, 사용자가 구현하고자 하는 오디오포멧을 지원하면 된다.

이것은  JDBC와 같은 개념이다. DB서버가 각각 다르더라도, JDBC 인터페이스를 통해 통일된 DB프로그래밍을 할수 있는 것과 같은 개념이다.

JavaZoom 의 mp3 플러인
* 특징
- mp3 재생만 지원 (MPEG 1, 2 and 2.5, Layers 1, 2, and 3)
- 현재는 mp3 재생만 지원 (Encoding, Converter, Write 기능은 아직 구현되지 않음)
- 메타데이타 ID3 태그 지원 (artist, album, date, copyright, comments, 정보를 뽑아올수 있다)

mp3 를 파일에서 정보를 뽑아오는 방법
1. File 객체를 이용하여 AudioFileFormat 객체를 생성한다.
2. getFormat() 객체를 호출한다.
3. AudioFormat 인스턴스에서 값을 뽑아온다.

File file = new File("filename.mp3");
AudioFileFormat baseFileFormat = null;
AudioFormat baseFormat = null;
baseFileFormat = AudioSystem.getAudioFileFormat(file);
baseFormat = baseFileFormat.getFormat();
AudioFileFormat.Type type = baseFileFormat.getType();
float frequency = baseFormat.getSampleRate();

To play MP3, you need first to call AudioSystem.getAudioInputStream(file) to get an AudioInputStream from an MP3 file, select the target format (i.e., PCM) according to input MP3 channels and sampling rate, and finally get an AudioInputStream with the target format. If JavaSound doesn't find a matching SPI implementation supporting the MP3-to-PCM conversion, then it will throw an exception.

File file = new File("filename.mp3");
AudioInputStream in= AudioSystem.getAudioInputStream(file);
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(
 AudioFormat.Encoding.PCM_SIGNED, 
 baseFormat.getSampleRate(), 
 16,
 baseFormat.getChannels(), 
 baseFormat.getChannels() * 2,
 baseFormat.getSampleRate(),
 false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
// Play now.
rawplay(decodedFormat, din);
in.close();

Second, you have to send the decoded PCM data to a SourceDataLine. This means you have to load PCM data from the decoded AudioInputStream into the SourceDataLine buffer until the end of file is reached. JavaSound will send this data to the sound card. Once the file is exhausted, the line resources must be closed.

private void rawplay(AudioFormat targetFormat, AudioInputStream din)  throws IOException, LineUnavailableException {
 byte[] data = new byte[4096];
 SourceDataLine line = getLine(targetFormat);
 if (line != null) {
 // Start
 line.start();
 int nBytesRead = 0, nBytesWritten = 0;
 while (nBytesRead != -1) {
 nBytesRead = din.read(data, 0, data.length);
 if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
 }
 // Stop
 line.drain();
 line.stop();
 line.close();
 din.close();
 }
}

private SourceDataLine getLine(AudioFormat audioFormat)
 throws LineUnavailableException {
 SourceDataLine res = null;
 DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
 res = (SourceDataLine) AudioSystem.getLine(info);
 res.open(audioFormat);
 return res;
}


If you're familiar with JavaSound API, you will notice that source code for playing MP3 is similar to the what you'd use to play a WAV file. The source code sample above has no dependencies upon the MP3 SPI implementation. It's transparent for the developer.

Notice that if the file to play was stored on a web server, we would have used:
URL url = new URL(http://www.myserver.com/filename.mp3); AudioInputStream in= AudioSystem.getAudioInputStream(url);

instead of:
File file = new File("filename.mp3"); AudioInputStream in= AudioSystem.getAudioInputStream(file);

Metadata
Most audio formats include metadata such as title, album, comments, compression quality, encoding, and copyright. ID3 tags, used for MP3, are the best-known metadata format. Depending on ID3 version (v1 or v2), they can be found either at the end or at the beginning of an MP3 file. They include information such as duration, title, album, artist, track number, date, genre, copyright, etc. They can even include lyrics and pictures. The famous (and free) SHOUTcast streaming MP3 server, from Nullsoft, uses a different scheme in order to provide additional metadata such as title streaming, which allows a player to display the current song being played from the online radio stream. All of these metadata items need to be parsed and exposed through the SPI implementation. As of J2SE 1.5, the JavaSound API standardizes the passing of metadata parameters through an immutable java.util.Map:

File file = new File("filename.mp3");
AudioFileFormat baseFileFormat = AudioSystem.getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
String key_author = "author";
String author = (String) properties.get(key_author);
String key_duration = "duration";
Long duration = (Long) properties.get(key_duration);

All metadata keys and types should be provided in the SPI documentation. However, common properties include:
"duration" (Long): Playback duration of file, in microseconds
"author" (String): Name of the author of the file
"title" (String): Title of the file
"copyright" (String): Copyright message
"comment" (String): Arbitrary text

Using Multiple SPIs in an Application
Adding MP3 audio capabilities to the Java platform means adding JAR files containing the MP3 SPI implementation to the runtime CLASSPATH. Adding Ogg Vorbis, Speex, Flac, or Monkey's Audio support would be similar, but could generate conflicts that make other SPI implementations fail. The following situation could occur:

Your runtime application CLASSPATH includes both MP3 and Ogg Vorbis SPIs.
Your application tries to play an MP3 file.
JavaSound's AudioSystem tries Ogg Vorbis SPI first.
The Ogg Vorbis SPI implementation doesn't detect that incoming file isn't an Ogg-Vorbis-compliant stream, so it doesn't throw any exception.
Your application tries to play an MP3 with the Ogg Vorbis SPI. At best you will get a runtime exception (NullPointerException, ArrayIndexOutOfBoundException), and in the worst case, you will hear weird noises or just deadlock.
In the example above, it's true that the problem comes from the Ogg Vorbis SPI implementation, but it's not easy for the SPI provider to have reliable controls (just think about streaming). Thus, each SPI provider has to pay attention to the others. That's the main practical drawback of the JavaSound plugin architecture. So don't be surprised if you have problems making multiple SPIs work together in your application.

Differences with JMF
JMF stands for Java Media Framework. It's an optional J2SE packages that adds multimedia support to the Java platform. It includes audio (GSM, QuickTime, etc.), video (AVI, QuickTime, H.263, etc.) and RTP streaming features. JMF provides a plugin architecture, but it is not compliant with that of JavaSound. In fact, MP3 support was previously included in JMF, but it was removed in 2002 because of licensing issues.

Conclusion
JavaSound rocks. It provides a plugin architecture allowing any third-party provider to add custom audio format support, such as for MP3 files. API is flexible enough to plug most heterogeneous (lossy, lossless) audio formats, whatever their parameters and metadata, to the Java platform -- "Write once, play anywhere."

References and Resources

JLayer: Java library for decoding and converting MP3 files
MP3 SPI: MP3 plugin for the Java platform
Speex SPI: Speex plugin for the Java platform
Ogg Vorbis SPI: Ogg Vorbis plugin for the Java platform
Monkey's Audio SPI: Monkey's Audio plugin for the Java platform
Flac SPI: Flac plugin for the Java platform
SHOUTcast: SHOUTcast streaming MP3 server
jlGui player: Music player for the Java platform -- a WinAmp clone
JavaSound: SUN homepage
Tritonus: Tritonus project
MP3 Tech: MP3 frame header info
ID3: ID3 definition
The JavaZOOM Team are the authors of the open source projects JLayer and jlGui.


<출처: http://zyint.tistory.com/1 >

+ Recent posts