NAP
playbackcomponent.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Nap includes
8 #include <nap/resourceptr.h>
9 #include <audio/utility/safeptr.h>
10 
11 // Audio includes
12 #include <audio/component/audiocomponentbase.h>
13 #include <audio/resource/audiobufferresource.h>
14 #include <audio/node/bufferplayernode.h>
15 #include <audio/node/multiplynode.h>
16 #include <audio/node/controlnode.h>
17 #include <audio/node/filternode.h>
18 
19 namespace nap
20 {
21  namespace audio
22  {
23 
24  // Forward declares
25  class AudioService;
26  class PlaybackComponentInstance;
27 
32  class NAPAPI PlaybackComponent : public AudioComponentBase
33  {
34  RTTI_ENABLE(AudioComponentBase)
36 
37  public:
39 
40  // Properties
42  std::vector<int> mChannelRouting = { };
43  bool mAutoPlay = true;
44  TimeValue mStartPosition = 0;
45  TimeValue mDuration = 0;
46  TimeValue mFadeInTime = 0;
47  TimeValue mFadeOutTime = 0;
48  ControllerValue mPitch = 1.0;
49  ControllerValue mGain = 1.0;
50  ControllerValue mStereoPanning = 0.5;
51 
55  bool isStereo() const { return mChannelRouting.size() == 2; }
56  };
57 
58 
65  {
66  RTTI_ENABLE(AudioComponentBaseInstance)
67  public:
69 
70  // Inherited from ComponentInstance
71  bool init(utility::ErrorState& errorState) override;
72 
73  void update(double deltaTime) override;
74 
75  // Inherited from AudioComponentBaseInstance
76  int getChannelCount() const override { return mGainNodes.size(); }
77 
78  OutputPin* getOutputForChannel(int channel) override { return &mGainNodes[channel]->audioOutput; }
79 
84  void start(TimeValue startPosition = 0, TimeValue duration = 0);
85 
89  void stop();
90 
94  void setGain(ControllerValue gain);
95 
100  void setChannelGain(int channel, ControllerValue gain);
101 
105  void setStereoPanning(ControllerValue panning);
106 
110  void setFadeInTime(TimeValue time);
111 
115  void setFadeOutTime(TimeValue time);
116 
120  void setPitch(ControllerValue pitch);
121 
125  bool isStereo() const { return mGainNodes.size() == 2; }
126 
130  bool isPlaying() const { return mPlaying; }
131 
135  ControllerValue getGain() const { return mGain; }
136 
140  ControllerValue getStereoPanning() const { return mStereoPanning; }
141 
145  ControllerValue getFadeInTime() const { return mFadeInTime; }
146 
150  ControllerValue getFadeOutTime() const { return mFadeOutTime; }
151 
155  ControllerValue getPitch() const { return mPitch; }
156 
160  TimeValue getCurrentPlayingTime() const { return mCurrentPlayingTime; }
161 
162  private:
163  void applyGain(TimeValue rampTime);
164 
165  std::vector<SafeOwner<BufferPlayerNode>> mBufferPlayers; // Nodes for each channel performing the actual audio playback.
166  std::vector<SafeOwner<MultiplyNode>> mGainNodes; // Nodes for each channel to gain the signal.
167  std::vector<SafeOwner<ControlNode>> mGainControls; // Nodes to control the gain for each channel.
168 
169  ControllerValue mGain = 0;
170  std::vector<ControllerValue> mChannelGains;
171  ControllerValue mStereoPanning = 0.5;
172  TimeValue mFadeInTime = 0;
173  TimeValue mFadeOutTime = 0;
174  ControllerValue mPitch = 1.0;
175  TimeValue mDuration = 0;
176  TimeValue mCurrentPlayingTime = 0;
177  std::vector<int> mChannelRouting;
178 
179  bool mPlaying = false; // Indicates wether the component is currently playing
180 
181  PlaybackComponent* mResource = nullptr; // The component's resource
182  NodeManager* mNodeManager = nullptr; // The audio node manager this component's audio nodes are managed by
183  AudioService* mAudioService = nullptr;
184  };
185 
186  }
187 
188 }
nap::audio::PlaybackComponentInstance::isStereo
bool isStereo() const
Definition: playbackcomponent.h:125
nap::audio::PlaybackComponentInstance::getPitch
ControllerValue getPitch() const
Definition: playbackcomponent.h:155
nap::audio::PlaybackComponentInstance::getCurrentPlayingTime
TimeValue getCurrentPlayingTime() const
Definition: playbackcomponent.h:160
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::audio::PlaybackComponent
Definition: playbackcomponent.h:32
nap::audio::PlaybackComponentInstance::getFadeOutTime
ControllerValue getFadeOutTime() const
Definition: playbackcomponent.h:150
nap::audio::PlaybackComponent::PlaybackComponent
PlaybackComponent()
Definition: playbackcomponent.h:38
nap::utility::ErrorState
Definition: errorstate.h:19
nap::audio::PlaybackComponentInstance
Definition: playbackcomponent.h:64
nap::audio::AudioComponentBaseInstance
Definition: audiocomponentbase.h:47
nap::audio::AudioComponentBase
Definition: audiocomponentbase.h:31
nap::audio::AudioService
Definition: audioservice.h:26
nap::audio::TimeValue
float TimeValue
Definition: audiotypes.h:119
nap::audio::PlaybackComponentInstance::getStereoPanning
ControllerValue getStereoPanning() const
Definition: playbackcomponent.h:140
nap::audio::PlaybackComponentInstance::getOutputForChannel
OutputPin * getOutputForChannel(int channel) override
Definition: playbackcomponent.h:78
nap::audio::PlaybackComponentInstance::isPlaying
bool isPlaying() const
Definition: playbackcomponent.h:130
nap::audio::NodeManager
Definition: audionodemanager.h:33
nap::audio::ControllerValue
float ControllerValue
Definition: audiotypes.h:113
nap::EntityInstance
Definition: entity.h:34
nap::audio::OutputPin
Definition: audiopin.h:204
nap::audio::PlaybackComponentInstance::getFadeInTime
ControllerValue getFadeInTime() const
Definition: playbackcomponent.h:145
nap::Component
Definition: component.h:151
nap::audio::PlaybackComponentInstance::getChannelCount
int getChannelCount() const override
Definition: playbackcomponent.h:76
nap
Definition: templateapp.h:17
nap::audio::PlaybackComponent::isStereo
bool isStereo() const
Definition: playbackcomponent.h:55
nap::audio::PlaybackComponentInstance::PlaybackComponentInstance
PlaybackComponentInstance(EntityInstance &entity, Component &resource)
Definition: playbackcomponent.h:68
nap::audio::PlaybackComponentInstance::getGain
ControllerValue getGain() const
Definition: playbackcomponent.h:135