ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Phone Sound Creation Guide
    Computer/J2ME for GSM 2004. 10. 14. 19:54

    Developers reference guide on creating audio SFX, melodies and vibration on Nokia, Motorola, Sharp, Sagem and Sony mobile devices.

    Introduction

    How many times have you thought your software works on a specific mobile device, because it works in the PC-emulator, only to find that once you try it on an actual device it crashes without any explanation why!

    This guide will help software developers create 100% working FX on Nokia, Motorola, Sharp, Sagem and Sony mobile devices. This practical guide will include full-code for each device.

    The most common FX required whilst developing mobile game software is the ability to:

    • Play melodies
    • Play wave-audio
    • Vibrate

    Although it is generally possible to create J2ME software that will work on all devices* it is impossible to create generic code that will activate these FX.

    *Apart from Nokia full screen mode, Motorola reverse soft-keys and Nokia 3650 alternative key layout.

    If the device you need is not listed below, please email me dan@jcodeworks.com and I will consider adding it.
     

    FX Reference Guide

    Please note: Although it might be possible to achieve melody, wave-audio and vibrate using different types of file-format I will only list one. The format described will (a) work!!! (b) use the least amount of processor time and (c) use the least amount of resources.

    Devices:


    Coming soon: (Email me if you need this NOW)

    • Sony Ericsson S700, K700, K500, F500 family
    • Siemens S65, CX66
    • Nokia 6630
    • Motorola E680




    Nokia 3650, 6600 and N-Gage

    API: javax.microedition.media.*

    Restriction: Only play 1 FX at a time.

    NOTE: You can add a listener that will inform you of Player events (e.g. Stopped - when Player finishes FX).  To do this use: player.addPlayerListener(*class*).  The specified *class* must implement PlayerListener.

    To catch the Listener events, use the following method inside *class*:

      public void playerUpdate(Player player, String event, Object obj) {
         //player indicates which Player caused the event
         //event informs you of event type
      }

    Also: I have not tried PlayerListener on Nokia 6600 and N-Gage and so, cannot confirm that it works properly.

    - - -

    Melody: Available - plays .mid files (format zero)

    The great thing about MIDI files is that you can use your favourite professional audio sequencing package to create them. It's the standard of all standards in the music creation world.

    To LOAD a MIDI file:

      import javax.microedition.media.*;
      . . . .
      Player player;
      try {
         player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("melody.mid"),"audio/midi");
         player.realize();
         player.prefetch();  //Remove this line for Nokia 6600 and N-Gage
      } catch (Exception e0) {}


    To PLAY the melody, ensure that you STOP all other players:

      try {
         player.stop();
      catch (Exception e1) {}

    … and then play your required melody with:

      try {
         player.prefetch(); //Remove this line for Nokia 3650
         player.setMediaTime(0); //Remove this for Nokia 6600 and N-Gage
         player.setLoopCount(n); //n=number of times. -1=forever.
         player.start();
      catch (Exception e2) {}


    Note: Playing a melody does NOT seem to cause any serious delays.  So you will NOT need to place it in a separate thread to the main game code.

    To clean-up a melody resource:

      if (player != null) {
         try {
           player.close();
         } catch (Exception e3) {}
         player = null;
      }

    - - -

    Wave Audio: Available - plays .amr files (8 bit, rate can vary depending on quality required)

    AMR files are a lot smaller than .WAV files. To create AMR files use the MIKSOFT Mobile AMR converter. Check it out at
    www.miksoft.8m.com

    To LOAD a wave-audio:

      import javax.microedition.media.*;
      . . . .
      Player player;
      try {
         player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("beep.amr"),"audio/amr");
         player.realize();
         player.prefetch();
      } catch (Exception e0) {}


    To PLAY the audio, ensure that you STOP all other players:

      try {
         player.stop();
      catch (Exception e1) {}
       

    … and then play your required wave-audio with:

      try {
         player.setMediaTime(0);
         player.start();
      catch (Exception e2) {}


    Note: Calling the start() method does seem to take some time before calling back.  So you will need to place it in a separate thread to the main game code.

    To clean-up a wave-audio resource:

      if (player != null) {
         try {
           player.close();
         } catch (Exception e3) {}
         player = null;
      }

    - - -

    Vibrate: Not available.




    Nokia 7650

    API: com.nokia.mid.sound.*

    Restriction: Only play 1 FX at a time.

    NOTE: There does NOT seem to be any Sound listeners implemented on this device, so you will need to create your own means of determining if the Sound has finished using an elapsed time clock counter.

    - - -

    Melody: Not Available.

    - - -

    Wave Audio: Available – plays .WAV files (8bit, 8khz)

    WARNING: I have found that short wave-audio (e.g. under .7 of a second) do not play properly e.g. produce clicking noises or don't play at all.

    To LOAD a wave-audio:

      import com.Nokia.sound.*;
      . . . .
      byte[] bytes = readDataFromJar("beep.wav");
      Sound sound;
      if (bytes != null)
         sound = new Sound(bytes, Sound.FORMAT_WAV);

      . . . .
      //utility method used by code above…
      public byte[] readDataFromJar(String name) {
         //get data from JAR resource
         ByteArrayOutputStream bout;
         InputStream in;
         byte[] res = null;
         try {
           in = *canvas ref*.getClass().getResourceAsStream(name);
           bout = new ByteArrayOutputStream();
           for (int ret = in.read(); ret >= 0; ret = in.read())
               bout.write(ret);
           res = bout.toByteArray();
         } catch (Exception e) {}
         return res;
      }


    To PLAY the audio, ensure that you STOP all other wave-audio:

      if (sound != null)
         sound.stop();

    … and then play your required wave-audio with:

      if (sound != null)
         sound.play(n); //n = number of repeats, e.g. 1

    Note: Calling the play() method does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

    To clean-up a wave-audio resource:

      if (sound != null) {
         sound.stop();
         sound = null;
      }

    BUG NOTIFICATION: Some older 7650 firmware versions (version number unknown) crashes the application if you use the command 'setGain'.  Once the application terminates, the device will display an error notification: jes-8e-javax.microedition.lcdui0@ 1...CMdaAudioPlayerU 2 (or similar - depending on firmware version).  The application will crash even if you enclose the command with a try/catch exception handler.

    - - -

    Vibrate: Not available.




    Motorola V525 and V600

    API: Use javax.microedition.media.* for melody and wave-audio.
    Use the MIDP2 display.vibrate command for vibration.

    Restriction: Only play 1 FX at a time.

    NOTE: You can add a listener that will inform you of Player events (e.g. Stopped - when Player finishes FX).  To do this use: player.addPlayerListener(*class*).  The specified *class* must implement PlayerListener.

    To catch the Listener events, use the following method inside *class*:

      public void playerUpdate(Player player, String event, Object obj) {
         //player indicates which Player caused the event
         //event informs you of event type
      }

    - - -

    Melody: Available - plays .mid files (format zero)

    The great thing about MIDI files is that you can use your favourite professional audio sequencing package to create them. It's the standard of all standards in the music creation world.

    To LOAD a MIDI file:

      import javax.microedition.media.*;
      . . . .
      Player player;
      try {
         player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("melody.mid"),"audio/midi");
         player.realize();
      } catch (Exception e0) {}


    To PLAY the melody, ensure that you STOP and DEALLOCATE all other players:

      try {
         player.stop();
      catch (Exception e1) {}

      try {
         player.deallocate();
      catch (Exception e2) {}
       

    … and then play your required melody with:

      try {
         player.prefetch();
      catch (Exception e3) {}

      try {
         player.setMediaTime(0);
      catch (Exception e4) {}

      player.setLoopCount(n); //n=number of times. -1=forever.

      try {
         player.start();
      catch (Exception e5) {}


    Note: Calling the start() method does NOT seem to cause any serious delays.  So you will NOT need to place it in a separate thread to the main game code.

    To clean-up a melody resource:

      if (player != null) {
         try {
           player.close();
         } catch (Exception e6) {}
         player = null;
      }

    - - -

    Wave Audio: Available - plays .amr files (8 bit, rate can vary depending on quality required)

    AMR files are a lot smaller than .WAV files. To create AMR files use the MIKSOFT Mobile AMR converter. Check it out at
    www.miksoft.8m.com

    To LOAD a wave-audio:

      import javax.microedition.media.*;
      . . . .
      Player player;
      try {
         player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("beep.amr"),"audio/amr");
         player.realize();
         player.prefetch();
      } catch (Exception e0) {}


    To PLAY the wave-audio, ensure that you STOP and DEALLOCATE all other players:

      try {
         player.stop();
      catch (Exception e1) {}

      try {
         player.deallocate();
      catch (Exception e2) {}
       

    … and then play your required wave-audio with:

      try {
         player.prefetch();
      catch (Exception e3) {}

      try {
         player.setMediaTime(0);
      catch (Exception e4) {}

      try {
         player.start();
      catch (Exception e5) {}


    Note: Calling the start() method does NOT seem to cause any serious delays.  So you will NOT need to place it in a separate thread to the main game code.

    To clean-up a wave-audio resource:

      if (player != null) {
         try {
           player.close();
         } catch (Exception e6) {}
         player = null;
      }

    - - -

    Vibrate: Available.

    Use the MIDP2 built-in display class command:

      *display*.vibrate(duration); //duration is in milliseconds





    Sagem myV65 and myV75

    API: javax.microedition.media.*

    Restriction: Only play 1 FX at a time.

    NOTE: There does NOT seem to be any Player listeners implemented on this device, so you will need to create your own means of determining if the Player has finished using an elapsed time clock counter.

    - - -

    Melody: ***To be fully investigated***
    Although it is possible to play MIDI files it is NOT 100% compatible.  For example: I have not been able to play drum tracks. I need to  investigate this in more depth and I will then write up my discoveries.

    - - -

    Wave Audio: Available – plays .WAV files (8bit, 8khz)

    WARNING: The Sagem is extremely fussy about wave-audio.  You must only have 1 Player pre-fetched at any one time.

    To LOAD a wave-audio:

      import javax.microedition.media.*;
      . . . .
      Player player;
      try {
         player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("beep.wav"),"audio/x-wav");
         player.realize();
      } catch (Exception e0) {}


    To PLAY the audio, ensure that you STOP and DEALLOCATE all other players:

      try {
         player.stop();
      catch (Exception e1) {}

      try {
         player.deallocate();
      catch (Exception e2) {}

    … and then play your required wave-audio with:

      try {
         player.prefetch();
      catch (Exception e3) {}

      try {
         player.setMediaTime(0);
      catch (Exception e4) {}

      try {
         player.start();
      catch (Exception e5) {}


    Note: Calling the stop(), deallocate(), prefetch() and start() methods does NOT seem to cause any serious delays.  So you will NOT need to place it in a separate thread to the main game code.

    To clean-up a wave-audio resource:

      if (player != null) {
         try {
           player.close();
         } catch (Exception e5) {}
         player = null;
      }

    As I mentioned earlier, the Sagem device is extremely fussy about wave-audio, and so I have tried to catch every possible problem with an exception. Remove the try/catches if you dare, but don't be too surprised if you start getting unexpected crashes!

    - - -

    Vibrate: Not available.


    Sharp GX family

    Melody: Available - plays .mmf files

    To play a melody, create a MIDI tune (format ZERO - max 4 channels) then use the Yamaha tool ATS-MA2-SMAF to convert it to .MMF format (get this from
    http://smaf-yamaha.com/)
    Also: download the .PDF documentation files, as they are very useful.

    To LOAD melody:

      static public void loadMelody() {
        try {
         byte[] data=readDataFromJar("/resources/tune.mmf"));
         if (data!=null) {
           mediaPlayer_tune=new MediaPlayer((byte[])null);
           mediaPlayer_tune.setMediaData(data);
         }
        } catch (Exception ex) {}
      }


    To PLAY the melody, ensure that you STOP all other players:

      static public void playMelody(int idx) {
        try {
         loadMelody();
         if (mediaPlayer_tune!=null)
           //boolean states if melody repeats forever
           mediaPlayer_tune.play(true);
        } catch (Exception ex) {}
      }

    - - -

    Wave Audio: Available - plays .mmf files

    I use .WAV files and convert them to .mmf files which I create using the Yamaha tool called "WAV-SMAF" (download this from
    http://smaf-yamaha.com/
    Also: download the .PDF documentation files, as they are very useful.

    To LOAD audio:

      MediaPlayer[] mediaPlayer;
      String[] sounds={"sfx1.mmf","sfx2.mmf"};
      int numberOfSounds=sounds.length;

      static public void loadAudio() {
           mediaPlayer=new MediaPlayer[numberOfSounds];
           Media media=new Media();
           byte[] data;
           for (int i = 0; i < numberOfSounds; i++) {
             try {
               data=readDataFromJar("/resources/" + sounds[i]);
               if (data!=null) {
                 mediaPlayer[i]=new MediaPlayer((byte[])null);
                 mediaPlayer[i].setMediaData(data);
                 mediaPlayer[i].setMediaPlayerListener(media);
                 Utility.hasSound = true;
               }
             } catch (Exception ex) {}
         }

      static public byte[] readDataFromJar(String name) {
         //get data from JAR resource
         ByteArrayOutputStream bout;
         InputStream in;
         byte[] res = null;
         try {
           in = *canvas ref*.getClass().getResourceAsStream(name);
           bout = new ByteArrayOutputStream();
           for (int ret = in.read(); ret >= 0; ret = in.read())
             bout.write(ret);
           res = bout.toByteArray();
         } catch (Exception e) {}
         return res;
        }
      }
       

    To PLAY the audio:

      //idx is the sound FX that you want to play
      mediaPlayer[idx].play();

    - - -

    Vibrate:

    To initialise:

      DeviceControl dc;

      try {
         dc = DeviceControl.getDefaultDeviceControl();
      } catch (Exception ex) {}

    To START vibrate:

      if (dc!=null)
           dc.setDeviceActive(DeviceControl.VIBRATION, true);

    To STOP vibrate:

      if (dc!=null)
           dc.setDeviceActive(DeviceControl.VIBRATION, false);

    I use a timer thread to delay switching OFF the vibrate.




    Sony-Ericsson T6nn and Z600

    API: javax.microedition.media.*

    Restriction: Only play 1 FX at a time.

    NOTE: You can add a listener that will inform you of Player events (e.g. Stopped - when Player finishes FX).  To do this use: player.addPlayerListener(*class*).  The specified *class* must implement PlayerListener.

    To catch the Listener events, use the following method inside *class*:

      public void playerUpdate(Player player, String event, Object obj) {
         //player indicates which Player caused the event
         //event informs you of event type
      }

    - - -

    Melody: Available - plays .mid files (format zero)

    The great thing about MIDI files is that you can use your favourite professional audio sequencing package to create them. It's the standard of all standards in the music creation world.

    To LOAD a MIDI file:

      import javax.microedition.media.*;
      . . . .
      Player player;
      try {
         player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("melody.mid"),"audio/midi");
         player.realize();
      } catch (Exception e0) {}


    To PLAY the melody, ensure that you STOP all other players:

      try {
         if (player.getState() != Player.CLOSED)
           player.stop();
      catch (Exception e1) {}
       

    … and then play your required melody with:

      try {
         player.prefetch();
         player.setLoopCount(n); //n=number of times. -1=forever.
         player.start();
      catch (Exception e2) {}


    Note: Calling the prefetch() method does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

    To clean-up a melody resource:

      if (player != null) {
         try {
           player.close();
         } catch (Exception e3) {}
         player = null;
      }

    - - -

    Wave Audio: Available - plays .amr files (8 bit, rate can vary depending on quality required)

    AMR files are a lot smaller than .WAV files. To create AMR files use the MIKSOFT Mobile AMR converter. Check it out at
    www.miksoft.8m.com

    Note: Many thanks to David Harries for revising the wave audio code.


    To LOAD a wave-audio:

      import javax.microedition.media.*;
      . . . .
      Player player;
      try {
         player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("beep.amr"),"audio/amr");
         player.realize();
      } catch (Exception e0) {}

      player.prefetch();


    To PLAY the audio, ensure that you STOP all other players:

      try {
         if (player.getState() != Player.PREFETCHED)
           player.stop();
      catch (Exception e1) {}
       

    … and then play your required wave-audio with:

      try {
         player.setLoopCount(1);
         player.start();
      catch (Exception e2) {}


    Note: The prefetch() method does seem to take some time before calling back. So if you need to prefetch on the fly in a tight game loop you will need to place it in a separate thread to the main game code.

    To clean-up a wave-audio resource:

      if (player != null) {
         try {
           player.close();
         } catch (Exception e3) {}
         player = null;
      }

    - - -

    Vibrate: Available - using .imy file

    To LOAD a Vibrate player:

      import javax.microedition.media.*;
      . . . .
      Player player;
      try {
         player = Manager.createPlayer( *canvas ref*.getClass().getResourceAsStream("vibrate.imy"),"audio/iMelody");
         player.realize();
         player.prefetch();
      } catch (Exception e0) {}
       

    The VIBRATE.IMY is a text file with the following lines of code (to change the vibration delay alter the settings in the MELODY line):

      BEGIN:IMELODY
      VERSION:1.2
      FORMAT:CLASS1.0
      BEAT:200
      STYLE:S1
      MELODY:vibeonr5vibeoff
      END:IMELODY

    To START the vibration, ensure that you STOP all other players:

      try {
         if (player.getState() != Player.CLOSED)
           player.stop();
      catch (Exception e1) {}
       

    … and then START the vibration with:

      try {
         player.start();
      catch (Exception e2) {}


    Note: Calling the start() method does NOT seem to cause any serious delays.  So you will NOT need to place it in a separate thread to the main game code.

    To clean-up a vibration resource:

      if (player != null) {
         try {
           player.close();
         } catch (Exception e3) {}
         player = null;
      }

Designed by Tistory.