internal SoundInstance(Sound staticSound, AudioListener listener, bool forceLoadInMemory)
        {
            Listener = listener;
            engine = staticSound.AudioEngine;
            sound = staticSound;
            spatialized = staticSound.Spatialized;

            var streamed = staticSound.StreamFromDisk && !forceLoadInMemory;

            if (engine.State == AudioEngineState.Invalidated)
                return;

            Source = AudioLayer.SourceCreate(listener.Listener, staticSound.SampleRate, streamed ? CompressedSoundSource.NumberOfBuffers : 1, staticSound.Channels == 1, spatialized, streamed);
            if (Source.Ptr == IntPtr.Zero)
            {
                throw new Exception("Failed to create an AudioLayer Source");
            }

            if (streamed)
            {
                soundSource = new CompressedSoundSource(this, staticSound.CompressedDataUrl, staticSound.NumberOfPackets, staticSound.SampleRate, staticSound.Channels, staticSound.MaxPacketLength);
            }
            else
            {
                if (sound.PreloadedBuffer.Ptr == IntPtr.Zero)
                {
                    staticSound.LoadSoundInMemory(); //this should be already loaded by the serializer, but in the case of forceLoadInMemory might not be the case yet.
                }
                AudioLayer.SourceSetBuffer(Source, staticSound.PreloadedBuffer);
            }

            ResetStateToDefault();
        }
        /// <summary>
        /// Created a new <see cref="AudioEmitterSoundController"/> instance.
        /// </summary>
        /// <param name="parent">The parent AudioEmitterComponent to which the controller is associated.</param>
        /// <param name="sound">The underlying Sound to be controlled</param>
        /// <remarks>A <see cref="sound"/> can be associated to several controllers.</remarks>
        internal AudioEmitterSoundController(AudioEmitterComponent parent, Sound sound)
        {
            if(sound == null)
                throw new ArgumentNullException(nameof(sound));

            this.sound = sound;

            Volume = 1;
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SunBurnSound"/> class.
 /// </summary>
 /// <param name="nativeSound">The native sound.</param>
 public XenkoSound(object nativeSound)
     : base(nativeSound)
 {
     sound = nativeSound as Sound;
     if (sound != null)
     {
         soundInstance = sound.CreateInstance();
     }
 }
Example #4
0
 internal void UnregisterSound(Sound disposedSound)
 {
     lock (notDisposedSounds)
     {
         if (!notDisposedSounds.Remove(disposedSound))
             throw new AudioSystemInternalException("Try to remove a disposed sound not in the list of registered sounds.");
     }
 }
Example #5
0
 internal void RegisterSound(Sound newSound)
 {
     lock (notDisposedSounds)
     {
         notDisposedSounds.Add(newSound);
     }
 }
        public void DetachSound(Sound sound)
        {
            if (sound == null)
            {
                throw new ArgumentNullException(nameof(sound));
            }
            if (!SoundToController.ContainsKey(sound))
            {
                throw new ArgumentException("The provided Sound is not currently attached to this emitter component.");
            }

            var oldController = SoundToController[sound];
            SoundToController.Remove(sound);
            ControllerCollectionChanged?.Invoke(this, new ControllerCollectionChangedEventArgs(Entity, oldController, this, NotifyCollectionChangedAction.Remove));
        }
        public void AttachSound(Sound sound)
        {
            if (sound == null)
            {
                throw new ArgumentNullException(nameof(sound));
            }
            if (sound.Channels > 1)
            {
                throw new InvalidOperationException("The provided Sound has more than one channel. It can not be localized in the 3D scene.");
            }

            if(SoundToController.ContainsKey(sound))
                return;

            var newController = new AudioEmitterSoundController(this, sound);
            SoundToController[sound] = newController;
            ControllerCollectionChanged?.Invoke(this, new ControllerCollectionChangedEventArgs(Entity, newController, this, NotifyCollectionChangedAction.Add ));
        }
        public AudioEmitterSoundController GetSoundController(Sound sound)
        {
            if (sound == null)
            {
                throw new ArgumentNullException(nameof(sound));
            }
            if (!SoundToController.ContainsKey(sound))
            {
                throw new ArgumentException("The provided Sound has not been attached to the EmitterComponent.");
            }

            return SoundToController[sound];
        }