示例#1
0
文件: Source.cs 项目: ermau/Gablarski
        public void Pause()
        {
            OpenAL.DebugFormat("Pausing source {0}", this.sourceID);

            alSourcePause(this.sourceID);
            OpenAL.ErrorCheck();
        }
示例#2
0
        public static SourceBuffer[] Generate(int count)
        {
            OpenAL.DebugFormat("Generating {0} source buffers", count);

            lock (lck)
            {
                if (Buffers == null)
                {
                    Buffers = new Dictionary <uint, SourceBuffer> (count);
                }

                SourceBuffer[] buffers = new SourceBuffer[count];

                uint[] bufferIDs = new uint[count];
                alGenBuffers(count, bufferIDs);
                OpenAL.ErrorCheck();

                for (int i = 0; i < count; ++i)
                {
                    OpenAL.DebugFormat("Generated source buffer {0}", bufferIDs[i]);

                    buffers[i] = new SourceBuffer(bufferIDs[i]);
                    Buffers.Add(buffers[i].bufferID, buffers[i]);
                }

                return(buffers);
            }
        }
示例#3
0
        private unsafe byte[] GetSamples(int numSamples, bool block)
        {
            ThrowIfDisposed();

            int bytesPerSample = (int)Format.GetBytesPerSample();

            byte[] samples = new byte[numSamples * bytesPerSample];

            while (this.capturing && block && this.AvailableSamples < numSamples)
            {
                Thread.Sleep(0);
            }

            int diff = numSamples - this.AvailableSamples;

            if (diff > 0)
            {
                numSamples -= diff;
                for (int i = diff * bytesPerSample; i < samples.Length; i++)
                {
                    samples[i]   = 0;
                    samples[++i] = 0;
                }
            }

            fixed(byte *pcmPtr = pcm)
            alcCaptureSamples(this.Handle, new IntPtr(pcmPtr), numSamples);

            OpenAL.ErrorCheck(this);
            Buffer.BlockCopy(pcm, 0, samples, 0, samples.Length);

            return(samples);
        }
示例#4
0
文件: Source.cs 项目: ermau/Gablarski
        public static Source[] Generate(int count)
        {
            OpenAL.DebugFormat("Generating {0} sources", count);

            if (count > MaxSources)
            {
                OpenAL.Log.ErrorFormat("Requested {0} sources which is more than maximum {1}", count, MaxSources);
                throw new InvalidOperationException();
            }

            Source[] sources = new Source[count];

            uint[] sourceIDs = new uint[count];
            alGenSources(count, sourceIDs);
            OpenAL.ErrorCheck();

            for (int i = 0; i < count; ++i)
            {
                OpenAL.DebugFormat("Generated source {0}", sourceIDs[i]);

                sources[i] = new Source(sourceIDs[i]);
            }

            return(sources);
        }
示例#5
0
        internal Context(IntPtr handle, PlaybackDevice device)
        {
            this.Handle = handle;
            this.Device = device;

            OpenAL.DebugFormat("Context {0} created for {1}", handle, device.Name);
        }
示例#6
0
文件: Source.cs 项目: ermau/Gablarski
        internal static void SetPropertyF(uint sourceID, FloatSourceProperty property, float value)
        {
            OpenAL.DebugFormat("Setting source {0} property {1} to {2}", sourceID, property, value);

            alSourcef(sourceID, property, value);
            OpenAL.ErrorCheck();
        }
示例#7
0
文件: OpenAL.cs 项目: ermau/Gablarski
        public static IEnumerable <CaptureDevice> GetCaptureDevices(out CaptureDevice defaultDevice)
        {
            //if (!IsCaptureSupported)
            //	throw new NotSupportedException();

            OpenAL.Debug("Getting capture devices");

            defaultDevice = null;

            string defaultName = Marshal.PtrToStringAnsi(alcGetString(IntPtr.Zero, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));

            var strings = ReadStringsFromMemory(alcGetString(IntPtr.Zero, ALC_CAPTURE_DEVICE_SPECIFIER));

            CaptureDevice[] devices = new CaptureDevice[strings.Length];
            for (int i = 0; i < strings.Length; ++i)
            {
                string s = strings[i];
                devices[i] = new CaptureDevice(s);

                if (s == defaultName)
                {
                    defaultDevice = devices[i];
                }

                OpenAL.DebugFormat("Found capture device {0}{1}", s, (s == defaultName) ? " (Default)" : String.Empty);
            }

            return(devices);
        }
示例#8
0
文件: Source.cs 项目: ermau/Gablarski
        public void Stop()
        {
            OpenAL.DebugFormat("Stopping source {0}", this.sourceID);

            alSourceStop(this.sourceID);
            OpenAL.ErrorCheck();
        }
示例#9
0
文件: Source.cs 项目: ermau/Gablarski
        protected void Queue(uint[] bufferIDs)
        {
            OpenAL.DebugFormat("Enqueuing buffers {0} to source {1}", bufferIDs.Implode(", "), this.sourceID);

            alSourceQueueBuffers(this.sourceID, bufferIDs.Length, bufferIDs);
            OpenAL.ErrorCheck();
        }
