示例#1
0
        public SoundEffect LoadSoundEffect(string filename)
        {
            WaveFile wave = WaveFile.LoadFromFile(filename);

            var bufferDescription = new SoundBufferDescription
            {
                BufferBytes    = wave.SampleData.Length,
                Format         = new WaveFormat((int)wave.Format.SamplesPerSec, wave.Format.BitsPerSample, wave.Format.Channels),
                AlgorithmFor3D = Guid.Empty
            };

            var secondaryBuffer = new SecondarySoundBuffer(_directSound, bufferDescription);
            var capabilities    = secondaryBuffer.Capabilities;

            var dataStream1 = secondaryBuffer.Lock(0, capabilities.BufferBytes, LockFlags.EntireBuffer, out var dataStream2);

            dataStream1.Write(wave.SampleData, 0, wave.SampleData.Length);

            secondaryBuffer.Unlock(dataStream1, dataStream2);

            return(new SoundEffect(secondaryBuffer));
        }