Ejemplo n.º 1
0
 public PerceptualNote(
     PerceptualNoteID id,
     Duration duration,
     PerceptualTime length,
     IDuratedItem <Cell> cell,
     Note note
     )
 {
     ID            = id;
     this.duration = duration;
     Length        = length;
     Cell          = cell;
     Note          = note;
 }
Ejemplo n.º 2
0
        public bool Analyze(
            Duration duration,
            MusicBrain brain,
            PerceptualMemory memory
            )
        {
            bool flag = false;

            var memorymodule =
                (MemoryModule)
                memory.MemoryModule <PerceptualNote>();

            var measures =
                memory.MemoryModule <Measure>();

            var notes =
                memory.Analyses <Note>(duration);

            foreach (Note note in notes)
            {
                if (memorymodule.perceptualnotes_map.ContainsKey(note.ID))
                {
                    continue;
                }

                var perceptualnotes =
                    new List <PerceptualNote>();

                var singlelength =
                    PerceptualTime.Decompose(note.Duration.Length).OneOrNothing();

                var cells =
                    memory.Analyses <Cell>(note.Duration);

                var measures_intersecting =
                    measures
                    .Knowledge
                    .Intersecting(note.Duration);

                if (singlelength.Key == default(PerceptualTime) || measures_intersecting.Count() > 1)
                {
                    var i = 0;

                    foreach (var cell_durateditem in cells)
                    {
                        var cell         = cell_durateditem.Value;
                        var cellduration = cell_durateditem.Duration;

                        var cellcutduration = cellduration.Intersection(note.Duration);

                        var lengths =
                            PerceptualTime.Decompose(cellcutduration.Length);

                        foreach (var length in lengths)
                        {
                            var cutduration =
                                new Duration {
                                Start  = length.Value + cellduration.Start,
                                Length = length.Key.TimeLength()
                            };

                            var perceptualnote =
                                new PerceptualNote(
                                    new PerceptualNoteID(note.ID, i++),
                                    cutduration,
                                    length.Key,
                                    cell_durateditem,
                                    note
                                    );

                            memorymodule.Editable.Add(perceptualnote, cutduration);

                            perceptualnotes.Add(perceptualnote);
                        }
                    }

                    if (perceptualnotes.Count > 0)
                    {
                        memorymodule.perceptualnotes_map.Add(note.ID, perceptualnotes.ToArray());
                        flag = true;
                    }
                }
                else
                {
                    var perceptualnote =
                        new PerceptualNote(
                            new PerceptualNoteID(
                                note.ID,
                                0
                                ),
                            note.Duration,
                            singlelength.Key,
                            cells.First(),
                            note
                            );

                    memorymodule.Editable.Add(perceptualnote, note.Duration);

                    memorymodule.perceptualnotes_map.Add(note.ID, new[] { perceptualnote });
                    flag = true;
                }
            }

            return(flag);
        }
Ejemplo n.º 3
0
        public static IEnumerable <KeyValuePair <PerceptualTime, Time> > Decompose(Time length)
        {
            // I'm sorry in advance for anyone who has to read this method.

            var fractions_whole = length / Time.Note_128th_3rd_5th_7th;

            var tuplet = TupletClass.None;

            if ((fractions_whole % 3) != 0)
            {
                tuplet |= TupletClass.Triplet;
            }
            else
            {
                fractions_whole /= 3;
            }

            if ((fractions_whole % 5) != 0)
            {
                tuplet |= TupletClass.Pentuplet;
            }
            else
            {
                fractions_whole /= 5;
            }

            if ((fractions_whole % 7) != 0)
            {
                tuplet |= TupletClass.Septuplet;
            }
            else
            {
                fractions_whole /= 7;
            }

            // fractions_base is in
            var bits = new BitArray(new int[] { fractions_whole });

            // the pattern ...01...10... indicates a dotted note

            var state_dots = -1;
            var head_value = LengthClass.None;
            var offset     = Time.Zero;

            // bit 0 (128th note) to bit 7 (whole note)
            for (
                var place_value =
                    tuplet != TupletClass.None ?
                    LengthClass.Half :
                    LengthClass.Whole;
                place_value < LengthClass.Invalid;
                place_value++
                )
            {
                var i =
                    (int)LengthClass.Invalid -
                    (int)place_value -
                    1;

                if (bits[i])
                {
                    state_dots++;

                    if (state_dots == 0)
                    {
                        head_value = place_value;
                    }
                }
                else
                {
                    if (state_dots >= 0)
                    {
                        var ptime =
                            new PerceptualTime(
                                tuplet,
                                head_value,
                                state_dots
                                );

                        yield return
                            (new KeyValuePair <PerceptualTime, Time>(
                                 ptime,
                                 offset
                                 ));

                        state_dots = -1;
                        head_value = LengthClass.None;
                        offset    += ptime.TimeLength();
                    }
                }
            }

            if (state_dots >= 0)
            {
                var ptime =
                    new PerceptualTime(
                        tuplet,
                        head_value,
                        state_dots
                        );

                yield return
                    (new KeyValuePair <PerceptualTime, Time>(
                         ptime,
                         offset
                         ));

                state_dots = -1;
                head_value = LengthClass.None;
                offset    += ptime.TimeLength();
            }
        }