Beispiel #1
0
        private void ResetChannelVolumes()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new DisposeDelegate(ResetChannelVolumes));
                return;
            }

            mVoice.SetChannelVolumes(2, GetChannelVolumes((float)mPan));
        }
Beispiel #2
0
        public void Play( Form on )
        {
            var screens = Screen.AllScreens;
            var screens_left  = screens.Min( screen => screen.Bounds.Left  );
            var screens_right = screens.Max( screen => screen.Bounds.Right );
            var screens_width = screens_right-screens_left;

            var bestScreen = screens.OrderByDescending( screen => {
                var area = screen.Bounds;
                area.Intersect( on.Bounds );
                return area.Width*area.Height;
            }).First();

            var balances = new[]{1.5f,1.5f};
            if ( screens.Length==3 && DisplayBalances.ContainsKey(bestScreen.DeviceName) ) balances = DisplayBalances[bestScreen.DeviceName];

            var path   = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\.Default\"+Name+@"\.Current").GetValue(null) as string;
            var stream = new WaveStream(path);
            var buffer = new AudioBuffer() { AudioBytes=(int)stream.Length, AudioData=stream, Flags=BufferFlags.EndOfStream };

            var voice = new SourceVoice( XAudio2, stream.Format );
            voice.SubmitSourceBuffer( buffer );
            voice.SetChannelVolumes( balances.Length, balances );
            voice.BufferEnd += (sender,ctx) => {
                try {
                    on.BeginInvoke(new Action(()=>{
                        voice.Dispose();
                        buffer.Dispose();
                        stream.Dispose();
                    }));
                } catch ( InvalidOperationException ) {
                    // herp derp on must be disposed/gone
                }
            };
            voice.Start();
        }
Beispiel #3
0
        private void PlaySoundAsync()
        {
            try
            {
                int numberOfSamples = _bufferSize / _waveFormat.BlockAlign;

                while (true)
                {
                    if (_playerState != PlayerState.Stopped)
                    {
                        while (_playerState != PlayerState.Stopped)
                        {
                            if (_playEvent.Wait(_waitPrecision))
                            {
                                break;
                            }
                        }

                        for (int i = 0; i < numberOfSamples; i++)
                        {
                            float value = (float)(Math.Sin(2 * Math.PI * _valueRate * i / _waveFormat.SampleRate) * _valueAmp);
                            _dataStream.Write(value);
                            _dataStream.Write(value);
                        }

                        _dataStream.Position = 0;

                        while (_sourceVoice.State.BuffersQueued == _audioBuffersRing.Length)
                        {
                            _bufferEndEvent.WaitOne(_waitPrecision);
                        }

                        if (_bufferSize > _memBuffers[_nextBuffer].Size)
                        {
                            if (_memBuffers[_nextBuffer].Pointer != IntPtr.Zero)
                            {
                                Utilities.FreeMemory(_memBuffers[_nextBuffer].Pointer);
                            }

                            _memBuffers[_nextBuffer].Pointer = Utilities.AllocateMemory(_bufferSize);
                            _memBuffers[_nextBuffer].Size    = _bufferSize;
                        }

                        // Copy the memory from MediaFoundation AudioDecoder to the buffer that is going to be played.
                        Utilities.CopyMemory(_memBuffers[_nextBuffer].Pointer, _dataStream.DataPointer, _bufferSize);

                        // Set the pointer to the data.
                        _audioBuffersRing[_nextBuffer].AudioDataPointer = _memBuffers[_nextBuffer].Pointer;
                        _audioBuffersRing[_nextBuffer].AudioBytes       = _bufferSize;

                        if (!_isConvolutionOn)
                        {
                            var   angle = _horizontalAngle > 90 ? MAX_HORIZONATL_ANGLE_ABS - _horizontalAngle : _horizontalAngle;
                            float rightChannelVolume = (float)(angle + MAX_HORIZONATL_ANGLE_ABS / 2) /
                                                       (HORIZONTAL_ANGLE_RANGE / 2);
                            float leftChannelVolume = 1 - rightChannelVolume;
                            _sourceVoice.SetChannelVolumes(2, new[] { rightChannelVolume, leftChannelVolume });
                        }

                        _sourceVoice.SubmitSourceBuffer(_audioBuffersRing[_nextBuffer], null);

                        // Go to next entry in the ringg audio buffer
                        _nextBuffer = ++_nextBuffer % _audioBuffersRing.Length;
                    }
                }
            }
            finally
            {
                Close();
            }
        }