windows_volume_catcher.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @file windows_volume_catcher.cpp
  3. * @brief A Windows implementation of volume level control of all audio
  4. * channels opened by a process.
  5. *
  6. * @cond
  7. * $LicenseInfo:firstyear=2010&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. * @endcond
  28. */
  29. #include "linden_common.h"
  30. #include <mmeapi.h>
  31. #include "volume_catcher.h"
  32. #include "llsingleton.h"
  33. class VolumeCatcherImpl : public LLSingleton<VolumeCatcherImpl>
  34. {
  35. friend class LLSingleton<VolumeCatcherImpl>;
  36. public:
  37. void setVolume(F32 volume);
  38. // Remember pan for calculating individual channel levels later
  39. LL_INLINE void setPan(F32 pan) { mPan = pan; }
  40. private:
  41. // This is a singleton class: both callers and the component implementation
  42. // should use getInstance() to find the instance.
  43. VolumeCatcherImpl();
  44. ~VolumeCatcherImpl() override = default;
  45. private:
  46. F32 mVolume;
  47. F32 mPan;
  48. };
  49. VolumeCatcherImpl::VolumeCatcherImpl()
  50. : mVolume(1.f), // default volume is max
  51. mPan(0.f) // default pan is centered
  52. {
  53. }
  54. void VolumeCatcherImpl::setVolume(F32 volume)
  55. {
  56. mVolume = volume;
  57. // Set both left/right to same volume.
  58. // *TODO: use pan value to set independently
  59. DWORD left_channel = (DWORD)(mVolume * 65535.0f);
  60. DWORD right_channel = (DWORD)(mVolume * 65535.0f);
  61. DWORD hw_volume = left_channel << 16 | right_channel;
  62. ::waveOutSetVolume(NULL, hw_volume);
  63. }
  64. /////////////////////////////////////////////////////
  65. VolumeCatcher::VolumeCatcher()
  66. {
  67. pimpl = VolumeCatcherImpl::getInstance();
  68. }
  69. void VolumeCatcher::setVolume(F32 volume)
  70. {
  71. pimpl->setVolume(volume);
  72. }
  73. void VolumeCatcher::setPan(F32 pan)
  74. {
  75. pimpl->setPan(pan);
  76. }
  77. void VolumeCatcher::pump()
  78. {
  79. // No periodic tasks are necessary for this implementation.
  80. }