Fixed bug in looping samples playback.
authorStephane Peter <megastep@lokigames.com>
Tue, 30 Nov 1999 01:52:16 +0000
changeset 212d26b0f75a05
parent 20 ef0f22476bc5
child 22 5104018a0c45
Fixed bug in looping samples playback.
Added -l command line option to playwave to loop the sample.
Makefile.in
mixer.c
playwave.c
     1.1 --- a/Makefile.in	Tue Nov 23 05:25:15 1999 +0000
     1.2 +++ b/Makefile.in	Tue Nov 30 01:52:16 1999 +0000
     1.3 @@ -1,4 +1,4 @@
     1.4 -# Makefile.in generated automatically by automake 1.4 from Makefile.am
     1.5 +# Makefile.in generated automatically by automake 1.4a from Makefile.am
     1.6  
     1.7  # Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
     1.8  # This Makefile.in is free software; the Free Software Foundation
     1.9 @@ -48,9 +48,10 @@
    1.10  AUTOHEADER = @AUTOHEADER@
    1.11  
    1.12  INSTALL = @INSTALL@
    1.13 -INSTALL_PROGRAM = @INSTALL_PROGRAM@ $(AM_INSTALL_PROGRAM_FLAGS)
    1.14 +INSTALL_PROGRAM = @INSTALL_PROGRAM@
    1.15  INSTALL_DATA = @INSTALL_DATA@
    1.16  INSTALL_SCRIPT = @INSTALL_SCRIPT@
    1.17 +INSTALL_STRIP_FLAG =
    1.18  transform = @program_transform_name@
    1.19  
    1.20  NORMAL_INSTALL = :
    1.21 @@ -253,8 +254,8 @@
    1.22  	$(mkinstalldirs) $(DESTDIR)$(bindir)
    1.23  	@list='$(bin_PROGRAMS)'; for p in $$list; do \
    1.24  	  if test -f $$p; then \
    1.25 -	    echo " $(LIBTOOL)  --mode=install $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`"; \
    1.26 -	    $(LIBTOOL)  --mode=install $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`; \
    1.27 +	    echo " $(LIBTOOL)  --mode=install $(INSTALL_PROGRAM) $(INSTALL_STRIP_FLAG) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`"; \
    1.28 +	    $(LIBTOOL)  --mode=install $(INSTALL_PROGRAM) $(INSTALL_STRIP_FLAG) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`; \
    1.29  	  else :; fi; \
    1.30  	done
    1.31  
    1.32 @@ -422,7 +423,7 @@
    1.33  	@for file in $(DISTFILES); do \
    1.34  	  d=$(srcdir); \
    1.35  	  if test -d $$d/$$file; then \
    1.36 -	    cp -pr $$/$$file $(distdir)/$$file; \
    1.37 +	    cp -pr $$d/$$file $(distdir)/$$file; \
    1.38  	  else \
    1.39  	    test -f $(distdir)/$$file \
    1.40  	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
    1.41 @@ -494,7 +495,7 @@
    1.42  all-am: Makefile $(LTLIBRARIES) $(PROGRAMS) $(HEADERS)
    1.43  all-redirect: all-recursive
    1.44  install-strip:
    1.45 -	$(MAKE) $(AM_MAKEFLAGS) AM_INSTALL_PROGRAM_FLAGS=-s install
    1.46 +	$(MAKE) $(AM_MAKEFLAGS) INSTALL_STRIP_FLAG=-s install
    1.47  installdirs: installdirs-recursive
    1.48  installdirs-am:
    1.49  	$(mkinstalldirs)  $(DESTDIR)$(libdir) $(DESTDIR)$(bindir) \
     2.1 --- a/mixer.c	Tue Nov 23 05:25:15 1999 +0000
     2.2 +++ b/mixer.c	Tue Nov 30 01:52:16 1999 +0000
     2.3 @@ -102,19 +102,32 @@
     2.4  				}
     2.5  			}
     2.6  			if ( channel[i].playing > 0 ) {
     2.7 -				mixable = channel[i].playing;
     2.8 -				if ( mixable > len ) {
     2.9 -					mixable = len;
    2.10 -				}
    2.11  				volume = (channel[i].volume*channel[i].chunk->volume) /
    2.12  					MIX_MAX_VOLUME;
    2.13 -				SDL_MixAudio(stream,channel[i].samples,mixable,volume);
    2.14 -				channel[i].samples += mixable;
    2.15 -				channel[i].playing -= mixable;
    2.16 -				if ( ! channel[i].playing && channel[i].looping ) {
    2.17 -					if ( --channel[i].looping ) {
    2.18 -						channel[i].samples = channel[i].chunk->abuf;
    2.19 -						channel[i].playing = channel[i].chunk->alen;
    2.20 +				mixable = channel[i].playing;
    2.21 +				/* If looping the sample and we are at its end, make sure
    2.22 +				   we will still return a full buffer */
    2.23 +				if ( channel[i].looping && mixable < len ) {
    2.24 +					char buffer[len]; /* Hum, gcc feature */
    2.25 +					int remaining = len - mixable;
    2.26 +					memcpy(buffer, channel[i].samples, mixable);
    2.27 +					memcpy(buffer+mixable, channel[i].chunk->abuf, remaining);
    2.28 +					SDL_MixAudio(stream, buffer, len, volume);
    2.29 +					--channel[i].looping;
    2.30 +					channel[i].samples = channel[i].chunk->abuf + remaining;
    2.31 +					channel[i].playing = channel[i].chunk->alen - remaining;
    2.32 +				} else {
    2.33 +					if ( mixable > len ) {
    2.34 +						mixable = len;
    2.35 +					}
    2.36 +					SDL_MixAudio(stream,channel[i].samples,mixable,volume);
    2.37 +					channel[i].samples += mixable;
    2.38 +					channel[i].playing -= mixable;
    2.39 +					if ( ! channel[i].playing && channel[i].looping ) {
    2.40 +						if ( --channel[i].looping ) {
    2.41 +							channel[i].samples = channel[i].chunk->abuf;
    2.42 +							channel[i].playing = channel[i].chunk->alen;
    2.43 +						}
    2.44  					}
    2.45  				}
    2.46  			}
     3.1 --- a/playwave.c	Tue Nov 23 05:25:15 1999 +0000
     3.2 +++ b/playwave.c	Tue Nov 30 01:52:16 1999 +0000
     3.3 @@ -39,20 +39,20 @@
     3.4  
     3.5  void CleanUp(void)
     3.6  {
     3.7 +	if ( wave ) {
     3.8 +		Mix_FreeChunk(wave);
     3.9 +		wave = NULL;
    3.10 +	}
    3.11  	if ( audio_open ) {
    3.12  		Mix_CloseAudio();
    3.13  		audio_open = 0;
    3.14  	}
    3.15 -	if ( wave ) {
    3.16 -		Mix_FreeChunk(wave);
    3.17 -		wave = NULL;
    3.18 -	}
    3.19  	SDL_Quit();
    3.20  }
    3.21  
    3.22  void Usage(char *argv0)
    3.23  {
    3.24 -	fprintf(stderr, "Usage: %s [-8] [-r rate] [-m] <wavefile>\n", argv0);
    3.25 +	fprintf(stderr, "Usage: %s [-8] [-r rate] [-l] [-m] <wavefile>\n", argv0);
    3.26  }
    3.27  	
    3.28  main(int argc, char *argv[])
    3.29 @@ -60,6 +60,7 @@
    3.30  	Uint32 audio_rate;
    3.31  	Uint16 audio_format;
    3.32  	int audio_channels;
    3.33 +	int loops = 0;
    3.34  	int i;
    3.35  
    3.36  	/* Initialize variables */
    3.37 @@ -76,6 +77,9 @@
    3.38  		if ( strcmp(argv[i], "-m") == 0 ) {
    3.39  			audio_channels = 1;
    3.40  		} else
    3.41 +		if ( strcmp(argv[i], "-l") == 0 ) {
    3.42 +			loops = -1;
    3.43 +		} else
    3.44  		if ( strcmp(argv[i], "-8") == 0 ) {
    3.45  			audio_format = AUDIO_U8;
    3.46  		} else {
    3.47 @@ -103,9 +107,14 @@
    3.48  		exit(2);
    3.49  	} else {
    3.50  		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
    3.51 -		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
    3.52 +		printf("Opened audio at %d Hz %d bit %s", audio_rate,
    3.53  			(audio_format&0xFF),
    3.54  			(audio_channels > 1) ? "stereo" : "mono");
    3.55 +		if ( loops ) {
    3.56 +		  printf(" (looping)\n");
    3.57 +		} else {
    3.58 +		  putchar('\n');
    3.59 +		}
    3.60  	}
    3.61  	audio_open = 1;
    3.62  
    3.63 @@ -118,7 +127,7 @@
    3.64  	}
    3.65  
    3.66  	/* Play and then exit */
    3.67 -	Mix_PlayChannel(0, wave, 0);
    3.68 +	Mix_PlayChannel(0, wave, loops);
    3.69  	while ( Mix_Playing(0) ) {
    3.70  		SDL_Delay(100);
    3.71  	}