Need help with the work of the "Speex" codec. There is an implementation in java, data from the microphone is encoded there and data from the network is decoded. There is encryption and decryption of data, which I do not need, but I only need to encode and decode and I cannot repeat it.

The code of the original that encodes, encrypts and sends to the network:

public void run() { byte buf_speech[] = new byte[320]; byte buf_bits[] = new byte [1600]; byte temp_buf_bits[] = new byte [60]; while(true) { int index = 0; buf_bits[index++] = ProxyBuffer.SPEECH_PACKET; int ii; for(ii = 0; ii < 8; ++ii) { audio_.getTargetLine().read(buf_speech, 0, buf_speech.length); if(!speechDetector.speechDetecting(buf_speech, buf_speech.length)) { for(int i = 0; i < buf_speech.length; ++i) buf_speech[i] = 0; } int sizeBytes = CodecOfSpeech.encode(buf_speech, temp_buf_bits); buf_bits[index++] = (byte)sizeBytes; for(int i = 0; i < sizeBytes; ++i) { buf_bits[index++] = temp_buf_bits[i]; } } buf_bits[index++] = (byte)0xff; //шифрование if(ProxyBuffer.desKey != null) { byte bufCrypt[] = null; try { bufCrypt = ProxyBuffer.desOut.doFinal(buf_bits, 1, index - 1); } catch(javax.crypto.IllegalBlockSizeException e) { System.out.println(e.getMessage()); System.exit(-1); } catch(javax.crypto.BadPaddingException e) { System.out.println(e.getMessage()); System.exit(-1); } for(int i0 = 0; i0 < bufCrypt.length; ++i0) { buf_bits[i0 + 1] = bufCrypt[i0]; } index = bufCrypt.length + 1; } int crc = 0; for(int iii = 0; iii < index; ++iii) { crc += buf_bits[iii]; } buf_bits[index++] = (byte)crc; ++countForAlive; if((index <= 6 * ii + 2 + 10 + 8) && (countForAlive < 120/ii)) continue; countForAlive = 0; DatagramPacket packet = new DatagramPacket(buf_bits, index, address_, port_); outComingTraffic += packet.getLength() + 8 + 24; try { socket_.send(packet); } catch(IOException exc) { System.out.println("ReadThread" + exc.getMessage()); continue; } //<editor-fold defaultstate="collapsed" desc="test"> //*/ /* //Zeroing for test purpose only audio_.getTargetLine().read(buf_speech, 0, buf_speech.length); int sizeBytes = CodecOfSpeech.encode(buf_speech, temp_buf_bits); CodecOfSpeech.decode(temp_buf_bits, sizeBytes, buf_speech); for(int ii = 0; ii < buf_speech.length; ++ii) { ProxyBuffer.proxyArray[ProxyBuffer.proxyHead++] = buf_speech[ii]; if(ProxyBuffer.proxyHead >= ProxyBuffer.proxyArray.length) ProxyBuffer.proxyHead = 0; } //*/ } } 

here is my attempt:

 public void run() { mWindow.stopCapture = false; mWindow.audioLine.start(); byte buf_speech[] = new byte[320]; byte temp_buf_bits[] = new byte[60]; int index = 0; jCodec = new JSpeexCodec(); try { mWindow.inBuf[index++] = 1; while(!mWindow.stopCapture) { int ii; for(ii = 0; ii < 8; ++ii) { mWindow.audioLine.read(buf_speech, 0, buf_speech.length); if(!speechDetector.speechDetecting(buf_speech, buf_speech.length)) { for(int i = 0; i < buf_speech.length; ++i) buf_speech[i] = 0; } int sizeBytes = jCodec.encode(buf_speech, temp_buf_bits); mWindow.inBuf[index++] = (byte)sizeBytes; for(int i = 0; i < sizeBytes; ++i) { mWindow.inBuf[index++] = temp_buf_bits[i]; } } } mWindow.inBuf[index++] = (byte)0xff; java.awt.EventQueue.invokeLater(() -> { mWindow.speakBtn.setEnabled(true); mWindow.mcBtn.setEnabled(true); mWindow.ftm.addFiles(mWindow.mFile); }); if(audioLine != null) { audioLine.close(); } } catch(Exception e) { System.out.println(e.getMessage()); } } 

Audio parameters for recording AudioFormat.Encoding.PCM_SIGNED, 8000, 16, 1, 2, 8000, false

The implementation of the original for decoding:

 public void run() { byte buf[] = new byte[1600]; byte speech[] = new byte[320]; while(true) { DatagramPacket packet = new DatagramPacket(buf, buf.length); try { socket_.receive(packet); } catch(Exception exc) { continue; } incominTraffic += packet.getLength() + 8 + 24; int index = 0; int crc = 0; for(int ii = 0; ii < packet.getLength() - 1; ++ii) crc += buf[ii]; if(buf[packet.getLength() - 1] != (byte)crc) continue; if(buf[0] == ProxyBuffer.CALL_PACKET) { if(packet.getLength() != 5) continue; if((buf[1] != 0x77) || (buf[2] != 0x77) || (buf[3] != 0x77)) continue; if(ProxyBuffer.peerAddress == null) ProxyBuffer.peerAddress = packet.getAddress(); ProxyBuffer.recvCallPacket = true; ProxyBuffer.liveConnect = true; buf[0] = ProxyBuffer.ACCEPT_CALL; buf[1] = 0x55; buf[2] = 0x55; buf[3] = 0x55; crc = 0; for(int ii = 0; ii < 4; ++ii) { crc += buf[ii]; } buf[4] = (byte)crc; packet.setData(buf, 0, 5); for(int ii = 0; ii < 4; ++ii) { try { socket_.send(packet); } catch(IOException exc) { System.out.println("sendException " + exc.getMessage()); continue; } try { sleep(100); } catch(InterruptedException exc) { } } MainStream.peerPortIP = packet.getPort(); ProxyBuffer.acceptCall = true; continue; } if(buf[0] == ProxyBuffer.ACCEPT_CALL) { if(packet.getLength() != 5) continue; if((buf[1] != 0x55) || (buf[2] != 0x55) || (buf[3] != 0x55)) continue; MainStream.peerPortIP = packet.getPort(); ProxyBuffer.liveConnect = true; ProxyBuffer.recvCallPacket = true; ProxyBuffer.acceptCall = true; continue; } if(buf[0] == ProxyBuffer.SPEECH_PACKET) { ++index; ProxyBuffer.liveConnect = true; //шифрование if(ProxyBuffer.desKey != null) { byte tempDeCryptoBuf[] = null; if((packet.getLength() - 2) % 8 != 0) continue; try { tempDeCryptoBuf = ProxyBuffer.desIn.doFinal(buf, 1, packet.getLength() - 2); } catch(javax.crypto.IllegalBlockSizeException e) { System.out.println("IllegalBlockSizeException" + e.getMessage()); continue; } catch(javax.crypto.BadPaddingException e) { System.out.println("BadPaddingException" + e.getMessage()); continue; } if(tempDeCryptoBuf != null) for(int i0 = 0; i0 < tempDeCryptoBuf.length; i0++) { buf[i0 + 1] = tempDeCryptoBuf[i0]; } else continue; } byte temp_buf[]; while(true){ try { if(buf[index] != 0xff){ if((buf[index] > 60) || (buf[index] < 0)) break; temp_buf = new byte[buf[index++]]; for(int ii = 0; ii < temp_buf.length; ++ii) temp_buf[ii] = buf[index++]; CodecOfSpeech.decode(temp_buf, temp_buf.length, speech); for(int ii = 0; ii < speech.length; ++ii){ ProxyBuffer.proxyArray[ProxyBuffer.proxyHead++] = speech[ii]; if(ProxyBuffer.proxyHead >= ProxyBuffer.proxyArray.length) ProxyBuffer.proxyHead = 0; } } else break; } catch(IndexOutOfBoundsException exc){ System.out.println("IndexOutOfBoundsException " + exc.getMessage()); break; } catch(Exception exc){ System.out.println("Exception " + exc.getMessage()); break; } } } } } } 

My implementation:

 public void run() { byte temp_buf[]; byte speech[] = new byte[320]; int index = 0; int indArr = 0; mWindow.outBuf = new byte[1024000]; byte[] bufferAudio = new byte[source_line.getBufferSize()]; source_line.write(mWindow.outBuf, 0, source_line.getBufferSize()); if(mWindow.inBuf[0] == 1) { index++; while(true){ try { if(mWindow.inBuf[index] != 0xff){ if((mWindow.inBuf[index] > 60) || (mWindow.inBuf[index] < 0)) break; temp_buf = new byte[mWindow.inBuf[index++]]; for(int ii = 0; ii < temp_buf.length; ++ii) temp_buf[ii] = mWindow.inBuf[index++]; jDecode.decode(temp_buf, temp_buf.length, speech); for(int ii = 0; ii < speech.length; ++ii){ mWindow.outBuf[indArr++] = speech[ii]; }//конец for }//конец if else break; }//конец try catch(IndexOutOfBoundsException exc){ System.out.println("IndexOutOfBoundsException " + exc.getMessage()); break; } catch(Exception exc){ System.out.println("Exception " + exc.getMessage()); break; } }//конец while } if(mWindow.outBuf.length >= 0) { source_line.write(mWindow.outBuf, 0, mWindow.outBuf.length); } int ind = 0; while(true) { for(int i = 0; i < 320; i++) { if(index != ind) { bufferAudio[i] = mWindow.outBuf[ind++]; if(ind >= mWindow.outBuf.length) ind = 0; } else { bufferAudio[i++] = 0; bufferAudio[i] = 0; } } source_line.write(bufferAudio, 0, 320); } source_line.drain(); source_line.close(); java.awt.EventQueue.invokeLater(() -> { mWindow.mcBtn.setEnabled(true); mWindow.speakBtn.setEnabled(true); mWindow.PlayBtn.setEnabled(true); }); 

My implementation encodes and decodes, but something is wrong. Does not play decoded data in normal mode.

Help to understand what the error.

    0