diff AudioProcessor.cpp @ 16:7e1510b47336 pygar svn.17

[svn r17] added rest of items for core
author punk
date Tue, 27 Apr 2010 22:54:50 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/AudioProcessor.cpp	Tue Apr 27 22:54:50 2010 -0400
     1.3 @@ -0,0 +1,104 @@
     1.4 +#include <stdio.h>
     1.5 +#include <pthread.h>
     1.6 +#include <semaphore.h>
     1.7 +
     1.8 +#include "asim/provides/connected_application.h"
     1.9 +//#include "asim/provides/SndfileWavUtil.h"
    1.10 +
    1.11 +#include "asim/rrr/client_stub_AUDIOPROCESSORRRR.h"
    1.12 +
    1.13 +using namespace std;
    1.14 +
    1.15 +pthread_mutex_t CONNECTED_APPLICATION_CLASS::lock;
    1.16 +pthread_cond_t  CONNECTED_APPLICATION_CLASS::cond;
    1.17 +sem_t CONNECTED_APPLICATION_CLASS::throttle;
    1.18 +
    1.19 +// constructor
    1.20 +CONNECTED_APPLICATION_CLASS::CONNECTED_APPLICATION_CLASS(VIRTUAL_PLATFORM vp) :
    1.21 +    clientStub(new AUDIOPROCESSORRRR_CLIENT_STUB_CLASS(this))
    1.22 +{
    1.23 +}
    1.24 +
    1.25 +// destructor
    1.26 +CONNECTED_APPLICATION_CLASS::~CONNECTED_APPLICATION_CLASS()
    1.27 +{
    1.28 +}
    1.29 +
    1.30 +// init
    1.31 +void
    1.32 +CONNECTED_APPLICATION_CLASS::Init()
    1.33 +{
    1.34 +
    1.35 +  pthread_mutex_init(&lock, NULL);
    1.36 +  pthread_cond_init(&cond, NULL);
    1.37 +  sem_init(&throttle, 0, 64);
    1.38 +
    1.39 +}
    1.40 +
    1.41 +void
    1.42 +CONNECTED_APPLICATION_CLASS::UpdateSemaphore()
    1.43 +{
    1.44 +  sem_post(&throttle);
    1.45 +}
    1.46 +
    1.47 +void
    1.48 +CONNECTED_APPLICATION_CLASS::EndSimulation()
    1.49 +{
    1.50 +  printf("EndSimulation Called\n");
    1.51 +  fflush(stdout);
    1.52 +  pthread_mutex_lock(&lock);
    1.53 +  // Do something about the race occuring here
    1.54 +  pthread_cond_signal(&cond); 
    1.55 +  pthread_mutex_unlock(&lock);
    1.56 +  printf("EndSimulation done\n");
    1.57 +  fflush(stdout);  
    1.58 +}
    1.59 +
    1.60 +// main
    1.61 +void
    1.62 +CONNECTED_APPLICATION_CLASS::Main()
    1.63 +{
    1.64 +  FILE *inputFile;
    1.65 +  UINT16 sample;
    1.66 +  
    1.67 +  // Convert input wav to pcm
    1.68 +  generate_pcm("input.wav","input.pcm");
    1.69 +
    1.70 +  //Send data to the machine here.
    1.71 +  inputFile = fopen("input.pcm","r");
    1.72 +  assert(inputFile);
    1.73 +
    1.74 +
    1.75 +  int count = 0;
    1.76 +
    1.77 +  printf("main: about to enter loop %d\n", count);  
    1.78 +
    1.79 +  while(fread(&sample, 2, 1, inputFile)) {
    1.80 +    if(count%1000 == 0)
    1.81 +      printf("main: %d\n", count);
    1.82 +    count++;
    1.83 +    sem_wait(&throttle);
    1.84 +    clientStub->SendUnprocessedStream(Data,(UINT32)sample);
    1.85 +  } 
    1.86 +
    1.87 +  printf("main: out of loop\n");
    1.88 +
    1.89 +  // Need to put lock here to prevent potential race condition
    1.90 +  pthread_mutex_lock(&lock);
    1.91 +  clientStub->SendUnprocessedStream(EndOfFile,0);
    1.92 +
    1.93 +  printf("main: wait for end of file\n");
    1.94 +
    1.95 +  pthread_cond_wait(&cond, &lock);
    1.96 +  pthread_mutex_unlock(&lock);
    1.97 +
    1.98 +  printf("main: lastt data out\n");
    1.99 +
   1.100 +  // Convert input wav to pcm
   1.101 +  generate_wav("out_hw.pcm","input.wav","out_hw.wav");
   1.102 +
   1.103 +  printf("generate wav done\n");
   1.104 +
   1.105 +  fflush(stdout);
   1.106 +  exit(0);
   1.107 +}