Search code examples
c++caudiobass

How to mute / unmute BASS?


How can i mute and unmute BASS playback (platform independend)? Until now i save the current volume before muting, set volume to 0 and set it back if unmute.

Example:
part of my C++ class

volume_t lastVolume; // 0.0f = silent, 1.0f = max (volume_t = float)

// ...

bool mute(bool mute)
{
    if( mute )
    {
        lastVolume = getVolume(); // Save current volume

        return setVolume(0.0f); // Set volume to silent
    }
    else
    {
        return setVolume(lastVolume); // restore last volume before muting
    }
}

Is there a better way to do this? In the BASS Api documentation there's only one mute function:

BOOL BASS_WASAPI_SetMute(
    BOOL mute
);

However, this looks good, but unfortunately its part of BASSWASAPI (WASAPI I/O on Windows Vista and later - which is not crossplatform).


Solution

  • Here's my solution:

    class PlayerBASS : public virtual AbstractPlayer
    {
    public:
    
        // ...
    
        /**
         * Set volume on current channel.
         * 
         * @param volume      volume (0.0f - 1.0f)
         */
        bool setVolume(volume_t volume)
        {
            return BASS_ChannelSetAttribute(streamHandle, BASS_ATTRIB_VOL, volume);
        }
    
        /**
         * Get volume on current channel.
         *
         * @return            volume (0.0f - 1.0f)
         */
        volume_t getVolume()
        {
            float value;
            BASS_ChannelGetAttribute(streamHandle, BASS_ATTRIB_VOL, &value);
    
            return value;
        }
    
        /**
         * Mute / Unmute the volume on current channel.
         *
         * @return            'true' if successful, 'false' if error
         */
        bool mute(bool mute)
        {    
            if( mute == muted ) // dont mute if already muted (and vice versa)
                return true;
    
            bool rtn; // returnvalue
    
            if( mute ) // mute
            {
                lastVolume = getVolume(); // save current volume
                rtn = setVolume(0.0f); // set volume to 0.0f (= mute)
                muted = true; // set mute-state
            }
            else // unmute
            {
                rtn = setVolume(lastVolume); // restore volume
                muted = false; // set mute-state
            }
    
            return rtn; // returnvalue
        }
    
        // ...
    
    private:
        // ...
        HSTREAM streamHandle; // Streamhandle
        bool muted; // flag for mute-state ('true': muted, 'false': unmuted) - init with 'false'
        volume_t lastVolume; // lastVolume
    };
    

    Don't use BASS_SetVolume() / BASS_GetVolume() here - it will set volume of your whole system!

    Thats it!