Beispiel #1
0
        private void PlaySound(Stream sound)
        {
            DirectSound ds = new DirectSound();

            ds.SetCooperativeLevel(this.Handle, CooperativeLevel.Priority);


            WaveFormat format = WaveFormat.CreateCustomFormat(
                WaveFormatEncoding.Pcm,
                44100,
                2,
                4 * 44100,
                4,
                16
                );

            SoundBufferDescription primaryDesc = new SoundBufferDescription();

            primaryDesc.Format      = format;
            primaryDesc.Flags       = BufferFlags.GlobalFocus;
            primaryDesc.BufferBytes = 8 * 4 * 44100;
            PrimarySoundBuffer pBuffer = new PrimarySoundBuffer(ds, primaryDesc);

            SoundBufferDescription secondDesc = new SoundBufferDescription();

            secondDesc.Format      = format;
            secondDesc.Flags       = BufferFlags.GlobalFocus | BufferFlags.ControlPositionNotify | BufferFlags.GetCurrentPosition2;
            secondDesc.BufferBytes = 8 * 4 * 44100;

            SecondarySoundBuffer secondBuffer = new SecondarySoundBuffer(ds, secondDesc);

            secondBuffer.Write(sound.ReadAll(), 0, LockFlags.None);
            secondBuffer.Play(0, PlayFlags.None);
        }
Beispiel #2
0
        bool InitializeDirectSound(IntPtr windowHandler)
        {
            try
            {
                // Initialize the direct sound interface pointer for the default sound device.
                _DirectSound = new DirectSound();

                // Set the cooperative level to priority so the format of the primary sound buffer can be modified.
                if (_DirectSound.SetCooperativeLevel(windowHandler, CooperativeLevel.Priority) != Result.Ok)
                {
                    return(false);
                }

                // Setup the primary buffer description.
                var buffer = new SoundBufferDescription();
                buffer.Flags          = BufferFlags.PrimaryBuffer | BufferFlags.ControlVolume;
                buffer.AlgorithmFor3D = Guid.Empty;

                // Get control of the primary sound buffer on the default sound device.
                _PrimaryBuffer = new PrimarySoundBuffer(_DirectSound, buffer);

                // Setup the format of the primary sound buffer.
                // In this case it is a .
                _PrimaryBuffer.Format = new WaveFormat(44100, 16, 2);
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        bool InitializeDirectSound(IntPtr hwnd)
        {
            try {
                directSound = new SharpDX.DirectSound.DirectSound();
                directSound.SetCooperativeLevel(hwnd, CooperativeLevel.Priority);

                var soundBufferDescription = new SoundBufferDescription {
                    Flags          = BufferFlags.PrimaryBuffer | BufferFlags.ControlVolume,
                    BufferBytes    = 0,
                    Format         = null,
                    AlgorithmFor3D = Guid.Empty
                };

                primaryBuffer = new PrimarySoundBuffer(directSound, soundBufferDescription);

                var samplesPerSec   = 44100;
                var bitsPerSample   = 16;
                var nChannels       = 2;
                var blockAlign      = bitsPerSample / 8 * nChannels;
                var nAvgBytesPerSec = samplesPerSec * blockAlign;
                var waveFormat      = WaveFormat.CreateCustomFormat(
                    WaveFormatEncoding.Pcm,
                    samplesPerSec,
                    nChannels,
                    nAvgBytesPerSec,
                    blockAlign,
                    bitsPerSample
                    );

                primaryBuffer.Format = waveFormat;
            } catch { return(false); }
            return(true);
        }
        private void InitialiseDirectSound(IntPtr windowHandle, int sampleRate)
        {
            var waveFormat = new WaveFormat(sampleRate, 16, 2);

            _directSound = new DirectSound();
            _directSound.SetCooperativeLevel(windowHandle, CooperativeLevel.Priority);

            var primaryBufferDescription = new SoundBufferDescription()
            {
                Flags          = BufferFlags.PrimaryBuffer,
                AlgorithmFor3D = Guid.Empty,
            };

            _primarySoundBuffer = new PrimarySoundBuffer(_directSound, primaryBufferDescription)
            {
                Format = waveFormat
            };

            _soundBufferLength = waveFormat.ConvertLatencyToByteSize(500);

            var secondaryBufferDescription = new SoundBufferDescription()
            {
                Format         = waveFormat,
                Flags          = BufferFlags.GetCurrentPosition2 | BufferFlags.GlobalFocus,
                BufferBytes    = _soundBufferLength,
                AlgorithmFor3D = Guid.Empty,
            };

            _soundBuffer = new SecondarySoundBuffer(_directSound, secondaryBufferDescription);
        }
Beispiel #5
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);
        }
        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();
            }
        }
Beispiel #7
0
 public void Create()
 {
     device = new DirectSound();
     device.SetCooperativeLevel(handle, CooperativeLevel.Priority);
     pBufferDescription.Flags = BufferFlags.PrimaryBuffer;
     pBuffer        = new PrimarySoundBuffer(device, pBufferDescription);
     pBuffer.Format = audioFormat;
     Reset();
 }
