Skip to content

Commit

Permalink
can change operator plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mes51 committed Nov 8, 2017
1 parent 2e8cdc6 commit 8944bb0
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 28 deletions.
10 changes: 7 additions & 3 deletions Intervallo/Command/ActionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ public class ActionCommand : CommandBase
{
public ActionCommand(MainWindow window, Action action) : this(window, action, false) { }

public ActionCommand(MainWindow window, Action action, bool forceEnable) : base(window)
public ActionCommand(MainWindow window, Action action, bool forceEnable) : this(window, (parameter) => action(), forceEnable) { }

public ActionCommand(MainWindow window, Action<object> action) : this(window, action, false) { }

public ActionCommand(MainWindow window, Action<object> action, bool forceEnable) : base(window)
{
Action = action;
ForceEnable = forceEnable;
}

Action Action { get; }
Action<object> Action { get; }

bool ForceEnable { get; }

Expand All @@ -28,7 +32,7 @@ public override bool CanExecute(object parameter)

public override void Execute(object parameter)
{
Action();
Action(parameter);
}
}
}
3 changes: 3 additions & 0 deletions Intervallo/Config/PitchOperationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public class PitchOperationSettings
{
[DataMember(Name = "framePeriod")]
public double FramePeriod { get; set; } = 5.0;

[DataMember(Name = "useOperatorName")]
public string UseOperatorName { get; set; } = "";
}
}
1 change: 1 addition & 0 deletions Intervallo/Form/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<MenuItem Header="{x:Static p:LangResources.MainMenu_ToolMenu}">
<MenuItem Header="{x:Static p:LangResources.MainMenu_ClearCache}" Command="{Binding ElementName=Window, Path=ClearCacheCommand}"/>
<Separator />
<MenuItem Name="UseOperatorMenu" Header="{x:Static p:LangResources.Menu_UseOperator}" />
<MenuItem Header="{x:Static p:LangResources.MainMenu_Option}" Command="{Binding ElementName=Window, Path=OptionCommand}"/>
</MenuItem>
<MenuItem Header="{x:Static p:LangResources.MainMenu_HelpMenu}">
Expand Down
90 changes: 65 additions & 25 deletions Intervallo/Form/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public partial class MainWindow : Window
public CommandBase ExitCommand { get; }
public CommandBase LoadScaleCommand { get; }
public CommandBase LoadScaleFromWaveCommand { get; }
public CommandBase UseOperatorCommand { get; }
public CommandBase PreviewCommand { get; }
public CommandBase ClearCacheCommand { get; }
public CommandBase OptionCommand { get; }
Expand All @@ -51,14 +52,25 @@ public MainWindow()
ExitCommand = new ActionCommand(this, () => Close(), true);
LoadScaleCommand = new LoadScaleCommand(this, true);
LoadScaleFromWaveCommand = new LoadScaleCommand(this, false);
UseOperatorCommand = new ActionCommand(this, (plugin) =>
{
var name = plugin.GetType().FullName;
foreach (var item in UseOperatorMenu.Items.Cast<MenuItem>())
{
item.IsChecked = item.CommandParameter.GetType().FullName == name;
}
ApplicationSettings.Setting.PitchOperation.UseOperatorName = name;
});
PreviewCommand = new PreviewCommand(this);
ClearCacheCommand = new ActionCommand(this, () => CacheFile.ClearChaceFile(), true);
OptionCommand = new ActionCommand(this, () => {
OptionCommand = new ActionCommand(this, () =>
{
var window = new OptionWindow();
window.Owner = this;
window.ShowDialog();
}, true);
AboutCommand = new ActionCommand(this, () => {
AboutCommand = new ActionCommand(this, () =>
{
var window = new AboutWindow();
window.Owner = this;
window.ShowDialog();
Expand Down Expand Up @@ -97,6 +109,8 @@ public MainWindow()
[ImportMany]
List<IAudioOperator> AudioOperatorPlugins { get; set; }

IAudioOperator SelectedOperator => AudioOperatorPlugins.Find((p) => p.GetType().FullName == ApplicationSettings.Setting.PitchOperation.UseOperatorName);

[ImportMany]
List<IScaleLoader> ScaleLoaderPlugins { get; set; }

Expand All @@ -109,18 +123,6 @@ void LoadPlugin()
{
container.ComposeParts(this);
}

ScaleLoaderPlugins.Sort((a, b) => b.PluginName.CompareTo(a.PluginName));
foreach (var loader in ScaleLoaderPlugins)
{
var item = new MenuItem();
item.Header = loader.PluginName + "...";
item.Command = LoadScaleCommand;
item.CommandParameter = loader;
item.CommandTarget = this;
LoadScaleMenu.Items.Insert(0, item);
}
ScaleLoaderPlugins.Reverse();
}
catch (Exception e)
{
Expand All @@ -132,11 +134,47 @@ void LoadPlugin()
MessageBox.ShowError(LangResources.Fatal_CannotLoadAudioOperator, LangResources.MessageBoxTitle_CannotLoadAudioOperator);
Close();
}
else if ((ScaleLoaderPlugins?.Count ?? 0) < 1)
else
{
AudioOperatorPlugins.Sort((a, b) => a.PluginName.CompareTo(b.PluginName));
foreach (var op in AudioOperatorPlugins)
{
var item = new MenuItem();
item.Header = op.PluginName;
item.IsCheckable = true;
item.IsChecked = ApplicationSettings.Setting.PitchOperation.UseOperatorName == op.GetType().FullName;
item.Command = UseOperatorCommand;
item.CommandParameter = op;
item.CommandTarget = this;
UseOperatorMenu.Items.Add(item);
}

if (string.IsNullOrEmpty(ApplicationSettings.Setting.PitchOperation.UseOperatorName) || !UseOperatorMenu.Items.Cast<MenuItem>().Any((i) => i.IsChecked))
{
ApplicationSettings.Setting.PitchOperation.UseOperatorName = AudioOperatorPlugins[0].GetType().FullName;
((MenuItem)UseOperatorMenu.Items[0]).IsChecked = true;
}
}

if ((ScaleLoaderPlugins?.Count ?? 0) < 1)
{
MessageBox.ShowWarning(LangResources.Error_CannotLoadScaleLoader, LangResources.MessageBoxTitle_CannotLoadScaleLoader);
ScaleLoaderPlugins = new List<IScaleLoader>();
}
else
{
ScaleLoaderPlugins.Sort((a, b) => b.PluginName.CompareTo(a.PluginName));
foreach (var loader in ScaleLoaderPlugins)
{
var item = new MenuItem();
item.Header = loader.PluginName + "...";
item.Command = LoadScaleCommand;
item.CommandParameter = loader;
item.CommandTarget = this;
LoadScaleMenu.Items.Insert(0, item);
}
ScaleLoaderPlugins.Reverse();
}
}

#region TODO: Move to ModelView
Expand Down Expand Up @@ -217,18 +255,19 @@ await Task.Run(() =>
MainView.MessageText = LangResources.ProgressMessage_AnalyzingWave;
});

AnalyzedAudio = CacheFile.FindCache<AnalyzedAudioCache>(WaveData.Hash + AudioOperatorPlugins[0].GetType().FullName + ApplicationSettings.Setting.PitchOperation.FramePeriod)
var selectedOperator = SelectedOperator;
AnalyzedAudio = CacheFile.FindCache<AnalyzedAudioCache>(WaveData.Hash + selectedOperator.GetType().FullName + ApplicationSettings.Setting.PitchOperation.FramePeriod)
.GetOrElse(() =>
{
var aa = AudioOperatorPlugins[0].Analyze(new WaveData(WaveData.Data, WaveData.Fs), ApplicationSettings.Setting.PitchOperation.FramePeriod, (p) =>
var aa = selectedOperator.Analyze(new WaveData(WaveData.Data, WaveData.Fs), ApplicationSettings.Setting.PitchOperation.FramePeriod, (p) =>
{
Dispatcher.Invoke(() =>
{
MainView.Progress = p * 0.75 + 25.0;
});
});
var result = new AnalyzedAudioCache(AudioOperatorPlugins[0].GetType(), aa, WaveData.Data.Length, WaveData.Fs, WaveData.Hash);
CacheFile.SaveCache(result, WaveData.Hash + AudioOperatorPlugins[0].GetType().FullName + ApplicationSettings.Setting.PitchOperation.FramePeriod);
var result = new AnalyzedAudioCache(selectedOperator.GetType(), aa, WaveData.Data.Length, WaveData.Fs, WaveData.Hash);
CacheFile.SaveCache(result, WaveData.Hash + selectedOperator.GetType().FullName + ApplicationSettings.Setting.PitchOperation.FramePeriod);
return result;
});

Expand Down Expand Up @@ -275,7 +314,7 @@ async void ExportWave(string filePath, double[] newF0)
await Task.Run(() =>
{
var edited = AnalyzedAudio.AnalyzedAudio.ReplaceF0(newF0);
var synthesizedAudio = AudioOperatorPlugins[0].Synthesize(edited, (p) =>
var synthesizedAudio = AudioOperatorPlugins.Find(p => p.GetType() == AnalyzedAudio.OperatorType).Synthesize(edited, (p) =>
{
Dispatcher.Invoke(() =>
{
Expand Down Expand Up @@ -324,7 +363,7 @@ void ApplyScale(double[] newScale)

PreviewStream.Dispose();
var edited = AnalyzedAudio.AnalyzedAudio.ReplaceF0(newF0);
PreviewStream = new WaveCacheStream(AudioOperatorPlugins[0].Synthesize(edited));
PreviewStream = new WaveCacheStream(AudioOperatorPlugins.Find(p => p.GetType() == AnalyzedAudio.OperatorType).Synthesize(edited));
Player.SetStream(PreviewStream);
}

Expand Down Expand Up @@ -374,18 +413,19 @@ await Task.Run(() =>
try
{
var wave = Wavefile.Read(filePath);
var aac = CacheFile.FindCache<AnalyzedAudioCache>(wave.Hash + AudioOperatorPlugins[0].GetType().FullName)
var selectedOperator = SelectedOperator;
var aac = CacheFile.FindCache<AnalyzedAudioCache>(wave.Hash + selectedOperator.GetType().FullName)
.GetOrElse(() =>
{
var aa = AudioOperatorPlugins[0].Analyze(new Plugin.WaveData(wave.Data, wave.Fs), ApplicationSettings.Setting.PitchOperation.FramePeriod, (p) =>
var aa = selectedOperator.Analyze(new Plugin.WaveData(wave.Data, wave.Fs), ApplicationSettings.Setting.PitchOperation.FramePeriod, (p) =>
{
Dispatcher.Invoke(() =>
{
MainView.Progress = p * 0.75 + 25.0;
});
});
var result = new AnalyzedAudioCache(AudioOperatorPlugins[0].GetType(), aa, wave.Data.Length, wave.Fs, wave.Hash);
CacheFile.SaveCache(result, wave.Hash + AudioOperatorPlugins[0].GetType().FullName);
var result = new AnalyzedAudioCache(selectedOperator.GetType(), aa, wave.Data.Length, wave.Fs, wave.Hash);
CacheFile.SaveCache(result, wave.Hash + selectedOperator.GetType().FullName);
return result;
});

Expand Down
9 changes: 9 additions & 0 deletions Intervallo/Properties/LangResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Intervallo/Properties/LangResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@
<data name="MainMenu_ToolMenu" xml:space="preserve">
<value>ツール(_T)</value>
</data>
<data name="Menu_UseOperator" xml:space="preserve">
<value>解析・生成プラグインの選択(_S)</value>
</data>
<data name="MessageBoxTitle_CannotLoadAudioOperator" xml:space="preserve">
<value>音声解析プラグインの読み込み失敗</value>
</data>
Expand Down

0 comments on commit 8944bb0

Please sign in to comment.