Пример #1
0
 internal void Update()
 {
     if (Listener.Ptr == IntPtr.Zero)
     {
         return;
     }
     AudioLayer.ListenerPush3D(Listener, ref Position, ref forward, ref up, ref Velocity, ref WorldTransform);
 }
Пример #2
0
 /// <summary>
 /// Platform specifc implementation of <see cref="Destroy"/>.
 /// </summary>
 internal void DestroyAudioEngine()
 {
     if (AudioDevice.Ptr != IntPtr.Zero)
     {
         AudioLayer.ListenerDestroy(DefaultListener.Listener);
         AudioLayer.Destroy(AudioDevice);
     }
 }
Пример #3
0
        /// <summary>
        /// Initialize audio engine
        /// </summary>
        internal virtual void InitializeAudioEngine(AudioLayer.DeviceFlags flags)
        {
            AudioDevice = AudioLayer.Create(audioDevice.Name == "default" ? null : audioDevice.Name, flags);
            if (AudioDevice.Ptr == IntPtr.Zero)
            {
                State = AudioEngineState.Invalidated;
            }

            DefaultListener = new AudioListener(this);
        }
Пример #4
0
        public AudioListener(AudioEngine engine)
        {
            if (engine.State == AudioEngineState.Invalidated)
            {
                return;
            }

            Listener = AudioLayer.ListenerCreate(engine.AudioDevice);
            AudioLayer.ListenerEnable(Listener);
        }
Пример #5
0
        /// <summary>
        /// Disposes the Listener
        /// </summary>
        public void Dispose()
        {
            if (Listener.Ptr == IntPtr.Zero)
            {
                return;
            }

            AudioLayer.ListenerDisable(Listener);
            AudioLayer.ListenerDestroy(Listener);
        }
Пример #6
0
        /// <summary>
        /// Should be called from working thread only (or add proper locks)
        /// </summary>
        protected override void SeekInternal()
        {
            storageBuffer.CountDataBytes = 0;

            //To set the begin flag to true
            PrepareInternal();
            MediaCurrentTime = mediaCurrentTimeMax = TimeSpan.Zero;

            //Seek
            AudioLayer.SourceFlushBuffers(soundInstance.Source);
            SeekInternalImpl(commandSeekTime);
        }
Пример #7
0
        /// <summary>
        /// Disposes the Listener
        /// </summary>
        public void Dispose()
        {
            if (Listener.Ptr == IntPtr.Zero)
            {
                return;
            }

#if !STRIDE_PLATFORM_IOS
            AudioLayer.ListenerDisable(Listener);
            AudioLayer.ListenerDestroy(Listener);
#endif
        }
Пример #8
0
        private void ActivateAudioSession()
        {
            const double preferedAudioLatency = 0.005;

            // start the AudioSession
            var audioSession = AVAudioSession.SharedInstance();

            AVAudioSession.Notifications.ObserveInterruption((sender, args) =>
            {
                if (args.InterruptionType == AVAudioSessionInterruptionType.Began)
                {
                    AudioLayer.ListenerDisable(DefaultListener.Listener);
                }
                else
                {
                    AudioLayer.ListenerEnable(DefaultListener.Listener);
                }
            });

            // set the audio category so that: playback/recording is possible, playback is mixed with background music, music is stopped when screen is locked
            var error = audioSession.SetCategory(AVAudioSessionCategory.SoloAmbient);

            if (error != null)
            {
                Logger.Warning($"Failed to set the audio category to 'Ambient'. [Error info: {error.UserInfo}]");
                State = AudioEngineState.Invalidated;
                return;
            }

            // Reduce the buffer size for better latency response..
            audioSession.SetPreferredIOBufferDuration(preferedAudioLatency, out error);
            if (error != null)
            {
                Logger.Warning($"Failed to set the audio IO buffer duration to '{preferedAudioLatency}'. [Error info: {error.UserInfo}]");
            }

            // set the preferred sampling rate of the application
            if (AudioSampleRate != 0)
            {
                audioSession.SetPreferredSampleRate(AudioSampleRate, out error);
                Logger.Warning($"Failed to set the audio session preferred sampling rate to '{AudioSampleRate}'. [Error info: {error.UserInfo}]");
            }

            // activate the sound for the application
            error = audioSession.SetActive(true);
            if (error != null)
            {
                Logger.Warning($"Failed to activate the audio session. [Error info: {error.UserInfo}]");
                State = AudioEngineState.Invalidated;
            }
        }
