/// <summary>
        /// The get or create notes in db.
        /// </summary>
        /// <param name="alphabet">
        /// The alphabet.
        /// </param>
        /// <returns>
        /// The <see cref="T:long[]"/>.
        /// </returns>
        public long[] GetOrCreateNotesInDb(Alphabet alphabet)
        {
            var newNotes = new List <Note>();
            var result   = new Note[alphabet.Cardinality];

            ValueNote[] notesAlphabet = alphabet.Cast <ValueNote>().ToArray();
            string[]    stringNotes   = notesAlphabet.Select(n => n.ToString()).ToArray();
            Dictionary <string, Note> existingNotes = db.Note.Where(n => stringNotes.Contains(n.Value))
                                                      .ToDictionary(n => n.Value);

            for (int i = 0; i < notesAlphabet.Length; i++)
            {
                ValueNote note            = notesAlphabet[i];
                int[]     pitches         = GetOrCreatePitchesInDb(note.Pitches);
                string    localStringNote = stringNotes[i];

                if (existingNotes.ContainsKey(localStringNote))
                {
                    result[i] = existingNotes[localStringNote];
                    if (note.Triplet != result[i].Triplet ||
                        note.Duration.Denominator != result[i].Denominator ||
                        note.Duration.Numerator != result[i].Numerator ||
                        note.Tie != result[i].Tie)
                    {
                        throw new Exception("Found in db note is not equal to local note.");
                    }
                }
                else
                {
                    result[i] = new Note
                    {
                        Value       = localStringNote,
                        Triplet     = note.Triplet,
                        Denominator = note.Duration.Denominator,
                        Numerator   = note.Duration.Numerator,
                        Tie         = note.Tie,
                        Pitch       = db.Pitch.Where(p => pitches.Contains(p.Id)).ToList(),
                        Notation    = Notation.Notes
                    };
                    newNotes.Add(result[i]);
                }
            }

            db.Note.AddRange(newNotes);
            db.SaveChanges();
            return(result.Select(n => n.Id).ToArray());
        }
Beispiel #2
0
        /// <summary>
        /// The details.
        /// </summary>
        /// <param name="id">
        /// The music sequence's id.
        /// </param>
        /// <returns>
        /// The <see cref="System.Threading.Tasks.Task"/>.
        /// </returns>
        public async Task <ActionResult> Details(long?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new LibiadaWebEntities())
            {
                MusicSequence musicSequence = db.MusicSequence.Include(m => m.Matter).Single(m => m.Id == id);
                if (musicSequence == null)
                {
                    return(HttpNotFound());
                }

                var musicChainAlphabet = db.GetAlphabetElementIds(musicSequence.Id)
                                         .Select(el => db.Fmotif.Single(f => f.Id == el))
                                         .ToList();
                var musicChainBuilding = db.GetSequenceBuilding(musicSequence.Id);
                var sortedFmotifs      = new Dictionary <LibiadaWeb.Fmotif, int>();
                for (int i = 0; i < musicChainAlphabet.Count; i++)
                {
                    sortedFmotifs.Add(musicChainAlphabet[i], musicChainBuilding.Count(el => el == i + 1));
                }
                sortedFmotifs = sortedFmotifs.OrderByDescending(pair => pair.Value)
                                .ToDictionary(pair => pair.Key, pair => pair.Value);

                var fmotifsChain = new List <Fmotif>();
                foreach (var fmotif in sortedFmotifs.Keys)
                {
                    var newFmotif = new Fmotif(fmotif.FmotifType, musicSequence.PauseTreatment, fmotif.Id);

                    var fmotifAlphabet = db.GetFmotifAlphabet(fmotif.Id);
                    var fmotifBuilding = db.GetFmotifBuilding(fmotif.Id);
                    foreach (var position in fmotifBuilding)
                    {
                        var dbNoteId   = fmotifAlphabet.ElementAt(position - 1);
                        var dbNote     = db.Note.Single(n => n.Id == dbNoteId);
                        var newPitches = new List <Pitch>();
                        foreach (var pitch in dbNote.Pitch)
                        {
                            newPitches.Add(new Pitch(pitch.Midinumber));
                        }

                        var newNote = new ValueNote(newPitches,
                                                    new Duration(dbNote.Numerator, dbNote.Denominator),
                                                    dbNote.Triplet,
                                                    dbNote.Tie);
                        newNote.Id = dbNote.Id;
                        newFmotif.NoteList.Add(newNote);
                    }
                    fmotifsChain.Add(newFmotif);
                }
                var result = new Dictionary <string, object> {
                    { "fmotifs", fmotifsChain },
                    { "sequentialTransfer", musicSequence.SequentialTransfer }
                };
                ViewBag.data = JsonConvert.SerializeObject(new Dictionary <string, object> {
                    { "data", result }
                });
                return(View(musicSequence));
            }
        }