From 328077c35aca9936b0bf800419b53321563b393a Mon Sep 17 00:00:00 2001 From: a-sommer Date: Sat, 24 Sep 2022 13:34:59 -0400 Subject: [PATCH] Initial groundwork for playing tones - Added tonePlayer, concrete class for playing tones (in progress) - Modified PlatformPlayer if/else to route x-tone-seq correctly - Added initial implementations to Nokia Sound (in progress) --- src/com/nokia/mid/sound/Sound.java | 63 +++++++++++++++++--- src/org/recompile/mobile/PlatformPlayer.java | 51 ++++++++++++---- 2 files changed, 97 insertions(+), 17 deletions(-) diff --git a/src/com/nokia/mid/sound/Sound.java b/src/com/nokia/mid/sound/Sound.java index 6381d0497..de667180f 100644 --- a/src/com/nokia/mid/sound/Sound.java +++ b/src/com/nokia/mid/sound/Sound.java @@ -16,6 +16,14 @@ */ package com.nokia.mid.sound; +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import javax.microedition.media.Manager; +import javax.microedition.media.MediaException; +import javax.microedition.media.Player; + + public class Sound { public static final int FORMAT_TONE = 1; @@ -24,25 +32,63 @@ public class Sound public static final int SOUND_STOPPED = 1; public static final int SOUND_UNINITIALIZED = 3; - - public Sound(byte[] data, int type) { } + private Player player; + + public Sound(byte[] data, int type) + { + try + { + if (type == FORMAT_TONE) + { + player = Manager.createPlayer(new ByteArrayInputStream(data), "audio/x-tone-seq"); + } + else if (type == FORMAT_WAV) + { + player = Manager.createPlayer(new ByteArrayInputStream(data), "audio/wav"); + } + } + catch (MediaException exception) { } + catch (IOException exception) { } + } public Sound(int freq, long duration) { } - public static int getConcurrentSoundCount(int type) { return 1; } public int getGain() { return 0; } - public int getState() { return 1; } + public int getState() + { + int state = player.getState(); + + switch (state) + { + case Player.STARTED: + return SOUND_PLAYING; + case Player.PREFETCHED: + case Player.REALIZED: + return SOUND_STOPPED; + case Player.UNREALIZED: + case Player.CLOSED: + default: + return SOUND_UNINITIALIZED; + } + } public static int[] getSupportedFormats() { return new int[]{}; } - public void init(byte[] data, int type) { } + public void init(byte[] data, int type) + { + // init's functionality is covered when Manager creates the player - leave empty + } public void init(int freq, long duration) { } - public void play(int loop) { } + public void play(int loop) + { + player.setLoopCount(loop); + player.start(); + } public void release() { } @@ -52,6 +98,9 @@ public void setGain(int gain) { } public void setSoundListener(SoundListener listener) { } - public void stop() { } + public void stop() + { + player.stop(); + } } diff --git a/src/org/recompile/mobile/PlatformPlayer.java b/src/org/recompile/mobile/PlatformPlayer.java index b0cf3699f..8e5c23d48 100644 --- a/src/org/recompile/mobile/PlatformPlayer.java +++ b/src/org/recompile/mobile/PlatformPlayer.java @@ -60,17 +60,19 @@ public PlatformPlayer(InputStream stream, String type) { player = new midiPlayer(stream); } - else + else if (type.equalsIgnoreCase("audio/x-wav") || type.equalsIgnoreCase("audio/wav")) { - if(type.equalsIgnoreCase("audio/x-wav") || type.equalsIgnoreCase("audio/wav")) - { - player = new wavPlayer(stream); - } - else /* TODO: Implement a player for amr and mpeg audio types */ - { - System.out.println("No Player For: "+contentType); - player = new audioplayer(); - } + player = new wavPlayer(stream); + } + else if (type.equalsIgnoreCase("audio/x-tone-seq")) + { + // + player = new tonePlayer(stream); + } + else /* TODO: Implement a player for amr and mpeg audio types */ + { + System.out.println("No Player For: "+contentType); + player = new audioplayer(); } } controls[0] = new volumeControl(); @@ -342,6 +344,35 @@ public boolean isRunning() } } + /* Todo: Implement tone playing functionality */ + private class tonePlayer extends audioplayer + { + private InputStream toneStream; + private int loops = 0; + + public tonePlayer(InputStream stream) { toneStream = stream; } + + public void start() + { + // Todo implement functionality to play a tone sequence + state = Player.STARTED; + } + + public void stop() + { + // Todo implement functionality to stop playing a tone sequence + state = Player.PREFETCHED; + } + + public void setLoopCount(int count) { if (count > -1) { loops = count; } } + + public boolean isRunning() + { + // Todo implementation depends on start() + return false; + } + } + // Controls // private class midiControl implements javax.microedition.media.control.MIDIControl