Skip to content

Commit

Permalink
Fixed bug in looping samples playback.
Browse files Browse the repository at this point in the history
Added -l command line option to playwave to loop the sample.
  • Loading branch information
Stephane Peter committed Nov 30, 1999
1 parent b6c6872 commit 781113f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
13 changes: 7 additions & 6 deletions 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
Expand Down Expand Up @@ -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 = :
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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) \
Expand Down
35 changes: 24 additions & 11 deletions mixer.c
Expand Up @@ -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;
}
}
}
}
Expand Down
23 changes: 16 additions & 7 deletions playwave.c
Expand Up @@ -39,27 +39,28 @@ 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] <wavefile>\n", argv0);
fprintf(stderr, "Usage: %s [-8] [-r rate] [-l] [-m] <wavefile>\n", argv0);
}

main(int argc, char *argv[])
{
Uint32 audio_rate;
Uint16 audio_format;
int audio_channels;
int loops = 0;
int i;

/* Initialize variables */
Expand All @@ -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 {
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down

0 comments on commit 781113f

Please sign in to comment.