CreateMixer() public method

Create a mixer using the stream attributes
public CreateMixer ( MediaPortal.MusicPlayer.BASS.MusicStream stream ) : bool
stream MediaPortal.MusicPlayer.BASS.MusicStream
return bool
    /// <summary>
    /// Called to start playback of next files internally, without makinbg use of g_player.
    /// This gives us the capability to achieve gapless playback.
    /// </summary>
    /// <param name="filePath"></param>
    private bool PlayInternal(string filePath)
    {
      if (filePath == string.Empty)
      {
        return false;
      }

      // Cue support
      if (HandleCueFile(ref filePath, true))
      {
        return true;
      }

      _filePath = filePath;

      MusicStream stream = new MusicStream(filePath);

      if (stream.BassStream == 0)
      {
        return false;
      }

      _streams.Add(stream);

      if (stream.Filetype.FileMainType == FileMainType.CDTrack)
      {
        _isCDDAFile = true;
      }
      else
      {
        _isCDDAFile = false;
      }

      if (_mixer == null)
      {
        _mixer = new MixerStream(this);
        _mixer.MusicStreamMessage += OnMusicStreamMessage;
        if (!_mixer.CreateMixer(stream))
        {
          Log.Error("BASS: Could not create Mixer. Aborting playback.");
          return false;
        }
      }
      else
      {
        if (NewMixerNeeded(stream))
        {
          Log.Debug("BASS: New stream has different number of channels or sample rate. Need a new mixer.");
          // Free Mixer
          _mixer.Dispose();
          _mixer = null;
          _mixer = new MixerStream(this);
          _mixer.MusicStreamMessage += OnMusicStreamMessage;
          if (!_mixer.CreateMixer(stream))
          {
            Log.Error("BASS: Could not create Mixer. Aborting playback.");
            return false;
          }
        }
      }

      // Enable events, for various Playback Actions to be handled
      stream.MusicStreamMessage += new MusicStream.MusicStreamMessageHandler(OnMusicStreamMessage);

      SetCueTrackEndPosition(stream, false);

      // Plug in the stream into the Mixer
      if (!_mixer.AttachStream(stream))
      {
        return false;
      }

      return true;
    }
    /// <summary>
    /// Starts Playback of the given file
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public override bool Play(string filePath)
    {
      if (!_initialized)
      {
        return false;
      }

      MusicStream currentStream = GetCurrentStream();

      bool result = true;
      Speed = 1; // Set playback Speed to normal speed

      try
      {
        if (currentStream != null && filePath.ToLowerInvariant().CompareTo(currentStream.FilePath.ToLowerInvariant()) == 0)
        {
          // Selected file is equal to current stream
          if (_state == PlayState.Paused)
          {
            // Resume paused stream
            currentStream.ResumePlayback();

            result = Bass.BASS_Start();

            if (Config.MusicPlayer == AudioPlayer.Asio)
            {
              result = BassAsio.BASS_ASIO_ChannelReset(false, 0, BASSASIOReset.BASS_ASIO_RESET_PAUSE);   // Continue playback of Paused stream
            }
            else if (Config.MusicPlayer == AudioPlayer.WasApi)
            {
              BassWasapi.BASS_WASAPI_Start();
            }

            if (result)
            {
              _state = PlayState.Playing;

              if (PlaybackStateChanged != null)
              {
                PlaybackStateChanged(this, PlayState.Paused, _state);
              }
            }

            return result;
          }
        }
        else
        {
          // Cue support
          if (HandleCueFile(ref filePath))
          {
            return true;
          }
        }

        // If we're not Crossfading, we want to stop the current stream at this time
        if (currentStream != null && currentStream.IsPlaying)
        {
          if (!currentStream.IsCrossFading)
          {
            currentStream.FadeOutStop();
          }
        }

        _state = PlayState.Init;

        if (filePath == string.Empty)
        {
          return result;
        }

        _filePath = filePath;

        MusicStream stream = new MusicStream(filePath);

        if (stream.BassStream == 0)
        {
          return false;
        }

        _streams.Add(stream);
        if (stream.Filetype.FileMainType == FileMainType.CDTrack)
        {
          _isCDDAFile = true;
        }
        else
        {
          _isCDDAFile = false;
        }

        bool playbackStarted = false;

        if (_mixer == null)
        {
          _mixer = new MixerStream(this);
          if (!_mixer.CreateMixer(stream))
          {
            Log.Error("BASS: Could not create Mixer. Aborting playback.");
            return false;
          }

          // Start a StreamCopy for Visualisation purposes
          if (HasViz)
          {
            _streamcopy.ChannelHandle = _mixer.BassStream;
            _streamcopy.Start();
          }
        }
        else
        {
          if (!_mixer.UpMixing)
          {
            BASS_CHANNELINFO chinfo = Bass.BASS_ChannelGetInfo(_mixer.BassStream);
            if (!_mixer.WasApiShared &&
                (chinfo.freq != stream.ChannelInfo.freq || chinfo.chans != stream.ChannelInfo.chans))
            {
              if (stream.ChannelInfo.freq != _mixer.WasApiMixedFreq ||
                  stream.ChannelInfo.chans != _mixer.WasApiMixedChans)
              {
                Log.Debug("BASS: New stream has different number of channels or sample rate. Need a new mixer.");
                // The new stream has a different frequency or number of channels
                // We need a new mixer
                _mixer.Dispose();
                _mixer = null;
                _mixer = new MixerStream(this);
                if (!_mixer.CreateMixer(stream))
                {
                  Log.Error("BASS: Could not create Mixer. Aborting playback.");
                  return false;
                }

                if (HasViz)
                {
                  _streamcopy.ChannelHandle = _mixer.BassStream;
                }
              }
            }
          }
        }

        // Enable events, for various Playback Actions to be handled
        stream.MusicStreamMessage += new MusicStream.MusicStreamMessageHandler(OnMusicStreamMessage);

        SetCueTrackEndPosition(stream);

        // Plugin the stream into the Mixer
        result = _mixer.AttachStream(stream);

        if (Config.MusicPlayer == AudioPlayer.Asio && !BassAsio.BASS_ASIO_IsStarted())
        {
          BassAsio.BASS_ASIO_Stop();
          playbackStarted = BassAsio.BASS_ASIO_Start(0);
        }
        else if (Config.MusicPlayer == AudioPlayer.WasApi && !BassWasapi.BASS_WASAPI_IsStarted())
        {
          playbackStarted = BassWasapi.BASS_WASAPI_Start();
        }
        else
        {
          if (Bass.BASS_ChannelIsActive(_mixer.BassStream) == BASSActive.BASS_ACTIVE_PLAYING)
          {
            playbackStarted = true;
          }
          else
          {
            playbackStarted = Bass.BASS_ChannelPlay(_mixer.BassStream, false);
          }
        }

        if (stream.BassStream != 0 && playbackStarted)
        {
          Log.Info("BASS: playback started");

          // Slide in the Stream over the Cross fade Interval
          stream.SlideIn();

          GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_PLAYBACK_STARTED, 0, 0, 0, 0, 0, null);
          msg.Label = _filePath;
          GUIWindowManager.SendThreadMessage(msg);
          NotifyPlaying = true;

          NeedUpdate = true;
          _IsFullScreen = GUIGraphicsContext.IsFullScreenVideo;
          _VideoPositionX = GUIGraphicsContext.VideoWindow.Left;
          _VideoPositionY = GUIGraphicsContext.VideoWindow.Top;
          _VideoWidth = GUIGraphicsContext.VideoWindow.Width;
          _VideoHeight = GUIGraphicsContext.VideoWindow.Height;

          // Re-Add the Viswindow to the Mainform Control (It got removed on a manual Stop)
          SetVisualizationWindow();
          SetVideoWindow();

          PlayState oldState = _state;
          _state = PlayState.Playing;

          if (oldState != _state && PlaybackStateChanged != null)
          {
            PlaybackStateChanged(this, oldState, _state);
          }

          if (PlaybackStart != null)
          {
            PlaybackStart(this, stream.TotalStreamSeconds);
          }
        }

        else
        {
          Log.Error("BASS: Unable to play {0}.  Reason: {1}.", filePath,
                    Enum.GetName(typeof(BASSError), Bass.BASS_ErrorGetCode()));

          stream.Dispose();
          result = false;
        }
      }
      catch (Exception ex)
      {
        result = false;
        Log.Error("BASS: Play caused an exception:  {0}.", ex);
      }

      return result;
    }