Skip to main content
gamedev.net
🔒 Locked

Mix_Volume

Started by Mybowlcut 6 replies 1.1k views
Original Post
Mybowlcut
Mybowlcut
I'm creating a volume slider in my game and it involves use of Mix_Volume. I'm having a problem with it...
const int Audio_Engine::Get_Volume() {
	volume = Mix_Volume(-1, -1);
	return volume;
}

void Audio_Engine::Set_Volume(const int new_volume) {
	volume = Mix_Volume(-1, new_volume);
}

When I call Mix_Volume in Set_Volume(), volume doesn't change. However, when I call Mix_Volume() in Get_Volume(), the proper value is returned. I am aware that it's because of the different parameters, but I'm not aware of what the difference is. Mix_Volume documentation. It says there that regardless of the volume being sent that I should receive the current average volume if I pass -1 as the channel parameter. Helpppppp!

Mybowlcut
Mybowlcut
2 years later and I'm brought back to this problem... here is another example:
void SDL_Audio_Player::Set_Volume(const Slider_Value& slider_value){    int vol = slider_value.numerator * MIX_MAX_VOLUME / slider_value.denominator;    volume_before_mute = Mix_Volume(-1, vol);    int test = Mix_Volume(-1, -1);}
volume_before_mute is 128, but test is 118 (what I actually set the volume to).

SDL_GetError returns an empty string..

Cheers.

Zakwayda
Zakwayda
Can you clarify the problem a bit? I gather that when you check to see what the current volume is, you get the value that you expect (e.g. 'test' is set to '118'). Is the problem then that there's no perceptible change in the overall volume of the audio? Or am I misunderstanding?
Mybowlcut
Mybowlcut
Quote:
Original post by jyk
Can you clarify the problem a bit? I gather that when you check to see what the current volume is, you get the value that you expect (e.g. 'test' is set to '118'). Is the problem then that there's no perceptible change in the overall volume of the audio? Or am I misunderstanding?
void SDL_Audio_Player::Set_Volume(const Slider_Value& slider_value){    int vol = slider_value.numerator * MIX_MAX_VOLUME / slider_value.denominator;    volume_before_mute = Mix_Volume(-1, vol);    int test = Mix_Volume(-1, -1);}
Basically, the first call to Mix_Volume does set the volume properly, however the value returned from that call is the same as what is was before. Only the second call gets the updated volume.

So what I believe is happening is that setting the volume with Mix_Volume doesn't return the updated volume, but passing in -1 to get the average volume does return the update volume. It doesn't say anything about this in the docs, so I was curious as to why it was so.

Edit: From the docs:
Quote:
Returns: current volume of the channel. If channel is -1, the average volume is returned.


Cheers.

Zakwayda
Zakwayda
Quote:
Basically, the first call to Mix_Volume does set the volume properly, however the value returned from that call is the same as what is was before. Only the second call gets the updated volume.
I think that's the intended behavior. The confusion might stem from the wording of the documentation, which is a bit ambiguous:
Quote:
Returns: current volume of the channel.
It's not really clear from the context what 'current' means here, but if you look at the source you can see that the function returns the value that the volume variable had before being reset to the new value. For example, say the volume is at 100. The following:
std::cout << Mix_Volume(-1, 50) << '\n;
Would print '100', not '50'.

Does that clear things up at all?
Mybowlcut
Mybowlcut
Wow. I had thought that that might be a possibility, but... I dismissed it because honestly, who would expect or desire that behaviour? :s

That clears it up though, thanks! How do you get the source code? I only have the header files which don't have the function definitions in them..

Cheers!

Zakwayda
Zakwayda
Quote:
How do you get the source code?
You can get it here.

The last official release has a few bugs in it (which may or may not be a problem for you, depending on how you're using the library). For the most up-to-date source (in which at least some of these bugs are fixed), check out the code from the Subversion repository. If you just want to have the code around for reference though, the last official release should suffice.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.