示例#10
0
        public void Activate()
        {
            OpenAL.DebugFormat("Activating context {1} for device {0}", this.Device.Name, Handle);

            alcMakeContextCurrent(this.Handle);
            CurrentContext = this;
            OpenAL.ErrorCheck(this.Device);
        }
示例#11
0
文件: Source.cs 项目: ermau/Gablarski
        // ReSharper restore InconsistentNaming

        internal static float GetPropertyF(uint sourceID, FloatSourceProperty property)
        {
            float value;

            alGetSourcef(sourceID, property, out value);
            OpenAL.ErrorCheck();

            return(value);
        }
示例#12
0
        private int GetSamplesAvailable()
        {
            ThrowIfDisposed();

            int samples;

            OpenAL.alcGetIntegerv(this.Handle, ALCEnum.ALC_CAPTURE_SAMPLES, 4, out samples);
            OpenAL.ErrorCheck(this);
            return(samples);
        }
示例#13
0
文件: Source.cs 项目: ermau/Gablarski
        protected void PlayCore(bool check)
        {
            if (check && this.IsPlaying)
            {
                return;
            }

            OpenAL.DebugFormat("Playing source {0}", this.sourceID);

            alSourcePlay(this.sourceID);
            OpenAL.ErrorCheck();
        }
示例#14
0
        // ReSharper restore InconsistentNaming

        public static Context Create(PlaybackDevice device)
        {
            OpenAL.DebugFormat("Creating context for {0}", device.Name);

            Context c = new Context(alcCreateContext(device.Handle, IntPtr.Zero), device);

            OpenAL.ErrorCheck(device);

            device.Context = c;

            return(c);
        }
示例#15
0
        public void FreeSource(Source source)
        {
            OpenAL.DebugFormat("SourcePool: Freeing source {0}", source);

            lock (owners)
            {
                owners[source] = default(T);

                if (source == lastSource)
                {
                    lastSource = null;
                }
            }
        }
示例#16
0
        /// <summary>
        /// Stops capturing.
        /// </summary>
        public void StopCapture()
        {
            if (!IsOpen)
            {
                throw new InvalidOperationException("Device not open");
            }

            ThrowIfDisposed();

            OpenAL.DebugFormat("Stopping capture for {0}", Name);

            this.capturing = false;
            alcCaptureStop(this.Handle);
            OpenAL.ErrorCheck(this);
        }
示例#17
0
文件: OpenAL.cs 项目: ermau/Gablarski
        internal static bool GetIsExtensionPresent(string extension)
        {
            sbyte result;

            if (extension.StartsWith("ALC"))
            {
                result = alcIsExtensionPresent(IntPtr.Zero, extension);
            }
            else
            {
                result = alIsExtensionPresent(extension);
                OpenAL.ErrorCheck();
            }

            return(result == 1);
        }
示例#18
0
文件: Source.cs 项目: ermau/Gablarski
        protected void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            OpenAL.DebugFormat("Destroying source {0}", this.sourceID);

            uint[] id = new[] { this.sourceID };
            alDeleteSources(1, id);

            OpenAL.DebugFormat("Destroyed source {0}", this.sourceID);

            this.disposed = true;
        }
示例#19
0
        public void FreeSources(IEnumerable <Source> sources)
        {
            lock (owners)
            {
                foreach (Source csource in sources)
                {
                    OpenAL.DebugFormat("SourcePool: Freeing source {0}", csource);
                    owners[csource] = default(T);

                    if (csource == lastSource)
                    {
                        lastSource = null;
                    }
                }
            }
        }
示例#20
0
文件: OpenAL.cs 项目: ermau/Gablarski
        internal static bool GetIsExtensionPresent(Device device, string extension)
        {
            sbyte result;

            if (extension.StartsWith("ALC"))
            {
                result = alcIsExtensionPresent(device.Handle, extension);
                OpenAL.ErrorCheck(device);
            }
            else
            {
                result = alIsExtensionPresent(extension);
                OpenAL.ErrorCheck();
            }

            return(result == 1);
        }
示例#21
0
        public void Tick()
        {
            List <KeyValuePair <Source, T> > finished;

            lock (owners)
                finished = owners.Where(kvp => kvp.Key.IsStopped && kvp.Value != null).ToList();

            for (int i = 0; i < finished.Count; ++i)
            {
                var s = finished[i].Key;
                var o = finished[i].Value;

                OpenAL.DebugFormat("SourcePool: {0} is stopped, freeing", s);
                FreeSource(s);
                OnSourceFinished(new SourceFinishedEventArgs <T> (o, s));
            }
        }
示例#22
0
        protected override void Dispose(bool disposing)
        {
            if (Handle == IntPtr.Zero)
            {
                return;
            }

            OpenAL.DebugFormat("Destroying capture device {0}", Name);

            Close();
            Handle   = IntPtr.Zero;
            this.pcm = null;

            this.disposed = true;

            OpenAL.DebugFormat("Destroyed capture device {0}", Name);
        }
