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@64
|
25 printf("SendSide Created\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@13
|
68 // main
|
punk@13
|
69 void
|
punk@13
|
70 CONNECTED_APPLICATION_CLASS::Main()
|
punk@13
|
71 {
|
rlm@34
|
72 //rlm: two files
|
punk@13
|
73 FILE *inputFile;
|
punk@52
|
74 FILE *inputFile1;
|
punk@13
|
75 UINT16 sample;
|
punk@52
|
76 UINT16 sample1;
|
rlm@34
|
77
|
punk@72
|
78 bool fileFini[NUM_VOICES];
|
punk@72
|
79 for (int n = 0; n < NUM_VOICES; n++)
|
punk@72
|
80 fileFini[n] = false;
|
punk@72
|
81
|
rlm@34
|
82 //rlm: not sure if normal ints are ok here; using UINT16 because I know it works
|
punk@52
|
83 UINT16 channel0 = 0;
|
punk@52
|
84 UINT16 channel1 = 1;
|
punk@31
|
85
|
punk@31
|
86 //init processor
|
punk@31
|
87 int sleepCount = 0;
|
punk@31
|
88 int result = 0;
|
punk@31
|
89
|
punk@72
|
90 int numCoreFini = 0;
|
punk@31
|
91
|
punk@64
|
92 printf("SendSide Main\n");
|
punk@31
|
93 fflush(stdout);
|
punk@31
|
94
|
punk@13
|
95 // Convert input wav to pcm
|
rlm@34
|
96
|
rlm@34
|
97 //rlm: for now we are going to going to just have 2 files with "common names"
|
rlm@34
|
98
|
punk@13
|
99 generate_pcm("input.wav","input.pcm");
|
punk@52
|
100 generate_pcm("input1.wav", "input1.pcm");
|
rlm@34
|
101
|
punk@13
|
102
|
punk@13
|
103 //Send data to the machine here.
|
rlm@34
|
104 //rlm: two files
|
punk@13
|
105 inputFile = fopen("input.pcm","r");
|
punk@53
|
106 inputFile1 = fopen("input1.pcm", "r");
|
rlm@34
|
107
|
punk@52
|
108 assert(inputFile1);
|
punk@13
|
109 assert(inputFile);
|
punk@13
|
110
|
punk@13
|
111 int count = 0;
|
punk@13
|
112
|
punk@65
|
113 sleep(1);
|
punk@57
|
114 printf("main:samples about to start sending %d\n", count);
|
punk@13
|
115
|
rlm@34
|
116 //rlm: here we read both files. later refactor into a function.
|
rlm@34
|
117 // also, this will terminate when the FIRST file reaches its end.
|
punk@68
|
118 while( !feof(inputFile) || !feof(inputFile1))
|
punk@72
|
119 {
|
punk@72
|
120
|
punk@65
|
121 sem_wait(&throttle);
|
punk@65
|
122
|
punk@37
|
123 if(count%1000 == 0)
|
punk@37
|
124 printf("main: %d\n", count);
|
punk@37
|
125 count++;
|
punk@37
|
126
|
rlm@34
|
127 //rlm: two files.
|
punk@68
|
128
|
punk@68
|
129 if (!fileFini[0])
|
punk@68
|
130 {
|
punk@68
|
131 if (fread(&sample, 2, 1, inputFile))
|
punk@68
|
132 clientStub->SendUnprocessedStream((UINT32)channel0 , Data,(UINT32)sample);
|
punk@68
|
133 else{
|
punk@68
|
134 fileFini[0] = true;
|
punk@68
|
135 numCoreFini++;
|
punk@68
|
136 if (numCoreFini >= NUM_VOICES) // Need lock on last eof
|
punk@68
|
137 // Need to put lock here to prevent potential race condition
|
punk@68
|
138 pthread_mutex_lock(&lock);
|
punk@68
|
139 clientStub->SendUnprocessedStream((UINT32)channel0, EndOfFile,0);
|
punk@68
|
140 }
|
punk@68
|
141 }
|
punk@72
|
142
|
punk@72
|
143 if (!fileFini[1])
|
punk@68
|
144 {
|
punk@68
|
145 if (fread(&sample1, 2 , 1, inputFile1))
|
punk@68
|
146 clientStub->SendUnprocessedStream((UINT32)channel1 , Data,(UINT32)sample1);
|
punk@72
|
147 else
|
punk@72
|
148 {
|
punk@68
|
149 fileFini[1] = true;
|
punk@72
|
150 numCoreFini++;
|
punk@68
|
151 if (numCoreFini >= NUM_VOICES) // Need lock on last eof
|
punk@68
|
152 // Need to put lock here to prevent potential race condition
|
punk@68
|
153 pthread_mutex_lock(&lock);
|
punk@68
|
154 clientStub->SendUnprocessedStream((UINT32)channel1, EndOfFile,0);
|
punk@72
|
155 }
|
punk@72
|
156 }
|
punk@72
|
157 }
|
punk@13
|
158
|
punk@13
|
159 printf("main: out of loop\n");
|
punk@13
|
160 printf("main: wait for end of file\n");
|
punk@13
|
161
|
punk@13
|
162 pthread_cond_wait(&cond, &lock);
|
punk@13
|
163 pthread_mutex_unlock(&lock);
|
punk@13
|
164
|
rlm@23
|
165 printf("main: last data out\n");
|
punk@13
|
166
|
punk@13
|
167 // Convert input wav to pcm
|
rlm@34
|
168 // this part is ok because there is always only one input file.
|
punk@13
|
169 generate_wav("out_hw.pcm","input.wav","out_hw.wav");
|
punk@13
|
170
|
punk@13
|
171 printf("generate wav done\n");
|
punk@13
|
172
|
punk@50
|
173 // printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n");
|
punk@13
|
174 // Dump the stats file
|
punk@13
|
175
|
punk@13
|
176 STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats();
|
punk@13
|
177 STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile();
|
punk@13
|
178
|
punk@31
|
179 fflush(stdout);
|
punk@53
|
180 // exit(0);
|
punk@31
|
181 }
|