using System; using System.Collections.Generic; using System.Linq; using Melanchall.DryWetMidi.Smf; using Melanchall.DryWetMidi.Smf.Interaction; using SBRL.Utilities; namespace MusicBoxConverter { public class MusicBoxScoreGenerator { public Vector2 offset { get; set; } = new Vector2(10, 10); public Vector2 scaleFactor { get; set; } = new Vector2(0.03f, 4f); public float holeSize { get; set; } = 1f; private int trackLength; public int TrackLength { get { if (trackLength == 0) trackLength = (int)AllNotes().Max((Note arg) => arg.Time); return trackLength; } } public int MaxNoteNumber { get { return MusicBox.HighestNote.NoteNumber; } } public int MinNoteNumber { get { return MusicBox.LowestNote.NoteNumber; } } public MusicBox MusicBox { get; private set; } MidiFile midiFile; public MusicBoxScoreGenerator(string filename, MusicBox inMusicBox) { MusicBox = inMusicBox; midiFile = MidiFile.Read(filename); // Set the scale factor based on the strip height of the music box scaleFactor = new Vector2( scaleFactor.X, MusicBox.StripHeightMm / MusicBox.NoteCount ); foreach(Note note in AllNotes()) { if (!MusicBox.IsValidNote(note)) Console.Error.WriteLine($"Warning: The note {note} at {note.Time} can't be played by the {MusicBox}."); } } public void Output(string destinationFilename) { Vector2 area = new Vector2(TrackLength, MusicBox.NoteCount).Multiply(scaleFactor); Vector2 size = area.Add(offset.Multiply(2)); SvgWriter svg = new SvgWriter( destinationFilename, $"{size.X}mm", $"{size.Y}mm" ); svg.UnitSuffix = "mm"; for(float i = 0; i < area.Y; i += scaleFactor.Y) { Vector2 start = offset.Add(new Vector2(0, i)); svg.WriteLine(start, start.Add(new Vector2(area.X, 0)), "darkgreen", 1); } svg.WriteRectangle(offset, area); foreach(Note note in AllNotes()) { Console.WriteLine("[{0}] {1}{2}/{3}", note.Time, note.NoteName, note.Octave, note.NoteNumber); svg.WriteCircle( new Vector2( offset.X + note.Time * scaleFactor.X, offset.Y + (MusicBox.NoteCount - MusicBox.NoteToBoxNumber(note)) * scaleFactor.Y ), holeSize // radius ); } svg.Complete(); } public IEnumerable AllNotes() { foreach(TrackChunk chunk in midiFile.Chunks.OfType()) { using(NotesManager notesManager = new NotesManager(chunk.Events)) { foreach(Note note in notesManager.Notes) yield return note; } } } } }