Beispiel #8
0
 public void Create()
 {
     device = new DirectSound();
     device.SetCooperativeLevel(handle, CooperativeLevel.Priority);
     pBufferDescription.Flags = BufferFlags.PrimaryBuffer;
     pBuffer = new PrimarySoundBuffer(device, pBufferDescription);
     pBuffer.Format = audioFormat;
     Reset();
 }
 private void ShutdownDirectSound()
 {
     // Release the primary sound buffer pointer.
     _PrimaryBuffer?.Dispose();
     _PrimaryBuffer = null;
     // Release the direct sound interface pointer.
     _DirectSound?.Dispose();
     _DirectSound = null;
 }
Beispiel #10
0
        public void run()
        {
            directSound = new DirectSound();


            IntPtr hwnd = GetDesktopWindow();

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

            // Create PrimarySoundBuffer
            var primaryBufferDesc = new SoundBufferDescription();

            primaryBufferDesc.Flags          = BufferFlags.PrimaryBuffer;
            primaryBufferDesc.AlgorithmFor3D = Guid.Empty;

            primarySoundBuffer = new PrimarySoundBuffer(directSound, primaryBufferDesc);


            // Create SecondarySoundBuffer
            int soundLatencyInMilliseconds = 100;

            var secondaryBufferDesc = new SoundBufferDescription();

            secondaryBufferDesc.BufferBytes = soundStreamer.waveFormat.ConvertLatencyToByteSize(soundLatencyInMilliseconds);
            secondaryBufferDesc.Format      = soundStreamer.waveFormat;
            secondaryBufferDesc.Flags       = BufferFlags.GetCurrentPosition2 | BufferFlags.ControlPositionNotify | BufferFlags.GlobalFocus |
                                              BufferFlags.ControlVolume | BufferFlags.StickyFocus | BufferFlags.Trueplayposition;
            secondaryBufferDesc.AlgorithmFor3D = Guid.Empty;



            secondarySoundBuffer = new SecondarySoundBuffer(directSound, secondaryBufferDesc);



            NotificationPosition n = new NotificationPosition();

            n.Offset     = (secondaryBufferDesc.BufferBytes / 4) * 1;
            n.WaitHandle = soundStreamer.wait;

            NotificationPosition n2 = new NotificationPosition();

            n2.Offset     = (secondaryBufferDesc.BufferBytes / 4) * 3;
            n2.WaitHandle = soundStreamer.wait;

            secondarySoundBuffer.SetNotificationPositions(new NotificationPosition[] { n, n2 });

            soundStreamerThread = new Thread(soundStreamer.loop);
            soundStreamer.secondarySoundBuffer = secondarySoundBuffer;
            soundStreamerThread.Start();


            // play the sound
            secondarySoundBuffer.Play(0, PlayFlags.Looping);
        }
