Jump to content


Photo

Volume Control


  • Please log in to reply
10 replies to this topic

#1 Pickle

Pickle

    Mega GP Mania

  • X-treme Team
  • 4074 posts
  • Gender:Male
  • Location:Detroit, Michigan

Posted 06 February 2008 - 04:49 PM

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?

#2 yaustar

yaustar

    UK GP32 & GP2X Owner

  • GP Guru
  • 2713 posts
  • Location:UK

Posted 06 February 2008 - 05:54 PM

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();
    }


#3 Pickle

Pickle

    Mega GP Mania

  • X-treme Team
  • 4074 posts
  • Gender:Male
  • Location:Detroit, Michigan

Posted 06 February 2008 - 06:03 PM

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.

#4 fishybawb

fishybawb

    Hired Geek

  • GP32 Hardcore
  • PipPipPipPipPipPip
  • 1114 posts
  • Location:York, UK

Posted 07 February 2008 - 12:03 AM

QUOTE(yaustar @ Feb 6 2008, 05:54 PM) View Post

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.gif


#5 Senor Quack

Senor Quack

    I feel a great disturbance in the source

  • GP Guru
  • 1165 posts
  • Gender:Male
  • Location:USA

Posted 07 February 2008 - 01:10 AM

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;
}


Edited by Senor Quack, 07 February 2008 - 02:25 AM.


#6 Pickle

Pickle

    Mega GP Mania

  • X-treme Team
  • 4074 posts
  • Gender:Male
  • Location:Detroit, Michigan

Posted 07 February 2008 - 01:57 AM

QUOTE(Senor Quack @ Feb 6 2008, 08:10 PM) View Post

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?

Edited by Pickle, 07 February 2008 - 01:57 AM.


#7 Senor Quack

Senor Quack

    I feel a great disturbance in the source

  • GP Guru
  • 1165 posts
  • Gender:Male
  • Location:USA

Posted 07 February 2008 - 02:14 AM

QUOTE(Pickle @ Feb 6 2008, 08:57 PM) View Post

QUOTE(Senor Quack @ Feb 6 2008, 08:10 PM) View Post

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);
    }
}

Edited by Senor Quack, 07 February 2008 - 02:18 AM.


#8 Pickle

Pickle

    Mega GP Mania

  • X-treme Team
  • 4074 posts
  • Gender:Male
  • Location:Detroit, Michigan

Posted 07 February 2008 - 02:19 AM

Perfect!

#9 Alex.

Alex.

    Retired

  • GP Guru
  • 4582 posts
  • Gender:Male

Posted 07 February 2008 - 03:45 AM

QUOTE(Senor Quack @ Feb 6 2008, 08:10 PM) View Post

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 smile.gif

#10 Senor Quack

Senor Quack

    I feel a great disturbance in the source

  • GP Guru
  • 1165 posts
  • Gender:Male
  • Location:USA

Posted 07 February 2008 - 03:47 AM

QUOTE(Alex. @ Feb 6 2008, 10:45 PM) View Post

QUOTE(Senor Quack @ Feb 6 2008, 08:10 PM) View Post

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 smile.gif


Well, in all honesty I think I got the code from an old post on here, maybe Squidge.

#11 yaustar

yaustar

    UK GP32 & GP2X Owner

  • GP Guru
  • 2713 posts
  • Location:UK

Posted 07 February 2008 - 11:22 AM

QUOTE(fishybawb @ Feb 6 2008, 01:03 PM) View Post

QUOTE(yaustar @ Feb 6 2008, 05:54 PM) View Post

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.gif

Yeeeeeeeaaaahh... shhh...... ph34r.gif