public void QueueSection(string streamKey, string sectionKey)
        {
            var audioStream = GetAudioStream(streamKey);

            if (audioStream == null)
            {
                throw new Exception("Cannot find stream " + streamKey);
            }

            var audioSection = GetAudioSection(streamKey, sectionKey);

            if (audioSection == null)
            {
                throw new Exception("Cannot find section " + sectionKey + " for stream " + streamKey);
            }

            if (!audioSection.HasStartAndEnd)
            {
                throw new Exception("Cannot find start and end for section " + sectionKey + " for stream " + streamKey);
            }

            var startPosition = audioSection.HasOffset
                ? audioSection.Offset.Position + 500
                : audioSection.Start.Position;

            AudioStreamHelper.SetPosition(audioStream, startPosition);
            SetSectionTempo(audioStream, audioSection);
        }
 private void OnSectionEnd(AudioSection audioSection, AudioStreamSection streamSection)
 {
     if (audioSection.LoopIndefinitely || audioSection.HasOffset)
     {
         AudioStreamHelper.SetPosition(streamSection.AudioStream, audioSection.Start.Position);
         ApplySyncIfOneExistsForCurrentPosition(streamSection.AudioStream);
     }
     else
     {
         PlayNextSection(audioSection, streamSection);
     }
 }
        /// <summary>
        ///     Starts playing the raw loop track at the offset of the raw-loop section.
        /// </summary>
        public void PlayRawLoop()
        {
            if (RawLoopTrack == null)
            {
                return;
            }

            // DebugHelper.WriteLine("Playing in raw-loop mode");

            AudioStreamHelper.Pause(RawLoopTrack);
            AudioStreamHelper.SetPosition(RawLoopTrack, RawLoopTrack.RawLoopOffset);
            AudioStreamHelper.Play(RawLoopTrack);
        }
        public AudioStream Load(string streamKey, string filename)
        {
            if (!File.Exists(filename))
            {
                throw new Exception("Cannot find file " + filename);
            }
            if (GetStreamSection(streamKey) != null)
            {
                throw new Exception("AudioStream already exists with streamKey " + streamKey);
            }

            var audioStream = new Sample
            {
                Filename = filename
            };

            var tags = TagHelper.LoadTags(filename);

            if (tags != null)
            {
                if (tags.Gain.HasValue)
                {
                    audioStream.Gain = tags.Gain.Value;
                }
                if (tags.Bpm.HasValue)
                {
                    audioStream.Bpm = tags.Bpm.Value;
                }

                audioStream.Description = TrackHelper.GuessTrackDescription(filename, tags.Artist, tags.Title);
            }

            AudioStreamHelper.LoadAudio(audioStream);

            audioStream.SyncProc = OnSync;
            AudioStreamHelper.AddToMixer(audioStream, Output);
            AudioStreamHelper.SetPosition(audioStream, 0);

            lock (_streamSections)
            {
                _streamSections.Add(new AudioStreamSection
                {
                    Key           = streamKey,
                    AudioStream   = audioStream,
                    AudioSections = new List <AudioSection>()
                });
            }

            return(audioStream);
        }
        /// <summary>
        ///     Sets the raw loop positions.
        /// </summary>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        /// <param name="offset">The offset.</param>
        public void SetRawLoopPositions(long start, long end, long offset)
        {
            if (RawLoopTrack == null)
            {
                return;
            }
            if (start < 0 || end > RawLoopTrack.Length)
            {
                return;
            }
            if (end <= 0 || end <= start)
            {
                return;
            }

            var maxEnd = RawLoopTrack.Length - 500;

            if (end > maxEnd)
            {
                end = maxEnd;
            }

            if (offset < start || offset > end)
            {
                offset = start;
            }

            var track = RawLoopTrack;

            track.RawLoopStart  = start;
            track.RawLoopEnd    = end;
            track.RawLoopOffset = offset;

            AudioStreamHelper.SetPosition(track, track.RawLoopOffset);

            ClearTrackSyncPositions(track);

            // set track syncs
            SetTrackSync(track, track.Length, SyncType.TrackEnd);
            SetTrackSync(track, track.RawLoopEnd, SyncType.EndRawLoop);
        }
        public void ForceFadeNow(ForceFadeType fadeType)
        {
            if (PlayState != PlayState.Playing)
            {
                return;
            }

            if (NextTrack == null || CurrentTrack == null)
            {
                return;
            }

            if (fadeType == ForceFadeType.SkipToEnd)
            {
                AudioStreamHelper.SetPosition(CurrentTrack,
                                              CurrentTrack.FadeOutStart - CurrentTrack.SecondsToSamples(0.01M));
            }
            else
            {
                CurrentForceFadeType = fadeType;
                IsForceFadeNowMode   = true;

                var length = 0D;
                switch (fadeType)
                {
                case ForceFadeType.Cut:
                    length = GetCutFadeLength(CurrentTrack);
                    break;

                case ForceFadeType.PowerDown:
                    length = GetPowerDownFadeLength(CurrentTrack);
                    CurrentTrack.PowerDownOnEnd = true;
                    break;

                case ForceFadeType.QuickFade:
                    length = GetCutFadeLength(CurrentTrack);
                    break;

                case ForceFadeType.SkipToEnd:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(fadeType), fadeType, null);
                }

                if (CurrentTrack.FadeOutLengthSeconds < length)
                {
                    length = CurrentTrack.FadeOutLengthSeconds;
                }

                CurrentTrack.EndLoopCount = 0;

                BassMix.BASS_Mixer_ChannelRemoveSync(CurrentTrack.ChannelId, CurrentTrack.FadeOutStartSyncId);
                CurrentTrack.FadeOutStartSyncId = int.MinValue;
                BassMix.BASS_Mixer_ChannelRemoveSync(CurrentTrack.ChannelId, CurrentTrack.FadeOutEndSyncId);
                CurrentTrack.FadeOutEndSyncId = int.MinValue;

                var position = AudioStreamHelper.GetPosition(CurrentTrack);
                CurrentTrack.FadeOutStart = position + CurrentTrack.SecondsToSamples(0.05M);
                SetTrackSync(CurrentTrack, CurrentTrack.FadeOutStart, SyncType.StartFadeOut);

                CurrentTrack.FadeOutEnd = CurrentTrack.FadeOutStart + CurrentTrack.SecondsToSamples(length);
                SetTrackSync(CurrentTrack, CurrentTrack.FadeOutEnd, SyncType.EndFadeOut);
            }
        }