Subversion Repositories Projects

Rev

Rev 229 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/******************************************
 *
 * Voice output for MK
 *                      
 * Author:        Marcus -LiGi- Bueschleb
 *
 *******************************************/


import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.util.*;
import java.io.*;


public class MKStatusVoice
    implements Runnable,PlayerListener
{

    org.ligi.ufo.MKCommunicator mk=null;
    DUBwiseCanvas canvas=null;
    Player player;
   
    public final int PLAYERSTATE_IDLE=0;
    public final int PLAYERSTATE_PLAYING=1;
    public final int PLAYERSTATE_FIN=2;


    public int volume=100;
    public int delay=5;

    int act_player_state=PLAYERSTATE_IDLE;
    VolumeControl vc;

    public MKStatusVoice(org.ligi.ufo.MKCommunicator _mk,DUBwiseCanvas _canvas)
    {
        canvas=_canvas;
        mk=_mk;
        new Thread( this ).start(); // fire up main Thread
    }

    public void playerUpdate(Player player,  String event, Object data)
    {
      if(event == PlayerListener.END_OF_MEDIA) {
          try {
              defplayer();
          }
          catch(MediaException me) {      }
          act_player_state=PLAYERSTATE_FIN;
          player=null;

      }
    }
   
    void defplayer() throws MediaException {
      if (player != null) {
         if(player.getState() == Player.STARTED) {
            player.stop();
         }
         if(player.getState() == Player.PREFETCHED) {
            player.deallocate();
         }
         if(player.getState() == Player.REALIZED ||   player.getState() == Player.UNREALIZED) {
           player.close();
         }
      }
      player = null;
    }


    public void start_playing(String name)
    {
        try {
               
            try {
                player = Manager.createPlayer(getClass().getResourceAsStream(name+".mp3"), "audio/mp3");
            }
            catch (Exception e)  {
                player = Manager.createPlayer(getClass().getResourceAsStream(name+".wav"), "audio/x-wav");
            }
                        player.addPlayerListener(this);
                        player.realize();
                        act_player_state=PLAYERSTATE_PLAYING;

                        vc = (VolumeControl) player.getControl("VolumeControl");
                        if(vc != null) {
                            vc.setLevel(volume);
                        }

                        player.prefetch();
                        player.setLoopCount(1);
                        player.start();
        }
        catch (Exception e)  {
           
        }      
    }
   
    public void wait_for_end()
    {
        while (act_player_state!=PLAYERSTATE_FIN)
            {
                try { Thread.sleep(5); }
                catch (Exception e)  {   }
            }
               

    }

    int info_from_debug_set=-1;
    public void run()
    {
        while(true)
            {
                if (mk.connected&&(canvas.settings.do_sound)&&(mk.debug_data.UBatt()!=-1)&&(!mk.force_disconnect))
                    {
                        int ubatt=mk.debug_data.UBatt();
               
                        if (info_from_debug_set!=mk.stats.debug_data_count)
                            {
                                info_from_debug_set=mk.stats.debug_data_count;
                                start_playing(""+(ubatt/10));
                                wait_for_end();
                               
                                if((ubatt%10)!=0)
                                    {
                                        start_playing("komma");
                                        wait_for_end();
                                        start_playing(""+(ubatt%10));
                                        wait_for_end();
                                    }
                                start_playing("volt");
                            }
       
                    }

                try {
                    if (delay<1)
                        Thread.sleep(1000);
                    else
                        Thread.sleep(delay*1000);
                }
                catch (Exception e)  {   }
               
            }
    }
   

   
}