view modules/bluespec/Pygar/core/AudioCoreSystem.cpp @ 76:8bd0e4d37ad2 pygar svn.77 tip

[svn r77] I don't know why my last change didn't go through grumble grumble....
author rlm
date Wed, 12 May 2010 08:58:23 -0400
parents a15cc186e07d
children
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 2
15 pthread_mutex_t CONNECTED_APPLICATION_CLASS::lock;
16 pthread_cond_t CONNECTED_APPLICATION_CLASS::cond;
17 sem_t CONNECTED_APPLICATION_CLASS::throttle;
20 // constructor
21 CONNECTED_APPLICATION_CLASS::CONNECTED_APPLICATION_CLASS(VIRTUAL_PLATFORM vp) :
23 clientStub(new AUDIOCORERRR_CLIENT_STUB_CLASS(this))
24 {
25 printf("SendSide Constructed\n");
26 }
28 // destructor
29 CONNECTED_APPLICATION_CLASS::~CONNECTED_APPLICATION_CLASS()
30 {
31 }
33 // init
34 void
35 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 stats
44 //STATS_DEVICE_SERVER_CLASS::GetInstance()->SetupStats();
45 }
47 //This isn't called.
48 void
49 CONNECTED_APPLICATION_CLASS::UpdateSemaphore()
50 {
51 printf("UpdateSemaphore\n");
52 sem_post(&throttle);
53 }
55 void
56 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 here
62 pthread_cond_signal(&cond);
63 pthread_mutex_unlock(&lock);
64 printf("EndSimulation done\n");
65 fflush(stdout);
66 }
68 bool CONNECTED_APPLICATION_CLASS::areAllFilesComplete(int numVoices, bool isFini[])
69 {
70 bool result = false;
71 for (int n = 0; n < numVoices; n++)
72 result |= isFini[n];
73 return result;
74 }
76 // main
77 void
78 CONNECTED_APPLICATION_CLASS::Main()
79 {
81 //rlm: two files
82 FILE *inputFiles[NUM_VOICES];
83 UINT16 sample;
85 bool fileFini[NUM_VOICES];
86 UINT16 channel[NUM_VOICES];
87 for (int n = 0; n < NUM_VOICES; n++)
88 {
89 fileFini[n] = false;
90 channel[n] = n;
91 }
93 fflush(stdout);
95 // Convert input wav to pcm
97 printf("word!\n");
99 for (int i = 0; i < NUM_VOICES; i++)
100 {
101 char filename[256];
102 sprintf(filename, "input%d", i);
103 char wavname[256];
104 strcpy(wavname, filename);
105 strcat(wavname, ".wav");
106 char pcmname[256];
107 strcpy(pcmname, filename);
108 strcat(pcmname, ".pcm");
110 printf("Opening audio file %s and creating %s.\n", wavname, pcmname);
111 inputFiles[i] = fopen(wavname, "r");
113 generate_pcm(wavname, pcmname);
115 assert(inputFiles[i]);
116 }
118 //init processor
119 int sleepCount = 0;
120 int result = 0;
121 int count = 0;
123 int numCoreFini = 0;
125 printf("SendSide Main\n");
126 fflush(stdout);
128 // Convert input wav to pcm
130 sleep(2);
131 printf("main:samples about to start sending %d\n", count);
133 //rlm: here we read both files. later refactor into a function.
134 // also, this will terminate when the FIRST file reaches its end.
135 while(!areAllFilesComplete(NUM_VOICES, fileFini))
136 {
137 sem_wait(&throttle);
139 if(count%1000 == 0)
140 printf("main: %d\n", count);
141 count++;
143 //rlm: two files.
144 for (int i = 0; i < NUM_VOICES; i++)
145 {
146 if (!fileFini[i])
147 {
148 if (fread(&sample, 2, 1, inputFiles[i]))
149 clientStub->SendUnprocessedStream((UINT32)channel[i] , Data,(UINT32)sample);
150 else{
151 fileFini[i] = true;
152 numCoreFini++;
153 if (numCoreFini >= NUM_VOICES) // Need lock on last eof
154 // Need to put lock here to prevent potential race condition
155 pthread_mutex_lock(&lock);
156 clientStub->SendUnprocessedStream((UINT32)channel[i], EndOfFile,0);
157 }
158 }
159 }
160 }
162 printf("main: out of loop\n");
163 printf("main: wait for end of file\n");
165 pthread_cond_wait(&cond, &lock);
166 pthread_mutex_unlock(&lock);
168 printf("main: last data out\n");
170 // Convert input wav to pcm
171 // this part is ok because there is always only one input file.
172 generate_wav("out_hw.pcm","input0.wav","out_hw.wav");
174 printf("generate wav done\n");
176 // printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n");
177 // Dump the stats file
179 STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats();
180 STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile();
182 fflush(stdout);
183 // exit(0);
184 }