private static MusicNote CheckCache(string note, int a4Reference) { MusicNote musicNote = null; if (MusicNoteCache == null) { MusicNoteCache = new BiMap <string, MusicNote>(); } if (MusicNoteCache.Forward.ContainsKey(note)) { musicNote = MusicNoteCache.Forward[note]; } if (musicNote == null) { musicNote = new MusicNote() { Key = note, Reference = a4Reference }; musicNote.Error += MusicNote_Error; musicNote.InitializeNote(); if (musicNote.IsValid) { FactoryCreateNote?.Invoke(musicNote, new EventArgs()); MusicNoteCache.Add(note, musicNote); } else { return(null); } } return(musicNote); }
/// <summary> /// Plays the note using the current <see cref="IPlayer"/> for <see cref="Duration"/>ms /// </summary> public void PlayNote() { if (DesiredOctave > Frequencies.Count() - 1) { var error = new ArgumentException($"{DesiredOctave} is too high of an Octave to play {Key}", nameof(DesiredOctave)); Error?.Invoke(this, error); } else { try { string[] desiredChord = null; switch (ChordType) { case ChordType.Note: desiredChord = new string[] { Key }; break; case ChordType.Power: desiredChord = PowerChord; break; case ChordType.MinorThird: desiredChord = MinorChord3; break; case ChordType.MajorThird: desiredChord = MajorChord3; break; case ChordType.MinorSeventh: desiredChord = MinorChord7; break; case ChordType.MajorSeventh: desiredChord = MajorChord7; break; } PlayingNote?.Invoke(this, new EventArgs()); var musicNotes = desiredChord.Select(sn => MusicNote.Create(sn)); var frequencies = musicNotes.Select(mn => new FrequencyDuration(mn.Key, mn.DesiredOctave, mn.Frequencies[DesiredOctave], Duration)); NotePlayer?.Play(frequencies, Instrument); } catch (Exception ex) { Error?.Invoke(this, ex); } } }