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; }
@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(); } }
public SpeexCodec(boolean wide) { _speexmode = wide ? 1 : 0; _sampleRate = wide ? 16000 : 8000; _iaxcn = wide ? CodecFace.AUDIO_SPEEX16 : CodecFace.AUDIO_SPEEX; _name = wide ? "SPEEX" : "SPEEX"; _spxe = new SpeexEncoder(); int q = wide ? 7 : 3; _spxe.init(_speexmode, q, _sampleRate, 1); // _mode, _quality, _sampleRate, _channels); _spxe.getEncoder().setComplexity(2); Log.debug( "Speex mode " + _spxe.getEncoder().getMode() + " encoder configured with rate =" + _spxe.getEncoder().getSamplingRate() + " Quality =" + q + " compexity = " + _spxe.getEncoder().getComplexity() + " packet size = " + _spxe.getEncoder().getEncodedFrameSize() / 8 + " bitrate = " + _spxe.getEncoder().getBitRate() / 1024.0); _spxd = new SpeexDecoder(); _spxd.init(_speexmode, _sampleRate, 1, false); // _mode, _sampleRate, _channels, false); _aframesz = wide ? 320 : 160; // number of shorts in an audio frame; }