Mercurial > pygar
view modules/bluespec/Pygar/core/AudioCoreSystem.cpp @ 74:31fef269ae58 pygar svn.75
[svn r75] presentation for wednesdaty
author | rlm |
---|---|
date | Wed, 12 May 2010 02:25:34 -0400 |
parents | 34fc182a1daa |
children | a15cc186e07d |
line wrap: on
line source
1 #include <stdio.h>2 #include <pthread.h>3 #include <semaphore.h>5 #include "asim/provides/connected_application.h"6 #include "asim/provides/stats_device.h"7 //#include "asim/provides/SndfileWavUtil.h"9 #include "asim/rrr/client_stub_AUDIOCORERRR.h"11 using namespace std;13 #define NUM_VOICES 215 pthread_mutex_t CONNECTED_APPLICATION_CLASS::lock;16 pthread_cond_t CONNECTED_APPLICATION_CLASS::cond;17 sem_t CONNECTED_APPLICATION_CLASS::throttle;20 // constructor21 CONNECTED_APPLICATION_CLASS::CONNECTED_APPLICATION_CLASS(VIRTUAL_PLATFORM vp) :23 clientStub(new AUDIOCORERRR_CLIENT_STUB_CLASS(this))24 {25 printf("SendSide Created\n");26 }28 // destructor29 CONNECTED_APPLICATION_CLASS::~CONNECTED_APPLICATION_CLASS()30 {31 }33 // init34 void35 CONNECTED_APPLICATION_CLASS::Init()36 {38 printf("InitSend Side\n");39 pthread_mutex_init(&lock, NULL);40 pthread_cond_init(&cond, NULL);41 sem_init(&throttle, 0, 256);43 // enable stats44 //STATS_DEVICE_SERVER_CLASS::GetInstance()->SetupStats();45 }47 //This isn't called.48 void49 CONNECTED_APPLICATION_CLASS::UpdateSemaphore()50 {51 printf("UpdateSemaphore\n");52 sem_post(&throttle);53 }55 void56 CONNECTED_APPLICATION_CLASS::EndSimulation()57 {58 printf("EndSimulation Called\n");59 fflush(stdout);60 pthread_mutex_lock(&lock);61 // Do something about the race occuring here62 pthread_cond_signal(&cond);63 pthread_mutex_unlock(&lock);64 printf("EndSimulation done\n");65 fflush(stdout);66 }68 // main69 void70 CONNECTED_APPLICATION_CLASS::Main()71 {72 //rlm: two files73 FILE *inputFile;74 FILE *inputFile1;75 UINT16 sample;76 UINT16 sample1;78 bool fileFini[NUM_VOICES];79 for (int n = 0; n < NUM_VOICES; n++)80 fileFini[n] = false;82 //rlm: not sure if normal ints are ok here; using UINT16 because I know it works83 UINT16 channel0 = 0;84 UINT16 channel1 = 1;86 //init processor87 int sleepCount = 0;88 int result = 0;90 int numCoreFini = 0;92 printf("SendSide Main\n");93 fflush(stdout);95 // Convert input wav to pcm97 //rlm: for now we are going to going to just have 2 files with "common names"99 generate_pcm("input.wav","input.pcm");100 generate_pcm("input1.wav", "input1.pcm");103 //Send data to the machine here.104 //rlm: two files105 inputFile = fopen("input.pcm","r");106 inputFile1 = fopen("input1.pcm", "r");108 assert(inputFile1);109 assert(inputFile);111 int count = 0;113 sleep(1);114 printf("main:samples about to start sending %d\n", count);116 //rlm: here we read both files. later refactor into a function.117 // also, this will terminate when the FIRST file reaches its end.118 while( !feof(inputFile) || !feof(inputFile1))119 {121 sem_wait(&throttle);123 if(count%1000 == 0)124 printf("main: %d\n", count);125 count++;127 //rlm: two files.129 if (!fileFini[0])130 {131 if (fread(&sample, 2, 1, inputFile))132 clientStub->SendUnprocessedStream((UINT32)channel0 , Data,(UINT32)sample);133 else{134 fileFini[0] = true;135 numCoreFini++;136 if (numCoreFini >= NUM_VOICES) // Need lock on last eof137 // Need to put lock here to prevent potential race condition138 pthread_mutex_lock(&lock);139 clientStub->SendUnprocessedStream((UINT32)channel0, EndOfFile,0);140 }141 }143 if (!fileFini[1])144 {145 if (fread(&sample1, 2 , 1, inputFile1))146 clientStub->SendUnprocessedStream((UINT32)channel1 , Data,(UINT32)sample1);147 else148 {149 fileFini[1] = true;150 numCoreFini++;151 if (numCoreFini >= NUM_VOICES) // Need lock on last eof152 // Need to put lock here to prevent potential race condition153 pthread_mutex_lock(&lock);154 clientStub->SendUnprocessedStream((UINT32)channel1, EndOfFile,0);155 }156 }157 }159 printf("main: out of loop\n");160 printf("main: wait for end of file\n");162 pthread_cond_wait(&cond, &lock);163 pthread_mutex_unlock(&lock);165 printf("main: last data out\n");167 // Convert input wav to pcm168 // this part is ok because there is always only one input file.169 generate_wav("out_hw.pcm","input.wav","out_hw.wav");171 printf("generate wav done\n");173 // printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n");174 // Dump the stats file176 STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats();177 STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile();179 fflush(stdout);180 // exit(0);181 }