<commit_before>using System;
using System.Threading;
using System.Threading.Tasks;
using Anotar.NLog;
using NAudio.Wave;
using SOVND.Client.Audio;
using SOVND.Client.Util;
using SpotifyClient;

namespace SOVND.Client.Modules
{
    public class NowPlayingHandler : SOVNDModule
    {
        private Track playingTrack;
        private CancellationTokenSource songToken;
        private SpotifyTrackDataPipe streamingaudio;
        private readonly string _channel;

        private readonly DateTime UnixTimeBase = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

        public NowPlayingHandler(AuthPair auth, string channelName) : base(auth)
        {
            // TODO Use channel/nowplaying/starttime to seek to correct position
            // TODO Convert nowplaying to a JSON object so songid and playtime come in at the same time?

            _channel = channelName;
            streamingaudio = new SpotifyTrackDataPipe();

            On["/" + channelName + "/nowplaying/songid"] = _ =>
            {
                string song = _.Message;
                if (string.IsNullOrWhiteSpace(song))
                {
                    playingTrack = null;
                    LogTo.Warn("Server asked to play empty song on channel {0}", _channel);
                    OnStop(); // TODO Danger danger
                    return;
                }
                int time = 0;

                //PlaySong(song, UnixTimeBase.AddSeconds(time).ToLocalTime());
                Task.Run(() => PlaySong(song));
            };

            Run();
        }

        private async Task PlaySong(string songID)
        {
            await PlaySong(songID, DateTime.MinValue);
        }

        private async Task PlaySong(string songID, DateTime startTime)
        {
            LogTo.Debug("Playing: {0}", songID);

            if (playingTrack?.SongID == songID)
                return;

            if (streamingaudio != null)
            {
                streamingaudio.DoComplete();
                streamingaudio = new SpotifyTrackDataPipe();
            }

            playingTrack = new Track(songID);

            WaveOut player = new WaveOut(App.WindowHandle);

            LogTo.Trace("Streaming");

            streamingaudio.StartStreaming(startTime, playingTrack.TrackPtr, provider =>
            {
                player.Init(provider);
                player.Play();
            });

            streamingaudio.OnComplete = () =>
            {
                LogTo.Trace("Done streaming");

                streamingaudio.StopStreaming();
                player.Pause();
                player.Dispose();
            };
        }

        protected override void OnStop()
        {
            if (streamingaudio != null)
                streamingaudio.DoComplete();
            streamingaudio = null;
        }
    }
}<commit_msg>Playing is still broken. Suspect server.<commit_after>using System;
using System.Threading;
using System.Threading.Tasks;
using Anotar.NLog;
using NAudio.Wave;
using SOVND.Client.Audio;
using SOVND.Client.Util;
using SpotifyClient;

namespace SOVND.Client.Modules
{
    public class NowPlayingHandler : SOVNDModule
    {
        private Track playingTrack;
        private CancellationTokenSource songToken;
        private SpotifyTrackDataPipe streamingaudio;
        private readonly string _channel;

        private readonly DateTime UnixTimeBase = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

        public NowPlayingHandler(AuthPair auth, string channelName) : base(auth)
        {
            // TODO Use channel/nowplaying/starttime to seek to correct position
            // TODO Convert nowplaying to a JSON object so songid and playtime come in at the same time?

            _channel = channelName;
            
            On["/" + channelName + "/nowplaying/songid"] = _ =>
            {
                string song = _.Message;
                StopStreaming();

                if (string.IsNullOrWhiteSpace(song))
                {
                    playingTrack = null;
                    LogTo.Warn("Server asked to play empty song on channel {0}", _channel);
                    return;
                }

                int time = 0;

                //PlaySong(song, UnixTimeBase.AddSeconds(time).ToLocalTime());
                Task.Run(() => PlaySong(song));
            };

            Run();
        }

        private void PlaySong(string songID)
        {
            PlaySong(songID, DateTime.MinValue);
        }

        private void PlaySong(string songID, DateTime startTime)
        {
            LogTo.Debug("Playing: {0}", songID);
            if (playingTrack?.SongID == songID)
                return;

            if (streamingaudio != null)
                streamingaudio.DoComplete();

            streamingaudio = new SpotifyTrackDataPipe();
            playingTrack = new Track(songID);
            WaveOut player = new WaveOut(App.WindowHandle);

            LogTo.Trace("Streaming");

            streamingaudio.StartStreaming(startTime, playingTrack.TrackPtr, provider =>
            {
                player.Init(provider);
                player.Play();
            });

            streamingaudio.OnComplete = () =>
            {
                LogTo.Trace("Done streaming");

                streamingaudio.StopStreaming();
                player.Pause();
                player.Dispose();
            };
        }

        public void StopStreaming()
        {
            if (streamingaudio != null)
                streamingaudio.DoComplete();
            streamingaudio = null;
        }

        protected override void OnStop()
        {
            StopStreaming();
        }
    }
}
{'fraction_non_alphanumeric': 0.08941382327209099, 'fraction_numerical': 0.004549431321084864, 'mean_word_length': 1.61961503208066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '356187', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1393, 'n_words': 350}