wavestream.c
changeset 214 ab571e095c22
parent 173 0821a4ea0008
child 241 503416fca921
equal deleted inserted replaced
213:0cab09308739 214:ab571e095c22
    90 /* Currently we only support a single stream at a time */
    90 /* Currently we only support a single stream at a time */
    91 static WAVStream *music = NULL;
    91 static WAVStream *music = NULL;
    92 
    92 
    93 /* This is the format of the audio mixer data */
    93 /* This is the format of the audio mixer data */
    94 static SDL_AudioSpec mixer;
    94 static SDL_AudioSpec mixer;
       
    95 static int wavestream_volume = MIX_MAX_VOLUME;
    95 
    96 
    96 /* Function to load the WAV/AIFF stream */
    97 /* Function to load the WAV/AIFF stream */
    97 static FILE *LoadWAVStream (const char *file, SDL_AudioSpec *spec,
    98 static FILE *LoadWAVStream (const char *file, SDL_AudioSpec *spec,
    98 					long *start, long *stop);
    99 					long *start, long *stop);
    99 static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
   100 static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
   106 {
   107 {
   107 	mixer = *mixerfmt;
   108 	mixer = *mixerfmt;
   108 	return(0);
   109 	return(0);
   109 }
   110 }
   110 
   111 
   111 /* Unimplemented */
   112 void WAVStream_SetVolume(int volume)
   112 extern void WAVStream_SetVolume(int volume)
   113 {
   113 {
   114 	wavestream_volume = volume;
   114 }
   115 }
   115 
   116 
   116 /* Load a WAV stream from the given file */
   117 /* Load a WAV stream from the given file */
   117 extern WAVStream *WAVStream_LoadSong(const char *file, const char *magic)
   118 WAVStream *WAVStream_LoadSong(const char *file, const char *magic)
   118 {
   119 {
   119 	WAVStream *wave;
   120 	WAVStream *wave;
   120 	SDL_AudioSpec wavespec;
   121 	SDL_AudioSpec wavespec;
   121 
   122 
   122 	if ( ! mixer.format ) {
   123 	if ( ! mixer.format ) {
   146 	}
   147 	}
   147 	return(wave);
   148 	return(wave);
   148 }
   149 }
   149 
   150 
   150 /* Start playback of a given WAV stream */
   151 /* Start playback of a given WAV stream */
   151 extern void WAVStream_Start(WAVStream *wave)
   152 void WAVStream_Start(WAVStream *wave)
   152 {
   153 {
   153 	clearerr(wave->wavefp);
   154 	clearerr(wave->wavefp);
   154 	fseek(wave->wavefp, wave->start, SEEK_SET);
   155 	fseek(wave->wavefp, wave->start, SEEK_SET);
   155 	music = wave;
   156 	music = wave;
   156 }
   157 }
   157 
   158 
   158 /* Play some of a stream previously started with WAVStream_Start() */
   159 /* Play some of a stream previously started with WAVStream_Start() */
   159 extern void WAVStream_PlaySome(Uint8 *stream, int len)
   160 void WAVStream_PlaySome(Uint8 *stream, int len)
   160 {
   161 {
   161 	long pos;
   162 	long pos;
   162 
   163 
   163 	if ( music && ((pos=ftell(music->wavefp)) < music->stop) ) {
   164 	if ( music && ((pos=ftell(music->wavefp)) < music->stop) ) {
   164 		if ( music->cvt.needed ) {
   165 		if ( music->cvt.needed ) {
   190 			if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) {
   191 			if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) {
   191 				original_len--;
   192 				original_len--;
   192 			}
   193 			}
   193 			music->cvt.len = original_len;
   194 			music->cvt.len = original_len;
   194 			SDL_ConvertAudio(&music->cvt);
   195 			SDL_ConvertAudio(&music->cvt);
   195 			memcpy(stream, music->cvt.buf, music->cvt.len_cvt);
   196 			SDL_MixAudio(stream, music->cvt.buf, music->cvt.len_cvt, wavestream_volume);
   196 		} else {
   197 		} else {
   197 			if ( (music->stop - pos) < len ) {
   198 			if ( (music->stop - pos) < len ) {
   198 				len = (music->stop - pos);
   199 				len = (music->stop - pos);
   199 			}
   200 			}
   200 			fread(stream, len, 1, music->wavefp);
   201 			fread(stream, len, 1, music->wavefp);
   201 		}
   202 		}
   202 	}
   203 	}
   203 }
   204 }
   204 
   205 
   205 /* Stop playback of a stream previously started with WAVStream_Start() */
   206 /* Stop playback of a stream previously started with WAVStream_Start() */
   206 extern void WAVStream_Stop(void)
   207 void WAVStream_Stop(void)
   207 {
   208 {
   208 	music = NULL;
   209 	music = NULL;
   209 }
   210 }
   210 
   211 
   211 /* Close the given WAV stream */
   212 /* Close the given WAV stream */
   212 extern void WAVStream_FreeSong(WAVStream *wave)
   213 void WAVStream_FreeSong(WAVStream *wave)
   213 {
   214 {
   214 	if ( wave ) {
   215 	if ( wave ) {
   215 		/* Clean up associated data */
   216 		/* Clean up associated data */
   216 		if ( wave->wavefp ) {
   217 		if ( wave->wavefp ) {
   217 			fclose(wave->wavefp);
   218 			fclose(wave->wavefp);
   222 		free(wave);
   223 		free(wave);
   223 	}
   224 	}
   224 }
   225 }
   225 
   226 
   226 /* Return non-zero if a stream is currently playing */
   227 /* Return non-zero if a stream is currently playing */
   227 extern int WAVStream_Active(void)
   228 int WAVStream_Active(void)
   228 {
   229 {
   229 	int active;
   230 	int active;
   230 
   231 
   231 	active = 0;
   232 	active = 0;
   232 	if ( music && (ftell(music->wavefp) < music->stop) ) {
   233 	if ( music && (ftell(music->wavefp) < music->stop) ) {