Пример #9
0
        protected override void Destroy()
        {
            base.Destroy();

            if (AudioEngine == null || AudioEngine.State == AudioEngineState.Invalidated)
            {
                return;
            }

            if (!StreamFromDisk)
            {
                AudioLayer.BufferDestroy(PreloadedBuffer);
            }
        }
Пример #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicSoundSource"/> class.
        /// 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)
        {
            nativeBufferSizeBytes = maxBufferSizeBytes;
            prebufferedTarget     = (int)Math.Ceiling(numberOfBuffers / 3.0);

            this.soundInstance = soundInstance;
            for (var i = 0; i < numberOfBuffers; i++)
            {
                var buffer = AudioLayer.BufferCreate(nativeBufferSizeBytes);
                deviceBuffers.Add(buffer);
                freeBuffers.Enqueue(deviceBuffers[i]);
            }

            if (readFromDiskWorker == null)
            {
                readFromDiskWorker = Task.Factory.StartNew(Worker, TaskCreationOptions.LongRunning);
            }
        }
Пример #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SoundInstance"/> class using a dynamic sound source.
        /// </summary>
        /// <param name="engine">The audio engine that will be used to play this instance</param>
        /// <param name="listener">The listener of this instance</param>
        /// <param name="dynamicSoundSource">The source from where the PCM data will be fetched</param>
        /// <param name="sampleRate">The sample rate of this audio stream</param>
        /// <param name="mono">Set to true if the souce is mono, false if stereo</param>
        /// <param name="spatialized">If the SoundInstance will be used for spatialized audio set to true, if not false, if true mono must also be true</param>
        /// <param name="useHrtf">If the engine should use Hrtf for spatialization</param>
        /// <param name="directionalFactor"></param>
        /// <param name="environment"></param>
        public SoundInstance(AudioEngine engine, AudioListener listener, DynamicSoundSource dynamicSoundSource, int sampleRate, bool mono, bool spatialized = false, bool useHrtf = false, float directionalFactor = 0.0f, HrtfEnvironment environment = HrtfEnvironment.Small)
        {
            Listener         = listener;
            this.engine      = engine;
            this.spatialized = spatialized;
            soundSource      = dynamicSoundSource;

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

            Source = AudioLayer.SourceCreate(listener.Listener, sampleRate, dynamicSoundSource.MaxNumberOfBuffers, mono, spatialized, true, useHrtf, directionalFactor, environment);
            if (Source.Ptr == IntPtr.Zero)
            {
                throw new Exception("Failed to create an AudioLayer Source");
            }

            ResetStateToDefault();
        }
Пример #12
0
        public AudioListener(AudioEngine engine)
        {
            if (engine.State == AudioEngineState.Invalidated)
            {
                return;
            }

#if STRIDE_PLATFORM_IOS
            if (engine.DefaultListener == null)
            {
                Listener = AudioLayer.ListenerCreate(engine.AudioDevice);
                AudioLayer.ListenerEnable(Listener);
            }
            else
            {
                Listener = engine.DefaultListener.Listener;
            }
#else
            Listener = AudioLayer.ListenerCreate(engine.AudioDevice);
            AudioLayer.ListenerEnable(Listener);
#endif
        }
Пример #13
0
 internal void Apply3D(AudioLayer.Source source)
 {
     AudioLayer.SourcePush3D(source, ref Position, ref forward, ref up, ref Velocity, ref WorldTransform);
 }