-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundSource.h
176 lines (149 loc) · 6.02 KB
/
SoundSource.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Copyright (c) 2008-2016 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#pragma once
#include "../Audio/AudioDefs.h"
#include "../Scene/Component.h"
#include "soloud.h"
#include "soloud_speech.h"
#include "soloud_wav.h"
namespace Urho3D
{
class Audio;
class Sound;
class SoundStream;
// Compressed audio decode buffer length in milliseconds
static const int STREAM_BUFFER_LENGTH = 100;
/// %Sound source component with stereo position. A sound source needs to be created to a node to be considered "enabled" and be able to play, however that node does not need to belong to a scene.
class URHO3D_API SoundSource : public Component
{
URHO3D_OBJECT(SoundSource, Component);
public:
/// Construct.
SoundSource(Context* context);
/// Destruct. Remove self from the audio subsystem
virtual ~SoundSource();
/// Register object factory.
static void RegisterObject(Context* context);
/// Play a sound.
virtual void Play(Sound* sound);
/// Play a sound with specified frequency.
void Play(Sound* sound, float frequency);
/// Play a sound with specified frequency and gain.
void Play(Sound* sound, float frequency, float gain);
/// Play a sound with specified frequency, gain and panning.
void Play(Sound* sound, float frequency, float gain, float panning);
/// Start playing a sound stream.
void Play(SoundStream* stream);
/// Stop playback.
void Stop();
/// Set sound type, determines the master gain group.
void SetSoundType(const String& type);
/// Set frequency.
void SetFrequency(float frequency);
/// Set gain. 0.0 is silence, 1.0 is full volume.
void SetGain(float gain);
/// Set attenuation. 1.0 is unaltered. Used for distance attenuated playback.
void SetAttenuation(float attenuation);
/// Set stereo panning. -1.0 is full left and 1.0 is full right.
void SetPanning(float panning);
/// \deprecated Set whether sound source will be automatically removed from the scene node when playback stops. Note: this is deprecated, consider subscribing to the SoundFinished event instead.
URHO3D_DEPRECATED void SetAutoRemove(bool enable);
/// Set new playback position.
void SetPlayPosition(signed char* pos);
/// Return sound.
Sound* GetSound() const { return sound_; }
/// Return playback position.
volatile signed char* GetPlayPosition() const { return position_; }
/// Return sound type, determines the master gain group.
String GetSoundType() const { return soundType_; }
/// Return playback time position.
float GetTimePosition() const { return timePosition_; }
/// Return frequency.
float GetFrequency() const { return frequency_; }
/// Return gain.
float GetGain() const { return gain_; }
/// Return attenuation.
float GetAttenuation() const { return attenuation_; }
/// Return stereo panning.
float GetPanning() const { return panning_; }
/// \deprecated Return autoremove mode.
URHO3D_DEPRECATED bool GetAutoRemove() const { return autoRemove_; }
/// Return whether is playing.
bool IsPlaying() const;
/// Update the sound source. Perform subclass specific operations. Called by Audio.
virtual void Update(float timeStep);
/// Mix sound source output to a 32-bit clipping buffer. Called by Audio.
void Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolation);
/// Update the effective master gain. Called internally and by Audio when the master gain changes.
void UpdateMasterGain();
/// Set sound attribute.
void SetSoundAttr(const ResourceRef& value);
/// Set sound position attribute.
void SetPositionAttr(int value);
/// Return sound attribute.
ResourceRef GetSoundAttr() const;
/// Set sound playing attribute
void SetPlayingAttr(bool value);
/// Return sound position attribute.
int GetPositionAttr() const;
protected:
/// Audio subsystem.
SharedPtr<Audio> audio_;
/// SoundSource type, determines the master gain group.
String soundType_;
/// SoundSource type hash.
StringHash soundTypeHash_;
/// Frequency.
float frequency_;
/// Gain.
float gain_;
/// Attenuation.
float attenuation_;
/// Stereo panning.
float panning_;
/// Autoremove timer.
float autoRemoveTimer_;
/// Effective master gain.
float masterGain_;
/// Autoremove flag.
bool autoRemove_;
/// Whether finished event should be sent on playback stop.
bool sendFinishedEvent_;
unsigned int handle_;
private:
/// Sound that is being played.
SharedPtr<Sound> sound_;
/// Sound stream that is being played.
SharedPtr<SoundStream> soundStream_;
/// Playback position.
volatile signed char* position_;
/// Playback fractional position.
volatile int fractPosition_;
/// Playback time position.
volatile float timePosition_;
/// Decode buffer.
SharedPtr<Sound> streamBuffer_;
/// Unused stream bytes from previous frame.
int unusedStreamSize_;
//SoLoud::Speech speech;
};
}