Beispiel #1
0
        async void LoadScaleFromWave(string filePath)
        {
            PauseAudio();

            MainView.Progress = 0.0;
            Lock = true;
            MainView.MessageText = LangResources.ProgressMessage_LoadScale;

            await Task.Run(() =>
            {
                try
                {
                    var wave             = Wavefile.Read(filePath);
                    var selectedOperator = SelectedOperator;
                    var aac = CacheFile.FindCache <AnalyzedAudioCache>(wave.Hash + selectedOperator.GetType().FullName)
                              .GetOrElse(() =>
                    {
                        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(selectedOperator.GetType(), aa, wave.Data.Length, wave.Fs, wave.Hash);
                        CacheFile.SaveCache(result, wave.Hash + selectedOperator.GetType().FullName);
                        return(result);
                    });

                    Dispatcher.Invoke(() =>
                    {
                        MainView.Progress = 1000.0;
                        ApplyScale(aac.AnalyzedAudio.F0);
                        Lock = false;
                    });
                }
                catch (InvalidDataException)
                {
                    MessageBox.ShowWarning(LangResources.Error_UnsupportedWaveFile, LangResources.MessageBoxTitle_CannotLoadWaveFile);

                    Dispatcher.Invoke(() =>
                    {
                        Lock = false;
                    });
                }
                catch (Exception e)
                {
                    MessageBox.ShowWarning(LangResources.Error_CannodLoadWaveFile, exception: e);

                    Dispatcher.Invoke(() =>
                    {
                        Lock = false;
                    });
                }
            });
        }
Beispiel #2
0
        async void LoadWave(string filePath)
        {
            if (Player?.PlaybackState == PlaybackState.Playing)
            {
                PauseAudio();
            }

            await Task.Run(() =>
            {
                try
                {
                    WaveData = Wavefile.Read(filePath);

                    var waveLineCache = WaveLineCache.CreateCache(WaveData.Data, WaveData.Fs, WaveData.Hash);

                    Dispatcher.Invoke(() =>
                    {
                        MainView.Wave        = waveLineCache;
                        MainView.SampleRange = 0.To(30000);

                        SeekPlayer = new SeekPlayer(WaveData.Fs);

                        Player                  = new WavePlayer(WaveData.Fs);
                        Player.EnableLoop       = true;
                        Player.PlaybackStopped += (s, ea) =>
                        {
                            Player.SeekToStart();
                            MainView.IndicatorPosition = Player.GetCurrentSample();
                        };
                        PreviewStream = new RawWaveStream(WaveData.Data);
                        Player.SetStream(PreviewStream);

                        MainView.Progress    = 25.0;
                        MainView.MessageText = LangResources.ProgressMessage_AnalyzingWave;
                    });

                    var selectedOperator = SelectedOperator;
                    AnalyzedAudio        = CacheFile.FindCache <AnalyzedAudioCache>(WaveData.Hash + selectedOperator.GetType().FullName + ApplicationSettings.Setting.PitchOperation.FramePeriod)
                                           .GetOrElse(() =>
                    {
                        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(selectedOperator.GetType(), aa, WaveData.Data.Length, WaveData.Fs, WaveData.Hash);
                        CacheFile.SaveCache(result, WaveData.Hash + selectedOperator.GetType().FullName + ApplicationSettings.Setting.PitchOperation.FramePeriod);
                        return(result);
                    });

                    Dispatcher.Invoke(() =>
                    {
                        MainView.AudioScale         = new AudioScaleModel(AnalyzedAudio.AnalyzedAudio.F0, AnalyzedAudio.AnalyzedAudio.FramePeriod, AnalyzedAudio.SampleCount, AnalyzedAudio.SampleRate);
                        MainView.EditableAudioScale = new AudioScaleModel(AnalyzedAudio.AnalyzedAudio.F0, AnalyzedAudio.AnalyzedAudio.FramePeriod, AnalyzedAudio.SampleCount, AnalyzedAudio.SampleRate);
                        Lock = false;
                    });
                }
                catch (InvalidDataException)
                {
                    MessageBox.ShowWarning(LangResources.Error_UnsupportedWaveFile, LangResources.MessageBoxTitle_CannotLoadWaveFile);

                    Dispatcher.Invoke(() =>
                    {
                        Player?.Stop();
                        Player?.Dispose();
                        MainView.Wave       = null;
                        MainView.AudioScale = null;
                        Lock = false;
                    });
                }
                catch (Exception e)
                {
                    MessageBox.ShowWarning(LangResources.Error_CannodLoadWaveFile, exception: e);

                    Dispatcher.Invoke(() =>
                    {
                        Player?.Stop();
                        Player?.Dispose();
                        MainView.Wave       = null;
                        MainView.AudioScale = null;
                        Lock = false;
                    });
                }
            });
        }