send each client a different mix without themselves

This commit is contained in:
Stephen Birarda 2013-02-04 08:35:49 -08:00
parent e5b374c631
commit 0787692014

View file

@ -11,6 +11,7 @@
#include <pthread.h> #include <pthread.h>
#include <errno.h> #include <errno.h>
#include <fstream> #include <fstream>
#include <limits>
const int MAX_AGENTS = 1000; const int MAX_AGENTS = 1000;
const int LOGOFF_CHECK_INTERVAL = 1000; const int LOGOFF_CHECK_INTERVAL = 1000;
@ -28,7 +29,8 @@ const int MIN_SAMPLE_VALUE = std::numeric_limits<int16_t>::min();
const int MAX_SOURCE_BUFFERS = 10; const int MAX_SOURCE_BUFFERS = 10;
int16_t* wc_noise_buffer; int16_t* whiteNoiseBuffer;
int whiteNoiseLength;
#define ECHO_DEBUG_MODE 0 #define ECHO_DEBUG_MODE 0
@ -143,6 +145,7 @@ void *send_buffer_thread(void *args)
timeval now; timeval now;
int16_t *clientMix = new int16_t[BUFFER_LENGTH_SAMPLES]; int16_t *clientMix = new int16_t[BUFFER_LENGTH_SAMPLES];
int16_t *masterMix = new int16_t[BUFFER_LENGTH_SAMPLES];
gettimeofday(&firstSend, NULL); gettimeofday(&firstSend, NULL);
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
@ -157,10 +160,16 @@ void *send_buffer_thread(void *args)
sentBytes = 0; sentBytes = 0;
int sampleOffset = floor(diffclock(firstSend, now) * (SAMPLE_RATE / 1000) + 0.5); int sampleOffset = floor(diffclock(firstSend, now) * (SAMPLE_RATE / 1000) + 0.5);
memcpy(clientMix, wc_noise_buffer + sampleOffset, BUFFER_LENGTH_BYTES); sampleOffset = sampleOffset % whiteNoiseLength;
memcpy(masterMix, whiteNoiseBuffer + sampleOffset, BUFFER_LENGTH_BYTES);
for (int i = 0; i < num_agents; i++) {
if (agents[i].active) {
memcpy(clientMix, masterMix, BUFFER_LENGTH_BYTES);
for (int b = 0; b < MAX_SOURCE_BUFFERS; b++) { for (int b = 0; b < MAX_SOURCE_BUFFERS; b++) {
if (!sourceBuffers[b].transmitted) { if (b != i && !sourceBuffers[b].transmitted) {
for (int s = 0; s < BUFFER_LENGTH_SAMPLES; s++) { for (int s = 0; s < BUFFER_LENGTH_SAMPLES; s++) {
// we have source buffer data for this sample // we have source buffer data for this sample
int mixSample = clientMix[s] + sourceBuffers[b].sourceAudioData[s]; int mixSample = clientMix[s] + sourceBuffers[b].sourceAudioData[s];
@ -168,15 +177,13 @@ void *send_buffer_thread(void *args)
int sampleToAdd = std::max(mixSample, MIN_SAMPLE_VALUE); int sampleToAdd = std::max(mixSample, MIN_SAMPLE_VALUE);
sampleToAdd = std::min(sampleToAdd, MAX_SAMPLE_VALUE); sampleToAdd = std::min(sampleToAdd, MAX_SAMPLE_VALUE);
clientMix[s] += sampleToAdd; clientMix[s] = sampleToAdd;
} }
sourceBuffers[b].transmitted = true; sourceBuffers[b].transmitted = true;
} }
} }
for (int i = 0; i < num_agents; i++) {
if (agents[i].active) {
sockaddr_in dest_address = agents[i].agent_addr; sockaddr_in dest_address = agents[i].agent_addr;
sentBytes = sendto(handle, clientMix, BUFFER_LENGTH_BYTES, sentBytes = sendto(handle, clientMix, BUFFER_LENGTH_BYTES,
@ -222,16 +229,16 @@ bool different_clients(sockaddr_in addr1, sockaddr_in addr2)
void white_noise_buffer_init() { void white_noise_buffer_init() {
// open a pointer to the audio file // open a pointer to the audio file
FILE *whiteNoiseFile = fopen("workclub.raw", "r"); FILE *whiteNoiseFile = fopen("wild.raw", "r");
// get length of file // get length of file
std::fseek(whiteNoiseFile, 0, SEEK_END); std::fseek(whiteNoiseFile, 0, SEEK_END);
int lengthInSamples = std::ftell(whiteNoiseFile) / sizeof(int16_t); whiteNoiseLength = std::ftell(whiteNoiseFile) / sizeof(int16_t);
std::rewind(whiteNoiseFile); std::rewind(whiteNoiseFile);
// read that amount of samples from the file // read that amount of samples from the file
wc_noise_buffer = new int16_t[lengthInSamples]; whiteNoiseBuffer = new int16_t[whiteNoiseLength];
std::fread(wc_noise_buffer, sizeof(int16_t), lengthInSamples, whiteNoiseFile); std::fread(whiteNoiseBuffer, sizeof(int16_t), whiteNoiseLength, whiteNoiseFile);
// close it // close it
std::fclose(whiteNoiseFile); std::fclose(whiteNoiseFile);