Esempio n. 1
0
        //Function for adding a new sound to the soundbuffer.
        private void SetBuffer(ref short[] tmpSound)
        {
            soundStack.Push((short[])sounds.Clone());
            //tmpSound = lowPass(ref tmpSound, 1000);
            int value;

            for (int j = 0; j < sounds.Length; j++)
            {
                value = ((int)sounds[j] + (int)tmpSound[j] - (int)(sounds[j] * tmpSound[j]) / short.MaxValue);
                //Test if the value is bigger then a short, if so set it to maximum value for a short
                if (value > short.MaxValue)
                {
                    value = short.MaxValue;
                }
                //Same thing for negative values.
                else if (value < short.MinValue)
                {
                    value = short.MinValue;
                }
                sounds[j] = (short)value;
            }
            // Lock the buffer
            DataStream dataPart2;
            var        dataPart1 = secondarySoundBuffer.Lock(0, capabilities.BufferBytes, LockFlags.EntireBuffer, out dataPart2);

            foreach (short val in sounds)
            {
                dataPart1.Write(val);
                dataPart1.Write(val);
            }
            // Unlock the buffer
            secondarySoundBuffer.Unlock(dataPart1, dataPart2);
        }
        public unsafe void QueueAudio()
        {
            // the first time through begin writing from the write cursor
            if (_soundBufferCursor == -1)
            {
                _soundBuffer.GetCurrentPosition(out _, out int writeCursor);
                _soundBufferCursor = writeCursor;
            }

            int bytesToWrite = _sampleCount * sizeof(AudioFrame <short>);

            DataStream ds1 = _soundBuffer.Lock(_soundBufferCursor, bytesToWrite, LockFlags.None, out DataStream ds2);

            fixed(AudioFrame <short> *p = _sampleBuffer)
            {
                byte *b = (byte *)p;

                ds1.WriteRange((IntPtr)b, ds1.Length);

                if (ds2 != null)
                {
                    b += ds1.Length;
                    ds2.WriteRange((IntPtr)b, ds2.Length);
                }
            }

            _soundBuffer.Unlock(ds1, ds2);

            _soundBufferCursor = (_soundBufferCursor + bytesToWrite) % _soundBufferLength;
            _sampleCount       = 0;
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            DirectSound directSound = new DirectSound();

            var form = new Form();
            form.Text = "SharpDX - DirectSound Demo";

            // Set Cooperative Level to PRIORITY (priority level can call the SetFormat and Compact methods)
            //
            directSound.SetCooperativeLevel(form.Handle, CooperativeLevel.Priority);

            // Create PrimarySoundBuffer
            var primaryBufferDesc = new SoundBufferDescription();
            primaryBufferDesc.Flags = BufferFlags.PrimaryBuffer;
            primaryBufferDesc.AlgorithmFor3D = Guid.Empty;

            var primarySoundBuffer = new PrimarySoundBuffer(directSound, primaryBufferDesc);

            // Play the PrimarySound Buffer
            primarySoundBuffer.Play(0, PlayFlags.Looping);

            // Default WaveFormat Stereo 44100 16 bit
            WaveFormat waveFormat = new WaveFormat();

            // Create SecondarySoundBuffer
            var secondaryBufferDesc = new SoundBufferDescription();
            secondaryBufferDesc.BufferBytes = waveFormat.ConvertLatencyToByteSize(60000);
            secondaryBufferDesc.Format = waveFormat;
            secondaryBufferDesc.Flags = BufferFlags.GetCurrentPosition2 | BufferFlags.ControlPositionNotify | BufferFlags.GlobalFocus |
                                        BufferFlags.ControlVolume | BufferFlags.StickyFocus;
            secondaryBufferDesc.AlgorithmFor3D = Guid.Empty;
            var secondarySoundBuffer = new SecondarySoundBuffer(directSound, secondaryBufferDesc);

            // Get Capabilties from secondary sound buffer
            var capabilities = secondarySoundBuffer.Capabilities;

            // Lock the buffer
            DataStream dataPart2;
            var dataPart1 =secondarySoundBuffer.Lock(0, capabilities.BufferBytes,  LockFlags.EntireBuffer, out dataPart2);

            // Fill the buffer with some sound
            int numberOfSamples = capabilities.BufferBytes/waveFormat.BlockAlign;
            for (int i = 0; i < numberOfSamples; i++)
            {
                double vibrato = Math.Cos(2 * Math.PI * 10.0 * i /waveFormat.SampleRate);
                short value = (short) (Math.Cos(2*Math.PI*(220.0 + 4.0 * vibrato)*i/waveFormat.SampleRate)*16384); // Not too loud
                dataPart1.Write(value);
                dataPart1.Write(value);
            }

            // Unlock the buffer
            secondarySoundBuffer.Unlock(dataPart1, dataPart2);

            // Play the song
            secondarySoundBuffer.Play(0, PlayFlags.Looping);
           
            Application.Run(form);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("1 - Direct sound");
            Console.WriteLine("2 - Windows media player");
            Console.WriteLine("Twój wybór:");
            string wartosc = Console.ReadLine();

            if (wartosc == "1")
            {
                Console.Write("Podaj nazwe pliku z pulpitu: ");
                string      nazwa_pliku = Console.ReadLine();
                var         path        = "C:\\Users\\lab\\Desktop\\" + nazwa_pliku;
                DirectSound directSound = new DirectSound();

                var primaryBufferDesc = new SoundBufferDescription();
                primaryBufferDesc.Flags = BufferFlags.PrimaryBuffer;

                var primarySoundBuffer = new PrimarySoundBuffer(directSound, primaryBufferDesc);

                primarySoundBuffer.Play(0, PlayFlags.Looping);

                WaveFormat waveFormat = new WaveFormat();

                var secondaryBufferDesc = new SoundBufferDescription();
                secondaryBufferDesc.BufferBytes = waveFormat.ConvertLatencyToByteSize(60000);
                secondaryBufferDesc.Format      = waveFormat;
                secondaryBufferDesc.Flags       = BufferFlags.GetCurrentPosition2 | BufferFlags.ControlPositionNotify | BufferFlags.GlobalFocus |
                                                  BufferFlags.ControlVolume | BufferFlags.StickyFocus;
                secondaryBufferDesc.AlgorithmFor3D = Guid.Empty;
                var secondarySoundBuffer = new SecondarySoundBuffer(directSound, secondaryBufferDesc);
                var capabilities         = secondarySoundBuffer.Capabilities;

                //Stream stream = File.Open(path, FileMode.Open);
                //byte[] arrayTest = ReadFully(stream);
                byte[]     array = File.ReadAllBytes(path);
                DataStream dataPart2;
                var        dataPart1 = secondarySoundBuffer.Lock(0, capabilities.BufferBytes, LockFlags.EntireBuffer, out dataPart2);
                dataPart1.Read(array, 0, array.Length);
                int numberOfSamples = capabilities.BufferBytes / waveFormat.BlockAlign;
                for (int i = 0; i < numberOfSamples; i++)
                {
                    double vibrato = Math.Cos(2 * Math.PI * 10.0 * i / waveFormat.SampleRate);
                    short  value   = (short)(Math.Cos(2 * Math.PI * (220.0 + 4.0 * vibrato) * i / waveFormat.SampleRate) * 16384); // Not too loud
                    dataPart1.Write(value);
                }
                secondarySoundBuffer.Unlock(dataPart1, dataPart2);
                secondarySoundBuffer.Play(0, PlayFlags.None);
            }
            else
            {
                Console.Write("Podaj nazwe pliku z pulpitu: ");
                string nazwa_pliku = Console.ReadLine();
                player     = new WindowsMediaPlayer();
                player.URL = "C:\\Users\\lab\\Desktop\\" + nazwa_pliku;
                player.controls.play();
            }
        }
        private void FillBuffer(SecondarySoundBuffer buffer, T value)
        {
            DataStream ds = buffer.Lock(0, 0, LockFlags.EntireBuffer, out DataStream ds2);

            for (int i = 0; i < ds.Length; i++)
            {
                ds.Write(value);
            }

            buffer.Unlock(ds, ds2);
        }