Beispiel #11
0
        public Sound(IntPtr handle)
        {
            test[1] = test[0] + 2;
            test[2] = test[1] + 2;
            test[3] = test[2] + 1;
            test[4] = test[3] + 2;
            test[5] = test[4] + 2;
            test[6] = test[5] + 2;


            DirectSound directSound = new DirectSound();

            // Set Cooperative Level to PRIORITY (priority level can call the SetFormat and Compact methods)
            //
            directSound.SetCooperativeLevel(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 = new WaveFormat();

            // Create SecondarySoundBuffer
            var secondaryBufferDesc = new SoundBufferDescription();

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

            // Get Capabilties from secondary sound buffer
            capabilities = secondarySoundBuffer.Capabilities;
            sounds       = new short[capabilities.BufferBytes / waveFormat.BlockAlign];

            // Play the song
            secondarySoundBuffer.Play(0, PlayFlags.Looping);

            for (int i = 0; i < sounds.Length; i++)
            {
                sounds[i] = 0;
            }
        }
 private void ShutdownDirectSound()
 {
     // Release the listener interface.
     _Listener?.Dispose();
     _Listener = null;
     // Release the 3D Secondary Sound Buffer.
     _3DSecondarySoundBuffer?.Dispose();
     _3DSecondarySoundBuffer = null;
     // Release the primary sound buffer pointer.
     _PrimaryBuffer?.Dispose();
     _PrimaryBuffer = null;
     // Release the direct sound interface pointer.
     _DirectSound?.Dispose();
     _DirectSound = null;
 }
Beispiel #13
0
        void ShutdownDirectSound()
        {
            // Release the primary sound buffer pointer.
            if (_PrimaryBuffer != null)
            {
                _PrimaryBuffer.Dispose();
                _PrimaryBuffer = null;
            }

            // Release the direct sound interface pointer.
            if (_DirectSound != null)
            {
                _DirectSound.Dispose();
                _DirectSound = null;
            }
        }
Beispiel #14
0
        public AudioDevice(IntPtr hwnd)
        {
            // initialise directsound
            _directSound = new DirectSound();
            _directSound.SetCooperativeLevel(hwnd, CooperativeLevel.Priority);

            // create primary sound buffer
            var primaryBufferDesc = new SoundBufferDescription
            {
                Flags          = BufferFlags.PrimaryBuffer,
                AlgorithmFor3D = Guid.Empty
            };

            _primarySoundBuffer = new PrimarySoundBuffer(_directSound, primaryBufferDesc)
            {
                Format = new WaveFormat(),
            };
        }
Beispiel #15
0
        private void initDirectSound()
        {
            // create DirectSound object.
            directSound = new DirectSound();

            // set cooperative level.
            directSound.SetCooperativeLevel(windowHandle, SlimDX.DirectSound.CooperativeLevel.Priority);

            // create the primary sound buffer.
            SoundBufferDescription desc = new SoundBufferDescription();

            desc.Flags    = SlimDX.DirectSound.BufferFlags.PrimaryBuffer;
            primaryBuffer = new PrimarySoundBuffer(directSound, desc);

            // create secondary sound buffer
            bgMusicBuffer   = loadSoundFile("hustlepong_10.wav");
            paddleHitBuffer = loadSoundFile("tennis_ball_hit_by_racket.wav");
            wallHitBuffer   = loadSoundFile("tennis_ball_single_bounce_floor_001.wav");
        }
Beispiel #16
0
        public static void Startup()
        {
            DirectSoundDevice = new DirectSound();


            if (DirectSoundDevice.SetCooperativeLevel(Process.GetCurrentProcess().MainWindowHandle, CooperativeLevel.Priority).IsFailure)
            {
                throw new InvalidOperationException("Initializing DirectSound was failed.");
            }


            PrimaryBufferDesc = new SoundBufferDescription
            {
                Format      = null,
                Flags       = BufferFlags.PrimaryBuffer,
                SizeInBytes = 0,
            };


            PrimaryBuffer = new PrimarySoundBuffer(DirectSoundDevice, PrimaryBufferDesc);
            PrimaryBuffer.Play(0, PlayFlags.Looping);
        }
Beispiel #17
0
 public static void setListener(double X1, double Y1, double Z1,
                                double X2, double Y2, double Z2)
 {
     //if this is the first time calling this method,
     //instantiate the listener,
     //else just reset its position
     if (DSBListener == null)
     {
         SoundBufferDescription BufferDesc = new SoundBufferDescription();
         BufferDesc.Flags = SharpDX.DirectSound.BufferFlags.PrimaryBuffer
                            | SharpDX.DirectSound.BufferFlags.Control3D;
         primaryBuffer = new PrimarySoundBuffer(objDS, BufferDesc);
         //Finally, instantiate the listener using the PrimaryBuffer object
         DSBListener = new SoundListener3D(primaryBuffer);
         DSBListener.RolloffFactor = 1.0f;                 //Apply rolloff
         //according to realism.
         DSBListener.DistanceFactor = 0.3048f;
     }
     //To set orientation, a listener3DOrientation object must be passed which contains values for front.x,y,z, Etc.
     DSBListener.Position = Get3DVector(0.0, 0.0, 0.0);
     setOrientation(DSBListener,
                    X1, Y1, Z1, X2, Y2, Z2);
 }
        // Private Methods.
        private bool InitializeDirectSound(IntPtr windowHandler)
        {
            try
            {
                // Initialize the direct sound interface pointer for the default sound device.
                _DirectSound = new DirectSound();

                try
                {
                    _DirectSound.SetCooperativeLevel(windowHandler, CooperativeLevel.Priority);
                }
                catch
                {
                    return(false);
                }

                // Setup the primary buffer description.
                SoundBufferDescription primaryBufferDesc = new SoundBufferDescription()
                {
                    Flags          = BufferFlags.PrimaryBuffer | BufferFlags.ControlVolume,
                    AlgorithmFor3D = Guid.Empty
                };

                // Get control of the primary sound buffer on the default sound device.
                _PrimaryBuffer = new PrimarySoundBuffer(_DirectSound, primaryBufferDesc);

                // Setup the format of the primary sound buffer.
                // In this case it is a .
                _PrimaryBuffer.Format = new WaveFormat(44100, 16, 2);
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
        // Private Methods.
        private bool InitializeDirectSound(IntPtr windowHandler)
        {
            try
            {
                // Initialize the direct sound interface pointer for the default sound device.
                _DirectSound = new DirectSound();

                try
                {
                    _DirectSound.SetCooperativeLevel(windowHandler, CooperativeLevel.Priority);
                }
                catch
                {
                    return(false);
                }

                // Setup the primary buffer description.
                SoundBufferDescription primaryBufferDesc = new SoundBufferDescription()
                {
                    Flags          = BufferFlags.PrimaryBuffer | BufferFlags.ControlVolume | BufferFlags.Control3D,
                    AlgorithmFor3D = Guid.Empty
                };

                // Get control of the primary sound buffer on the default sound device.
                _PrimaryBuffer = new PrimarySoundBuffer(_DirectSound, primaryBufferDesc);

                _Listener          = new SoundListener3D(_PrimaryBuffer);
                _Listener.Deferred = false;
                _Listener.Position = new Vector3(0.0f, 0.0f, 0.0f);
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }