snd_sfx_play_chn() causes sounds to loop

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
ant512
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 27
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Thu Sep 29, 2016 12:31 am
Has thanked: 0
Been thanked: 0

snd_sfx_play_chn() causes sounds to loop

Post by ant512 »

I've got a bunch of 16-bit, mono, 11KHz RIFF-WAV files that I'm trying to play with the snd_sfx_play_chn() function. Everything works except the sounds loop until another sound is played on the same channel. Any ideas what's going wrong?

[EDIT] If I switch to snd_sfx_play() then the looping stops, but it seems that each new sound I trigger replays all of the previously-played sounds in addition to the new sound. Same if I switch to 22KHz or stereo files.
ant512
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 27
Joined: Thu Sep 29, 2016 12:31 am
Has thanked: 0
Been thanked: 0

Re: snd_sfx_play_chn() causes sounds to loop

Post by ant512 »

I've created a minimal example here (source/makefile/build script/romdisk/elf included):

soundtest.zip

The first sound plays OK. Then pause for 2 seconds; when the second sound plays, it *also* replays the original sound. Then the volume steadily increases as the system is overlaying multiple copies of the same sounds on different sound channels. It seems to happen regardless of the frequency of the WAV files or if I call snd_sfx_stop() before starting the new sound.

Code:

Code: Select all

#include <kos.h>
#include <dc/sound/sound.h>

extern uint8 romdisk[];

KOS_INIT_ROMDISK(romdisk);

int main(int argc, char* argv[]) {
	vid_set_mode(DM_320x240, PM_RGB555);
	snd_init();

	sfxhnd_t handle1 = snd_sfx_load("/rd/shipbullet.wav");
	sfxhnd_t handle2 = snd_sfx_load("/rd/shipexplosion.wav");

	int handle1Channel = 0;
	int handle2Channel = 0;
	int shouldStop = 0;

	while (1) {
		if (shouldStop) snd_sfx_stop(handle1Channel);
		handle1Channel = snd_sfx_play(handle1, 128, 128);
		thd_sleep(2000);

		if (shouldStop) snd_sfx_stop(handle2Channel);
		handle2Channel = snd_sfx_play(handle2, 128, 128);
		thd_sleep(2000);

		shouldStop = 1;
	}

	return 0;
}
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: snd_sfx_play_chn() causes sounds to loop

Post by BlueCrab »

That's... Strange. :?

I'll have to take a look when I've got a Dreamcast handy.
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Re: snd_sfx_play_chn() causes sounds to loop

Post by nymus »

I seem to recall mention in KOS source code of a Dreamcast hardware "feature" where it automatically loops samples for you.

I think it was meant for background music and such...
behold the mind
inspired by Dreamcast
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: snd_sfx_play_chn() causes sounds to loop

Post by BlueCrab »

nymus wrote:I seem to recall mention in KOS source code of a Dreamcast hardware "feature" where it automatically loops samples for you.

I think it was meant for background music and such...
The AICA does allow looping, yes. That said, it isn't exposed by the snd_sfx stuff, since snd_sfx is meant for one-shots and such.
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Re: snd_sfx_play_chn() causes sounds to loop

Post by nymus »

Are you using real hardware, ant512? On my Dreamcast, the files seem to be playing just fine; none of the looping/overlap issues you describe. The only problem is that after playing about three times, there is a long pause and then the program resumes the regular interval (probably related to the sleeping). Here's my output;

Code: Select all

KallistiOS Git revision 112df1a: Sat  2 Jul 2016 15:11:01 EDT
maple: active drivers:
    Dreameye (Camera): Camera
    Sound Input Peripheral: Microphone
    PuruPuru (Vibration) Pack: JumpPack
    VMU Driver: Clock, LCD, MemoryCard
    Mouse Driver: Mouse
    Keyboard Driver: Keyboard
    Controller Driver: Controller
    Lightgun: LightGun
  DMA Buffer at ac0af380
