comparison playsound/playsound.c @ 146:023c3e7f028b

Cleaned up the overflow buffer hack.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 23 Oct 2001 18:27:31 +0000
parents 195c96fa355a
children d51546293fd1
comparison
equal deleted inserted replaced
145:6c351e4243f8 146:023c3e7f028b
85 } /* output_decoders */ 85 } /* output_decoders */
86 86
87 87
88 static volatile int done_flag = 0; 88 static volatile int done_flag = 0;
89 89
90 static Uint8 *decoded_ptr = NULL;
91 static Uint32 decoded_bytes = 0;
92
90 static void audio_callback(void *userdata, Uint8 *stream, int len) 93 static void audio_callback(void *userdata, Uint8 *stream, int len)
91 { 94 {
92 static Uint8 overflow[16384]; /* this is a hack. */
93 static Uint8 *overflow_ptr;
94 static int overflowBytes = 0;
95 Sound_Sample *sample = (Sound_Sample *) userdata; 95 Sound_Sample *sample = (Sound_Sample *) userdata;
96 int bw = 0; /* bytes written to stream*/ 96 int bw = 0; /* bytes written to stream*/
97 Uint32 rc; /* return code */ 97 int cpysize;
98 98
99 if (overflowBytes > 0) 99 while (bw < len)
100 { 100 {
101 bw = (overflowBytes < len) ? overflowBytes : len; 101 if (!decoded_bytes)
102 memcpy(stream, overflow_ptr, bw); 102 {
103 overflow_ptr += bw; 103 if (sample->flags & (SOUND_SAMPLEFLAG_ERROR|SOUND_SAMPLEFLAG_EOF))
104 overflowBytes -= bw; 104 {
105 } /* if */ 105 memset(stream + bw, '\0', len - bw);
106 106 done_flag = 1;
107 while ((bw < len) && (!done_flag)) 107 return;
108 {
109 rc = Sound_Decode(sample);
110 if (rc > 0)
111 {
112 if ((bw + (int) rc) > len)
113 {
114 overflowBytes = (bw + rc) - len;
115 memcpy(overflow,
116 ((Uint8 *) sample->buffer) + (rc - overflowBytes),
117 overflowBytes);
118 overflow_ptr = overflow;
119 rc -= overflowBytes;
120 } /* if */ 108 } /* if */
121 109
122 memcpy(stream + bw, sample->buffer, rc); 110 decoded_bytes = Sound_Decode(sample);
123 bw += rc; 111 decoded_ptr = sample->buffer;
124 } /* if */ 112 } /* if */
125 113
126 if (sample->flags & SOUND_SAMPLEFLAG_EOF) 114 cpysize = len - bw;
127 done_flag = 1; 115 if (cpysize > decoded_bytes)
128 116 cpysize = decoded_bytes;
129 else if (sample->flags & SOUND_SAMPLEFLAG_ERROR) 117
130 { 118 if (cpysize > 0)
131 fprintf(stderr, "Error condition in decoding!\n" 119 {
132 " problem: [%s].\n", Sound_GetError()); 120 memcpy(stream + bw, decoded_ptr, cpysize);
133 done_flag = 1; 121 bw += cpysize;
134 } /* else if */ 122 decoded_ptr += bw;
123 decoded_bytes -= bw;
124 } /* if */
135 } /* while */ 125 } /* while */
136
137 assert(bw <= len);
138
139 if (bw < len)
140 memset(stream + bw, '\0', len - bw);
141 } /* audio_callback */ 126 } /* audio_callback */
142 127
143 128
144 static void output_usage(const char *argv0) 129 static void output_usage(const char *argv0)
145 { 130 {
360 printf("Now playing [%s]...\n", argv[i]); 345 printf("Now playing [%s]...\n", argv[i]);
361 346
362 done_flag = 0; 347 done_flag = 0;
363 SDL_PauseAudio(0); 348 SDL_PauseAudio(0);
364 while (!done_flag) 349 while (!done_flag)
350 {
365 SDL_Delay(10); 351 SDL_Delay(10);
352 } /* while */
366 SDL_PauseAudio(1); 353 SDL_PauseAudio(1);
354
355 if (sample->flags & SOUND_SAMPLEFLAG_ERROR)
356 {
357 fprintf(stderr, "Error in decoding sound file!\n"
358 " reason: [%s].\n", Sound_GetError());
359 } /* if */
360
367 SDL_CloseAudio(); /* reopen with next sample's format if possible */ 361 SDL_CloseAudio(); /* reopen with next sample's format if possible */
368
369 Sound_FreeSample(sample); 362 Sound_FreeSample(sample);
370 } /* for */ 363 } /* for */
371 364
372 Sound_Quit(); 365 Sound_Quit();
373 SDL_Quit(); 366 SDL_Quit();