Beispiel #1
0
        internal Sound(AudioDevice audioDevice, string file, SoundFlags flags) : base(audioDevice)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentNullException("file");
            }
            if (!System.IO.File.Exists(file))
            {
                throw new System.IO.FileNotFoundException("file", file);
            }

            this.File       = file;
            this.Flags      = flags;
            this.Supports3D = flags.HasFlag(SoundFlags.Support3D);
            this.IsStreamed = flags.HasFlag(SoundFlags.Streamed);
            this.AllowRead  = flags.HasFlag(SoundFlags.AllowRead);

            instances = new List <WeakReference <SoundInstance> >();

            if (!IsStreamed)
            {
                using (var source = SampleSourceFactory.FromFile(file))
                {
                    var bufferCount  = Supports3D ? source.Channels : 1;
                    var channelCount = Supports3D ? 1 : source.Channels;
                    var sampleRate   = source.SampleRate;
                    var samples      = source.ReadAll();

                    buffers = new List <AudioBuffer <short> >();
                    short[] samplesChannel = new short[samples.Length / bufferCount];
                    for (int i = 0; i < bufferCount; i++)
                    {
                        SampleConverter.To16Bit(samples, samplesChannel, i, bufferCount);
                        var buffer = new AudioBuffer <short>(audioDevice, !AllowRead);
                        buffer.SetData(AudioFormat.Short16, channelCount, samplesChannel, sampleRate);
                        buffers.Add(buffer);
                    }
                }
            }
        }
Beispiel #2
0
        internal SoundInstance(AudioDevice audioDevice, Sound sound)  : base(audioDevice)
        {
            if (sound == null)
            {
                throw new ArgumentNullException("sound");
            }
            if (sound.IsDisposed)
            {
                throw new ArgumentException("Sound is disposed.", "sound");
            }

            this.Sound = sound;

            IDs = new List <int>();
            if (sound.IsStreamed)
            {
                source = SampleSourceFactory.FromFile(sound.File);
            }
            int bufferCount;

            if (sound.IsStreamed)
            {
                bufferCount = sound.Supports3D ? source.Channels : 1;
            }
            else
            {
                bufferCount = sound.Buffers.Count;
            }

            IDs.AddRange(AL.GenSources(bufferCount));
            DotGame.OpenAL.AudioDevice.CheckALError();

            if (sound.IsStreamed)
            {
                ringbuffers = new AudioBuffer <short> [bufferCount, RINGBUFFER_COUNT];
                for (int i = 0; i < bufferCount; i++)
                {
                    for (int j = 0; j < RINGBUFFER_COUNT; j++)
                    {
                        ringbuffers[i, j] = new AudioBuffer <short>(AudioDeviceInternal, false);
                    }
                }
                ringBufferIndex = 0;
            }
            else
            {
                var buffers = sound.Buffers;
                for (int i = 0; i < buffers.Count; i++)
                {
                    AL.Source(IDs[i], ALSourcei.Buffer, buffers[i].ID);
                    DotGame.OpenAL.AudioDevice.CheckALError();
                }
            }

            if (AudioDevice.Capabilities.SupportsEfx)
            {
                directFilter = AudioDeviceInternal.Efx.GenFilter();
                AudioDeviceInternal.Efx.Filter(directFilter, EfxFilteri.FilterType, (int)EfxFilterType.Lowpass);
                Set(ALSourcei.EfxDirectFilter, directFilter);
            }

            sound.Register(this);
        }