Rev 2415 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2415 | Rev 2427 | ||
---|---|---|---|
Line 4... | Line 4... | ||
4 | #include |
4 | #include |
5 | 5 | ||
Line 6... | Line 6... | ||
6 | #include |
6 | #include |
7 | #include |
7 | #include |
8 | #include |
8 | #include "../winlib/winlib.h" |
9 | #include "sound.h" |
9 | #include "sound.h" |
10 | #include "fplay.h" |
10 | #include "fplay.h" |
Line 11... | Line 11... | ||
11 | 11 | ||
Line 12... | Line -... | ||
12 | - | ||
13 | astream_t astream; |
- | |
14 | 12 | ||
Line 15... | Line 13... | ||
15 | static SNDBUF hBuff; |
13 | astream_t astream; |
Line 16... | Line 14... | ||
16 | 14 | ||
Line -... | Line 15... | ||
- | 15 | extern uint8_t *decoder_buffer; |
|
Line 17... | Line 16... | ||
17 | extern uint8_t *decoder_buffer; |
16 | |
18 | 17 | extern volatile enum player_state player_state; |
|
Line -... | Line 18... | ||
- | 18 | ||
- | 19 | extern volatile uint32_t driver_lock; |
|
19 | extern volatile uint32_t status; |
20 | |
20 | 21 | static SNDBUF hBuff; |
|
21 | extern volatile uint32_t driver_lock; |
22 | |
22 | 23 | static int snd_format; |
|
23 | 24 | int sample_rate; |
|
Line 63... | Line 64... | ||
63 | }; |
64 | }; |
64 | 65 | ||
Line 65... | Line 66... | ||
65 | static uint64_t samples_lost; |
66 | static uint64_t samples_lost; |
66 | static double audio_delta; |
67 | static double audio_delta; |
- | 68 | static double last_time_stamp; |
|
- | 69 | ||
Line 67... | Line 70... | ||
67 | 70 | ||
68 | double get_master_clock() |
71 | double get_master_clock(void) |
69 | { |
72 | { |
Line 70... | Line 73... | ||
70 | double tstamp; |
73 | double tstamp; |
71 | 74 | ||
Line 114... | Line 117... | ||
114 | return 1; |
117 | return 1; |
115 | }; |
118 | }; |
116 | 119 | ||
Line -... | Line 120... | ||
- | 120 | ||
- | 121 | static void sync_audio(SNDBUF hbuff, int buffsize) |
|
- | 122 | { |
|
- | 123 | SND_EVENT evnt; |
|
- | 124 | uint32_t offset; |
|
- | 125 | double time_stamp; |
|
- | 126 | ||
- | 127 | #ifdef BLACK_MAGIC_SOUND |
|
- | 128 | ||
- | 129 | while( player_state != CLOSED) |
|
- | 130 | { |
|
- | 131 | GetNotify(&evnt); |
|
- | 132 | ||
- | 133 | if(evnt.code != 0xFF000001) |
|
- | 134 | { |
|
- | 135 | printf("invalid event code %d\n\r", evnt.code); |
|
- | 136 | continue; |
|
- | 137 | } |
|
- | 138 | ||
- | 139 | if(evnt.stream != hbuff) |
|
- | 140 | { |
|
- | 141 | printf("invalid stream %x hBuff= %x\n\r", |
|
- | 142 | evnt.stream, hbuff); |
|
- | 143 | continue; |
|
- | 144 | } |
|
- | 145 | ||
- | 146 | GetTimeStamp(hbuff, &time_stamp); |
|
- | 147 | audio_delta = time_stamp - last_time_stamp; |
|
- | 148 | ||
- | 149 | offset = evnt.offset; |
|
- | 150 | ||
- | 151 | mutex_lock(&astream.lock); |
|
- | 152 | { |
|
- | 153 | SetBuffer(hbuff, astream.buffer, offset, buffsize); |
|
- | 154 | samples_written+= buffsize/4; |
|
- | 155 | astream.count -= buffsize; |
|
- | 156 | if(astream.count) |
|
- | 157 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
|
- | 158 | mutex_unlock(&astream.lock); |
|
- | 159 | }; |
|
- | 160 | break; |
|
- | 161 | }; |
|
- | 162 | #endif |
|
- | 163 | ||
- | 164 | }; |
|
- | 165 | ||
117 | 166 | ||
118 | int audio_thread(void *param) |
167 | int audio_thread(void *param) |
119 | { |
168 | { |
- | 169 | SND_EVENT evnt; |
|
120 | SND_EVENT evnt; |
170 | |
121 | int buffsize; |
171 | int buffsize; |
122 | int samples; |
172 | int samples; |
123 | int err; |
173 | int err; |
- | 174 | char *errstr; |
|
Line 124... | Line 175... | ||
124 | char *errstr; |
175 | int active; |
125 | 176 | ||
126 | 177 | ||
Line 142... | Line 193... | ||
142 | 193 | ||
Line 143... | Line 194... | ||
143 | samples = buffsize/4; |
194 | samples = buffsize/4; |
Line 144... | Line 195... | ||
144 | 195 | ||
145 | while( (astream.count < buffsize*2) && |
196 | while( (astream.count < buffsize*2) && |
146 | (status != 0) ) |
197 | (player_state != CLOSED) ) |
Line 147... | Line 198... | ||
147 | yield(); |
198 | yield(); |
148 | 199 | ||
149 | mutex_lock(&astream.lock); |
200 | mutex_lock(&astream.lock); |
- | 201 | { |
|
150 | { |
202 | SetBuffer(hBuff, astream.buffer, 0, buffsize); |
151 | SetBuffer(hBuff, astream.buffer, 0, buffsize); |
203 | samples_written+= buffsize/4; |
152 | astream.count -= buffsize; |
204 | astream.count -= buffsize; |
153 | if(astream.count) |
205 | if(astream.count) |
154 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
206 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
Line 155... | Line -... | ||
155 | mutex_unlock(&astream.lock); |
- | |
156 | }; |
- | |
157 | - | ||
158 | if((err = PlayBuffer(hBuff, 0)) !=0 ) |
- | |
159 | { |
- | |
160 | errstr = "Cannot play buffer\n\r"; |
- | |
161 | goto exit_whith_error; |
- | |
162 | }; |
- | |
163 | - | ||
164 | 207 | mutex_unlock(&astream.lock); |
|
165 | #ifdef BLACK_MAGIC_SOUND |
208 | }; |
166 | 209 | ||
- | 210 | while( player_state != CLOSED) |
|
- | 211 | { |
|
Line 167... | Line -... | ||
167 | while( status != 0) |
- | |
168 | { |
- | |
169 | uint32_t offset; |
212 | uint32_t offset; |
170 | 213 | double event_stamp, wait_stamp; |
|
171 | GetNotify(&evnt); |
- | |
172 | 214 | int too_late = 0; |
|
173 | if(evnt.code != 0xFF000001) |
- | |
174 | { |
- | |
175 | printf("invalid event code %d\n\r", evnt.code); |
- | |
176 | continue; |
215 | |
177 | } |
216 | if(player_state == PAUSE) |
178 | 217 | { |
|
- | 218 | if( active ) |
|
- | 219 | { |
|
179 | if(evnt.stream != hBuff) |
220 | StopBuffer(hBuff); |
180 | { |
221 | active = 0; |
181 | printf("invalid stream %x hBuff= %x\n\r", |
- | |
182 | evnt.stream, hBuff); |
222 | } |
183 | continue; |
- | |
184 | } |
- | |
185 | - | ||
186 | GetTimeStamp(hBuff, &audio_delta); |
- | |
187 | samples_lost = audio_delta*sample_rate/1000; |
- | |
188 | 223 | delay(1); |
|
189 | offset = evnt.offset; |
224 | continue; |
190 | 225 | } |
|
191 | mutex_lock(&astream.lock); |
226 | else if(player_state == PLAY_RESTART) |
192 | { |
227 | { |
193 | SetBuffer(hBuff, astream.buffer, offset, buffsize); |
228 | GetTimeStamp(hBuff, &last_time_stamp); |
194 | astream.count -= buffsize; |
229 | if((err = PlayBuffer(hBuff, 0)) !=0 ) |
195 | if(astream.count) |
230 | { |
- | 231 | errstr = "Cannot play buffer\n\r"; |
|
- | 232 | goto exit_whith_error; |
|
- | 233 | }; |
|
196 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
234 | active = 1; |
197 | mutex_unlock(&astream.lock); |
- | |
198 | }; |
- | |
199 | break; |
- | |
200 | }; |
- | |
201 | #endif |
- | |
202 | - | ||
203 | printf("initial audio delta %f\n", audio_delta); |
- | |
204 | - | ||
205 | while( status != 0) |
- | |
Line 206... | Line 235... | ||
206 | { |
235 | sync_audio(hBuff, buffsize); |
Line 207... | Line 236... | ||
207 | uint32_t offset; |
236 | player_state = PLAY; |
208 | double event_stamp, wait_stamp; |
237 | printf("audio delta %f\n", audio_delta); |
Line 227... | Line 256... | ||
227 | 256 | ||
Line 228... | Line 257... | ||
228 | offset = evnt.offset; |
257 | offset = evnt.offset; |
Line 229... | Line 258... | ||
229 | 258 | ||
230 | while( (astream.count < buffsize) && |
259 | while( (astream.count < buffsize) && |
231 | (status != 0) ) |
260 | (player_state != CLOSED) ) |
232 | { |
261 | { |
233 | yield(); |
262 | yield(); |
234 | GetTimeStamp(hBuff, &wait_stamp); |
263 | GetTimeStamp(hBuff, &wait_stamp); |
235 | if( (wait_stamp - event_stamp) > |
264 | if( (wait_stamp - event_stamp) > |
Line 242... | Line 271... | ||
242 | break; |
271 | break; |
243 | } |
272 | } |
244 | }; |
273 | }; |
245 | 274 | ||
Line 246... | Line 275... | ||
246 | if((too_late == 1) || (status == 0)) |
275 | if((too_late == 1) || (player_state == CLOSED)) |
- | 276 | { |
|
- | 277 | too_late = 0; |
|
247 | continue; |
278 | continue; |
- | 279 | }; |
|
Line 248... | Line 280... | ||
248 | 280 | ||
249 | mutex_lock(&astream.lock); |
281 | mutex_lock(&astream.lock); |
- | 282 | SetBuffer(hBuff, astream.buffer, offset, buffsize); |
|
250 | SetBuffer(hBuff, astream.buffer, offset, buffsize); |
283 | samples_written+= buffsize/4; |
251 | astream.count -= buffsize; |
284 | astream.count -= buffsize; |
252 | if(astream.count) |
285 | if(astream.count) |
253 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
286 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
254 | mutex_unlock(&astream.lock); |
287 | mutex_unlock(&astream.lock); |