Beispiel #1
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);
        }
Beispiel #2
0
        public PlayForm()
        {
            InitializeComponent();

            // Initalize XAudio2
            xaudio2 = new XAudio2(XAudio2Flags.None, ProcessorSpecifier.DefaultProcessor);
            masteringVoice = new MasteringVoice(xaudio2);

            var waveFormat = new WaveFormat(44100, 32, 2);
             sourceVoice = new SourceVoice(xaudio2, waveFormat);

            int bufferSize = waveFormat.ConvertLatencyToByteSize(60000);
            DataStream dataStream = new DataStream(bufferSize, true, true);

            // Prepare the initial sound to modulate
            int numberOfSamples = bufferSize / waveFormat.BlockAlign;
            for (int i = 0; i < numberOfSamples; i++)
            {
                float value = (float)(Math.Cos(2 * Math.PI * 220.0 * i / waveFormat.SampleRate) * 0.5);
                dataStream.Write(value);
                dataStream.Write(value);
            }
            dataStream.Position = 0;

            audioBuffer = new AudioBuffer
                              {
                                  Stream = dataStream,
                                  Flags = BufferFlags.EndOfStream,
                                  AudioBytes = bufferSize,
                                  LoopBegin = 0,
                                  LoopLength = numberOfSamples,
                                  LoopCount = AudioBuffer.LoopInfinite
                              };

            // Set the effect on the source
            ModulatorEffect = new ModulatorEffect();
            modulatorDescriptor = new EffectDescriptor(ModulatorEffect);
            reverb = new Reverb(xaudio2);
            effectDescriptor = new EffectDescriptor(reverb);
            //sourceVoice.SetEffectChain(modulatorDescriptor, effectDescriptor);
            sourceVoice.SetEffectChain(modulatorDescriptor);
            //sourceVoice.EnableEffect(0);

            this.Closed += new EventHandler(PlayForm_Closed);
        }
Beispiel #3
0
        private int historySize; // buffers

        #endregion Fields

        #region Constructors

        public BeatDetector(WaveFormat waveFormat, int historyLength, int bufferSize)
        {
            //this.bufferSize = bufferSize;
            this.historyLength = historyLength;

            // Number of history buffers needed to be equivalent to historyLength (milliseconds)
            historySize = waveFormat.ConvertLatencyToByteSize(historyLength) / bufferSize;
            beatsSize = historySize;

            history = new float[historySize];
            historyIndex = 0;

            beats = new float[beatsSize];
            beatIndex = 0;

            fft = new FastFourierTransform(beatsSize, 1);

            InstantaneousEnergy = 0.0f;
            AverageEnergy = 0.0f;
        }
Beispiel #4
0
        /// <summary>
        /// SharpDX XAudio2 sample. Plays a generated sound with some reverb.
        /// </summary>
        static void Main(string[] args)
        {
            var xaudio2 = new XAudio2();
            var masteringVoice = new MasteringVoice(xaudio2);

            var waveFormat = new WaveFormat(44100, 32, 2);
            var sourceVoice = new SourceVoice(xaudio2, waveFormat);

            int bufferSize = waveFormat.ConvertLatencyToByteSize(60000);
            var dataStream = new DataStream(bufferSize, true, true);

            int numberOfSamples = bufferSize/waveFormat.BlockAlign;
            for (int i = 0; i < numberOfSamples; i++)
            {
                double vibrato = Math.Cos(2 * Math.PI * 10.0 * i / waveFormat.SampleRate);
                float value = (float) (Math.Cos(2*Math.PI*(220.0 + 4.0*vibrato)*i/waveFormat.SampleRate)*0.5); 
                dataStream.Write(value);
                dataStream.Write(value);
            }
            dataStream.Position = 0;

            var audioBuffer = new AudioBuffer {Stream = dataStream, Flags = BufferFlags.EndOfStream, AudioBytes = bufferSize};

            var reverb = new Reverb();
            var effectDescriptor = new EffectDescriptor(reverb);
            sourceVoice.SetEffectChain(effectDescriptor);
            sourceVoice.EnableEffect(0);

            sourceVoice.SubmitSourceBuffer(audioBuffer, null);

            sourceVoice.Start();

            Console.WriteLine("Play sound");
            for(int i = 0; i < 60; i++)
            {
                Console.Write(".");
                Console.Out.Flush();
                Thread.Sleep(1000);
            }
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new UIForm());

            UIHandler.Init();
            GraphicsManager.Init();
            ThemeManager.LoadDefault();
            InteropManager.Init();

            /*UIForm f1 = new UIForm(1);
            UIForm f2 = new UIForm(2);

            Button b = new Button();
            b.SetBounds(48, 48, 48, 48);
            b.Click += (Object s, EventArgs e) => {
                new UIForm(32).Show();
            };
            f1.Controls.Add(b);

            f2.Show();*/
            new WindowMain().Open();

            UIManager.SpawnUIUpdateThread();
            //Application.Run(f1);
            Application.Run();

            bool yes = true;
            if (yes) return;

            //
            XAudio2 audio = new XAudio2();
            WaveFormat format = new WaveFormat(44100, 32, 2);
            MasteringVoice mvoice = new MasteringVoice(audio);
            SourceVoice voice = new SourceVoice(audio, format);

            int bufferSize = format.ConvertLatencyToByteSize(500);

            DataStream stream = new DataStream(bufferSize, true, true);

            int samples = bufferSize / format.BlockAlign;
            for (int i = 0; i < samples; i++) {
                float val = (float)(Math.Sin(2*Math.PI*500*i/format.SampleRate));// * (0.5 + Math.Sin(2*Math.PI*2.2*i/format.SampleRate)*0.5));
                //if (val == 0) val = 1;
                //val = val / Math.Abs(val);
                for (int j = 0; j < 3; j++) val = val * val;
                stream.Write(val);
                stream.Write(val);
                //stream.Write(val * (float)(0.5 + Math.Sin(2 * Math.PI * 2.2 * i / format.SampleRate) * 0.5));
                //stream.Write(val * (float)(0.5 + Math.Cos(2 * Math.PI * 2.2 * i / format.SampleRate) * 0.5));
            }

            stream.Position = 0;

            AudioBuffer buffer = new AudioBuffer { Stream = stream, Flags = BufferFlags.EndOfStream, AudioBytes = bufferSize };
            //buffer.LoopCount = AudioBuffer.LoopInfinite;
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);
            voice.SubmitSourceBuffer(buffer, null);

            voice.Start();
            Thread.Sleep(3000);
            long pos = stream.Position;
            stream.Position = 0;
            for (int i = 0; i < samples; i++) {
                float val = (float)(Math.Sin(2 * Math.PI * 500 * i / format.SampleRate));// * (0.5 + Math.Sin(2*Math.PI*2.2*i/format.SampleRate)*0.5));
                //if (val == 0) val = 1;
                //val = val / Math.Abs(val);
                //for (int j = 0; j < 8; j++) val = val * val;
                stream.Write(val);
                stream.Write(val);
                //stream.Write(val * (float)(0.5 + Math.Sin(2 * Math.PI * 2.2 * i / format.SampleRate) * 0.5));
                //stream.Write(val * (float)(0.5 + Math.Cos(2 * Math.PI * 2.2 * i / format.SampleRate) * 0.5));
            }
            //stream.Position = pos;
            //voice.State.p
            while (voice.State.BuffersQueued > 0) Thread.Sleep(100);
            //Thread.Sleep(6000);
        }