Esempio n. 6
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));
        }
Esempio n. 7
0
        public void SetSound(string fileName, int volume)
        {
            if (FileName == fileName)
            {
                return;
            }

            FileName = fileName;
            if (File.Exists(FileName))
            {
                var desc = new SoundBufferDescription();
                desc.Flags = BufferFlags.GetCurrentPosition2 | BufferFlags.ControlFrequency | BufferFlags.ControlPan |
                             BufferFlags.ControlVolume | BufferFlags.GlobalFocus;

                // Open the wave file in binary.
                var reader = new BinaryReader(File.OpenRead(fileName));
                // Read in the wave file header.
                var chunkId        = new string(reader.ReadChars(4));
                var chunkSize      = reader.ReadInt32();
                var format         = new string(reader.ReadChars(4));
                var subChunkId     = new string(reader.ReadChars(4));
                var subChunkSize   = reader.ReadInt32();
                var audioFormat    = (WaveFormatEncoding)reader.ReadInt16();
                var numChannels    = reader.ReadInt16();
                var sampleRate     = reader.ReadInt32();
                var bytesPerSecond = reader.ReadInt32();
                var blockAlign     = reader.ReadInt16();
                var bitsPerSample  = reader.ReadInt16();
                var dataChunkId    = new string(reader.ReadChars(4));
                var dataSize       = reader.ReadInt32();

                // Check that the chunk ID is the RIFF format
                // and the file format is the WAVE format
                // and sub chunk ID is the fmt format
                // and the audio format is PCM
                // and the wave file was recorded in stereo format
                // and there is the data chunk header.
                // Otherwise return false.
                if (chunkId != "RIFF" || format != "WAVE" || subChunkId.Trim() != "fmt" || audioFormat != WaveFormatEncoding.Pcm || dataChunkId != "data" || dataSize < 0)
                {
                    _buffer = null;
                    return;
                }

                // Set the buffer description of the secondary sound buffer that the wave file will be loaded onto and the wave format.
                var bufferDesc = new SoundBufferDescription();
                bufferDesc.Flags = BufferFlags.GetCurrentPosition2 | BufferFlags.ControlPositionNotify | BufferFlags.GlobalFocus |
                                   BufferFlags.ControlVolume | BufferFlags.StickyFocus;
                bufferDesc.BufferBytes    = dataSize;
                bufferDesc.Format         = new WaveFormat(sampleRate, bitsPerSample, numChannels);
                bufferDesc.AlgorithmFor3D = Guid.Empty;

                // Create a temporary sound buffer with the specific buffer settings.
                _buffer = new SecondarySoundBuffer(_dSound, bufferDesc);
                // Read in the wave file data into the temporary buffer.
                var waveData = reader.ReadBytes(dataSize);

                // Close the reader
                reader.Close();

                // Lock the secondary buffer to write wave data into it.
                DataStream waveBufferData2;
                var        waveBufferData1 = _buffer.Lock(0, dataSize, LockFlags.None, out waveBufferData2);

                // Copy the wave data into the buffer.
                waveBufferData1.Write(waveData, 0, dataSize);

                // Unlock the secondary buffer after the data has been written to it.
                _buffer.Unlock(waveBufferData1, waveBufferData2);

                SetVolume(volume);
            }
            else
            {
                _buffer = null;
            }
        }
