This repository was archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·142 lines (116 loc) · 3.87 KB
/
main.c
File metadata and controls
executable file
·142 lines (116 loc) · 3.87 KB
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
#include <stdlib.h>
#include <time.h>
#include "channel.h"
#include "handshake.h"
#include "protocol.h"
#include "receiver.h"
#include "sender.h"
#include "wavpcm_io.h"
#include "globals.h"
#include "codec.h"
#include "encode.h"
#include "decode.h"
void _handshake();
void _transmit();
enum state handshakeState;
int main(int argc, char **argv) {
size_t bufPos;
size_t read;
short buffer[BUFFERSIZE];
short encoded[BUFFERSIZE];
struct wavpcm_input input;
struct wavpcm_output output;
struct decode_chunk_struct decode_chunk_left;
struct decode_chunk_struct decode_chunk_right;
struct encode_chunk_struct encode_chunk_left;
struct encode_chunk_struct encode_chunk_right;
// Initializations
srand(time(NULL));
_convFromOctets();
// Construct
buffer_construct();
channel_construct();
sender_construct();
receiver_construct();
// Handshake
#ifndef __ENC_NO_PRINTS__
printf("\n# Key Exchange\n");
printf("--------------\n\n");
#endif
handshakeState = SENDER_HELLO;
while (HANDSHAKE_FINISHED != handshakeState)
_handshake();
// Transmit
memset(&input, 0, sizeof(struct wavpcm_input));
input.resource = INPUTWAVFILE;
memset(&output, 0, sizeof(struct wavpcm_output));
output.resource = OUTPUTWAVFILE;
/* initialize structs */
memset(&encode_chunk_left, 0, sizeof(struct encode_chunk_struct));
memset(&encode_chunk_right, 0, sizeof(struct encode_chunk_struct));
memset(&decode_chunk_left, 0, sizeof(struct decode_chunk_struct));
memset(&decode_chunk_right, 0, sizeof(struct decode_chunk_struct));
/* initializing for quantisation */
encode_chunk_left.Qstep[0] = QSTART;
encode_chunk_right.Qstep[0] = QSTART;
decode_chunk_left.Qstep[0] = QSTART;
decode_chunk_right.Qstep[0] = QSTART;
encode_chunk_left.Qstep[1] = QSTART;
encode_chunk_right.Qstep[1] = QSTART;
decode_chunk_left.Qstep[1] = QSTART;
decode_chunk_right.Qstep[1] = QSTART;
encode_chunk_left.Qstep[2] = QSTART;
encode_chunk_right.Qstep[2] = QSTART;
decode_chunk_left.Qstep[2] = QSTART;
decode_chunk_right.Qstep[2] = QSTART;
encode_chunk_left.Qstep[3] = QSTART;
encode_chunk_right.Qstep[3] = QSTART;
decode_chunk_left.Qstep[3] = QSTART;
decode_chunk_right.Qstep[3] = QSTART;
wavpcm_input_open(&input);
wavpcm_output_copy_settings(&input, &output);
wavpcm_output_open(&output);
for (bufPos = 0; bufPos < input.samplesAvailable ; bufPos += (BUFFERSIZE/2)) {
read = wavpcm_input_read(&input, buffer);
encode(buffer, &encode_chunk_left, &encode_chunk_right, encoded);
while (buffer_isModified()) {}
buffer_write((field_t *) buffer, BUFFERSIZE*sizeof(short));
_transmit();
receiver_receiveData();
buffer_read((field_t *) buffer, BUFFERSIZE*sizeof(short));
decode(&decode_chunk_left, &decode_chunk_right, encoded, buffer);
wavpcm_output_write(&output, buffer, read);
}
wavpcm_output_close(&output);
exit(EXIT_SUCCESS);
}
void _handshake() {
switch (handshakeState) {
case SENDER_HELLO:
sender_senderHello();
handshakeState = RECEIVER_HELLO;
break;
case RECEIVER_HELLO:
if (ENC_ACCEPT_PACKET == receiver_receiverHello())
handshakeState = SENDER_ACKNOWLEDGE;
break;
case SENDER_ACKNOWLEDGE:
if (ENC_ACCEPT_PACKET == sender_senderAcknowledge())
handshakeState = RECEIVER_CHECK_ACKNOWLEDGE;
break;
case RECEIVER_CHECK_ACKNOWLEDGE:
if (ENC_ACCEPT_PACKET == receiver_checkSenderAcknowledge())
handshakeState = HANDSHAKE_FINISHED;
break;
case HANDSHAKE_FINISHED:
break;
}
}
void _transmit() {
if(sender_sendData() == ENC_COUNTER_WRAPAROUND) {
handshakeState = SENDER_HELLO;
while (HANDSHAKE_FINISHED != handshakeState)
_handshake();
_transmit();
}
}