/// <summary> /// Hide all the panels and shows the gaming panel. /// </summary> /// <param name="playerName">The player name</param> /// <param name="initialBpm">The initial bpm</param> /// <param name="gameMode">The game mode selected</param> public void StartGame(string playerName, int initialBpm, INoteGenerator gameMode) { this.ClearView(); this.ShowPlayingView(); this._controller.PlayerName = playerName; this._controller.InitialBpm = initialBpm; this._playingPanel.GameMode = gameMode; this._playingPanel.StartGame(); }
/// <summary> /// Adds a track and returns the reference /// </summary> /// <param name="seq"></param> /// <param name="gen"></param> /// <returns>Reference of added track</returns> public Track Add(MelodySequence seq, INoteGenerator gen) { if (gen as DrumGenerator != null || gen.Instrument == PatchNames.Helicopter) return AddPercussionTrack(seq, gen); Track t = new Track(gen.Instrument, (byte)channelIndex++); t.AddSequence(seq); ActiveComposition.Tracks.Add(t); generators.Add(gen); if (OnCompositionChange != null) OnCompositionChange(this, new EventArgs()); return t; }
/// <summary> /// Used when the user click the confirm button to start new game /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Confirm(object sender, EventArgs e) { if (!(string.IsNullOrWhiteSpace(this._txtUsername.Text))) { string name = this._txtUsername.Text; int initialBpm = (int)this._txtInitialBpm.Value; if (this._gameModeSelection.SelectedItem != null) { INoteGenerator gameMode = (INoteGenerator)this._gameModeSelection.SelectedItem; this._mainView.StartGame(name, initialBpm, gameMode); } else { MessageBox.Show("E' necessario inserire la modalità per iniziare una partita!"); } } else { MessageBox.Show("E' necessario inserire il nome del giocatore per iniziare una partita!"); } }
void LoadInstrumentalData() { Generator = new InstrumentalGenerator(category); new Thread(() => { StartSpinner(); var g = Generator as InstrumentalGenerator; g.OnPercentage += gen_OnPercentage; instrBox.Dispatcher.Invoke(() => { instrBox.Items.Clear(); ListBoxItem boxItem = new ListBoxItem(); boxItem.Content = "Drums"; boxItem.Tag = -1; instrBox.Items.Add(boxItem); }); g.Initialize(); instrBox.Dispatcher.Invoke(() => { var instrs = g.GetInstruments(7); foreach (var i in instrs) { ListBoxItem boxItem = new ListBoxItem(); boxItem.Content = i.ToString(); boxItem.Tag = i; instrBox.Items.Add(boxItem); } }); StopSpinner(); }).Start(); }
private void Generate() { int index = optionsTab.SelectedIndex; progressGenSlider.Value = 0; if (index == 0) { if (geneticInstrumentBox.SelectedItem == null) return; fitnessPlot.ResetAllAxes(); SetupLinePlot(fitnessPlot, "Average Fitness"); (fitnessPlot.Model.Series[0] as LineSeries).Points.Clear(); fitnessPlot.Model.Series[0].Unselect(); fitnessPlot.InvalidatePlot(); //GA IFitnessFunction fitness = null; //Options List<IMetric> activeMetrics = new List<IMetric>(); // if (metricChromaticTone.IsChecked == true) // activeMetrics.Add(new ChromaticTone()); if (metricChromaticToneDistance.IsChecked == true) activeMetrics.Add(new ChromaticToneDistance()); if (metricChromaticToneDuration.IsChecked == true) activeMetrics.Add(new ChromaticToneDuration()); if (metricMelodicBigram.IsChecked == true) activeMetrics.Add(new MelodicBigram()); if (metricMelodicInterval.IsChecked == true) activeMetrics.Add(new MelodicInterval()); // if (metricPitch.IsChecked == true) // activeMetrics.Add(new Pitch()); // if (metricPitchDistance.IsChecked == true) // activeMetrics.Add(new PitchDistance()); // if (metricRhythm.IsChecked == true) // activeMetrics.Add(new Rhythm()); if (metricRhythmicBigram.IsChecked == true) activeMetrics.Add(new RhythmicBigram()); if (metricRhythmicInterval.IsChecked == true) activeMetrics.Add(new RhythmicInterval()); fitness = GeneticMIDI.FitnessFunctions.MetricSimilarity.GenerateMetricSimilarityMulti(category.Compositions, activeMetrics.ToArray(), GeneticMIDI.FitnessFunctions.SimilarityType.Cosine); /* if (fitnessFuncCombo.SelectedIndex == 0) fitness = new GeneticMIDI.FitnessFunctions.MetricSimilarity(seq, activeMetrics.ToArray(), GeneticMIDI.FitnessFunctions.SimilarityType.Cosine); if (fitnessFuncCombo.SelectedIndex == 1) fitness = new GeneticMIDI.FitnessFunctions.MetricSimilarity(seq, activeMetrics.ToArray(), GeneticMIDI.FitnessFunctions.SimilarityType.Euclidian); if (fitnessFuncCombo.SelectedIndex == 2) fitness = new GeneticMIDI.FitnessFunctions.MetricSimilarity(seq, activeMetrics.ToArray(), GeneticMIDI.FitnessFunctions.SimilarityType.Pearson); if (fitnessFuncCombo.SelectedIndex == 3) fitness = new GeneticMIDI.FitnessFunctions.CrossCorrelation(seq); if (fitnessFuncCombo.SelectedIndex == 4) fitness = GeneticMIDI.FitnessFunctions.NCD.FromMelodies(category);*/ Instrument = (PatchNames)geneticInstrumentBox.SelectedItem; var gen = new GeneticGenerator(fitness, Instrument, category); gen.OnPercentage += gen_OnPercentage; gen.MaxGenerations = (int)maxGenerationSlider.Value; Generator = gen; new Thread(() => { var notes = gen.Generate(); var mel = notes; GeneratedSequence = mel; progressGenSlider.Dispatcher.Invoke(() => { progressGenSlider.Value = 100; }); }).Start(); } if (index == 1) { if (instrBox.SelectedItem as ListBoxItem == null || (instrBox.SelectedItem as ListBoxItem).Tag == null) return; if ((int)((instrBox.SelectedItem as ListBoxItem).Tag) == -1) { // Drum Generator DrumGenerator gen = new DrumGenerator(); Generator = gen; Instrument = PatchNames.Helicopter; new Thread(() => { StartSpinner(); gen.Initialize(new Databank(GeneticMIDI.Constants.LOCAL_LIBRARY_PATH)); GeneratedSequence = gen.Generate(); StopSpinner(); progressGenSlider.Dispatcher.Invoke(() => { progressGenSlider.Value = 100; }); }).Start(); } else { PatchNames instrument = (PatchNames)((instrBox.SelectedItem as ListBoxItem).Tag); Instrument = instrument; InstrumentalGenerator gen = Generator as InstrumentalGenerator; if (gen == null) return; gen.SetInstrument(Instrument); new Thread(() => { if (gen.IsInitialized) { StartSpinner(); GeneratedSequence = gen.GenerateInstrument(instrument); StopSpinner(); } progressGenSlider.Dispatcher.Invoke(() => { progressGenSlider.Value = 100; }); }).Start(); } } if(index == 2) { if (accompInstruBox.Items.Count == 0 || accompTrackBox.Items.Count == 0) return; Instrument = (PatchNames)(accompInstruBox.SelectedItem); Track track = (accompTrackBox.SelectedItem as ListBoxItem).Tag as Track; var melSeq = track.GetMainSequence() as MelodySequence; Random rnd = new Random(); if(accompMethoBox.SelectedIndex == 0) { AccompanyGeneratorMarkov gen = new AccompanyGeneratorMarkov(category, Instrument); Generator = gen; new Thread(() => { StartSpinner(); GeneratedSequence = gen.Generate(melSeq,rnd.Next()); StopSpinner(); progressGenSlider.Dispatcher.Invoke(() => { progressGenSlider.Value = 100; }); }).Start(); } else if (accompMethoBox.SelectedIndex == 1) { AccompanimentGeneratorANNFF gen = new AccompanimentGeneratorANNFF(category, Instrument); Generator = gen; gen.SetSequence(melSeq); new Thread(() => { StartSpinner(); gen.Load(); GeneratedSequence = gen.Generate(); StopSpinner(); progressGenSlider.Dispatcher.Invoke(() => { progressGenSlider.Value = 100; }); }).Start(); } } if(index == 3) { //stochasticLogPlot.Model.Series.Clear(); stochasticLogPlot.ResetAllAxes(); SetupLinePlot(stochasticLogPlot, "Log Likelihood"); (stochasticLogPlot.Model.Series[0] as LineSeries).Points.Clear(); stochasticLogPlot.Model.Series[0].Unselect(); stochasticLogPlot.InvalidatePlot(); if (loadInstrument.SelectedItem == null) return; Instrument = (PatchNames)(loadInstrument.SelectedItem); SamplingWithReplacement swr = new SamplingWithReplacement(category, Instrument); swr.OnProgressChange += swr_OnProgressChange; swr.MaxIterations = (int)loadGenerationsSlider.Value; Generator = swr; new Thread(() => { StartSpinner(); GeneratedSequence = swr.Generate(); StopSpinner(); progressGenSlider.Dispatcher.Invoke(() => { progressGenSlider.Maximum = 100; progressGenSlider.Value = 100; }); }).Start(); } if(index == 4) { if (randomInstrument.SelectedItem == null) return; Instrument = (PatchNames)randomInstrument.SelectedItem; int centralNotePitch = (int)randomOctave.Value * 12; int noteShift = (int)randomPitchVar.Value; int minNote = centralNotePitch - noteShift; int maxNote = centralNotePitch + noteShift; if (minNote <= 0) minNote = 1; if (maxNote >= 127) maxNote = 126; int durMin = (int)Math.Pow(randomDurationRange.LowerValue, 2); int durMax = (int)Math.Pow(randomDurationRange.UpperValue, 2); int length = (int)randomLength.Value; ScaleType scale = randomScale.SelectedItem as ScaleType; var gen = new ReflectingBrownNoteGenerator(new NoteRangeRestrictor(minNote, maxNote, durMin, durMax, scale), new Random(), -2, 2, -1, 1, Instrument); Generator = gen; gen.MaxNotes = length; new Thread(() => { StartSpinner(); GeneratedSequence = gen.Generate(); StopSpinner(); progressGenSlider.Dispatcher.Invoke(() => { progressGenSlider.Value = 100; }); }).Start(); Console.ReadLine(); } }
private void Button_Click_3(object sender, RoutedEventArgs e) { Composition comp; Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.DefaultExt = ".mid"; dlg.Filter = "MIDI Files (*.mid)|*.mid"; dlg.Title = "Load MIDI file"; Nullable<bool> result = dlg.ShowDialog(); if (result == true) { comp = Composition.LoadFromMIDI(dlg.FileName); TrackSelector trackSel = new TrackSelector(comp); if(trackSel.ShowDialog() == true) { this.GeneratedSequence = trackSel.SelectedSequence; this.Instrument = trackSel.SelectedInstrument; this.Generator = new ExistingSequenceGenerator(this.GeneratedSequence, this.Instrument); this.DialogResult = true; this.Close(); } } }
private Track AddPercussionTrack(MelodySequence seq, INoteGenerator gen) { Track t = new Track(gen.Instrument, 10); for(int i = 0 ; i < ActiveComposition.Tracks.Count; i++) { var ctrack = ActiveComposition.Tracks[i]; if(ctrack.Channel == 10) { ctrack.Clear(); ActiveComposition.Tracks.RemoveAt(i); break; } } t.AddSequence(seq); ActiveComposition.Tracks.Add(t); generators.Add(gen); if (OnCompositionChange != null) OnCompositionChange(this, new EventArgs()); return t; }