Esempio n. 8
0
        protected override bool LoadAudioFile(string audioFile, DirectSound directSound)
        {
            try
            {
                // Open the wave file in binary.
                var reader = new BinaryReader(File.OpenRead(SystemConfiguration.DataFilePath + audioFile));

                // Read in the wave file header.
                chunkId        = new string(reader.ReadChars(4));
                chunkSize      = reader.ReadInt32();
                format         = new string(reader.ReadChars(4));
                subChunkId     = new string(reader.ReadChars(4));
                subChunkSize   = reader.ReadInt32();
                audioFormat    = (WaveFormatEncoding )reader.ReadInt16();
                numChannels    = reader.ReadInt16();
                sampleRate     = reader.ReadInt32();
                bytesPerSecond = reader.ReadInt32();
                blockAlign     = reader.ReadInt16();
                bitsPerSample  = reader.ReadInt16();
                dataChunkId    = new string(reader.ReadChars(4));
                dataSize       = reader.ReadInt32();

                // Check that the chunk ID is the RIFF format
                // and the file format is the WAVE format
                // and sub chunk ID is the fmt format
                // and the audio format is PCM
                // and the wave file was recorded in stereo format
                // and at a sample rate of 44.1 KHz
                // and at 16 bit format
                // and there is the data chunk header.
                // Otherwise return false.
                if (chunkId != "RIFF" || format != "WAVE" || subChunkId.Trim() != "fmt" || audioFormat != WaveFormatEncoding.Pcm || numChannels != 2 || sampleRate != 44100 || bitsPerSample != 16 || dataChunkId != "data")
                {
                    return(false);
                }

                // Set the buffer description of the secondary sound buffer that the wave file will be loaded onto and the wave format.
                var buffer = new SoundBufferDescription();
                buffer.Flags          = BufferFlags.ControlVolume;
                buffer.BufferBytes    = dataSize;
                buffer.Format         = new WaveFormat(44100, 16, 2);
                buffer.AlgorithmFor3D = Guid.Empty;

                // Create a temporary sound buffer with the specific buffer settings.
                _SecondaryBuffer = new SecondarySoundBuffer(directSound, buffer);

                // Read in the wave file data into the temporary buffer.
                var waveData = reader.ReadBytes(dataSize);

                // Close the reader
                reader.Close();

                // Lock the secondary buffer to write wave data into it.
                DataStream waveBufferData2;
                var        waveBufferData1 = _SecondaryBuffer.Lock(0, dataSize, LockFlags.None, out waveBufferData2);

                // Copy the wave data into the buffer.
                waveBufferData1.Write(waveData, 0, dataSize);

                // Unlock the secondary buffer after the data has been written to it.
                var result = _SecondaryBuffer.Unlock(waveBufferData1, waveBufferData2);

                if (result != Result.Ok)
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
Esempio n. 9
0
        private SecondarySoundBuffer initializeSecondaryBuffer(string fileName, IntPtr handle)
        {
            var directSound = new DirectSound();

            directSound.SetCooperativeLevel(handle, CooperativeLevel.Exclusive);

            // Open the wave file in binary.
            var reader = new BinaryReader(File.OpenRead(FileName));

            // Read in the wave file header.
            var chunkId        = new string(reader.ReadChars(4));
            var chunkSize      = reader.ReadInt32();
            var format         = new string(reader.ReadChars(4));
            var subChunkId     = new string(reader.ReadChars(4));
            var subChunkSize   = reader.ReadInt32();
            var audioFormat    = (WaveFormatEncoding)reader.ReadInt16();
            var numChannels    = reader.ReadInt16();
            var sampleRate     = reader.ReadInt32();
            var bytesPerSecond = reader.ReadInt32();
            var blockAlign     = reader.ReadInt16();
            var bitsPerSample  = reader.ReadInt16();
            var dataChunkId    = new string(reader.ReadChars(4));
            var dataSize       = reader.ReadInt32();

            // Check that the chunk ID is the RIFF format
            // and the file format is the WAVE format
            // and sub chunk ID is the fmt format
            // and the audio format is PCM
            // and the wave file was recorded in stereo format
            // and at a sample rate of 44.1 KHz
            // and at 16 bit format
            // and there is the data chunk header.
            // Otherwise return false.
            if (chunkId != "RIFF" || format != "WAVE" || subChunkId.Trim() != "fmt" ||
                audioFormat != WaveFormatEncoding.Pcm || numChannels != 2 || sampleRate != 44100 ||
                bitsPerSample != 16 || dataChunkId != "data")
            {
                return(null);
            }

            // Set the buffer description of the secondary sound buffer that the wave file will be loaded onto and the wave format.
            var buffer = new SoundBufferDescription
            {
                Flags          = BufferFlags.ControlVolume,
                BufferBytes    = dataSize,
                Format         = new WaveFormat(44100, 16, 2),
                AlgorithmFor3D = Guid.Empty
            };

            // Create a temporary sound buffer with the specific buffer settings.
            var secondaryBuffer = new SecondarySoundBuffer(directSound, buffer);

            // Read in the wave file data into the temporary buffer.
            var waveData = reader.ReadBytes(dataSize);

            // Close the reader
            reader.Close();

            // Lock the secondary buffer to write wave data into it.
            var waveBufferData1 = secondaryBuffer.Lock(0, dataSize, LockFlags.None, out var waveBufferData2);

            // Copy the wave data into the buffer.
            waveBufferData1.Write(waveData, 0, dataSize);

            // Unlock the secondary buffer after the data has been written to it.
            secondaryBuffer.Unlock(waveBufferData1, waveBufferData2);

            return(secondaryBuffer);
        }