6 using SiliconStudio.Paradox.Audio.Wave;
7 using SiliconStudio.Core.Mathematics;
9 namespace SiliconStudio.
Paradox.Audio
38 internal virtual WaveFormat WaveFormat
40 get {
return soundEffect.WaveFormat; }
45 : base(correspSoundEffect.AudioEngine)
47 soundEffect = correspSoundEffect;
50 CreateVoice(soundEffect.WaveFormat);
52 ResetStateToDefault();
62 internal void ResetStateToDefault()
73 if(soundEffect != null)
74 soundEffect.StopConcurrentInstances(
this);
81 public void Play(
bool stopSiblingInstances)
83 PlayExtended(stopSiblingInstances);
86 #region Implementation of the ILocalizable Interface
100 pan = MathUtil.Clamp(value, -1, 1);
103 panChannelVolumes =
new[] { 1f, 1f + pan };
105 panChannelVolumes =
new[] { 1f - pan, 1f };
116 private float[] panChannelVolumes = { 1f, 1f };
122 if (listener == null)
123 throw new ArgumentNullException(
"listener");
126 throw new ArgumentNullException(
"emitter");
128 if(soundEffect.WaveFormat.Channels > 1)
129 throw new InvalidOperationException(
"Apply3D cannot be used on multi-channels sounds.");
136 Apply3DImpl(listener, emitter);
141 private float[] localizationChannelVolumes;
154 pitch = MathUtil.Clamp(value, -1, 1);
165 private float dopplerPitchFactor;
167 private void ComputeDopplerFactor(AudioListener listener, AudioEmitter emitter)
173 const float SoundSpeed = 343f;
174 const float SoundFreq = 600f;
175 const float SoundPeriod = 1 / SoundFreq;
178 if (emitter.DopplerScale <=
float.Epsilon || (emitter.Velocity ==
Vector3.
Zero && listener.Velocity ==
Vector3.
Zero))
180 dopplerPitchFactor = 1f;
184 var vecListEmit = emitter.Position - listener.Position;
185 var distListEmit = vecListEmit.Length();
187 var vecListEmitSpeed = emitter.Velocity - listener.Velocity;
190 dopplerPitchFactor = float.PositiveInfinity;
194 var timeSinceLastWaveArrived = 0f;
195 var lastWaveDistToListener = 0f;
196 const float DistLastWave = SoundPeriod * SoundSpeed;
197 if (DistLastWave > distListEmit)
198 timeSinceLastWaveArrived = (DistLastWave - distListEmit) / SoundSpeed;
200 lastWaveDistToListener = distListEmit - DistLastWave;
201 var nextVecListEmit = vecListEmit + SoundPeriod * vecListEmitSpeed;
202 var nextWaveDistToListener = nextVecListEmit.Length();
203 var timeBetweenTwoWaves = timeSinceLastWaveArrived + (nextWaveDistToListener - lastWaveDistToListener) / SoundSpeed;
204 var apparentFrequency = 1 / timeBetweenTwoWaves;
205 dopplerPitchFactor = (float) Math.Pow(apparentFrequency / SoundFreq, emitter.DopplerScale);
210 internal override void DestroyImpl()
212 if(soundEffect != null)
213 soundEffect.UnregisterInstance(
this);
215 PlatformSpecificDisposeImpl();
220 dopplerPitchFactor = 1f;
221 localizationChannelVolumes =
new[] { 0.5f, 0.5f };
227 UpdateStereoVolumes();
Represents a 3D audio listener in the audio scene. This object, used in combination with an AudioEmit...
static void Dot(ref Vector3 left, ref Vector3 right, out float result)
Calculates the dot product of two vectors.
AudioEngineState
Describe the possible states of the AudioEngine.
This class provides a loaded sound resource which is localizable in the 3D scene. ...
Represents a three dimensional mathematical vector.
void Apply3D(AudioListener listener, AudioEmitter emitter)
Applies 3D positioning to the sound. More precisely adjust the channel volumes and pitch of the sound...
Base class for sound that creates voices
static readonly Vector3 Zero
A SiliconStudio.Core.Mathematics.Vector3 with all of its components set to zero.
Represents the audio engine. In current version, the audio engine necessarily creates its context on ...
Represents a 3D audio emitter in the audio scene. This object, used in combination with an AudioListe...
void Play(bool stopSiblingInstances)
Play or resume the sound effect instance, specifying explicitly how to deal with sibling instances...
void Normalize()
Converts the vector into a unit vector.
void Reset3D()
Cancel the effect of possible previous calls to Apply3D.
Instance of a SoundEffect sound which can be independently localized and played.
override void StopConcurrentInstances()
Stops the sound instances in competition with this sound instance.
Interface for 3D localizable sound. The interface currently supports only mono and stereo sounds (ref...