Beispiel #6
0
        /// <summary>
        /// SharpDX X3DAudio sample. Plays a generated sound rotating around the listener.
        /// </summary>
        static void Main(string[] args)
        {
            var xaudio2 = new XAudio2();
            using (var masteringVoice = new MasteringVoice(xaudio2))
            {
                // Instantiate X3DAudio
                var x3dAudio = new X3DAudio(Speakers.Stereo);

                var emitter = new Emitter
                                  {
                                      ChannelCount = 1,
                                      CurveDistanceScaler = float.MinValue,
                                      OrientFront = new Vector3(0, 0, 1),
                                      OrientTop = new Vector3(0, 1, 0),
                                      Position = new Vector3(0, 0, 0),
                                      Velocity = new Vector3(0, 0, 0)
                                  };

                var listener = new Listener
                                   {
                                       OrientFront = new Vector3(0, 0, 1),
                                       OrientTop = new Vector3(0, 1, 0),
                                       Position = new Vector3(0, 0, 0),
                                       Velocity = new Vector3(0, 0, 0)
                                   };

                var waveFormat = new WaveFormat(44100, 32, 1);
                var sourceVoice = new SourceVoice(xaudio2, waveFormat);

                int bufferSize = waveFormat.ConvertLatencyToByteSize(60000);
                var dataStream = new DataStream(bufferSize, true, true);

                int numberOfSamples = bufferSize/waveFormat.BlockAlign;
                for (int i = 0; i < numberOfSamples; i++)
                {
                    float value = (float) (Math.Cos(2*Math.PI*220.0*i/waveFormat.SampleRate)*0.5);
                    dataStream.Write(value);
                }
                dataStream.Position = 0;

                var audioBuffer = new AudioBuffer
                                      {Stream = dataStream, Flags = BufferFlags.EndOfStream, AudioBytes = bufferSize};

                //var reverb = new Reverb();
                //var effectDescriptor = new EffectDescriptor(reverb);
                //sourceVoice.SetEffectChain(effectDescriptor);
                //sourceVoice.EnableEffect(0);

                sourceVoice.SubmitSourceBuffer(audioBuffer, null);

                sourceVoice.Start();

                Console.WriteLine("Play a sound rotating around the listener");
                for (int i = 0; i < 1200; i++)
                {
                    // Rotates the emitter
                    var rotateEmitter = Matrix.RotationY(i/5.0f);
                    var newPosition = Vector3.Transform(new Vector3(0, 0, 1000), rotateEmitter);
                    var newPositionVector3 = new Vector3(newPosition.X, newPosition.Y, newPosition.Z);
                    emitter.Velocity = (newPositionVector3 - emitter.Position)/0.05f;
                    emitter.Position = newPositionVector3;

                    // Calculate X3DAudio settings
                    var dspSettings = x3dAudio.Calculate(listener, emitter, CalculateFlags.Matrix | CalculateFlags.Doppler, 1, 2);

                    // Modify XAudio2 source voice settings
                    sourceVoice.SetOutputMatrix(1, 2, dspSettings.MatrixCoefficients);
                    sourceVoice.SetFrequencyRatio(dspSettings.DopplerFactor);

                    // Wait for 50ms
                    Thread.Sleep(50);
                }
            }
        }