<commit_before>﻿using System;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.UI;

public class PlaybackPositionPresenter : MonoBehaviour
{
    [SerializeField]
    CanvasEvents canvasEvents;
    [SerializeField]
    Slider playbackPositionController;
    [SerializeField]
    Text playbackTimeDisplayText;

    NotesEditorModel model;

    void Awake()
    {
        model = NotesEditorModel.Instance;
        model.OnLoadedMusicObservable.First().Subscribe(_ => Init());
    }

    void Init()
    {
        model.Audio.ObserveEveryValueChanged(audio => audio.clip.samples)
            .Subscribe(samples => playbackPositionController.maxValue = samples);


        // Input -> Audio timesamples -> Model timesamples -> UI

        // Input (scroll pad)
        var operateScrollPadObservable = this.UpdateAsObservable()
            .SkipUntil(canvasEvents.WaveformRegionOnMouseDownObservable
                .Where(_ => !Input.GetMouseButtonDown(1))
                .Where(_ => 0 > model.ClosestNotePosition.Value.ToSamples(model.Audio.clip.frequency)))
            .TakeWhile(_ => !Input.GetMouseButtonUp(0))
            .Select(_ => Input.mousePosition.x)
            .Buffer(2, 1).Where(b => 2 <= b.Count)
            .RepeatSafe()
            .Select(b => (b[0] - b[1])
                / model.CanvasWidth.Value
                * model.CanvasScaleFactor.Value
                * model.Audio.clip.samples)
            .Select(delta => model.Audio.timeSamples + delta);

        model.IsOperatingPlaybackPositionDuringPlay = canvasEvents.WaveformRegionOnMouseDownObservable
            .Where(_ => model.IsPlaying.Value)
            .Select(_ => !(model.IsPlaying.Value = false))
            .Merge(this.UpdateAsObservable()
                .Where(_ => model.IsOperatingPlaybackPositionDuringPlay.Value)
                .Where(_ => Input.GetMouseButtonUp(0))
                .Select(_ => !(model.IsPlaying.Value = true)))
            .ToReactiveProperty();

        // Input (mouse scroll wheel)
        var operateMouseScrollWheelObservable = canvasEvents.MouseScrollWheelObservable
            .Where(_ => !KeyInput.CtrlKey())
            .Select(delta => model.Audio.clip.samples / 100f * -delta)
            .Select(deltaSamples => model.Audio.timeSamples + deltaSamples);

        operateMouseScrollWheelObservable.Where(_ => model.IsPlaying.Value)
            .Do(_ => model.IsOperatingPlaybackPositionDuringPlay.Value = true)
            .Subscribe(_ => model.IsPlaying.Value = false);

        operateMouseScrollWheelObservable.Throttle(TimeSpan.FromMilliseconds(350))
            .Where(_ => model.IsOperatingPlaybackPositionDuringPlay.Value)
            .Do(_ => model.IsOperatingPlaybackPositionDuringPlay.Value = false)
            .Subscribe(_ => model.IsPlaying.Value = true);

        // Input (slider)
        var operatePlayPositionSliderObservable = playbackPositionController.OnValueChangedAsObservable()
            .DistinctUntilChanged();


        // Input -> Audio timesamples
        Observable.Merge(
                operateScrollPadObservable,
                operateMouseScrollWheelObservable,
                operatePlayPositionSliderObservable)
            .Select(timeSamples => Mathf.FloorToInt(timeSamples))
            .Select(timeSamples => Mathf.Clamp(timeSamples, 0, model.Audio.clip.samples - 1))
            .Subscribe(timeSamples => model.Audio.timeSamples = timeSamples);


        // Audio timesamples -> Model timesamples
        model.Audio.ObserveEveryValueChanged(audio => audio.timeSamples)<commit_msg>Add shortcut of move playback position by left/right arrow key<commit_after>﻿using System;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.UI;

public class PlaybackPositionPresenter : MonoBehaviour
{
    [SerializeField]
    CanvasEvents canvasEvents;
    [SerializeField]
    Slider playbackPositionController;
    [SerializeField]
    Text playbackTimeDisplayText;

    NotesEditorModel model;

    void Awake()
    {
        model = NotesEditorModel.Instance;
        model.OnLoadedMusicObservable.First().Subscribe(_ => Init());
    }

    void Init()
    {
        model.Audio.ObserveEveryValueChanged(audio => audio.clip.samples)
            .Subscribe(samples => playbackPositionController.maxValue = samples);


        // Input -> Audio timesamples -> Model timesamples -> UI

        // Input (arrow key)
        var operateArrowKeyObservable = Observable.Merge(
                this.UpdateAsObservable().Where(_ => Input.GetKey(KeyCode.LeftArrow)).Select(_ => 7),
                this.UpdateAsObservable().Where(_ => Input.GetKey(KeyCode.RightArrow)).Select(_ => -7))
            .Select(delta => delta * (KeyInput.CtrlKey() ? 5 : 1))
            .Select(delta => delta
                / model.CanvasWidth.Value
                * model.CanvasScaleFactor.Value
                * model.Audio.clip.samples)
            .Select(deltaSamples => model.Audio.timeSamples + deltaSamples);

        // Input (scroll pad)
        var operateScrollPadObservable = this.UpdateAsObservable()
            .SkipUntil(canvasEvents.WaveformRegionOnMouseDownObservable
                .Where(_ => !Input.GetMouseButtonDown(1))
                .Where(_ => 0 > model.ClosestNotePosition.Value.ToSamples(model.Audio.clip.frequency)))
            .TakeWhile(_ => !Input.GetMouseButtonUp(0))
            .Select(_ => Input.mousePosition.x)
            .Buffer(2, 1).Where(b => 2 <= b.Count)
            .RepeatSafe()
            .Select(b => (b[0] - b[1])
                / model.CanvasWidth.Value
                * model.CanvasScaleFactor.Value
                * model.Audio.clip.samples)
            .Select(delta => model.Audio.timeSamples + delta);

        model.IsOperatingPlaybackPositionDuringPlay = canvasEvents.WaveformRegionOnMouseDownObservable
            .Where(_ => model.IsPlaying.Value)
            .Select(_ => !(model.IsPlaying.Value = false))
            .Merge(this.UpdateAsObservable()
                .Where(_ => model.IsOperatingPlaybackPositionDuringPlay.Value)
                .Where(_ => Input.GetMouseButtonUp(0))
                .Select(_ => !(model.IsPlaying.Value = true)))
            .ToReactiveProperty();

        // Input (mouse scroll wheel)
        var operateMouseScrollWheelObservable = canvasEvents.MouseScrollWheelObservable
            .Where(_ => !KeyInput.CtrlKey())
            .Select(delta => model.Audio.clip.samples / 100f * -delta)
            .Select(deltaSamples => model.Audio.timeSamples + deltaSamples);

        operateMouseScrollWheelObservable.Where(_ => model.IsPlaying.Value)
            .Do(_ => model.IsOperatingPlaybackPositionDuringPlay.Value = true)
            .Subscribe(_ => model.IsPlaying.Value = false);

        operateMouseScrollWheelObservable.Throttle(TimeSpan.FromMilliseconds(350))
            .Where(_ => model.IsOperatingPlaybackPositionDuringPlay.Value)
            .Do(_ => model.IsOperatingPlaybackPositionDuringPlay.Value = false)
            .Subscribe(_ => model.IsPlaying.Value = true);

        // Input (slider)
        var operatePlayPositionSliderObservable = playbackPositionController.OnValueChangedAsObservable()
            .DistinctUntilChanged();


        // Input -> Audio timesamples
        Observable.Merge(
                operateArrowKeyObservable,
                operateScrollPadObservable,
                operateMouseScrollWheelObservable,
                operatePlayPositionSliderObservable)
            .Select(timeSamples => Mathf.FloorToInt(timeSamples))
            .Select(timeSamples => Mathf.Clamp(timeSamples, 0, model.Audio.clip.samples - 1))
            .Subscribe(timeSamples => model.Audio.timeSamples = timeSamples);


        // Audio timesamples -> Model timesamples
        model.Audio.ObserveEveryValueChanged(audio => audio.timeSamples)
{'fraction_non_alphanumeric': 0.10607411180440596, 'fraction_numerical': 0.004838915064306635, 'mean_word_length': 2.478299379982285, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 78, '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': '1206786', 'n_tokens_mistral': 2160, 'n_tokens_neox': 2032, 'n_words': 338}