punk@13
|
1 #include <stdio.h>
|
punk@13
|
2 #include <pthread.h>
|
punk@13
|
3 #include <semaphore.h>
|
punk@13
|
4
|
punk@13
|
5 #include "asim/provides/connected_application.h"
|
punk@13
|
6 #include "asim/provides/stats_device.h"
|
punk@13
|
7 //#include "asim/provides/SndfileWavUtil.h"
|
punk@13
|
8
|
punk@13
|
9 #include "asim/rrr/client_stub_AUDIOCORERRR.h"
|
punk@13
|
10
|
punk@13
|
11 using namespace std;
|
punk@13
|
12
|
punk@68
|
13 #define NUM_VOICES 2
|
punk@68
|
14
|
punk@13
|
15 pthread_mutex_t CONNECTED_APPLICATION_CLASS::lock;
|
punk@13
|
16 pthread_cond_t CONNECTED_APPLICATION_CLASS::cond;
|
punk@13
|
17 sem_t CONNECTED_APPLICATION_CLASS::throttle;
|
punk@13
|
18
|
punk@68
|
19
|
punk@13
|
20 // constructor
|
punk@13
|
21 CONNECTED_APPLICATION_CLASS::CONNECTED_APPLICATION_CLASS(VIRTUAL_PLATFORM vp) :
|
rlm@23
|
22
|
rlm@23
|
23 clientStub(new AUDIOCORERRR_CLIENT_STUB_CLASS(this))
|
punk@13
|
24 {
|
punk@75
|
25 printf("SendSide Constructed\n");
|
punk@13
|
26 }
|
punk@13
|
27
|
punk@13
|
28 // destructor
|
punk@13
|
29 CONNECTED_APPLICATION_CLASS::~CONNECTED_APPLICATION_CLASS()
|
punk@13
|
30 {
|
punk@13
|
31 }
|
punk@13
|
32
|
punk@13
|
33 // init
|
punk@13
|
34 void
|
punk@13
|
35 CONNECTED_APPLICATION_CLASS::Init()
|
punk@13
|
36 {
|
punk@13
|
37
|
punk@64
|
38 printf("InitSend Side\n");
|
punk@13
|
39 pthread_mutex_init(&lock, NULL);
|
punk@13
|
40 pthread_cond_init(&cond, NULL);
|
punk@65
|
41 sem_init(&throttle, 0, 256);
|
punk@13
|
42
|
punk@13
|
43 // enable stats
|
punk@64
|
44 //STATS_DEVICE_SERVER_CLASS::GetInstance()->SetupStats();
|
punk@13
|
45 }
|
punk@13
|
46
|
punk@57
|
47 //This isn't called.
|
punk@13
|
48 void
|
punk@13
|
49 CONNECTED_APPLICATION_CLASS::UpdateSemaphore()
|
punk@13
|
50 {
|
punk@64
|
51 printf("UpdateSemaphore\n");
|
punk@13
|
52 sem_post(&throttle);
|
punk@13
|
53 }
|
punk@13
|
54
|
punk@13
|
55 void
|
punk@13
|
56 CONNECTED_APPLICATION_CLASS::EndSimulation()
|
punk@13
|
57 {
|
punk@13
|
58 printf("EndSimulation Called\n");
|
punk@13
|
59 fflush(stdout);
|
punk@13
|
60 pthread_mutex_lock(&lock);
|
punk@13
|
61 // Do something about the race occuring here
|
punk@13
|
62 pthread_cond_signal(&cond);
|
punk@13
|
63 pthread_mutex_unlock(&lock);
|
punk@13
|
64 printf("EndSimulation done\n");
|
punk@13
|
65 fflush(stdout);
|
punk@13
|
66 }
|
punk@13
|
67
|
punk@75
|
68 bool CONNECTED_APPLICATION_CLASS::areAllFilesComplete(int numVoices, bool isFini[])
|
punk@75
|
69 {
|
punk@75
|
70 bool result = false;
|
punk@75
|
71 for (int n = 0; n < numVoices; n++)
|
punk@75
|
72 result |= isFini[n];
|
punk@75
|
73 return result;
|
punk@75
|
74 }
|
punk@75
|
75
|
punk@13
|
76 // main
|
punk@13
|
77 void
|
punk@13
|
78 CONNECTED_APPLICATION_CLASS::Main()
|
punk@13
|
79 {
|
punk@75
|
80
|
rlm@34
|
81 //rlm: two files
|
punk@75
|
82 FILE *inputFiles[NUM_VOICES];
|
punk@13
|
83 UINT16 sample;
|
rlm@34
|
84
|
punk@72
|
85 bool fileFini[NUM_VOICES];
|
punk@75
|
86 UINT16 channel[NUM_VOICES];
|
punk@72
|
87 for (int n = 0; n < NUM_VOICES; n++)
|
punk@75
|
88 {
|
punk@75
|
89 fileFini[n] = false;
|
punk@75
|
90 channel[n] = n;
|
punk@75
|
91 }
|
punk@72
|
92
|
punk@75
|
93 fflush(stdout);
|
punk@75
|
94
|
punk@75
|
95 // Convert input wav to pcm
|
punk@75
|
96
|
punk@75
|
97 printf("word!\n");
|
punk@75
|
98
|
punk@75
|
99 for (int i = 0; i < NUM_VOICES; i++)
|
punk@75
|
100 {
|
punk@75
|
101 char filename[256];
|
punk@75
|
102 sprintf(filename, "input%d", i);
|
punk@75
|
103 char wavname[256];
|
punk@75
|
104 strcpy(wavname, filename);
|
punk@75
|
105 strcat(wavname, ".wav");
|
punk@75
|
106 char pcmname[256];
|
punk@75
|
107 strcpy(pcmname, filename);
|
punk@75
|
108 strcat(pcmname, ".pcm");
|
punk@75
|
109
|
punk@75
|
110 printf("Opening audio file %s and creating %s.\n", wavname, pcmname);
|
punk@75
|
111 inputFiles[i] = fopen(wavname, "r");
|
punk@75
|
112
|
punk@75
|
113 generate_pcm(wavname, pcmname);
|
punk@75
|
114
|
punk@75
|
115 assert(inputFiles[i]);
|
punk@75
|
116 }
|
punk@31
|
117
|
punk@31
|
118 //init processor
|
punk@31
|
119 int sleepCount = 0;
|
punk@31
|
120 int result = 0;
|
punk@75
|
121 int count = 0;
|
punk@31
|
122
|
punk@72
|
123 int numCoreFini = 0;
|
punk@31
|
124
|
punk@64
|
125 printf("SendSide Main\n");
|
punk@31
|
126 fflush(stdout);
|
punk@31
|
127
|
punk@13
|
128 // Convert input wav to pcm
|
rlm@34
|
129
|
punk@75
|
130 sleep(2);
|
punk@57
|
131 printf("main:samples about to start sending %d\n", count);
|
punk@13
|
132
|
rlm@34
|
133 //rlm: here we read both files. later refactor into a function.
|
rlm@34
|
134 // also, this will terminate when the FIRST file reaches its end.
|
punk@75
|
135 while(!areAllFilesComplete(NUM_VOICES, fileFini))
|
punk@72
|
136 {
|
punk@65
|
137 sem_wait(&throttle);
|
punk@65
|
138
|
punk@37
|
139 if(count%1000 == 0)
|
punk@37
|
140 printf("main: %d\n", count);
|
punk@37
|
141 count++;
|
punk@37
|
142
|
rlm@34
|
143 //rlm: two files.
|
punk@75
|
144 for (int i = 0; i < NUM_VOICES; i++)
|
punk@68
|
145 {
|
punk@75
|
146 if (!fileFini[i])
|
punk@72
|
147 {
|
punk@75
|
148 if (fread(&sample, 2, 1, inputFiles[i]))
|
punk@75
|
149 clientStub->SendUnprocessedStream((UINT32)channel[i] , Data,(UINT32)sample);
|
punk@75
|
150 else{
|
punk@75
|
151 fileFini[i] = true;
|
punk@75
|
152 numCoreFini++;
|
punk@75
|
153 if (numCoreFini >= NUM_VOICES) // Need lock on last eof
|
punk@75
|
154 // Need to put lock here to prevent potential race condition
|
punk@75
|
155 pthread_mutex_lock(&lock);
|
punk@75
|
156 clientStub->SendUnprocessedStream((UINT32)channel[i], EndOfFile,0);
|
punk@75
|
157 }
|
punk@72
|
158 }
|
punk@72
|
159 }
|
punk@72
|
160 }
|
punk@13
|
161
|
punk@13
|
162 printf("main: out of loop\n");
|
punk@13
|
163 printf("main: wait for end of file\n");
|
punk@13
|
164
|
punk@13
|
165 pthread_cond_wait(&cond, &lock);
|
punk@13
|
166 pthread_mutex_unlock(&lock);
|
punk@13
|
167
|
rlm@23
|
168 printf("main: last data out\n");
|
punk@13
|
169
|
punk@13
|
170 // Convert input wav to pcm
|
rlm@34
|
171 // this part is ok because there is always only one input file.
|
punk@75
|
172 generate_wav("out_hw.pcm","input0.wav","out_hw.wav");
|
punk@13
|
173
|
punk@13
|
174 printf("generate wav done\n");
|
punk@13
|
175
|
punk@50
|
176 // printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n");
|
punk@13
|
177 // Dump the stats file
|
punk@13
|
178
|
punk@13
|
179 STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats();
|
punk@13
|
180 STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile();
|
punk@13
|
181
|
punk@31
|
182 fflush(stdout);
|
punk@53
|
183 // exit(0);
|
punk@31
|
184 }
|