Skip to content

Commit

Permalink
Movements in list, server refactoring, minor changes
Browse files Browse the repository at this point in the history
+ sending key presses to server
  • Loading branch information
ondracek-lukas committed May 5, 2020
1 parent 61fc568 commit 04d6c16
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 143 deletions.
93 changes: 65 additions & 28 deletions client.c
@@ -1,13 +1,14 @@
// Virtual Choir Rehearsal Room Copyright (C) 2020 Lukas Ondracek <ondracek.lukas@gmail.com>, use under GNU GPLv3

#include "main.h"

#include <string.h>
#include <pthread.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <float.h>

#include "main.h"
#include "stereoBuffer.h"
#include "net.h"
#include "tty.h"
Expand All @@ -20,6 +21,11 @@ pthread_t inputThread, outputThread, udpThread;
uint8_t clientID;
float aioLat = 0;
float dBAdj = 20;
char sHeloStr[SHELO_STR_LEN+1];
char *serverKeys;
char *serverKeysDesc;
char *clientKeysDesc;


volatile enum inputMode {
INPUT_DISCARD,
Expand Down Expand Up @@ -55,10 +61,10 @@ static void *outputWorker(void *none) {
char str[200];
char *s = str;

s += sprintf(s, "%-20s ", "system level:");
s += sprintf(s, "%-21s ", "system level:");
ttyFormatSndLevel(&s, dBAvg, dBPeak);
*s++ = '\n';
s += sprintf(s, "%-20s ", "adjusted level:");
s += sprintf(s, "%-21s ", "adjusted level:");
ttyFormatSndLevel(&s, dBAvg + dBAdj, dBPeak + dBAdj);

ttyResetStatus();
Expand Down Expand Up @@ -107,7 +113,7 @@ static void *inputWorker(void *none) {
switch (inputMode) {
case INPUT_SEND:
packet.blockIndex = blockIndex++;
packet.recBlockIndex = outputBuffer.readPos; // XXX check latency calc
packet.playBlockIndex = outputBuffer.readPos; // XXX check latency calc
send(udpSocket, (void *)&packet, sizeof(packet), 0);
break;
case INPUT_TO_OUTPUT:
Expand Down Expand Up @@ -139,15 +145,25 @@ static void *udpReceiver(void *none) {
union packet *packet = (union packet *) &packetRaw;
ssize_t size;
int statusIndex = -1;
char packetsCnt = 0;
uint8_t packetsCnt = 0;
bool packetsReceived[256];

while ((size = recv(udpSocket, packetRaw, sizeof(union packet), 0)) >= 0) {
switch (packetRaw[0]) {
case PACKET_HELO:
if (size != sizeof(struct packetServerHelo)) break;
packetRaw[size] = '\0';
clientID = packet->sHelo.clientID;
sbufferClear(&outputBuffer, packet->sHelo.initBlockIndex);
strncpy(sHeloStr, packet->sHelo.str, SHELO_STR_LEN+1);
sHeloStr[SHELO_STR_LEN]='\0';
serverKeys = sHeloStr;
serverKeysDesc = strchr(sHeloStr, '\n');
if (*serverKeysDesc == '\n') {
*serverKeysDesc++ = '\0';
} else {
serverKeysDesc = "\0";
}
clientKeysDesc = "[^C] exit";
printf("Connected.\n");
fflush(stdout);
__sync_synchronize();
Expand All @@ -162,12 +178,14 @@ static void *udpReceiver(void *none) {
if ((int)packet->sStat.statusIndex > statusIndex) {
statusIndex = packet->sStat.statusIndex;
packetsCnt = packet->sStat.packetsCnt;
for (int i = 0; i < packetsCnt; i++) {
for (int i = 0; i < 256; i++) {
packetsReceived[i] = false;
}
ttyResetStatus();
} else if (packet->sStat.statusIndex < statusIndex) {
break;
} else if (packetsCnt > packet->sStat.packetsCnt) {
packetsCnt = packet->sStat.packetsCnt;
}
packetsReceived[packet->sStat.packetIndex] = true;
ttyUpdateStatus(packet->sStat.str, STATUS_LINES_PER_PACKET * packet->sStat.packetIndex + 1);
Expand All @@ -178,15 +196,17 @@ static void *udpReceiver(void *none) {
}
}
if (complete) {
ttyPrintStatus(); // XXX different lines count
ttyUpdateStatus(serverKeysDesc, ttyStatusLines);
ttyUpdateStatus(clientKeysDesc, ttyStatusLines);
ttyPrintStatus();
}
break;
}
}
ttyClearStatus();
udpState = UDP_CLOSED;
inputMode = INPUT_DISCARD;
printf("Connection lost, connect again? (y/n): ");
printf("\nConnection lost, connect again? (y/n): ");
fflush(stdout);
}

Expand Down Expand Up @@ -231,7 +251,7 @@ int main() {
printf("\n== 2/4 == MEASURE DELAY OF SOUND SYSTEM =======================================\n\n");

printf(
"Now, take your headphones OFF your ears,\n"
"Take your headphones OFF your ears,\n"
"place microphone closer to audio source if possible,\n"
"and press space to continue...\n"
"\n"
Expand All @@ -240,9 +260,8 @@ int main() {
"\n"
"In case of failure, you can use loudspeaker temporarily.\n"
"\n"
"Later you will see this delay + all the remaining delay in both directions\n"
"(in application, network, etc.).\n");

"Later you will see delay in form `A+B ms`, where A is this delay\n"
"while B is all the remaining round-trip delay (in application, network, etc.).\n");

ttyReadKey();
do {
Expand Down Expand Up @@ -288,22 +307,24 @@ int main() {
"Connect your headphones and press space...\n");
ttyReadKey();
printf(
"---\n\n"
"Sing loudly for a while, then press space to continue...\n"
"\n"
"You may hear the sound being recorded by your microphone\n"
"and see its average (#) and peak (+) intensity levels on the scales below.\n"
"Set your microphone volume in system settings so that\n"
"it's peak intensity never exceeds -10 dB on the first scale; -------------+\n"
"test it by loud singing. |\n"
"At the same time further intensity adjustments will be performed |\n"
"and displayed on the second scale; |\n"
"the average level of laud singing on this scale may be just below -20 dB. |\n"
"Also set your microphone position |\n"
"to hear your voice but not your breathing. |\n"
" |\n"
"Press r to reset adjustments and space when done... average peak\n"
" -78 -20 -10 0 | |\n"
" | | | | V V\n");
// [########++++++------------------------] -xx dB ( -xx dB)
"it's peak intensity never exceeds -10 dB on the first scale; --------------+\n"
"test it by loud singing. |\n"
"At the same time further intensity adjustments will be performed |\n"
"and displayed on the second scale; |\n"
"the average level of laud singing on this scale may be just below -20 dB. |\n"
"Also set your microphone position |\n"
"to hear your voice but not your breathing. |\n"
" |\n"
"Press r to reset adjustments (and space when done)... average peak\n"
" -78 -20 -10 0 | |\n"
" | | | | V V\n");
// [########++++++------------------------] -xx dB ( -xx dB)

sbufferOutputStatsReset(&outputBuffer, true);
inputMode = INPUT_TO_OUTPUT;
Expand All @@ -330,6 +351,12 @@ int main() {


printf("\n== 4/4 == SERVER SETTINGS =====================================================\n\n");
printf(
"Disclaimer:\n"
" After entering server address,\n"
" sound being recorded as well as all key presses to this application\n"
" may be send unencrypted to the server.\n\n");

inputMode = INPUT_DISCARD;

char name[NAME_LEN+1];
Expand All @@ -354,7 +381,7 @@ int main() {
.dBAdj = dBAdj
};
strcpy(packet.name, name);
ssize_t err = send(udpSocket, (void *)&packet, (void *)strchr(packet.name, '\0') - (void *)&packet, 0);
send(udpSocket, (void *)&packet, (void *)strchr(packet.name, '\0') - (void *)&packet, 0);
}

udpState = UDP_OPEN;
Expand All @@ -366,7 +393,16 @@ int main() {
switch (udpState) {
case UDP_OPEN:
switch (c) {
case 'q': goto EXIT;
default:
if (strchr(serverKeys, c)) {
struct packetKeyPress packet = {
.type = PACKET_KEY_PRESS,
.clientID = clientID,
.playBlockIndex = outputBuffer.readPos,
.key = c
};
send(udpSocket, (void *)&packet, sizeof(struct packetKeyPress), 0);
}
}; break;
case UDP_CLOSED:
switch (c) {
Expand All @@ -390,11 +426,12 @@ int main() {
inputMode = INPUT_END;
outputMode = OUTPUT_END;
pthread_join(inputThread, NULL);
pthread_join(outputThread, NULL);
pthread_join(udpThread, NULL);
Pa_StopStream(paInputStream);
Pa_StopStream(paOutputStream);
Pa_Terminate();
netCleanup();

printf("n\n");
printf("\n");
}
7 changes: 4 additions & 3 deletions main.h
Expand Up @@ -2,9 +2,9 @@

#define _GNU_SOURCE

#define PROT_VERSION 2
#define APP_VERSION 0.2
#define UDP_PORT 1234
#define PROT_VERSION 3
#define APP_VERSION 0.3
#define UDP_PORT 64199
#define NAME_LEN 10
#define MAX_CLIENTS 100
#define BLOCKS_PER_STAT 100
Expand All @@ -22,6 +22,7 @@
#define STATUS_WIDTH 79
#define STATUS_HEIGHT 100
#define STATUS_LINES_PER_PACKET 4
#define SHELO_STR_LEN 500

#define STR(arg) STR2(arg)
#define STR2(arg) #arg
Expand Down
17 changes: 13 additions & 4 deletions net.h
Expand Up @@ -17,7 +17,8 @@
enum packetType {
PACKET_HELO,
PACKET_DATA,
PACKET_STATUS
PACKET_STATUS,
PACKET_KEY_PRESS
};

struct packetClientHelo {
Expand All @@ -31,11 +32,12 @@ struct packetServerHelo {
char type;
uint8_t clientID;
bindex_t initBlockIndex;
char str[SHELO_STR_LEN]; // "keys\nhelp"
};
struct packetClientData {
char type;
uint8_t clientID;
bindex_t recBlockIndex; // server index to be played on the client side
bindex_t playBlockIndex; // server index to be played on the client side
bindex_t blockIndex;
sample_t block[MONO_BLOCK_SIZE];
};
Expand All @@ -46,19 +48,26 @@ struct packetServerData {
};
struct packetStatusStr {
char type;
char packetIndex;
char packetsCnt;
uint8_t packetIndex;
uint8_t packetsCnt;
bindex_t statusIndex;
char str[STATUS_LINES_PER_PACKET * (STATUS_WIDTH + 1)];
// (size of whole IP packet might be limited to 576 B; IP header is 20--60 B, UDP header is 8 B)
};
struct packetKeyPress {
char type;
uint8_t clientID;
bindex_t playBlockIndex;
int key;
};

union packet {
struct packetClientHelo cHelo;
struct packetServerHelo sHelo;
struct packetClientData cData;
struct packetServerData sData;
struct packetStatusStr sStat;
struct packetKeyPress cKeyP;
};


Expand Down

0 comments on commit 04d6c16

Please sign in to comment.