public static KeySignature Create( DiatonicToneClass key, PitchTransform transform, Mode mode ) { var modesteps = new PitchTransform[] { PitchTransform.DoubleSharp, PitchTransform.DoubleSharp, PitchTransform.Sharp, PitchTransform.DoubleSharp, PitchTransform.DoubleSharp, PitchTransform.DoubleSharp, PitchTransform.Sharp }; var keysteps = new PitchTransform[] { PitchTransform.DoubleSharp, PitchTransform.DoubleSharp, PitchTransform.Sharp, PitchTransform.DoubleSharp, PitchTransform.DoubleSharp, PitchTransform.DoubleSharp, PitchTransform.Sharp }; var mode_copy = mode; while (mode_copy != Mode.Major) { // rotate L one step var step0 = modesteps[0]; for (int i = 1; i < modesteps.Length; i++) { modesteps[i - 1] = modesteps[i]; } modesteps[modesteps.Length - 1] = step0; mode_copy = (Mode)((int)mode_copy - 1); } var key_copy = key; while (key_copy != DiatonicToneClass.C) { // rotate L one step var step0 = keysteps[0]; for (int i = 1; i < keysteps.Length; i++) { keysteps[i - 1] = keysteps[i]; } keysteps[keysteps.Length - 1] = step0; key_copy = (DiatonicToneClass)((int)key_copy - 1); } var signature = new KeySignature(); var pitch = key.GetPitchClass(); for (int i = 0; i < 7; i++) { signature[key] = transform; transform += modesteps[i]; transform -= keysteps[i]; //TODO: should modesteps[i] or transform be used here pitch = modesteps[i].Transform(pitch); key = key.ToRight(); } return(signature); }
public AdornmentTrack( StorageObjectID storageobjectID, EditorFile file ) : base( storageobjectID, file ) { obj = this.Object(); binder_staffs = Staffs.Bind( obj.GetOrMake("staffs").ID, File ); binder_staffs.Deserializer = staff_obj => { using (var stream = staff_obj.OpenRead()) { using (var br = new BinaryReader(stream)) { var staff = new Staff(); staff.Lines = br.ReadInt32(); staff.MiddleHalfLine = br.ReadInt32(); staff.Shift = br.ReadInt32(); staff.Clef.BottomKey = new DiatonicTone(br.ReadInt32()); staff.Clef.Symbol = (ClefSymbol)br.ReadInt32(); return(staff); } } }; binder_staffs.Serializer = (staff_obj, staff) => { using (var stream = staff_obj.OpenWrite()) { using (var bw = new BinaryWriter(stream)) { bw.Write(staff.Lines); bw.Write(staff.MiddleHalfLine); bw.Write(staff.Shift); bw.Write(staff.Clef.BottomKey.Tones); bw.Write((int)staff.Clef.Symbol); } } }; binder_keysigs = KeySignatures.Bind( obj.GetOrMake("key-signatures").ID, File ); binder_keysigs.Deserializer = keysig_obj => { using (var stream = keysig_obj.OpenRead()) { using (var br = new BinaryReader(stream)) { var transform_a = new PitchTransform(br.ReadInt32()); var transform_b = new PitchTransform(br.ReadInt32()); var transform_c = new PitchTransform(br.ReadInt32()); var transform_d = new PitchTransform(br.ReadInt32()); var transform_e = new PitchTransform(br.ReadInt32()); var transform_f = new PitchTransform(br.ReadInt32()); var transform_g = new PitchTransform(br.ReadInt32()); var keysig = new KeySignature( transform_c, transform_d, transform_e, transform_f, transform_g, transform_a, transform_b ); return(keysig); } } }; binder_keysigs.Serializer = (keysig_obj, keysig) => { using (var stream = keysig_obj.OpenWrite()) { using (var bw = new BinaryWriter(stream)) { bw.Write(keysig[DiatonicToneClass.A].Steps); bw.Write(keysig[DiatonicToneClass.B].Steps); bw.Write(keysig[DiatonicToneClass.C].Steps); bw.Write(keysig[DiatonicToneClass.D].Steps); bw.Write(keysig[DiatonicToneClass.E].Steps); bw.Write(keysig[DiatonicToneClass.F].Steps); bw.Write(keysig[DiatonicToneClass.G].Steps); } } }; }
public void Import( EditorFile file, string filename, PorterOptions options ) { var midifile = new MidiFile(filename, false); var trackcontainer = file[TrackControllerContainer.ItemName] as TrackControllerContainer; var musictrackfactory = trackcontainer .Tracks .FactorySet .Factories .FirstOrDefault(_ => _ is MusicTrackFactory) .Name; for (int track_index = 0; track_index < midifile.Tracks; track_index++) { var events = midifile.Events.GetTrackEvents(track_index); var track = (MusicTrack)trackcontainer.Tracks.Create(musictrackfactory); foreach (var e in events) { switch (e.CommandCode) { case MidiCommandCode.NoteOn: { var e1 = (NoteOnEvent)e; var start = ImportTime(e1.AbsoluteTime, midifile); var length = ImportTime(e1.NoteLength, midifile); var duration = new Duration { Start = start, Length = length }; track.Melody.AddNote(new SemiTone(e1.NoteNumber - 12), duration); break; } case MidiCommandCode.NoteOff: { var e1 = (NoteEvent)e; break; } case MidiCommandCode.MetaEvent: { var meta = (MetaEvent)e; switch (meta.MetaEventType) { case MetaEventType.Copyright: break; case MetaEventType.CuePoint: break; case MetaEventType.DeviceName: break; case MetaEventType.EndTrack: break; case MetaEventType.KeySignature: { var keysigevent = (KeySignatureEvent)meta; var circle5index = (sbyte)keysigevent.SharpsFlats; Mode mode; switch (keysigevent.MajorMinor) { case 1: mode = Mode.Major; break; case 0: mode = Mode.Minor; break; default: throw new InvalidOperationException(); } PitchTransform transform; var key = CircleOfFifths.Index(circle5index, ChromaticPitchClass.C, out transform); var sig = KeySignature.Create( key, transform, mode ); var start = ImportTime(keysigevent.AbsoluteTime, midifile); track .Adornment .KeySignatures .OverwriteEverythingToRight( sig, start ); break; } case MetaEventType.Lyric: break; case MetaEventType.Marker: break; case MetaEventType.MidiChannel: break; case MetaEventType.MidiPort: break; case MetaEventType.ProgramName: break; case MetaEventType.SequencerSpecific: break; case MetaEventType.SequenceTrackName: { var text = (TextEvent)meta; track.Name.Value = text.Text; break; } case MetaEventType.SetTempo: var tempoevent = meta as TempoEvent; // Can midi files have linear varying tempos? // if so, then this code doesn't handle all midis. if (options.PortTempo) { track .Tempo .SetTempo( ImportTime(tempoevent.AbsoluteTime, midifile), (float)tempoevent.Tempo / 4 ); } break; case MetaEventType.SmpteOffset: break; case MetaEventType.TextEvent: break; case MetaEventType.TimeSignature: var timesigevent = (TimeSignatureEvent)meta; var timesig = new TimeSignature( new Simple( timesigevent.Numerator, timesigevent.Denominator ) ); track .Rhythm .TimeSignatures .OverwriteEverythingToRight( timesig, ImportTime(timesigevent.AbsoluteTime, midifile) ); break; case MetaEventType.TrackInstrumentName: break; case MetaEventType.TrackSequenceNumber: break; default: throw new InvalidOperationException(); } break; } case MidiCommandCode.PatchChange: { break; } case MidiCommandCode.ControlChange: { break; } default: throw new InvalidOperationException(); } } } }