Пример #1
0
        public SfmlSound(Config config)
        {
            try
            {
                Console.Write("Initialize sound: ");

                this.config = config;

                config.audio_soundvolume = Math.Clamp(config.audio_soundvolume, 0, this.MaxVolume);

                this.buffers    = new SoundBuffer[DoomInfo.SfxNames.Length];
                this.amplitudes = new float[DoomInfo.SfxNames.Length];

                for (var i = 0; i < DoomInfo.SfxNames.Length; i++)
                {
                    var name = "DS" + DoomInfo.SfxNames[i];

                    if (!DoomApplication.Instance.FileSystem.Exists(name))
                    {
                        continue;
                    }

                    int sampleRate;
                    int sampleCount;
                    var samples = SfmlSound.GetSamples(name, out sampleRate, out sampleCount);

                    if (samples != null)
                    {
                        this.buffers[i]    = new SoundBuffer(samples, 1, (uint)sampleRate);
                        this.amplitudes[i] = SfmlSound.GetAmplitude(samples, sampleRate, sampleCount);
                    }
                }

                this.channels = new Sound[SfmlSound.channelCount];
                this.infos    = new ChannelInfo[SfmlSound.channelCount];

                for (var i = 0; i < this.channels.Length; i++)
                {
                    this.channels[i] = new Sound();
                    this.infos[i]    = new ChannelInfo();
                }

                this.uiChannel  = new Sound();
                this.uiReserved = Sfx.NONE;

                this.masterVolumeDecay = (float)config.audio_soundvolume / this.MaxVolume;

                this.lastUpdate = DateTime.MinValue;

                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed");
                this.Dispose();
                ExceptionDispatchInfo.Throw(e);
            }
        }
Пример #2
0
        private static short[] GetSamples(string name, out int sampleRate, out int sampleCount)
        {
            var reader = new BinaryReader(DoomApplication.Instance.FileSystem.Read(name));

            if (reader.BaseStream.Length < 8)
            {
                sampleRate  = -1;
                sampleCount = -1;

                return(null);
            }

            reader.BaseStream.Position = 2;
            sampleRate  = reader.ReadUInt16();
            sampleCount = reader.ReadInt32();

            if (sampleCount >= 32 && SfmlSound.ContainsDmxPadding(reader, sampleCount))
            {
                reader.BaseStream.Position += 16;
                sampleCount -= 32;
            }

            if (sampleCount > 0)
            {
                var samples = new short[sampleCount];

                for (var t = 0; t < samples.Length; t++)
                {
                    samples[t] = (short)((reader.ReadByte() - 128) << 8);
                }

                return(samples);
            }
            else
            {
                return(null);
            }
        }