From 781113fe6ceefa3e83fccd294fcea1954c61b14e Mon Sep 17 00:00:00 2001 From: Stephane Peter Date: Tue, 30 Nov 1999 01:52:16 +0000 Subject: [PATCH] Fixed bug in looping samples playback. Added -l command line option to playwave to loop the sample. --- Makefile.in | 13 +++++++------ mixer.c | 35 ++++++++++++++++++++++++----------- playwave.c | 23 ++++++++++++++++------- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/Makefile.in b/Makefile.in index 531404d2..476aacff 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,4 +1,4 @@ -# Makefile.in generated automatically by automake 1.4 from Makefile.am +# Makefile.in generated automatically by automake 1.4a from Makefile.am # Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc. # This Makefile.in is free software; the Free Software Foundation @@ -48,9 +48,10 @@ AUTOMAKE = @AUTOMAKE@ AUTOHEADER = @AUTOHEADER@ INSTALL = @INSTALL@ -INSTALL_PROGRAM = @INSTALL_PROGRAM@ $(AM_INSTALL_PROGRAM_FLAGS) +INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_DATA = @INSTALL_DATA@ INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_FLAG = transform = @program_transform_name@ NORMAL_INSTALL = : @@ -253,8 +254,8 @@ install-binPROGRAMS: $(bin_PROGRAMS) $(mkinstalldirs) $(DESTDIR)$(bindir) @list='$(bin_PROGRAMS)'; for p in $$list; do \ if test -f $$p; then \ - echo " $(LIBTOOL) --mode=install $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`"; \ - $(LIBTOOL) --mode=install $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`; \ + echo " $(LIBTOOL) --mode=install $(INSTALL_PROGRAM) $(INSTALL_STRIP_FLAG) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`"; \ + $(LIBTOOL) --mode=install $(INSTALL_PROGRAM) $(INSTALL_STRIP_FLAG) $$p $(DESTDIR)$(bindir)/`echo $$p|sed 's/$(EXEEXT)$$//'|sed '$(transform)'|sed 's/$$/$(EXEEXT)/'`; \ else :; fi; \ done @@ -422,7 +423,7 @@ distdir: $(DISTFILES) @for file in $(DISTFILES); do \ d=$(srcdir); \ if test -d $$d/$$file; then \ - cp -pr $$/$$file $(distdir)/$$file; \ + cp -pr $$d/$$file $(distdir)/$$file; \ else \ test -f $(distdir)/$$file \ || ln $$d/$$file $(distdir)/$$file 2> /dev/null \ @@ -494,7 +495,7 @@ uninstall: uninstall-recursive all-am: Makefile $(LTLIBRARIES) $(PROGRAMS) $(HEADERS) all-redirect: all-recursive install-strip: - $(MAKE) $(AM_MAKEFLAGS) AM_INSTALL_PROGRAM_FLAGS=-s install + $(MAKE) $(AM_MAKEFLAGS) INSTALL_STRIP_FLAG=-s install installdirs: installdirs-recursive installdirs-am: $(mkinstalldirs) $(DESTDIR)$(libdir) $(DESTDIR)$(bindir) \ diff --git a/mixer.c b/mixer.c index 897eed98..fc124171 100644 --- a/mixer.c +++ b/mixer.c @@ -102,19 +102,32 @@ static void mix_channels(void *udata, Uint8 *stream, int len) } } if ( channel[i].playing > 0 ) { - mixable = channel[i].playing; - if ( mixable > len ) { - mixable = len; - } volume = (channel[i].volume*channel[i].chunk->volume) / MIX_MAX_VOLUME; - SDL_MixAudio(stream,channel[i].samples,mixable,volume); - channel[i].samples += mixable; - channel[i].playing -= mixable; - if ( ! channel[i].playing && channel[i].looping ) { - if ( --channel[i].looping ) { - channel[i].samples = channel[i].chunk->abuf; - channel[i].playing = channel[i].chunk->alen; + mixable = channel[i].playing; + /* If looping the sample and we are at its end, make sure + we will still return a full buffer */ + if ( channel[i].looping && mixable < len ) { + char buffer[len]; /* Hum, gcc feature */ + int remaining = len - mixable; + memcpy(buffer, channel[i].samples, mixable); + memcpy(buffer+mixable, channel[i].chunk->abuf, remaining); + SDL_MixAudio(stream, buffer, len, volume); + --channel[i].looping; + channel[i].samples = channel[i].chunk->abuf + remaining; + channel[i].playing = channel[i].chunk->alen - remaining; + } else { + if ( mixable > len ) { + mixable = len; + } + SDL_MixAudio(stream,channel[i].samples,mixable,volume); + channel[i].samples += mixable; + channel[i].playing -= mixable; + if ( ! channel[i].playing && channel[i].looping ) { + if ( --channel[i].looping ) { + channel[i].samples = channel[i].chunk->abuf; + channel[i].playing = channel[i].chunk->alen; + } } } } diff --git a/playwave.c b/playwave.c index 4c5c0828..3875253a 100644 --- a/playwave.c +++ b/playwave.c @@ -39,20 +39,20 @@ static Mix_Chunk *wave = NULL; void CleanUp(void) { - if ( audio_open ) { - Mix_CloseAudio(); - audio_open = 0; - } if ( wave ) { Mix_FreeChunk(wave); wave = NULL; } + if ( audio_open ) { + Mix_CloseAudio(); + audio_open = 0; + } SDL_Quit(); } void Usage(char *argv0) { - fprintf(stderr, "Usage: %s [-8] [-r rate] [-m] \n", argv0); + fprintf(stderr, "Usage: %s [-8] [-r rate] [-l] [-m] \n", argv0); } main(int argc, char *argv[]) @@ -60,6 +60,7 @@ main(int argc, char *argv[]) Uint32 audio_rate; Uint16 audio_format; int audio_channels; + int loops = 0; int i; /* Initialize variables */ @@ -76,6 +77,9 @@ main(int argc, char *argv[]) if ( strcmp(argv[i], "-m") == 0 ) { audio_channels = 1; } else + if ( strcmp(argv[i], "-l") == 0 ) { + loops = -1; + } else if ( strcmp(argv[i], "-8") == 0 ) { audio_format = AUDIO_U8; } else { @@ -103,9 +107,14 @@ main(int argc, char *argv[]) exit(2); } else { Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); - printf("Opened audio at %d Hz %d bit %s\n", audio_rate, + printf("Opened audio at %d Hz %d bit %s", audio_rate, (audio_format&0xFF), (audio_channels > 1) ? "stereo" : "mono"); + if ( loops ) { + printf(" (looping)\n"); + } else { + putchar('\n'); + } } audio_open = 1; @@ -118,7 +127,7 @@ main(int argc, char *argv[]) } /* Play and then exit */ - Mix_PlayChannel(0, wave, 0); + Mix_PlayChannel(0, wave, loops); while ( Mix_Playing(0) ) { SDL_Delay(100); }