Skip to content

Commit

Permalink
Disable max min slider editing when AudioPlayer is playing
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyaku365 committed May 15, 2020
1 parent 29142c9 commit db34892
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
2 changes: 2 additions & 0 deletions StimmingSignalGenerator/AppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class AppState : ReactiveObject
public GeneratorModeType GeneratorMode { get => generatorMode; set => this.RaiseAndSetIfChanged(ref generatorMode, value); }
public bool IsHDPlot { get => isHDPlot; set => this.RaiseAndSetIfChanged(ref isHDPlot, value); }
public bool IsPlotEnable { get => isPlotEnable; set => this.RaiseAndSetIfChanged(ref isPlotEnable, value); }
public bool IsPlaying { get => isPlaying; set => this.RaiseAndSetIfChanged(ref isPlaying, value); }

private GeneratorModeType generatorMode;
private bool isHDPlot;
private bool isPlotEnable;
private bool isPlaying;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NAudio.CoreAudioApi;
using NAudio.Wave;
using ReactiveUI;
using Splat;
using StimmingSignalGenerator.Generators;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -29,21 +30,25 @@ public MMDevice AudioDevice
public ReactiveCommand<Unit, Unit> StopCommand { get; }

private readonly AudioPlayer audioPlayer;

public AppState AppState { get; }
public AudioPlayerViewModel(ISampleProvider sampleProvider)
{
AppState = Locator.Current.GetService<AppState>();

audioPlayer = new AudioPlayer(sampleProvider);
PlayCommand = ReactiveCommand.Create(() => Play()).DisposeWith(Disposables);
StopCommand = ReactiveCommand.Create(() => Stop()).DisposeWith(Disposables);
}

public void Play()
{
AppState.IsPlaying = true;
audioPlayer.Play();
}

public void Stop()
{
AppState.IsPlaying = false;
audioPlayer.Stop();
}

Expand Down
65 changes: 35 additions & 30 deletions StimmingSignalGenerator/MVVM/ViewModels/ControlSliderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
using System.Collections.Generic;
using System.Text;
using ReactiveUI;

using Splat;

namespace StimmingSignalGenerator.MVVM.ViewModels
{
class ControlSliderViewModel : ViewModelBase
{
private double _value;
private double minValue;
private double maxValue;
private double tickFrequency;
private double smallChange;
private double largeChange;

public AppState AppState { get; }
public double Value { get => _value; set => this.RaiseAndSetIfChanged(ref _value, value); }
public double MinValue { get => minValue; set => this.RaiseAndSetIfChanged(ref minValue, value); }
public double MaxValue
{
get => maxValue; set
{
this.RaiseAndSetIfChanged(ref maxValue, value);
AdjustStepChange();
}
}
public double TickFrequency { get => tickFrequency; set => this.RaiseAndSetIfChanged(ref tickFrequency, value); }
public double SmallChange { get => smallChange; set => this.RaiseAndSetIfChanged(ref smallChange, value); }
public double LargeChange { get => largeChange; set => this.RaiseAndSetIfChanged(ref largeChange, value); }

public const double BasicSignalFreqMin = 300;
public const double Tick = 1;
public const double SmallTick = 0.01;
Expand All @@ -29,6 +37,8 @@ public static ControlSliderViewModel Vol(double initValue = 1) =>
public ControlSliderViewModel() : this(440, 0, 10000, 1, 10, 50) { }
public ControlSliderViewModel(double value, double minValue, double maxValue, double tickFrequency, double smallChange, double largeChange)
{
AppState = Locator.Current.GetService<AppState>();

Value = value;
MinValue = minValue;
MaxValue = maxValue;
Expand All @@ -37,32 +47,27 @@ public ControlSliderViewModel(double value, double minValue, double maxValue, do
LargeChange = largeChange;
}

public double Value { get => _value; set => this.RaiseAndSetIfChanged(ref _value, value); }
public double MinValue { get => minValue; set => this.RaiseAndSetIfChanged(ref minValue, value); }
public double MaxValue
private double _value;
private double minValue;
private double maxValue;
private double tickFrequency;
private double smallChange;
private double largeChange;
private void AdjustStepChange()
{
get => maxValue; set
if (MaxValue <= 1)
{
this.RaiseAndSetIfChanged(ref maxValue, value);
if (MaxValue <= 1)
{
TickFrequency = SmallChange = LargeChange = SuperSmallTick;
}
else if (MaxValue < 20)
{
TickFrequency = SmallChange = LargeChange = SmallTick;
}
else
{
TickFrequency = SmallChange = LargeChange = Tick;
}
TickFrequency = SmallChange = LargeChange = SuperSmallTick;
}
else if (MaxValue < 20)
{
TickFrequency = SmallChange = LargeChange = SmallTick;
}
else
{
TickFrequency = SmallChange = LargeChange = Tick;
}
}

public double TickFrequency { get => tickFrequency; set => this.RaiseAndSetIfChanged(ref tickFrequency, value); }
public double SmallChange { get => smallChange; set => this.RaiseAndSetIfChanged(ref smallChange, value); }
public double LargeChange { get => largeChange; set => this.RaiseAndSetIfChanged(ref largeChange, value); }

public static ControlSliderViewModel FromPOCO(POCOs.ControlSlider poco)
=> new ControlSliderViewModel().SetToPOCO(poco);
public ControlSliderViewModel SetToPOCO(POCOs.ControlSlider poco)
Expand Down
6 changes: 5 additions & 1 deletion StimmingSignalGenerator/MVVM/Views/ControlSliderView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<NumericUpDown Grid.Column="0" ShowButtonSpinner="False"
<!-- Disable max min editing to prevent value clip and jump when playing -->
<!-- Disable max min editing can be remove if avalonia implement UpdateSourceTrigger=LostFocus -->
<NumericUpDown Grid.Column="0" ShowButtonSpinner="False"
IsEnabled="{Binding !AppState.IsPlaying}"
Increment="{Binding LargeChange}"
Value="{Binding MinValue,Mode=TwoWay}" />
<NumericUpDown Grid.Column="1" ShowButtonSpinner="False"
Expand All @@ -26,6 +29,7 @@
Maximum="{Binding MaxValue, Mode=OneWay}"
Value="{Binding Value,Mode=TwoWay}" />
<NumericUpDown Grid.Column="2" ShowButtonSpinner="False"
IsEnabled="{Binding !AppState.IsPlaying}"
Increment="{Binding LargeChange}"
Value="{Binding MaxValue,Mode=TwoWay}" />

Expand Down
6 changes: 3 additions & 3 deletions StimmingSignalGenerator/TODO.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

//TODO master vol
//TODO preset vol
//-TODO preset vol
//TODO signal phase shift
//TODO phase, frequency lock and sync between signal
//-TODO Serialize setting
Expand All @@ -10,8 +10,8 @@
//TODO default preset to load when app init
//TODO playlist
//TODO UpdateSourceTrigger=LostFocus in ControlSlider (wait for avalonia implement https://github.com/AvaloniaUI/Avalonia/issues/3754)
//TODO IsPlaying in AppState
//TODO disable max min slider changing when AudioPlayer IsPlaying
//-TODO IsPlaying in AppState
//-TODO disable max min slider changing when AudioPlayer IsPlaying

//-TODO Waveform visualization (OxyPlot)
//-TODO Exp with faster way to plot or optimize oxyplot(use Decimator to reduce datapoint)
Expand Down

0 comments on commit db34892

Please sign in to comment.