Skip to content

Commit

Permalink
Nokia: Sound: Check for the actual incoming data on FORMAT_WAV
Browse files Browse the repository at this point in the history
Quite a few jars try to send MIDI data as FORMAT_WAV here, so do
a few checks before actually sending the data into platformPlayer.

Also, make it so any loop == 0 cases in play() are turned into
loop = -1 for continuous looping.
  • Loading branch information
AShiningRay committed Dec 2, 2024
1 parent 5ee1044 commit 5946959
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/com/nokia/mid/sound/Sound.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,16 @@ public void init(byte[] data, int type)
try { player = Manager.createPlayer(new ByteArrayInputStream(convertToMidi(data)), "audio/x-tone-seq"); }
catch (MidiUnavailableException e) { Mobile.log(Mobile.LOG_ERROR, Sound.class.getPackage().getName() + "." + Sound.class.getSimpleName() + ": " + " couldn't create Tone player:" + e.getMessage()); }
}
else if (type == FORMAT_WAV) { player = Manager.createPlayer(new ByteArrayInputStream(data), "audio/wav"); }
else if (type == FORMAT_WAV)
{

String format;
if(data[0] == 'M' && data[1] == 'T' && data[2] == 'h' && data[3] == 'd') { format = "audio/mid"; }
else if(data[0] == 'R' && data[1] == 'I' && data[2] == 'F' && data[3] == 'F') { format = "audio/wav"; }
else { Mobile.log(Mobile.LOG_WARNING, Sound.class.getPackage().getName() + "." + Sound.class.getSimpleName() + ": " + " couldn't find what format this is. Passing as FORMAT_WAV."); format = "audio/wav";}

player = Manager.createPlayer(new ByteArrayInputStream(data), format);
}
else { throw new IllegalArgumentException("Nokia Sound: Invalid audio format: " + type); }
}
catch (MediaException exception) { } catch (IOException exception) { }
Expand All @@ -133,7 +142,8 @@ public void init(int freq, long duration)
}

public void play(int loop)
{
{
if(loop == 0) { loop = -1; }
player.setLoopCount(loop);
player.start();
}
Expand Down

0 comments on commit 5946959

Please sign in to comment.