示例#23
0
        protected virtual void Dispose(bool disposing)
        {
            lock (lck)
            {
                if (this.disposed)
                {
                    return;
                }

                OpenAL.DebugFormat("Destroying source buffer {0}", this.bufferID);

                alDeleteBuffers(1, new[] { this.bufferID });
                Buffers.Remove(this.bufferID);

                this.disposed = true;
            }
        }
示例#24
0
        /// <summary>
        /// Opens the capture device with the specified <paramref name="frequency"/> and <paramref name="format"/>.
        /// </summary>
        /// <param name="frequency">The frequency to open the capture device with.</param>
        /// <param name="format">The audio format to open the device with.</param>
        /// <returns>Returns <c>this</c>.</returns>
        public CaptureDevice Open(uint frequency, OpenALAudioFormat format)
        {
            ThrowIfDisposed();

            OpenAL.DebugFormat("Opening capture device {0} at {1} {2}", Name, frequency, format);

            this.Format    = format;
            this.Frequency = frequency;

            uint bufferSize = format.GetBytes(format.GetSamplesPerSecond(frequency)) * 2;

            this.Handle = alcCaptureOpenDevice(this.Name, frequency, format, (int)bufferSize);
            OpenAL.ErrorCheck(this);

            pcm = new byte[bufferSize];

            return(this);
        }
示例#25
0
文件: Source.cs 项目: ermau/Gablarski
        public SourceBuffer[] Dequeue(int buffers)
        {
            OpenAL.DebugFormat("Dequeing {0} buffers for source {1}", buffers, this.sourceID);

            uint[] bufferIDs = new uint[buffers];
            alSourceUnqueueBuffers(this.sourceID, buffers, bufferIDs);
            OpenAL.ErrorCheck();

            SourceBuffer[] dequeued = new SourceBuffer[bufferIDs.Length];
            for (int i = 0; i < bufferIDs.Length; ++i)
            {
                OpenAL.DebugFormat("Dequeued source buffer {0} for source {1}", bufferIDs[i], this.sourceID);

                dequeued[i] = SourceBuffer.GetBuffer(bufferIDs[i]);
            }

            return(dequeued);
        }
示例#26
0
        public Source RequestSource(T owner)
        {
            OpenAL.DebugFormat("SourcePool: Requesting source for {0}", owner);

            Source free;

            lock (owners)
            {
                if (owner == lastOwner && lastSource != null)
                {
                    OpenAL.DebugFormat("SourcePool: Returning last source for {0}", owner);

                    return(lastSource);
                }

                free = owners.Where(kvp => kvp.Value == owner).Select(kvp => kvp.Key).FirstOrDefault();

                if (free == null)
                {
                    free = owners.Where(kvp => kvp.Value == null).Select(kvp => kvp.Key).FirstOrDefault();

                    if (free == null)
                    {
                        free = Source.Generate();
                        OpenAL.DebugFormat("SourcePool: Couldn't find a free source for {0}, created {1}", owner, free);
                    }
                    else
                    {
                        OpenAL.DebugFormat("SourcePool: Found free source {0} for {1}", free, owner);
                    }

                    owners[free] = owner;
                }
                else
                {
                    OpenAL.DebugFormat("SourcePool: Found owned source {0} for {1}", free, owner);
                }

                lastOwner  = owner;
                lastSource = free;
            }

            return(free);
        }
示例#27
0
        /// <summary>
        /// Opens the device.
        /// </summary>
        /// <returns>Returns <c>this</c>.</returns>
        public PlaybackDevice Open()
        {
            ThrowIfDisposed();

            OpenAL.DebugFormat("Opening playback device {0}", Name);

            this.Handle = alcOpenDevice(this.Name);

            if (this.Handle == IntPtr.Zero)
            {
                throw new Exception("Device failed to open for an unknown reason.");
            }
            else
            {
                OpenAL.ErrorCheck(this);
            }

            return(this);
        }
示例#28
0
        public void FreeSource(T sourceOwner)
        {
            OpenAL.DebugFormat("SourcePool: Freeing source for owner {0}", sourceOwner);

            lock (owners)
            {
                var source = owners.FirstOrDefault(kvp => kvp.Value == sourceOwner).Key;
                if (source == null)
                {
                    return;
                }

                owners[source] = default(T);

                if (source == lastSource)
                {
                    lastSource = null;
                }
            }
        }
示例#29
0
        protected virtual void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            if (this.Handle != IntPtr.Zero)
            {
                if (CurrentContext == this)
                {
                    alcMakeContextCurrent(IntPtr.Zero);
                    CurrentContext = null;
                }

                alcDestroyContext(this.Handle);
            }

            OpenAL.DebugFormat("Destroying from {2} context {1} for {0}", Device.Name, Handle, (disposing) ? "Dispose()" : "finalizer");

            this.disposed = true;
        }
示例#30
0
        protected override void Dispose(bool disposing)
        {
            if (Handle == IntPtr.Zero)
            {
                return;
            }

            if (disposing)
            {
                if (Context != null)
                {
                    Context.Dispose();
                }
            }

            OpenAL.DebugFormat("Destroying playback device {0}", Name);

            Close();
            Handle        = IntPtr.Zero;
            this.disposed = true;

            OpenAL.DebugFormat("Destroyed playback device {0}", Name);
        }