Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 90218d8

Browse files
committed
adds mp3 support and refactors library api
1 parent c2a77e4 commit 90218d8

20 files changed

Lines changed: 1759 additions & 235 deletions

README.md

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# JavaMP3
22

3-
**Currently only supports MP1 and MP2, see Status below. Support for MP3 is planned.**
3+
**Currently supports MPEG-1 Layer I/II/III (that is, most MP1, MP2, and MP3 files)**
44

55
## Introduction
66

7-
JavaMP3 is a lightweight and fast API for decoding MP1, MP2, and MP3 files.
7+
JavaMP3 is a lightweight (minimalist) and fast API for decoding MP1, MP2, and MP3 files.
88

99
This API lets you:
1010
- Decode MPEG-1/2/2.5 Layer I/II/III data (that is MP1, MP2, and MP3)
11-
- Get a javax.sound.sampled.AudioInputStream to easily create a Clip from the decoded data and use the javax.sound.sampled API
11+
- Get a javax.sound.sampled.AudioFormat to easily use the javax.sound.sampled API and play back the decoded data.
1212
- Get the decoded raw bytes in the format OpenAL takes so you can directly feed an OpenAL buffer with the decoded data
13-
- Decode the data from several types of input such as InputStream, Path, byte[], byte[] with length and offset
1413

1514
## Install
1615

@@ -29,39 +28,60 @@ JavaMP3 requires Java >= 8 to run. You can get this library using Maven by addin
2928

3029
## Quick example
3130

32-
This library is object-oriented: you can use one of the several static functions in the Sound class to get a Sound object, and then call methods on it to get data and metadata. Let's have a look at how to interact with the library.
31+
The only public class in this library is the Sound class. It is simply an InputStream that return decoded PCM sound samples as you read from the stream, decoded from the specified underlying stream.
3332

34-
35-
There are several ways to get a Sound object (ie decoding MPEG data) :
33+
There are also several metadata-related methods to be able to get the sound sampling frequency, stereo mode, ...
3634

3735
```java
38-
// Getting and decoding a sound from a file
36+
// Getting a Sound from a file
3937
Path path = Paths.get("res","crazy_dnb.mp3");
40-
Sound sound = Sound.createSound(path); // throws IOException if an error occured while reading the file or decoding its data
41-
42-
// Getting and decoding a sound from a resource file in your JAR
43-
InputStream in = MyClass.class.getResourceAsStream("/mp3/rick_astley.mp3");
44-
Sound sound = Sound.createSound(in) // throws IOException if an error occured while reading the resource file or decoding its data
38+
try(Sound sound = new Sound(new BufferedInputStream(Files.newInputStream(path)))) {
39+
// no need to buffer the SoundInputStream
40+
41+
// get sound metadata
42+
System.out.println(sound.getSamplingFrequency());
43+
44+
// let's copy the decoded data samples into a file!
45+
Files.copy(sound, Paths.get("/my/path/to/raw.raw"));
46+
}
47+
48+
49+
// Another example: getting and decoding a sound from a resource file in your JAR
50+
try(Sound sound = new Sound(new BufferedInputStream(MyClass.class.getResourceAsStream("/mp3/rick_astley.mp3")))) {
51+
52+
// ...
53+
54+
}
4555

46-
// You can also decode a sound from a byte array, see the library Javadoc
4756
```
4857

49-
Once you've got a Sound object, you may get the raw decoded PCM samples, or directly use the sound with the javax.sound.sampled API:
58+
As expected from an InputStream, the creation of the stream will **not block**, and it is only when reading bytes from the stream, that the processing will take place (for the Sound class, that is the MPEG decoding process). You may get metadata about the sound as soon as you have instantiated it.
5059

60+
Let's have a look at some examples on how to use the decoded raw PCM sound data samples:
5161

