Esempio n. 1
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();
     }
 }
Esempio n. 2
0
        public override void Start()
        {
            base.Start();

            triggeredEvent = (Trigger != null) ? new EventReceiver<bool>(Trigger.TriggerEvent) : null;

            sfxInstance = SoundEffect?.CreateInstance();
            sfxInstance?.Stop();
        }
        /// <summary>
        /// Sub classes can implement their own streaming sources.
        /// </summary>
        /// <param name="soundInstance">the sound instance associated.</param>
        /// <param name="numberOfBuffers">the size of the streaming ring-buffer.</param>
        /// <param name="maxBufferSizeBytes">the maximum size of each buffer.</param>
        protected DynamicSoundSource(SoundInstance soundInstance, int numberOfBuffers, int maxBufferSizeBytes)
        {
            prebufferedTarget = (int)Math.Ceiling(numberOfBuffers/(double)3);

            SoundInstance = soundInstance;
            for (var i = 0; i < numberOfBuffers; i++)
            {
                var buffer = AudioLayer.BufferCreate(maxBufferSizeBytes);
                deviceBuffers.Add(buffer);
                freeBuffers.Enqueue(deviceBuffers[i]);
            }
        }
Esempio n. 4
0
        public override async Task Execute()
        {
            music = SoundMusic.CreateInstance();

            if (!IsLiveReloading)
            {
                // start ambient music
                music.IsLooping = true;
                music.Play();
            }

            while (Game.IsRunning)
            {
                // wait for next frame
                await Script.NextFrame();
            }
        }
        /// <summary>
        /// This type of DynamicSoundSource is streamed from Disk and reads compressed Celt encoded data, used internally.
        /// </summary>
        /// <param name="instance">The associated SoundInstance</param>
        /// <param name="soundStreamUrl">The compressed stream internal URL</param>
        /// <param name="numberOfPackets"></param>
        /// <param name="sampleRate">The sample rate of the compressed data</param>
        /// <param name="channels">The number of channels of the compressed data</param>
        /// <param name="maxCompressedSize">The maximum size of a compressed packet</param>
        public CompressedSoundSource(SoundInstance instance, string soundStreamUrl, int numberOfPackets, int sampleRate, int channels, int maxCompressedSize) : base(instance, NumberOfBuffers, SamplesPerBuffer * MaxChannels * sizeof(short))
        {
            looped = instance.IsLooping;
            this.channels = channels;
            this.maxCompressedSize = maxCompressedSize;
            this.soundStreamUrl = soundStreamUrl;
            this.sampleRate = sampleRate;
            this.numberOfPackets = numberOfPackets;
            playRange = new PlayRange(TimeSpan.Zero, TimeSpan.Zero);

            if (readFromDiskWorker == null)
            {
                readFromDiskWorker = Task.Factory.StartNew(Worker, TaskCreationOptions.LongRunning);
            }

            NewSources.Add(this);
        }
Esempio n. 6
0
 /// <summary>
 /// Register a new instance to the soundEffect.
 /// </summary>
 /// <param name="instance">new instance to register.</param>
 private void RegisterInstance(SoundInstance instance)
 {
     Instances.Add(instance);
 }
Esempio n. 7
0
        /// <summary>
        /// Pauses this instance.
        /// </summary>
        public override void Pause()
        {
            if (sound == null)
            {
                return;
            }

            if (soundInstance == null)
            {
                soundInstance = sound.CreateInstance();
            }

            soundInstance.Pause();
        }
 internal void DestroySoundInstance(SoundInstance instance)
 {
     instance.Dispose();
     InstanceToListener.Remove(instance);
 }
 internal void DestroySoundInstance(SoundInstance instance)
 {
     instance.Dispose();
     InstanceToListener.Remove(instance);
 }
