예제 #1
0
        protected internal override void RenderUpdate()
        {
            var entitySpan = World.GetEntitySet(e => e.Has <AudioListener>()).ReadEntities();

            if (entitySpan.Length == 0)
            {
                return;
            }

            if (entitySpan.Length > 1)
            {
                throw new InvalidOperationException($"Only a single {nameof(AudioListener)} is allowed to exist in a world.");
            }

            var entity = entitySpan[0];

            Vector3 position;

            if (entity.Has <Transform>())
            {
                var transform = entity.Get <Transform>();
                var up        = transform.Up;
                var lookAt    = -transform.Forward;

                position = transform.Position;

                OrientationArray[0] = lookAt.x;
                OrientationArray[1] = lookAt.y;
                OrientationArray[2] = lookAt.z;
                OrientationArray[3] = up.x;
                OrientationArray[4] = up.y;
                OrientationArray[5] = up.z;
            }
            else
            {
                position = Vector3.Zero;

                for (int i = 0; i < OrientationArray.Length; i++)
                {
                    OrientationArray[i] = 0f;
                }
            }

            AL.Listener3(ListenerFloat3.Position, position.x, position.y, position.z);
            AL.Listener(ListenerFloatArray.Orientation, OrientationArray);

            AudioEngine.CheckALErrors();
        }
예제 #2
0
        public unsafe void SetData <T>(T[] data, int channelsNum, int bytesPerSample, int sampleRate) where T : unmanaged
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            this.channelsNum    = channelsNum;
            this.bytesPerSample = bytesPerSample;
            this.sampleRate     = sampleRate;

            LengthInSeconds = data.Length / (float)sampleRate / channelsNum;

            var format = GetSoundFormat(channelsNum, bytesPerSample);

            fixed(T *ptr = data)
            {
                AL.BufferData(bufferId, format, (IntPtr)ptr, data.Length * Marshal.SizeOf <T>(), sampleRate);
            }

            AudioEngine.CheckALErrors();
        }