vid_set_mode: 640x480IL NTSC
fs_romdisk: mounting image at 0x8c04f030 at /rd
dc-load console support enabled
maple: attached devices:
  A0: Dreamcast Controller          (01000000: Controller)
  A1: Visual Memory                 (0e000000: Clock, LCD, MemoryCard)
vid_set_mode: 320x240 NTSC
snd_init(): loading 3336 bytes into SPU RAM
snd_sfx: loading effect /rd/shipbullet.wav
WAVE file is stereo, 11025HZ, 16 bits/sample, 13508 bytes total, format 1
snd_sfx: loading effect /rd/shipexplosion.wav
WAVE file is stereo, 11025HZ, 16 bits/sample, 34924 bytes total, format 1

Played sample1 on channel 0

Played sample2 on channel 2

Played sample1 on channel 4

Played sample2 on channel 6

Played sample1 on channel 8

Played sample2 on channel 10

Played sample1 on channel 12

Played sample2 on channel 14

Played sample1 on channel 16

Played sample2 on channel 18

Played sample1 on channel 20

Played sample2 on channel 22
arch: shutting down kernel
maple: final stats -- device count = 2, vbl_cntr = 747, dma_cntr = 741
vid_set_mode: 640x480IL NTSC
fs_romdisk: unmounting image at 0x8c04f030 from /rd
I changed the code a bit because it wasn't exiting properly for me which meant rebooting the machine.

Code: Select all

                                         
#include <kos.h>

extern uint8 romdisk[];

KOS_INIT_ROMDISK(romdisk);
KOS_INIT_FLAGS(INIT_DEFAULT);

int main(int argc, char *argv[])
{
	vid_set_mode(DM_320x240, PM_RGB555);

	snd_init();

	// for manually ending the program
	maple_device_t *cont;
	cont_state_t *cont_state;

	sfxhnd_t sample1 = snd_sfx_load("/rd/shipbullet.wav");
	sfxhnd_t sample2 = snd_sfx_load("/rd/shipexplosion.wav");

	int sample1_channel = 0;
	int sample2_channel = 0;

	while(1)
	{
		if((cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER)) != NULL)
		{
			cont_state = (cont_state_t *) maple_dev_status(cont);
			if(NULL == cont_state) break;
			if(CONT_START  & cont_state->buttons) break;
		}

		sample1_channel = snd_sfx_play(sample1, 128, 128);
		printf("\nPlayed sample1 on channel %d\n", sample1_channel);
		thd_sleep(1000);

		sample2_channel = snd_sfx_play(sample2, 128, 128);
		printf("\nPlayed sample2 on channel %d\n", sample2_channel);
		thd_sleep(1000);
	}
	snd_shutdown();
}

behold the mind
inspired by Dreamcast
ant512
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 27
Joined: Thu Sep 29, 2016 12:31 am
Has thanked: 0
Been thanked: 0

Re: snd_sfx_play_chn() causes sounds to loop

Post by ant512 »

I'm testing on lxdream. I'll switch to real hardware and try it there.
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: snd_sfx_play_chn() causes sounds to loop

Post by bogglez »

Don't rely on lxdream for audio. It can't even play the audio of official games correctly and code comments mention it's WIP.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Re: snd_sfx_play_chn() causes sounds to loop

Post by nymus »

I've also noticed that it might be necessary to use a system besides sleeping to ensure that the sound system is ready. My testing is yielding inconsistent freezes, delays and even failures. Perhaps it's easier to use the mp3 or vorbis libraries since they handle all the low level details such as streaming, polling etc...
behold the mind
inspired by Dreamcast
ant512
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 27
Joined: Thu Sep 29, 2016 12:31 am
Has thanked: 0
Been thanked: 0

Re: snd_sfx_play_chn() causes sounds to loop

Post by ant512 »

Real hardware works fine; it's an lxdream bug.

Thanks everyone!
Post Reply