Volume Control


Pickle

Mega GP Mania
Joined
May 30, 2006
Messages
5,527
Location
Detroit, Michigan
Website
Visit site
I have some sw im messing with, it usig SDL_mixer for sound. I used the built in volume function's but it didnt control all the sounds (like music, the sw uses an hooked-in player).

Im pretty sure I can do this, but would modifying /dev/mixer override SDL and anything else and set the volume of all sound coming from the GP2X? In other words would it act like a master colume control?
 
This is the code I use to change volume:
CODE
void MusicManager::VolumeUp(void)
{
// Check the current volume is above 0
if(_CurrentVolume < MAX_VOLUME_LEVEL)
{
// Change the volume level up a notch
_CurrentVolume += 2;
Mix_VolumeMusic(_CurrentVolume);
}
}

void MusicManager::VolumeDown(void)
{
// Check the current volume is below 128
if(_CurrentVolume > MIN_VOLUME_LEVEL)
{
// Change the volume level up a notch
_CurrentVolume -= 2;
Mix_VolumeMusic(_CurrentVolume);
}
}

CODE
void SfxManager::VolumeDown(void)
{
// Check the current volume is above 0
if(_CurrentVolume > MIN_VOLUME_LEVEL)
{
// Change the volume level down a notch
_CurrentVolume -= 2;
// For all Chunk wrappers
vector<ChunkWrapper *>::iterator Itr = _VectorOfChunkPointers.begin();
while(Itr != _VectorOfChunkPointers.end() )
{
// Check if a Chunk exists
if( (*Itr)->_MixChunk)
{
// If so, change the volume
(*Itr)->_MixChunk->volume = _CurrentVolume;
}
++Itr;
}
}
}

void SfxManager::VolumeUp(void)
{
// Check the current volume is below 128
if(_CurrentVolume < MAX_VOLUME_LEVEL)
{
// Change the volume level up a notch
_CurrentVolume += 2;
// For all Chunk wrappers
vector<ChunkWrapper *>::iterator Itr = _VectorOfChunkPointers.begin();
while(Itr != _VectorOfChunkPointers.end() )
{
// Check if a Chunk exists
if( (*Itr)->_MixChunk)
{
// If so, change the volume
(*Itr)->_MixChunk->volume = _CurrentVolume;
}
++Itr;
}
}
}


CODE
// Change the volume
unsigned int CurrentInputState = _Kernel->GetInputManager()->GetCurrentInputState();
if( CurrentInputState & BUTTON_VOLUP )
{
_Kernel->GetSoundModuleWrapper()->GetMusicManager()->VolumeUp();
_Kernel->GetSoundModuleWrapper()->GetSfxManager()->VolumeUp();
}
if( CurrentInputState & BUTTON_VOLDOWN )
{
_Kernel->GetSoundModuleWrapper()->GetMusicManager()->VolumeDown();
_Kernel->GetSoundModuleWrapper()->GetSfxManager()->VolumeDown();
}
 
thanks, i am familar with those functions. Maybe I need to find each Mix_Chunk, which I dont think I really want to do. I know for sure the chunks arnt in a wrapper like you posted.

If the /dev/mixer can control the volume of the entire system it will be a much quicker.
 
yaustar said:
This is the code I use to change volume:

// Check the current volume is above 0
if(_CurrentVolume < MAX_VOLUME_LEVEL)

// Check the current volume is below 128
if(_CurrentVolume > MIN_VOLUME_LEVEL)
You use some strangely misleading comments there... Maybe it's just me :huh:
 
Last edited by a moderator:
Yep, /dev/mixer is a more reliable way to set sound in a program in my experience:

CODE

static void gp2x_set_volume(int newvol)
{
if ((newvol >= 0) && (newvol <= 100)) {
unsigned long soundDev = open("/dev/mixer", O_RDWR);
int vol = ((newvol << 8) | newvol);
ioctl(soundDev, SOUND_MIXER_WRITE_PCM, &vol);
close(soundDev);
}
}

// Returns 0-100, current mixer volume, -1 on error.
static int gp2x_get_volume(void)
{
int vol = -1;
unsigned long soundDev = open("/dev/mixer", O_RDONLY);
if (soundDev)
{
ioctl(soundDev, SOUND_MIXER_READ_PCM, &vol);
close(soundDev);
if (vol != -1) {
//just return one channel , not both channels, they're hopefully the same anyways
return (vol & 0xFF);
}
}
return vol;
}
 
Senor Quack said:
Yep, /dev/mixer is a more reliable way to set sound in a program in my experience:
Well that works sort of. With headphones on it changes the volume, but kills the right and only the left changes. Without headphones I can barely hear it, even going up to 100. Have you used this in any GP2X app's?
 
Last edited by a moderator:
Pickle said:
Senor Quack said:
Yep, /dev/mixer is a more reliable way to set sound in a program in my experience:
Well that works sort of. With headphones on it changes the volume, but kills the right and only the left changes. Without headphones I can barely hear it, even going up to 100. Have you used this in any GP2X app's?



Sorry, I pasted the wrong version of the setvolume function, here is the correct one:

CODE

//DKS - new
static void gp2x_set_volume(int newvol)
{
if ((newvol >= 0) && (newvol <= 100)) {
unsigned long soundDev = open("/dev/mixer", O_RDWR);
int vol = ((newvol << 8) | newvol);
ioctl(soundDev, SOUND_MIXER_WRITE_PCM, &vol);
close(soundDev);
}
}
 
Last edited by a moderator:
Senor Quack said:
Yep, /dev/mixer is a more reliable way to set sound in a program in my experience:

*snip*
That's excellent! Thank you so much for sharing that :)
 
Last edited by a moderator:
Alex. said:
Senor Quack said:
Yep, /dev/mixer is a more reliable way to set sound in a program in my experience:

*snip*
That's excellent! Thank you so much for sharing that :)


Well, in all honesty I think I got the code from an old post on here, maybe Squidge.
 
Last edited by a moderator:
fishybawb said:
yaustar said:
This is the code I use to change volume:

// Check the current volume is above 0
if(_CurrentVolume < MAX_VOLUME_LEVEL)

// Check the current volume is below 128
if(_CurrentVolume > MIN_VOLUME_LEVEL)
You use some strangely misleading comments there... Maybe it's just me :huh:

Yeeeeeeeaaaahh... shhh...... :ph34r:
 
Last edited by a moderator:
Back
Top