-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvoicesocket.cpp
69 lines (61 loc) · 1.7 KB
/
voicesocket.cpp
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
#include "voicesocket.h"
#define SampleRate 44100
#define ChannelCnt 1
#define SampleSize 32
#define BUFF_SIZE 4000
VoiceSocket::VoiceSocket(QObject *parent) :
QObject(parent)
{
//isEnabled = false;
connect(&udp, SIGNAL(readyRead()), this, SLOT(udp_readyRead()));
pitch_val = 0;
}
void VoiceSocket::set_dst_addr(QHostAddress ip, quint16 port)
{
peer_ip = ip;
peer_port = port;
}
void VoiceSocket::startListen(quint16 port)
{
udp.bind(QHostAddress::Any, port);
}
void VoiceSocket::writeData(QByteArray data)
{
QByteArray pitch_buffer;
process(data, pitch_buffer);
QByteArray data2 = qCompress(pitch_buffer, 9);
udp.writeDatagram(data2, peer_ip, peer_port);
}
void VoiceSocket::udp_readyRead()
{
int size = udp.pendingDatagramSize();
QHostAddress host;
quint16 port;
QByteArray data;
data.resize(size);
udp.readDatagram(data.data(), size, &host, &port);
QString b_host = host.toString();
QString R_host = b_host.right(b_host.length() - b_host.indexOf(":",3)-1);
QByteArray data2 = qUncompress(data);
emit readData(data2, R_host);
}
void VoiceSocket::process(QByteArray in, QByteArray& out)
{
#ifdef soundtouch2
SoundTouch pSoundTouch;
pSoundTouch.setSampleRate(SampleRate);
pSoundTouch.setChannels(ChannelCnt);
pSoundTouch.setPitchSemiTones(pitch_val);
int nSamples;
int nChannels;
int buffSizeSamples;
pSoundTouch.putSamples((SAMPLETYPE*)(in.data() ), in.count() / 4);
SAMPLETYPE sampleBuffer[BUFF_SIZE];
pSoundTouch.flush();
do
{
nSamples = pSoundTouch.receiveSamples(sampleBuffer, BUFF_SIZE);
out.append((char*)sampleBuffer, nSamples*4);
} while (nSamples != 0);
#endif
}