Example #1
0
        private void InitializeInternal()
        {
            Debug.WriteLine("Initialize, thread id: " + Thread.CurrentThread.ManagedThreadId);
            _callbackThread = null;
            var supportedFormats = new Queue<WaveFormat>(Device.SupportedFormats
                .OrderBy(x => Math.Abs(x.SampleRate - _source.WaveFormat.SampleRate))
                .ThenBy(x => Math.Abs(x.BitsPerSample - _source.WaveFormat.BitsPerSample))
                .ThenBy(x => Math.Abs(x.Channels - _source.WaveFormat.Channels)));

            var finalFormat = _source.WaveFormat;
            do
            {
                try
                {
                    _waveOutHandle = CreateWaveOutHandle(finalFormat);
                }
                catch (MmException exception)
                {
                    if (exception.Result == MmResult.BadFormat && supportedFormats.Count > 0)
                        finalFormat = supportedFormats.Dequeue();
                    else if (exception.Result == MmResult.BadFormat && supportedFormats.Count == 0)
                        throw new Exception("No valid format could be found.", exception);
                    else
                        throw;
                }
            } while (_waveOutHandle == IntPtr.Zero);

            if (finalFormat != _source.WaveFormat)
            {
                //the original format of the source is not supported
                //we have to convert the source
                //todo: test channel matrix conversion
                ChannelMatrix channelMatrix = null;
                if (UseChannelMixingMatrices)
                {
                    try
                    {
                        channelMatrix = ChannelMatrix.GetMatrix(_source.WaveFormat, finalFormat);
                    }
                    catch (Exception)
                    {
                        Debug.WriteLine("No channelmatrix was found.");
                    }
                }
                DmoResampler resampler = channelMatrix != null
                    ? new DmoChannelResampler(_source, channelMatrix, finalFormat)
                    : new DmoResampler(_source, finalFormat);
                resampler.Quality = 60;

                _source = resampler;
            }

            _failedBuffers.Clear();
            var bufferSize = (int) WaveSource.WaveFormat.MillisecondsToBytes(_latency);
            _buffers = new WaveOutBuffer[BufferCount];
            for (int i = 0; i < _buffers.Length; i++)
            {
                _buffers[i] = new WaveOutBuffer(_waveOutHandle, bufferSize, (IntPtr) i);
            }
        }
Example #2
0
 private bool FireUpBuffer(WaveOutBuffer buffer)
 {
     if (buffer.Refill(_source))
     {
         Interlocked.Increment(ref _activeBuffers);
         return true;
     }
     return false;
 }