view modules/bluespec/Pygar/core/AudioCoreSystem.cpp @ 69:5c963ab14143 pygar svn.70

[svn r70] added an attempt at multi-fileing
author punk
date Wed, 12 May 2010 00:07:40 -0400
parents 44cc00df1168
children 34fc182a1daa
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 Created\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 // main
69 void
70 CONNECTED_APPLICATION_CLASS::Main()
71 {
72 //rlm: two files
73 FILE *inputFile;
74 FILE *inputFile1;
75 UINT16 sample;
76 UINT16 sample1;
78 Bool fileFini[NUM_VOICES] = {false, false};
79 //rlm: not sure if normal ints are ok here; using UINT16 because I know it works
80 UINT16 channel0 = 0;
81 UINT16 channel1 = 1;
83 //init processor
84 int sleepCount = 0;
85 int result = 0;
87 int numcCoreFin = 0;
89 printf("SendSide Main\n");
90 fflush(stdout);
92 // Convert input wav to pcm
94 //rlm: for now we are going to going to just have 2 files with "common names"
96 generate_pcm("input.wav","input.pcm");
97 generate_pcm("input1.wav", "input1.pcm");
100 //Send data to the machine here.
101 //rlm: two files
102 inputFile = fopen("input.pcm","r");
103 inputFile1 = fopen("input1.pcm", "r");
105 assert(inputFile1);
106 assert(inputFile);
108 int count = 0;
110 sleep(1);
111 printf("main:samples about to start sending %d\n", count);
113 //rlm: here we read both files. later refactor into a function.
114 // also, this will terminate when the FIRST file reaches its end.
115 while( !feof(inputFile) || !feof(inputFile1))
116 {
118 sem_wait(&throttle);
120 if(count%1000 == 0)
121 printf("main: %d\n", count);
122 count++;
124 //rlm: two files.
126 if (!fileFini[0])
127 {
128 if (fread(&sample, 2, 1, inputFile))
129 clientStub->SendUnprocessedStream((UINT32)channel0 , Data,(UINT32)sample);
130 else{
131 fileFini[0] = true;
132 numCoreFini++;
133 if (numCoreFini >= NUM_VOICES) // Need lock on last eof
134 // Need to put lock here to prevent potential race condition
135 pthread_mutex_lock(&lock);
136 clientStub->SendUnprocessedStream((UINT32)channel0, EndOfFile,0);
137 }
138 }
139 if !fileFini[1])
140 {
141 if (fread(&sample1, 2 , 1, inputFile1))
142 clientStub->SendUnprocessedStream((UINT32)channel1 , Data,(UINT32)sample1);
143 else{
144 fileFini[1] = true;
145 numCoreFini++;
146 if (numCoreFini >= NUM_VOICES) // Need lock on last eof
147 // Need to put lock here to prevent potential race condition
148 pthread_mutex_lock(&lock);
149 clientStub->SendUnprocessedStream((UINT32)channel1, EndOfFile,0);
150 }
151 }
152 }
154 printf("main: out of loop\n");
155 printf("main: wait for end of file\n");
157 pthread_cond_wait(&cond, &lock);
158 pthread_mutex_unlock(&lock);
160 printf("main: last data out\n");
162 // Convert input wav to pcm
163 // this part is ok because there is always only one input file.
164 generate_wav("out_hw.pcm","input.wav","out_hw.wav");
166 printf("generate wav done\n");
168 // printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n");
169 // Dump the stats file
171 STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats();
172 STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile();
174 fflush(stdout);
175 // exit(0);
176 }