Esempio n. 10
0
        public override void Draw(RenderContext context)
        {
            base.Draw(context);

            foreach (var associatedData in ComponentDatas.Values)
            {
                var emitter     = associatedData.AudioEmitter;
                var worldMatrix = associatedData.TransformComponent.WorldMatrix;
                var pos         = worldMatrix.TranslationVector;

                if (!associatedData.AudioEmitterComponent.ShouldBeProcessed)
                {
                    // to be sure to have a valid velocity at any time we are forced to affect position even if Component need not to be processed.
                    emitter.Position = pos;
                    continue;
                }

                // First update the emitter data if required.
                emitter.Velocity = pos - emitter.Position;
                emitter.Position = pos;
                emitter.Forward  = Vector3.Normalize((Vector3)worldMatrix.Row3);
                emitter.Up       = Vector3.Normalize((Vector3)worldMatrix.Row2);

                // Then apply 3D localization
                var performedAtLeastOneApply = false;
                foreach (var controller in associatedData.AudioEmitterComponent.SoundToController.Values)
                {
                    foreach (var listenerComponent in audioSystem.Listeners.Keys)
                    {
                        //todo this will be improved when we make Sound behave more like Animations
                        SoundInstance instance = null;
                        foreach (var v in controller.InstanceToListener)
                        {
                            if (v.Value != listenerComponent)
                            {
                                continue;
                            }
                            instance = v.Key;
                            break;
                        }

                        if (instance == null)
                        {
                            continue;
                        }

                        if (!listenerComponent.Enabled)
                        {
                            instance.Stop();
                            continue;
                        }

                        // Apply3D localization
                        if (instance.PlayState == SoundPlayState.Playing || controller.ShouldBePlayed)
                        {
                            instance.Apply3D(emitter);
                            performedAtLeastOneApply = true;
                        }

                        //Apply parameters
                        if (instance.Volume != controller.Volume)
                        {
                            instance.Volume = controller.Volume;                                       // ensure that instance volume is valid
                        }
                        if (instance.IsLooped != controller.IsLooped)
                        {
                            instance.IsLooped = controller.IsLooped;
                        }

                        //Play if stopped
                        if (instance.PlayState != SoundPlayState.Playing && controller.ShouldBePlayed)
                        {
                            instance.Play(false);
                        }
                    }
                }

                associatedData.AudioEmitterComponent.ShouldBeProcessed = performedAtLeastOneApply;
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Create a new sound effect instance of the sound effect. 
        /// The audio data are shared between the instances so that useless memory copies is avoided. 
        /// Each instance that can be played and localized independently from others.
        /// </summary>
        /// <returns>A new sound instance</returns>
        /// <exception cref="ObjectDisposedException">The sound has already been disposed</exception>
        public SoundInstance CreateInstance(AudioListener listener = null, bool forceLoadInMemory = false)
        {
            if (listener == null)
            {
                listener = AudioEngine.DefaultListener;
            }

            CheckNotDisposed();

            var newInstance = new SoundInstance(this, listener, forceLoadInMemory) { Name = Name + " - Instance " + intancesCreationCount };

            RegisterInstance(newInstance);

            ++intancesCreationCount;

            return newInstance;
        }
Esempio n. 12
0
 /// <summary>
 /// Register a new instance to the soundEffect.
 /// </summary>
 /// <param name="instance">new instance to register.</param>
 private void RegisterInstance(SoundInstance instance)
 {
     Instances.Add(instance);
 }
Esempio n. 13
0
 /// <summary>
 /// Unregister a disposed Instance.
 /// </summary>
 /// <param name="instance"></param>
 internal void UnregisterInstance(SoundInstance instance)
 {
     if (!Instances.Remove(instance))
         throw new AudioSystemInternalException("Tried to unregister soundEffectInstance while not contained in the instance list.");
 }
Esempio n. 14
0
 /// <summary>
 /// Stop all registered instances different from the provided main instance
 /// </summary>
 /// <param name="mainInstance">The main instance of the sound effect</param>
 internal void StopConcurrentInstances(SoundInstance mainInstance)
 {
     foreach (var instance in Instances)
     {
         if (instance != mainInstance)
             instance.Stop();
     }
 }