Skip to content
Permalink
Browse files

Adding volume control support for sounds and sound mixer (#113)

  • Loading branch information
tlrasor authored and blacktm committed Jan 5, 2020
1 parent 14d37bb commit cd25b019adbd15a9bbcb7e786c8b4b9a14c1fc6d
Showing with 71 additions and 0 deletions.
  1. +14 −0 README.md
  2. +20 −0 include/simple2d.h
  3. +37 −0 src/sound.c
@@ -582,6 +582,20 @@ Play the sound like this:
S2D_PlaySound(snd);
```
You can get and set the volume of a sound like so:
```c
int volume = S2D_GetSoundVolume(snd);
S2D_SetSoundVolume(snd, 50); // set volume 50%
```
In addition, get and set the volume of all sounds like this, where the volume is a range between 0 (softest) and 100 (loudest):
```c
int volume = S2D_GetSoundMixVolume();
S2D_SetSoundMixVolume(50); // set volume 50%
```
Since sounds are allocated dynamically, free them using:
```c
@@ -536,6 +536,26 @@ S2D_Sound *S2D_CreateSound(const char *path);
*/
void S2D_PlaySound(S2D_Sound *snd);

/*
* Get the sound's volume
*/
int S2D_GetSoundVolume(S2D_Sound *snd);

/*
* Set the sound's volume a given percentage
*/
void S2D_SetSoundVolume(S2D_Sound *snd, int volume);

/*
* Get the sound mixer volume
*/
int S2D_GetSoundMixVolume();

/*
* Set the sound mixer volume a given percentage
*/
void S2D_SetSoundMixVolume(int volume);

/*
* Free the sound
*/
@@ -46,6 +46,43 @@ void S2D_PlaySound(S2D_Sound *snd) {
}


/*
* Get the sound's volume
*/
int S2D_GetSoundVolume(S2D_Sound *snd) {
if (!snd) return -1;
return ceil(Mix_VolumeChunk(snd->data, -1) * (100.0 / MIX_MAX_VOLUME));
}


/*
* Set the sound's volume a given percentage
*/
void S2D_SetSoundVolume(S2D_Sound *snd, int volume) {
if (!snd) return;
// Set volume to be a percentage of the maximum mix volume
Mix_VolumeChunk(snd->data, (volume / 100.0) * MIX_MAX_VOLUME);
}


/*
* Get the sound mixer volume
*/
int S2D_GetSoundMixVolume() {
return ceil(Mix_Volume(-1, -1) * (100.0 / MIX_MAX_VOLUME));
}


/*
* Set the sound mixer volume a given percentage
*/
void S2D_SetSoundMixVolume(int volume) {
// This sets the volume value across all channels
// Set volume to be a percentage of the maximum mix volume
Mix_Volume(-1, (volume / 100.0) * MIX_MAX_VOLUME);
}


/*
* Free the sound
*/

0 comments on commit cd25b01

Please sign in to comment.
You can’t perform that action at this time.