5262
```java
5363
// Creating a Clip from the sound and play it using the plain javax.sound.sampled API
5464
Sound sound = /* ... */;
65+
// We use an array to store the produced sound data (bad code style, but is okay for short sounds)
66+
// (We have to store the data in order to get the number of samples in it, because of the (dumb) Java sound API)
67+
ByteArrayOutputStream os = new ByteArrayOutputStream();
68+
// Read and decode the encoded sound data into the byte array output stream (blocking)
69+
int read = sound.decodeFullyInto(os);
70+
// A sample takes 2 bytes
71+
int samples = read / 2;
72+
// Java sound API stuff ...
5573
Clip clip = AudioSystem.getClip();
56-
AudioInputStream stream = sound.newAudioInputStream();
74+
AudioInputStream stream = new AudioInputStream(new ByteArrayInputStream(os.toByteArray()), sound.getAudioFormat(), samples);
5775
clip.open(stream);
5876
clip.start();
5977

6078
// Getting the raw decoded PCM samples to fill an OpenAL buffer (using LWJGL)
6179
Sound sound = /* ... */;
62-
int buffer = alGenBuffers();
63-
ByteBuffer data = BufferUtils.createByteBuffer(sound.getBytes().length);
64-
data.put(sound.getBytes()).flip(); // LWJGL needs a direct buffer, cannot simply wrap the array
80+
// Let's store the whole decoded data in an array, because we need the number of samples; it's okay for short sounds
81+
ByteArrayOutputStream os = new ByteArrayOutputStream();
82+
int read = sound.decodeFullyInto(os);
83+
ByteBuffer data = BufferUtils.createByteBuffer(read);
84+
data.put(os.toByteArray()).flip(); // LWJGL needs a direct buffer, cannot simply wrap the BAOS array
6585
alBufferData(buffer, sound.isStereo() ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, data, sound.getSamplingFrequency());
6686

6787
```
@@ -70,33 +90,35 @@ alBufferData(buffer, sound.isStereo() ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, d
7090

7191
The only public class is the Sound class.
7292

73-
The javadoc for the API is located at: https://mpeg.delthas.fr/
93+
The javadoc for the API is located at: http://www.javadoc.io/doc/fr.delthas/javamp3/
7494

75-
This API fully supports multithreaded calls, there is no shared static state. For example, if trying to decode multiple MPEG files from a music folder, you are encouraged to use multiple threads.
95+
You are encouraged to read the decoded data stream in a streaming way, and/or make use of multithreaded calls (i.e. decode the sound data in a background thread if your application needs to react in real-time to user input).
7696

7797
## Building
7898

79-
Simply run ```maven package```.
99+
Simply run ```mvn install```.
80100

81101

82102
## Status
83103

84104
- [X] MPEG-1 Audio Layer I Support
85105
- [X] MPEG-1 Audio Layer II Support
86-
- [ ] MPEG-1 Audio Layer III Support
106+
- [X] MPEG-1 Audio Layer III Support
87107
- [ ] MPEG-2 Audio Layer I Support
88108
- [ ] MPEG-2 Audio Layer II Support
89109
- [ ] MPEG-2 Audio Layer II Support
90110
- [ ] MPEG-2.5 Audio Layer I Support
91111
- [ ] MPEG-2.5 Audio Layer II Support
92112
- [ ] MPEG-2.5 Audio Layer III Support
93-
- [ ] Tests
113+
- [X] Tests
114+
- [ ] Fast seeking support
115+
- [ ] Fast samples count fetching support
94116

95117
## Misceallenous
96118

97119
### Tech
98120

99-
JavaMP3 uses no library except the standard Java library.
121+
JavaMP3 uses no library except the standard Java library, except for the compile-time JUnit dependency, for testing.
100122

101123
### License
102124

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
</distributionManagement>
5454

5555
<dependencies>
56+
<dependency>
57+
<groupId>junit</groupId>
58+
<artifactId>junit</artifactId>
59+
<version>4.12</version>
60+
<scope>test</scope>
61+
</dependency>
5662
</dependencies>
5763

5864
<build>

0 commit comments

Comments
 (0)