示例#1
0
文件: Sound.cs 项目: KHCmaster/PPD
        private void AddSound(Stream stream, string dictName)
        {
            if (sounds.ContainsKey(dictName))
            {
                return;
            }

            using (SoundStream wavFile = new SoundStream(stream))
            {
                byte[] data = new byte[wavFile.Length];
                if (wavFile.Read(data, 0, (int)wavFile.Length) != wavFile.Length)
                {
                    MessageBox.Show(PPDExceptionContentProvider.Provider.GetContent(PPDExceptionType.SoundReadError));
                    return;
                }
                var buffer = CreateSecondarySoundBuffer(wavFile.Format, (int)wavFile.Length);
                buffer.Write(data, 0, SharpDX.DirectSound.LockFlags.EntireBuffer);
                try
                {
                    this.sounds.Add(dictName, new BufferInfo(buffer));
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return;
                }
            }
        }
示例#2
0
        /// <summary>
        ///   Reads a maximum of count samples from the current stream, and writes the data to buffer, beginning at index.
        /// </summary>
        /// <param name="buffer">
        ///    When this method returns, this parameter contains the specified byte array with the values between index and (index + count -1) replaced by the 8 bit frames read from the current source.
        /// </param>
        /// <param name="count">The amount of frames to read.</param>
        /// <returns>The number of reads performed on the stream.</returns>
        private int read(float[] buffer, int count)
        {
            int reads;

            int blockSize = sizeof(float) * count * channels;

            byte[] block = new byte[blockSize];
            reads = waveStream.Read(block, 0, blockSize);

            // Convert from byte to float
            for (int j = 0; j < bufferSize; j++)
            {
                buffer[j] = BitConverter.ToSingle(block, j * sizeof(float));
            }

            return(reads);
        }
示例#3
0
        public WavLibrary(int index, string fileName, bool loop)
        {
            Index = index;

            _stream = new SoundStream(File.OpenRead(fileName));

            _desc = new SoundBufferDescription
            {
                BufferBytes = (int)_stream.Length,
                Flags       = BufferFlags.ControlVolume | BufferFlags.ControlPan | BufferFlags.GlobalFocus,
                Format      = _stream.Format
            };

            _data = new byte[_desc.BufferBytes];
            _stream.Read(_data, 0, (int)_stream.Length);

            _loop = loop;

            _bufferList = new List <SecondarySoundBuffer>();

            Play();
        }
示例#4
0
        private void LoadWaveFileIntoBuffers(out GCHandle handle, out DynSoundData sound, string filename)
        {
            sound = new DynSoundData();

            WaveFormat dataFormat;

            using (var stream = AssetManager.FileProvider.OpenStream(filename, VirtualFileMode.Open, VirtualFileAccess.Read))
                using (var memoryStream = new MemoryStream(new byte[stream.Length]))
                {
                    stream.CopyTo(memoryStream);
                    memoryStream.Position = 0;
                    var waveStreamReader = new SoundStream(memoryStream);
                    dataFormat = waveStreamReader.Format;
                    sound.Data = new byte[waveStreamReader.Length];
                    handle     = GCHandle.Alloc(sound.Data, GCHandleType.Pinned);

                    if (waveStreamReader.Read(sound.Data, 0, (int)waveStreamReader.Length) != waveStreamReader.Length)
                    {
                        throw new AudioSystemInternalException("The data length read in wave soundStream does not correspond to the stream's length.");
                    }
                }
            sound.Instance = new DynamicSoundEffectInstance(defaultEngine, dataFormat.SampleRate, (AudioChannels)dataFormat.Channels, (AudioDataEncoding)dataFormat.BitsPerSample);
        }
示例#5
0
        public void TestPlayableInterface()
        {
            WaveFormat dataFormat;

            using (var stream = AssetManager.FileProvider.OpenStream("EffectFishLamp", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                var memoryStream = new MemoryStream((int)stream.Length);
                stream.CopyTo(memoryStream);
                memoryStream.Position = 0;

                var waveStreamReader = new SoundStream(memoryStream);
                dataFormat = waveStreamReader.Format;
                bufferData = new byte[waveStreamReader.Length];

                if (waveStreamReader.Read(bufferData, 0, (int)waveStreamReader.Length) != waveStreamReader.Length)
                {
                    throw new AudioSystemInternalException("The data length read in wave soundStream does not correspond to the stream's length.");
                }
            }
            dynSEInstance = new DynamicSoundEffectInstance(defaultEngine, dataFormat.SampleRate, (AudioChannels)dataFormat.Channels, (AudioDataEncoding)dataFormat.BitsPerSample);
            dynSEInstance.BufferNeeded += SubmitBuffer;

            //////////////////
            // 1. Test play
            dynSEInstance.Play();
            Utilities.Sleep(2000);
            Assert.AreEqual(SoundPlayState.Playing, dynSEInstance.PlayState, "Music is not playing");

            //////////////////
            // 2. Test Pause
            dynSEInstance.Pause();
            Utilities.Sleep(600);
            Assert.AreEqual(SoundPlayState.Paused, dynSEInstance.PlayState, "Music is not Paused");
            dynSEInstance.Play();
            Utilities.Sleep(1000);

            //////////////////
            // 2. Test Stop
            dynSEInstance.Stop();
            bufferCount = 0;
            Utilities.Sleep(600);
            Assert.AreEqual(SoundPlayState.Stopped, dynSEInstance.PlayState, "Music is not Stopped");
            dynSEInstance.Play();
            Utilities.Sleep(9000);

            ///////////////////
            // 3. Test ExitLoop
            Assert.DoesNotThrow(dynSEInstance.ExitLoop, "ExitLoop crached");

            ///////////////
            // 4. Volume
            var value = 1f;
            var sign  = -1f;

            while (value <= 1f)
            {
                dynSEInstance.Volume = value;

                value += sign * 0.01f;
                Utilities.Sleep(30);

                if (value < -0.2)
                {
                    sign = 1f;
                }
            }
            Utilities.Sleep(2000);

            //////////////////
            // 5.Pan
            value = 0;
            sign  = -1f;
            while (value <= 1f)
            {
                dynSEInstance.Pan = value;

                value += sign * 0.01f;
                Utilities.Sleep(30);

                if (value < -1.2)
                {
                    sign = 1f;
                }
            }
            dynSEInstance.Pan = 0;
            Utilities.Sleep(2000);

            ////////////////////////////////////////////////////////////////////////////
            // 7. Wait until the end of the stream to check that there are not crashes
            Utilities.Sleep(50000);

            dynSEInstance.Dispose();
        }