Urbi SDK Remote for Java  2.7.5
SoundUtilities.java
00001 /*
00002  * Copyright (C) 2010-2011, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00011     package urbi;
00012 
00013 import javax.sound.sampled.*;
00014 import urbi.UClient;
00015 
00016 import java.io.*;
00017 
00018 /**
00019  * The SoundUtilities class is full of handy static methods.
00020  * <p>
00021  * <p>
00022  * @author Bastien Saltel
00023  */
00024 
00025 public class    SoundUtilities
00026 {
00027     private static float        sampleRate = 8000;
00028 
00029     private     static int              sampleSizeInBits = 8;
00030 
00031     private     static int              channels = 1;
00032 
00033     private static boolean      signed = true;
00034 
00035     private static boolean      bigEndian = false;
00036 
00037     /**
00038      * Sets the sample rate.
00039      * <p>
00040      */
00041     public static void          setSampleRate(float f)
00042     {
00043         sampleRate = f;
00044     }
00045 
00046     /**
00047      * Returns the sample rate.
00048      * <p>
00049      */
00050     public static float getSampleRate()
00051     {
00052         return sampleRate;
00053     }
00054 
00055     /**
00056      * Sets the sample size.
00057      * <p>
00058      */
00059     public static void          setSampleSizeInBits(int i)
00060     {
00061         sampleSizeInBits = i;
00062     }
00063 
00064     /**
00065      * Returns the sample size.
00066      * <p>
00067      */
00068     public static int           getSampleSizeInBits()
00069     {
00070         return sampleSizeInBits;
00071     }
00072 
00073     /**
00074      * Sets the number of channels.
00075      * <p>
00076      */
00077     public static void          setChannels(int i)
00078     {
00079         channels = i;
00080     }
00081 
00082     /**
00083      * Returns the number of channels.
00084      * <p>
00085      */
00086     public static int           getChannels()
00087     {
00088         return channels;
00089     }
00090 
00091     /**
00092      * Sets the signed value.
00093      * <p>
00094      */
00095     public static void          setSigned(boolean i)
00096     {
00097         signed = i;
00098     }
00099 
00100     /**
00101      * Returns true if audio data is signed.
00102      * <p>
00103      * @return True if audio data is signed,
00104      *         false otherwise.
00105      */
00106     public static boolean       getSigned()
00107     {
00108         return signed;
00109     }
00110 
00111     /**
00112      * Sets the big endian value.
00113      * <p>
00114      */
00115     public static void          setBigEndian(boolean i)
00116     {
00117         bigEndian = i;
00118     }
00119 
00120     /**
00121      * Returns true if audio data is in big endian mode.
00122      * <p>
00123      * @return True if audio data is in big endian mode,
00124      *         false otherwise.
00125      */
00126     public static boolean       getBigEndian()
00127     {
00128         return bigEndian;
00129     }
00130 
00131 
00132     /*
00133      * Returns the data audio format.
00134      * <p>
00135      */
00136     public static AudioFormat   getFormat()
00137     {
00138         return new AudioFormat(sampleRate,
00139                                sampleSizeInBits,
00140                                channels,
00141                                signed,
00142                                bigEndian);
00143     }
00144 
00145     /*
00146      * Sends an audio file to the remote host which the client is connected to.
00147      * <p>
00148      * @param file  The name of file.
00149      * @param client A reference to the client.
00150      */
00151     public static int   sendSound(String filename, UClient client) throws IOException
00152     {
00153         int             totalFramesRead = 0;
00154         File    file = new File(filename);
00155 
00156         try
00157         {
00158             AudioFileFormat fformat = AudioSystem.getAudioFileFormat(file);
00159             AudioFormat         format = fformat.getFormat();
00160             try
00161             {
00162                 AudioInputStream        ais = AudioSystem.getAudioInputStream(file);
00163                 int numBytes = 2048;
00164                 byte[] audioBytes = new byte[numBytes];
00165 
00166                 int numBytesRead = 0;
00167                 try
00168                 {
00169                     while ((numBytesRead = ais.read(audioBytes)) != -1)
00170                     {
00171                         totalFramesRead += numBytesRead;
00172 //                      client.send("speaker.val = BIN " + numBytesRead + " raw "
00173 //                                  + format.getChannels() + " " + (int)format.getSampleRate() +
00174 //                                  " " + format.getSampleSizeInBits() + " 1;");
00175                         client.sendBin (audioBytes, numBytesRead,
00176                                         "speaker.val = BIN " + numBytesRead + " raw "
00177                                         + format.getChannels() + " " + (int)format.getSampleRate() +
00178                                         " " + format.getSampleSizeInBits() + " 1;");
00179                     }
00180                 }
00181                 catch (IOException e)
00182                 {
00183                     System.out.println("Read error");
00184                 }
00185             }
00186             catch (UnsupportedAudioFileException e)
00187             {
00188             }
00189         }
00190         catch (UnsupportedAudioFileException e)
00191         {
00192         }
00193         return 0;
00194     }
00195 }