public void Mutate(Measure measure) { var randomRate = ThreadSafeRandom.ThisThreadsRandom.Next(0, 100); if (randomRate < Rate) { var geneticEventsManager = Singleton <GeneticEventsManager> .Instance(); var randomEvent = geneticEventsManager.RandomEventWithRate(); var index = ThreadSafeRandom.ThisThreadsRandom.Next(measure.Events.Count); if (randomEvent == (int)RestOrTie.Rest) { while (index + 1 < measure.Events.Count && measure.Events[index + 1].Number == (int)RestOrTie.Tie) { index = ThreadSafeRandom.ThisThreadsRandom.Next(measure.Events.Count); } measure.Events[index] = new Rest(randomEvent, index); return; } if (randomEvent == (int)RestOrTie.Tie) { while (index == 0 || (index - 1 >= 0 && measure.Events[index - 1].Number == (int)RestOrTie.Rest)) { index = ThreadSafeRandom.ThisThreadsRandom.Next(measure.Events.Count); } measure.Events[index] = new Tie(randomEvent, index); return; } var noteName = NoteUtilities.GetNoteName((SevenBitNumber)randomEvent).ToString(); measure.Events[index] = new Note(noteName, randomEvent, index); } }
private Measure GetNewMeasureWithOriginalTies(int order) { var events = new List <Event>(); var geneticEventsManager = Singleton <GeneticEventsManager> .Instance(); var indexOfevent = 0; while (events.Count < BaseMelody.SizeOfMeasure) { var randomEvent = geneticEventsManager.RandomRestOrNote(); if (BaseMelody.Measures[order].Events[indexOfevent].Number == (int)RestOrTie.Tie) { // Ensure that ties are not generated after rests if (events.Last().Number == (int)RestOrTie.Rest) { events.Remove(events.Last()); var note = geneticEventsManager.RandomNote(); var name = NoteUtilities.GetNoteName((SevenBitNumber)note).ToString(); events.Add(new Note(name, note, events.Count)); } events.Add(new Tie((int)RestOrTie.Tie, events.Count)); } else { // Ensure that ties are not generated in first position if (indexOfevent == 0 && (randomEvent == (int)RestOrTie.Tie)) { continue; } // Ensure that ties are not generated after rests if (events.Any() && events.Last().Number == (int)RestOrTie.Rest && randomEvent == (int)RestOrTie.Tie) { continue; } switch (randomEvent) { case (int)RestOrTie.Rest: events.Add(new Rest(randomEvent, events.Count)); break; default: var name = NoteUtilities.GetNoteName((SevenBitNumber)randomEvent).ToString(); events.Add(new Note(name, randomEvent, events.Count)); break; } } indexOfevent++; } return(new Measure(events, order)); }
/// <summary> /// Initializes a new instance of the <see cref="OctaveDefinition"/> with the /// specified octave number. /// </summary> /// <param name="octave">The number of an octave.</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="octave"/> is out of valid range.</exception> private OctaveDefinition(int octave) { Debug.Assert(octave >= MinOctaveNumber && octave <= MaxOctaveNumber, "An octave's number is out of range."); Number = octave; _notesDefinitions = Enum.GetValues(typeof(NoteName)) .Cast <NoteName>() .Where(n => NoteUtilities.IsNoteValid(n, octave)) .ToDictionary(n => n, n => NoteDefinition.Get(n, octave)); }
public Event Clone() { if (Number == (int)RestOrTie.Tie) { return(new Tie(Number, Order)); } if (Number == (int)RestOrTie.Rest) { return(new Rest(Number, Order)); } var name = NoteUtilities.GetNoteName((SevenBitNumber)Number).ToString(); return(new Note(name, Number, Order)); }
public void Mutate(Measure measure) { if (!measure.Events.OfType <Rest>().Any()) { return; } var randomRate = ThreadSafeRandom.ThisThreadsRandom.Next(0, 100); if (randomRate < Rate) { var geneticEventsManager = Singleton <GeneticEventsManager> .Instance(); for (int i = 0; i < measure.Events.Count; i++) { if (measure.Events[i].Number == (int)RestOrTie.Rest && i + 1 < measure.Events.Count && measure.Events[i + 1].Number == (int)RestOrTie.Tie) { var randomNote = geneticEventsManager.RandomNote(); var noteName = NoteUtilities.GetNoteName((SevenBitNumber)randomNote).ToString(); measure.Events[i] = new Note(noteName, randomNote, i); } } } }
/// <summary> /// Initializes a new instance of the <see cref="Note"/> with the specified /// note name, octave, length and absolute time. /// </summary> /// <param name="noteName">Name of the note.</param> /// <param name="octave">Number of the octave in scientific pitch notation.</param> /// <param name="length">Length of the note in units defined by time division of a MIDI file.</param> /// <param name="time">Absolute time of the note in units defined by the time division of a MIDI file.</param> /// <remarks> /// Octave number is specified in scientific pitch notation which means that 4 must be /// passed to <paramref name="octave"/> to get the middle C. /// </remarks> /// <exception cref="InvalidEnumArgumentException"><paramref name="noteName"/> specified an /// invalid value.</exception> /// <exception cref="ArgumentException">Note number is out of range for the specified note /// name and octave.</exception> public Note(NoteName noteName, int octave, long length, long time) : this(NoteUtilities.GetNoteNumber(noteName, octave)) { Length = length; Time = time; }
/// <summary> /// Sets note name and octave for current <see cref="Note"/>. /// </summary> /// <param name="noteName">Name of the note.</param> /// <param name="octave">Number of the octave in scientific pitch notation.</param> /// <remarks> /// Octave number is specified in scientific pitch notation which means that 4 must be /// passed to <paramref name="octave"/> to get the number of the middle C. /// </remarks> /// <exception cref="InvalidEnumArgumentException"><paramref name="noteName"/> specified an /// invalid value.</exception> /// <exception cref="ArgumentException">Note number is out of range for the specified note /// name and octave.</exception> public void SetNoteNameAndOctave(NoteName noteName, int octave) { NoteNumber = NoteUtilities.GetNoteNumber(noteName, octave); }
public NoteInfo(NoteName noteName, int octave, ITimeSpan time, ITimeSpan length, SevenBitNumber velocity) : this(NoteUtilities.GetNoteNumber(noteName, octave), time, length, velocity) { }
/// <summary> /// Sets the note number of the <see cref="NoteEvent"/> with the specified note name and octave. /// </summary> /// <param name="noteEvent">Note event to set the note number of.</param> /// <param name="noteName">Name of the note.</param> /// <param name="octave">Number of the octave.</param> /// <remarks> /// Octave number is specified in scientific pitch notation which means that 4 must be /// passed to get the number of the middle C. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="noteEvent"/> is null.</exception> /// <exception cref="InvalidEnumArgumentException"><paramref name="noteName"/> specified an /// invalid value.</exception> /// <exception cref="ArgumentException">Note number is out of range for the specified note /// name and octave.</exception> public static void SetNoteNumber(this NoteEvent noteEvent, NoteName noteName, int octave) { ThrowIfArgument.IsNull(nameof(noteEvent), noteEvent); noteEvent.NoteNumber = NoteUtilities.GetNoteNumber(noteName, octave); }
/// <summary> /// Gets octave of the note presented by the specified <see cref="NoteOnEvent"/>. /// </summary> /// <param name="noteEvent">Note event to get note octave of.</param> /// <returns>Note octave of the <paramref name="noteEvent"/>.</returns> /// <remarks> /// Octave number will be returned in scientific pitch notation which means /// that 4 will be returned for 60 note number. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="noteEvent"/> is null.</exception> public static int GetNoteOctave(this NoteEvent noteEvent) { ThrowIfArgument.IsNull(nameof(noteEvent), noteEvent); return(NoteUtilities.GetNoteOctave(noteEvent.NoteNumber)); }
/// <summary> /// Returns a <see cref="NoteDefinition"/> for the specified note name and octave number. /// </summary> /// <param name="noteName">The name of a note.</param> /// <param name="octave">The octave number.</param> /// <returns>A <see cref="NoteDefinition"/> for the <paramref name="noteName"/> and <paramref name="octave"/>.</returns> /// <remarks> /// Octave number is specified in scientific pitch notation which means that 4 must be /// passed to <paramref name="octave"/> to get the middle C definition. /// </remarks> /// <exception cref="InvalidEnumArgumentException"><paramref name="noteName"/> specified an /// invalid value.</exception> /// <exception cref="ArgumentException">Note number is out of range for the specified note /// name and octave.</exception> public static NoteDefinition Get(NoteName noteName, int octave) { return(Get(NoteUtilities.GetNoteNumber(noteName, octave))); }