Example #1
0
 public short[] decode_frame(byte[] bytes) {
   try {
     _spxd.processData(bytes, 0, bytes.length);
   } catch (StreamCorruptedException ex) {
     Log.error("Speex Decoder error " + ex.getMessage());
   }
   short audioOut[] = new short[_aframesz];
   int decsize = _spxd.getProcessedData(audioOut, 0);
   return audioOut;
 }
Example #2
0
  @Override
  public void run() {
    try {

      DatagramSocket dsocket = new DatagramSocket(portServer);
      DatagramSocket ssocket = new DatagramSocket();

      byte[] buffer = new byte[1024];

      DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

      SpeexDecoder speexDecoder = new SpeexDecoder();
      speexDecoder.init(0, 8000, 1, false);

      ByteBuffer storeBuffer = ByteBuffer.allocate(115000);
      int arrayLength;
      do {
        dsocket.receive(packet);

        arrayLength = packet.getLength();
        speexDecoder.processData(packet.getData(), 0, packet.getData().length);

        byte data[] = new byte[speexDecoder.getProcessedDataByteSize()];
        speexDecoder.getProcessedData(data, 0);

        System.out.println("Data length: " + data.length);

        storeBuffer.put(data);
        if (storeBuffer.position() > 114000) {
          String host = Mapping.getDestination(packet.getAddress().toString().substring(1));
          System.out.println("Client IP : " + packet.getAddress().toString());

          AudioInputStream stream = AudioUtils.soundBytesToAudio(storeBuffer.array());
          storeBuffer.clear();

          String fileName = "./resources/tmp/" + NameGenerator.getName() + ".wav";

          AudioUtils.saveAudioStreamToFile(stream, fileName);
          stream.close();

          String[] result = RecognitionEngine.recogniseAudio(fileName);

          ssocket.send(
              new DatagramPacket(
                  result[0].getBytes(),
                  result[0].getBytes().length,
                  InetAddress.getByName(host),
                  portClient));
        }
      } while (arrayLength > 0);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }