Skip to content

Commit

Permalink
ImaAdpcmDecoder: Reset the stream after reading its header.
Browse files Browse the repository at this point in the history
Failing to reset the stream will make FreeJ2ME load PCM files
without their header, which can cause issues with media playback.
The header only needs to be removed for IMA ADPCM files, as those
have to be decoded into PCM.
  • Loading branch information
AShiningRay committed Aug 9, 2023
1 parent 1fb6cf3 commit 5f68847
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/org/recompile/mobile/PlatformPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,12 @@ public wavPlayer(InputStream stream)
/*
* A wav header is generally 44-bytes long, and it is what we need to read in order to get
* the stream's format, frame size, bit rate, number of channels, etc. which gives us information
* on the kind of codec needed to play or decode the incoming stream.
* on the kind of codec needed to play or decode the incoming stream. The stream needs to be reset
* or else PCM files will be loaded without a header and it might cause issues with playback.
*/
stream.mark(48);
wavHeaderData = adpcmDec.readHeader(stream);
stream.reset();

/* We only check for IMA ADPCM at the moment. */
if(wavHeaderData[0] != 17) /* If it's not IMA ADPCM we don't need to do anything to the stream. */
Expand Down
3 changes: 3 additions & 0 deletions src/org/recompile/mobile/WavImaAdpcmDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ private void buildHeader(final byte[] buffer, final short numChannels, final int
/* Decode the received IMA WAV ADPCM stream into a signed PCM16LE byte array, then return it to PlatformPlayer. */
public ByteArrayInputStream decodeImaAdpcm(InputStream stream, int[] wavHeaderData) throws IOException
{
/* Remove the header from the stream, we shouldn't "decode" it as if it was a sample */
readHeader(stream);

final byte[] input = new byte[stream.available()];
readInputStreamData(stream, input, 0, stream.available());

Expand Down

0 comments on commit 5f68847

Please sign in to comment.