ADSRReader.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*******************************************************************************
  2. * Copyright 2009-2016 Jörg Müller
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. #pragma once
  17. /**
  18. * @file ADSRReader.h
  19. * @ingroup fx
  20. * The ADSRReader class.
  21. */
  22. #include "fx/EffectReader.h"
  23. AUD_NAMESPACE_BEGIN
  24. /**
  25. * This class is an ADSR filters.
  26. */
  27. class AUD_API ADSRReader : public EffectReader
  28. {
  29. private:
  30. enum ADSRState
  31. {
  32. ADSR_STATE_INVALID = 0, /// Invalid ADSR state or finished.
  33. ADSR_STATE_ATTACK = 1, /// Initial attack state.
  34. ADSR_STATE_DECAY = 2, /// Decay state.
  35. ADSR_STATE_SUSTAIN = 3, /// Sustain state.
  36. ADSR_STATE_RELEASE = 4 /// Release state.
  37. };
  38. /**
  39. * Attack time.
  40. */
  41. float m_attack;
  42. /**
  43. * Decay time.
  44. */
  45. float m_decay;
  46. /**
  47. * Sustain level.
  48. */
  49. float m_sustain;
  50. /**
  51. * Release time.
  52. */
  53. float m_release;
  54. /**
  55. * Current state.
  56. */
  57. ADSRState m_state;
  58. /**
  59. * Current level.
  60. */
  61. float m_level;
  62. // delete copy constructor and operator=
  63. ADSRReader(const ADSRReader&) = delete;
  64. ADSRReader& operator=(const ADSRReader&) = delete;
  65. void AUD_LOCAL nextState(ADSRState state);
  66. public:
  67. /**
  68. * Creates a new ADSR reader.
  69. * \param reader The reader to read from.
  70. * \param attack The attack time in seconds.
  71. * \param decay The decay time in seconds.
  72. * \param sustain The sustain level, should be in range [0 - 1].
  73. * \param release The release time in seconds.
  74. */
  75. ADSRReader(std::shared_ptr<IReader> reader, float attack, float decay, float sustain, float release);
  76. virtual ~ADSRReader();
  77. virtual void read(int& length, bool& eos, sample_t* buffer);
  78. /**
  79. * Triggers the release.
  80. */
  81. void release();
  82. };
  83. AUD_NAMESPACE_END