neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
audio_bitstream.ipp
Go to the documentation of this file.
1// audio_bitstream.ipp
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2021 Leigh Johnston. All Rights Reserved.
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include <neogfx/neogfx.hpp>
23
24namespace neogfx
25{
26 template <typename Interface>
27 inline audio_bitstream<Interface>::audio_bitstream(audio_sample_rate aSampleRate, float aAmplitude) :
28 iSampleRate{ aSampleRate }, iAmplitude{ aAmplitude }
29 {
30 }
31
32 template <typename Interface>
33 inline audio_bitstream<Interface>::audio_bitstream(i_audio_device const& aDevice, float aAmplitude) :
34 audio_bitstream{ aDevice.data_format().sampleRate, aAmplitude }
35 {
36 }
38 template <typename Interface>
40 {
41 }
43 template <typename Interface>
45 {
46 return iSampleRate;
47 }
48
49 template <typename Interface>
51 {
52 iSampleRate = aSampleRate;
53 }
54
55
56 template <typename Interface>
58 {
59 return iAmplitude;
60 }
61
62 template <typename Interface>
63 inline void audio_bitstream<Interface>::set_amplitude(float aAmplitude)
64 {
65 iAmplitude = aAmplitude;
66 }
67
68 template <typename Interface>
70 {
71 return iEnvelope != std::nullopt;
72 }
73
74 template <typename Interface>
76 {
77 return iEnvelope.value();
78 }
79
80 template <typename Interface>
82 {
83 iEnvelope = std::nullopt;
84 }
85
86 template <typename Interface>
88 {
89 iEnvelope = aEnvelope;
90 }
91
92 template <typename Interface>
94 {
95 if (!has_envelope())
96 return amplitude();
97 auto const attack = static_cast<audio_sample_count>(envelope().attack * sample_rate());
98 if (aIndex < attack)
99 return mix(0.0f, amplitude(), static_cast<float>(aIndex) / attack);
100 aIndex -= attack;
101 aLength -= attack;
102 auto const decay = static_cast<audio_sample_count>(envelope().decay * sample_rate());
103 if (aIndex < decay)
104 return mix(amplitude(), amplitude() * envelope().sustain, static_cast<float>(aIndex) / decay);
105 aIndex -= decay;
106 aLength -= decay;
107 auto const release = static_cast<audio_sample_count>(envelope().release * sample_rate());
108 if (aIndex < aLength - release)
109 return amplitude() * envelope().sustain;
110 aIndex -= (aLength - release);
111 aLength -= (aLength - release);
112 if (aIndex < release)
113 return mix(amplitude() * envelope().sustain, 0.0f, static_cast<float>(aIndex) / release);
114 return 0.0f;
115 }
116}
float amplitude() const final
audio_sample_rate sample_rate() const final
adsr_envelope const & envelope() const final
void set_amplitude(float aAmplitude) final
float apply_envelope(audio_sample_index aIndex, audio_sample_count aLength) const
audio_bitstream(audio_sample_rate aSampleRate, float aAmplitude=1.0f)
bool has_envelope() const final
void set_envelope(adsr_envelope const &aEnvelope) final
void set_sample_rate(audio_sample_rate aSampleRate) override
point mix(const point &aLhs, const point &aRhs, double aMixValue)
std::uint64_t audio_sample_count
std::uint64_t audio_sample_index
std::uint64_t audio_sample_rate