Can't alter sample volume on the fly

Report any Hollywood bugs here
Post Reply
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Can't alter sample volume on the fly

Post by jPV »

It seems that I can't alter the playback volume of a sample on-the-fly when it's playing. SetVolume() only works when the sample isn't playing. According the documentation it should work on-the-fly... and with music objects it does work.

I have only tested on MorphOS, but there seems to be a topic which might be related (even though not exactly the same): viewtopic.php?p=21437

I'll report this to the bugs section to get noticed better.

Code: Select all

; create a sample
smpdata = {}
slen = 32
For k = 0 To 3000
	For i = 0 To (slen\2)-1
		smpdata[k*slen+i] = -128
		smpdata[k*slen+i+(slen\2)] = 127
	Next
Next
CreateSample(1, smpdata, 6982)

; play it
PlaySample(1)

Wait(50)
Print("Volume should decrease now.")

; try to set its volume
For Local vol = 64 To 0 Step -1
	Wait(5)
	;StopSample(1) ; it works when sample isn't playing
	SetVolume(1, vol) ; Doesn't work on-the-fly as supposed!
	;PlaySample(1) ; ugly work-around that doesn't sound good
Next

WaitLeftMouse()

BTW. there's a small mistake in the documentation ("k" vs. "vol"):

Code: Select all

For k = 64 To 0 Step -1
   SetVolume(1,vol)
Next
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Can't alter sample volume on the fly

Post by airsoftsoftwair »

This is a known issue, see here: http://forums.hollywood-mal.com/viewtop ... f=2&t=1510

Unfortunately, it's not easy to fix, it would require a complete rewrite of the sample playback logic and I'm not sure if it's worth it because it could affect the performance. In 99% of the cases users don't want to modify the volume of a playing sample which is why I'm reluctant to use a completely different, potentially slower way to play samples just for the 1% that need to modify the volume of playing sample. A workaround is to use Hollywood's legacy audio driver which allows you to change the volume of samples on the fly.

The doc is fixed now btw :)

Btw, only AmigaOS 4, MorphOS and AROS are affected by this bug.
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can't alter sample volume on the fly

Post by Bugala »

Didnt check if this is so, but I guess after changing volume you could check every channel if they are playing a sample, then get the current location of the sample(s) played, then stop them, and play again starting from that location. If that is doable, then that should be a workaround for the problem.
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Can't alter sample volume on the fly

Post by jPV »

Is there a reason (the same?) that you can't pause a sample either? My current work-around is stopsample+setvolume+playsample, but that starts the sample from the beginning again... which isn't nice for longer samples. If the volume could be changed while the sample is paused, it would be a better solution even though there would be small stuttering as the result.
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can't alter sample volume on the fly

Post by Bugala »

I took a look now, it seems you can use GetAttrbibute() to get #ATTRPOSITION: which would tell the position that sample is at that point.

Meaning that my idea of When changing volume, check all the samples playing, check their positions, and stop them, works.

But, I didnt find a way to start playing a sample from this ATTRPOSITION location, which means that the second part, where sample would be played, starting from the same location, dont seem to be doable.

@Andreas, a Wishlist: Wouldnt it be logical to also have a way to play a sample from some certain position, since what is the use of ATTRPOSITION, if you cant use it for anything? In addition, this would work as a work around for Amigas on the problem.
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Can't alter sample volume on the fly

Post by airsoftsoftwair »

jPV wrote: Mon Jun 23, 2025 10:13 am Is there a reason (the same?) that you can't pause a sample either?
Samples are meant to be rather short, like a few seconds or so, which is why I didn't implement pause functionality because for longer sounds music objects should be used instead.
jPV wrote: Mon Jun 23, 2025 10:13 am My current work-around is stopsample+setvolume+playsample, but that starts the sample from the beginning again... which isn't nice for longer samples.
Is there any reason why you use samples and not music objects? Keep in mind that samples are always buffered entirely in memory so if you use longer samples you'll consume lots of memory. 1 second of the 44.1khz 16-bit stereo audio needs 176400 bytes of memory already.
Bugala wrote: Mon Jun 23, 2025 12:47 pm @Andreas, a Wishlist: Wouldnt it be logical to also have a way to play a sample from some certain position, since what is the use of ATTRPOSITION, if you cant use it for anything?
See above. Samples should normally be really short, like sound fx you hear during a game (audio feedback when firing of a gun, collecting diamonds, getting hit by an enemy etc.) For longer sounds Hollywood has music objects.
Post Reply