punk@13: #include punk@13: #include punk@13: #include punk@13: punk@13: #include "asim/provides/connected_application.h" punk@13: #include "asim/provides/stats_device.h" punk@13: //#include "asim/provides/SndfileWavUtil.h" punk@13: punk@13: #include "asim/rrr/client_stub_AUDIOCORERRR.h" punk@13: punk@13: using namespace std; punk@13: punk@68: #define NUM_VOICES 2 punk@68: punk@13: pthread_mutex_t CONNECTED_APPLICATION_CLASS::lock; punk@13: pthread_cond_t CONNECTED_APPLICATION_CLASS::cond; punk@13: sem_t CONNECTED_APPLICATION_CLASS::throttle; punk@13: punk@68: punk@13: // constructor punk@13: CONNECTED_APPLICATION_CLASS::CONNECTED_APPLICATION_CLASS(VIRTUAL_PLATFORM vp) : rlm@23: rlm@23: clientStub(new AUDIOCORERRR_CLIENT_STUB_CLASS(this)) punk@13: { punk@64: printf("SendSide Created\n"); punk@13: } punk@13: punk@13: // destructor punk@13: CONNECTED_APPLICATION_CLASS::~CONNECTED_APPLICATION_CLASS() punk@13: { punk@13: } punk@13: punk@13: // init punk@13: void punk@13: CONNECTED_APPLICATION_CLASS::Init() punk@13: { punk@13: punk@64: printf("InitSend Side\n"); punk@13: pthread_mutex_init(&lock, NULL); punk@13: pthread_cond_init(&cond, NULL); punk@65: sem_init(&throttle, 0, 256); punk@13: punk@13: // enable stats punk@64: //STATS_DEVICE_SERVER_CLASS::GetInstance()->SetupStats(); punk@13: } punk@13: punk@57: //This isn't called. punk@13: void punk@13: CONNECTED_APPLICATION_CLASS::UpdateSemaphore() punk@13: { punk@64: printf("UpdateSemaphore\n"); punk@13: sem_post(&throttle); punk@13: } punk@13: punk@13: void punk@13: CONNECTED_APPLICATION_CLASS::EndSimulation() punk@13: { punk@13: printf("EndSimulation Called\n"); punk@13: fflush(stdout); punk@13: pthread_mutex_lock(&lock); punk@13: // Do something about the race occuring here punk@13: pthread_cond_signal(&cond); punk@13: pthread_mutex_unlock(&lock); punk@13: printf("EndSimulation done\n"); punk@13: fflush(stdout); punk@13: } punk@13: punk@13: // main punk@13: void punk@13: CONNECTED_APPLICATION_CLASS::Main() punk@13: { rlm@34: //rlm: two files punk@13: FILE *inputFile; punk@52: FILE *inputFile1; punk@13: UINT16 sample; punk@52: UINT16 sample1; rlm@34: punk@68: Bool fileFini[NUM_VOICES] = {false, false}; rlm@34: //rlm: not sure if normal ints are ok here; using UINT16 because I know it works punk@52: UINT16 channel0 = 0; punk@52: UINT16 channel1 = 1; punk@31: punk@31: //init processor punk@31: int sleepCount = 0; punk@31: int result = 0; punk@31: punk@68: int numcCoreFin = 0; punk@31: punk@64: printf("SendSide Main\n"); punk@31: fflush(stdout); punk@31: punk@13: // Convert input wav to pcm rlm@34: rlm@34: //rlm: for now we are going to going to just have 2 files with "common names" rlm@34: punk@13: generate_pcm("input.wav","input.pcm"); punk@52: generate_pcm("input1.wav", "input1.pcm"); rlm@34: punk@13: punk@13: //Send data to the machine here. rlm@34: //rlm: two files punk@13: inputFile = fopen("input.pcm","r"); punk@53: inputFile1 = fopen("input1.pcm", "r"); rlm@34: punk@52: assert(inputFile1); punk@13: assert(inputFile); punk@13: punk@13: int count = 0; punk@13: punk@65: sleep(1); punk@57: printf("main:samples about to start sending %d\n", count); punk@13: rlm@34: //rlm: here we read both files. later refactor into a function. rlm@34: // also, this will terminate when the FIRST file reaches its end. punk@68: while( !feof(inputFile) || !feof(inputFile1)) punk@68: { punk@68: punk@65: sem_wait(&throttle); punk@65: punk@37: if(count%1000 == 0) punk@37: printf("main: %d\n", count); punk@37: count++; punk@37: rlm@34: //rlm: two files. punk@68: punk@68: if (!fileFini[0]) punk@68: { punk@68: if (fread(&sample, 2, 1, inputFile)) punk@68: clientStub->SendUnprocessedStream((UINT32)channel0 , Data,(UINT32)sample); punk@68: else{ punk@68: fileFini[0] = true; punk@68: numCoreFini++; punk@68: if (numCoreFini >= NUM_VOICES) // Need lock on last eof punk@68: // Need to put lock here to prevent potential race condition punk@68: pthread_mutex_lock(&lock); punk@68: clientStub->SendUnprocessedStream((UINT32)channel0, EndOfFile,0); punk@68: } punk@68: } punk@68: if !fileFini[1]) punk@68: { punk@68: if (fread(&sample1, 2 , 1, inputFile1)) punk@68: clientStub->SendUnprocessedStream((UINT32)channel1 , Data,(UINT32)sample1); punk@68: else{ punk@68: fileFini[1] = true; punk@68: numCoreFini++; punk@68: if (numCoreFini >= NUM_VOICES) // Need lock on last eof punk@68: // Need to put lock here to prevent potential race condition punk@68: pthread_mutex_lock(&lock); punk@68: clientStub->SendUnprocessedStream((UINT32)channel1, EndOfFile,0); punk@68: } punk@68: } punk@68: } punk@13: punk@13: printf("main: out of loop\n"); punk@13: printf("main: wait for end of file\n"); punk@13: punk@13: pthread_cond_wait(&cond, &lock); punk@13: pthread_mutex_unlock(&lock); punk@13: rlm@23: printf("main: last data out\n"); punk@13: punk@13: // Convert input wav to pcm rlm@34: // this part is ok because there is always only one input file. punk@13: generate_wav("out_hw.pcm","input.wav","out_hw.wav"); punk@13: punk@13: printf("generate wav done\n"); punk@13: punk@50: // printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n"); punk@13: // Dump the stats file punk@13: punk@13: STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats(); punk@13: STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile(); punk@13: punk@31: fflush(stdout); punk@53: // exit(0); punk@31: }