Exemplo n.º 1
0
        void UpdateVolume2()
        {
            float value = CurrentVirtualChannel.GetTotalVolume() * CurrentVirtualChannel.GetRolloffFactor();

            Al.alSourcef(alSource, Al.AL_GAIN, value);
            OpenALSoundWorld.CheckError();
        }
        unsafe void UpdateVolume2()
        {
            float value = CurrentVirtualChannel.GetTotalVolume() * CurrentVirtualChannel.GetRolloffFactor();

            int volume;

            if (value > 0.001f)
            {
                volume = (int)(20.0f * 100.0f * Math.Log10(value));
            }
            else
            {
                volume = DSound.DSBVOLUME_MIN;
            }

            if (volume < DSound.DSBVOLUME_MIN)
            {
                volume = DSound.DSBVOLUME_MIN;
            }
            if (volume > DSound.DSBVOLUME_MAX)
            {
                volume = DSound.DSBVOLUME_MAX;
            }

            //update volume for dublicate buffers (IDirectSoundBuffer8.SetVolume problem)
            //it need have volume not equal to original buffer.
            bool isOriginalBuffer = false;
            IDirectSoundBuffer *originalBuffer = (IDirectSoundBuffer *)
                                                 currentSound.soundBuffers[0].ToPointer();

            isOriginalBuffer = originalBuffer == currentSoundBuffer;

            if (!isOriginalBuffer)
            {
                int originalBufferVolume = 0;
                IDirectSoundBuffer.GetVolume(originalBuffer, out originalBufferVolume);

                if (volume == originalBufferVolume)
                {
                    if (volume == DSound.DSBVOLUME_MAX)
                    {
                        volume = DSound.DSBVOLUME_MAX - 1;
                    }
                    else
                    {
                        volume++;
                    }
                }
            }

            //change volume
            int hr = IDirectSoundBuffer.SetVolume(currentSoundBuffer, volume);

            if (Wrapper.FAILED(hr))
            {
                DirectSoundWorld.Warning("IDirectSoundBuffer.SetVolume", hr);
            }

            //update volume of dublicate buffers
            if (isOriginalBuffer)
            {
                List <DirectSoundRealChannel> realChannels = DirectSoundWorld.Instance.realChannels;
                for (int nRealChannel = 0; nRealChannel < realChannels.Count; nRealChannel++)
                {
                    DirectSoundRealChannel realChannel = realChannels[nRealChannel];
                    if (realChannel != this && realChannel.currentSound == currentSound)
                    {
                        realChannel.UpdateVolume();
                    }
                }
            }
        }