18.07.2015, 08:06. Показов 1286. Ответов 1
Уважаемые форумчане нужна помощь с работой кодека Speex, есть реализация на java
Arrow.rar, там кодируются данные получаемые с микрофона и декодируются данные получаемые из сети, есть шифрование и дешифровка данных, что в мне нужно, а повторить только кодирование и декодирование у меня не получается вот код оригинала который кодирует:
| Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
| 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;
}
//*/
}
} |
|
вот моя попытка:
| Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| 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());
}
} |
|
параметры аудио для записи AudioFormat.Encoding.PCM_SIGNED, 8000, 16, 1, 2, 8000, false
реализация оригинала для декодирования:
| Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
| 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;
}
}
}
}
}
} |
|
а вот моя реализация:
| Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
| 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);
}); |
|
мой вариант реализации кодирует и декодирует, но что-то не так потому что не воспроизводит в нормальном режиме декодированные данные, вот проект полностью
examenJavaSave2Byte.rar, большая просьба помогите разобраться в чем ошибка.