private void PlaybackTimer_Tick(object sender, EventArgs e) { MetricTimeSpan current = currentPlayback.GetCurrentTime <MetricTimeSpan>(); MetricTimeSpan duration = currentPlayback.GetDuration <MetricTimeSpan>(); statusLabel.Text = $"Playing {Path.GetFileName(midiPath)} [{ToTimeString(current)} - {ToTimeString(duration)}]"; }
static string ChordToString(Chord chord, TempoMap tempoMap) { string representation = "\t\t{.music_notes = (struct MusicNote[]) {\n"; //append each note to the chord int numNotes = 0; foreach (Note note in chord.Notes) { if (numNotes != 0) { representation += ",\n"; } representation += NoteToString(note, tempoMap); numNotes++; } representation += "\n\t\t}"; //find the duration MetricTimeSpan chordDuration = LengthConverter.ConvertTo <MetricTimeSpan>(chord.Length, chord.Time, tempoMap); double seconds = chordDuration.TotalMicroseconds * MICROSECONDS_TO_SECONDS; representation += ", .duration = " + seconds + ", .num_notes = " + numNotes + "}"; return(representation); }
public void Chord_DefaultOctave() { var defaultVelocity = (SevenBitNumber)90; var defaultOctave = 2; var chordLength = MusicalTimeSpan.Sixteenth.Triplet(); var chordTime1 = new MetricTimeSpan(0, 1, 12); var chordTime2 = chordTime1.Add(chordLength, TimeSpanMode.TimeLength); var pattern = new PatternBuilder() .SetVelocity(defaultVelocity) .SetOctave(defaultOctave) .MoveToTime(chordTime1) .Chord(new[] { NoteName.C, NoteName.G }, chordLength) .Repeat() .Build(); TestNotes(pattern, new[] { new NoteInfo(NoteName.C, defaultOctave, chordTime1, chordLength, defaultVelocity), new NoteInfo(NoteName.G, defaultOctave, chordTime1, chordLength, defaultVelocity), new NoteInfo(NoteName.C, defaultOctave, chordTime2, chordLength, defaultVelocity), new NoteInfo(NoteName.G, defaultOctave, chordTime2, chordLength, defaultVelocity) }); }
public void MoveToFirstSnapPoint_ByData_AtZero_CheckReturnValue() { var snapPoint1Time = new MetricTimeSpan(0, 0, 1); var snapPoint2Time = new MetricTimeSpan(0, 0, 0); var snapPoint3Time = new MetricTimeSpan(0, 0, 3); using (var playback = Get10SecondsPlayback()) { var snapPoint1 = playback.Snapping.AddSnapPoint(snapPoint1Time, "X"); var snapPoint2 = playback.Snapping.AddSnapPoint(snapPoint2Time, "Y"); var snapPoint3 = playback.Snapping.AddSnapPoint(snapPoint3Time, "Z"); Assert.IsTrue(playback.MoveToFirstSnapPoint("Y"), "Failed to move to first snap point."); CheckCurrentTime(playback, snapPoint2Time, "moved first time"); Assert.IsTrue(playback.MoveToFirstSnapPoint("Y"), "Failed to move to first snap point again."); CheckCurrentTime(playback, snapPoint2Time, "moved second time"); snapPoint2.IsEnabled = false; Assert.IsFalse(playback.MoveToFirstSnapPoint("Y"), "Moves to disabled snap point."); CheckCurrentTime(playback, snapPoint2Time, "moved third time"); playback.MoveToTime(new MetricTimeSpan(0, 0, 5)); Assert.IsTrue(playback.MoveToFirstSnapPoint("Z"), "Failed to move to first enabled snap point."); CheckCurrentTime(playback, snapPoint3Time, "moved fourth time"); } }
public void LoadSong(String songName) { // Load audio clip (from "Assets/Resources/Music") audioSource = GetComponent <AudioSource>(); audioSource.Stop(); audioSource.clip = Resources.Load <AudioClip>("Music/" + songName); // Load midi data (from "Assets/Resources/MusicData") notesInLanes = new List <List <GameNote> >(); var midiFile = MidiFile.Read(Application.dataPath + "/Resources/MusicData/" + songName + ".mid"); var tempoMap = midiFile.GetTempoMap(); // Load midi data into note names and note hit times allNotes = new List <GameNote>(); foreach (var note in midiFile.GetNotes()) { NoteName noteName = note.NoteName; MetricTimeSpan metricTimeSpan = note.TimeAs <MetricTimeSpan>(tempoMap); allNotes.Add(new GameNote(noteName, timeInSeconds(metricTimeSpan))); } for (int i = 0; i < 4; i++) { List <GameNote> laneNotes = allNotes.Where(note => note.NoteName == laneNoteNames[i]).ToList(); notesInLanes.Add(laneNotes); } sampleRate = audioSource.clip.frequency; startingTempo = tempoMap.Tempo.AtTime(0); }
private void btn_start_Click(object sender, EventArgs e) { IEnumerable <Note> notes = midiObj.notes; TempoMap tempoMap = midiObj.tempoMap; foreach (Note n in notes) { MetricTimeSpan metricTime = n.TimeAs <MetricTimeSpan>(tempoMap); NoteObj tmpNote = new NoteObj( new Point( getNotePosition(n).X, 0), new MyTime( metricTime.Minutes, metricTime.Seconds, metricTime.Milliseconds), n.NoteName.ToString(), getNoteWidth(n)); listOfNotes.Add(tmpNote); } _minutes = 0; _seconds = 0; _miliseconds = 0; hits = 0; misses = 0; lbl_hits.Text = "Hits: 0"; lbl_misses.Text = "Misses: 0"; btn_openMidi.Hide(); lbl_midiName.Hide(); btn_start.Hide(); enablePianoButtons(); timerPiano.Start(); }
private static string CalcFactor(long noteAbsTime, TempoMap tempoMap) //calculate the factor needed for presenting time in seconds { MetricTimeSpan metricTime = TimeConverter.ConvertTo <MetricTimeSpan>(noteAbsTime, tempoMap); long seconds = 3600 * metricTime.Hours + 60 * metricTime.Minutes + metricTime.Seconds; long miliSeconds = metricTime.Milliseconds; return($"{seconds}.{miliSeconds}"); }
private string ToTimeString(MetricTimeSpan time) { string seconds = time.Seconds.ToString(); if (time.Seconds < 10) { seconds = seconds.Insert(0, "0"); } return($"{time.Minutes}:{seconds}"); }
private void GetNotesAtTime(int currentTime, int seconds = 0, int miliseconds = 0) { IEnumerable <Note> notes = file.GetNotes().AtTime(new MetricTimeSpan(0, currentTime, seconds, miliseconds), tempoMap); foreach (Note note in notes) { MetricTimeSpan metricTime = note.TimeAs <MetricTimeSpan>(tempoMap); BarBeatTicksTimeSpan musicalTime = note.TimeAs <BarBeatTicksTimeSpan>(tempoMap); MetricTimeSpan musicalLength = note.LengthAs <MetricTimeSpan>(tempoMap); //Debug.Log("NOTE: " +note + " - NOTELENGTH: " + musicalLength); } }
static string RestToString(MetricTimeSpan restDuration) { string representation = "\t\t{.music_notes = (struct MusicNote[]) {\n"; double seconds = restDuration.TotalMicroseconds * MICROSECONDS_TO_SECONDS; representation += "\t\t\t{.note = \"S\", .octave = 0, .duration = " + seconds + ", .peak_intensity = 0, .sustain_intensity = 0, .adsr_envelope = (double[]) {0, 0, 0, 0}}\n"; representation += "\t\t}, .duration = " + seconds + ", .num_notes = 1}"; return(representation); }
public void SetNoteLength() { Assert.AreEqual(MusicalTimeSpan.Quarter, PatternBuilder.DefaultNoteLength, "Default note length is invalid."); var patternBuilder = new PatternBuilder(); Assert.AreEqual(PatternBuilder.DefaultNoteLength, patternBuilder.NoteLength, "Invalid initial note length."); var noteLength = new MetricTimeSpan(0, 0, 20); patternBuilder.SetNoteLength(noteLength); Assert.AreEqual(noteLength, patternBuilder.NoteLength, "Invalid note length after change."); }
static void Main(string[] args) { var midiFile = MidiFile.Read(args[0]); var chords = midiFile.GetChords(); var tempoMap = midiFile.GetTempoMap(); List <List <Chord> > tracks = SortChords(chords); #if DEBUG //output some debug data if necessary foreach (List <Chord> track in tracks) { System.Console.WriteLine("--TRACK START--"); MetricTimeSpan totalTrackTime = new MetricTimeSpan(0); foreach (Chord chord in track) { MetricTimeSpan chordStartTime = TimeConverter.ConvertTo <MetricTimeSpan>(chord.Time, tempoMap); MetricTimeSpan chordDuration = LengthConverter.ConvertTo <MetricTimeSpan>(chord.Length, chord.Time, tempoMap); MetricTimeSpan chordEndTime = chordStartTime + chordDuration; System.Console.WriteLine("start: " + chordStartTime + " end: " + chordEndTime); totalTrackTime += chordDuration; } System.Console.WriteLine("--TRACK END--"); System.Console.WriteLine("TIME ELAPSED: " + totalTrackTime + "\n\n"); } #else //translate the tracks into code readable by the synth string songRepresentation = "{.music_tracks = (struct MusicTrack[]) {\n"; foreach (List <Chord> track in tracks) { songRepresentation += TrackToString(track, tempoMap); if (tracks[tracks.Count - 1] != track) { songRepresentation += ",\n"; } } songRepresentation += "\n}, .num_tracks = " + tracks.Count + "}"; System.Console.Write(songRepresentation); #endif }
public void ShiftEvents_ValidFiles_Metric() { var distance = new MetricTimeSpan(0, 1, 0); foreach (var midiFile in TestFilesProvider.GetValidFiles()) { midiFile.ShiftEvents(distance); var tempoMap = midiFile.GetTempoMap(); Assert.IsTrue(midiFile.GetTimedEvents() .Select(e => e.TimeAs <MetricTimeSpan>(tempoMap).CompareTo(distance)) .All(t => t >= 0), "Some events are not shifted."); } }
public float BeatsToSecs(float beat) { if (!_HasMidiFile()) { return(0.0f); } float frac = beat % 1.0f; int beats = (int)beat; int ticks = (int)(frac * _TicksPerBeat()); BeatTimeSpan inputSpan = new BeatTimeSpan(beats, ticks); MetricTimeSpan outputSpan = TimeConverter.ConvertTo <MetricTimeSpan>(inputSpan, _tempoMap); float secs = outputSpan.TotalMicroseconds / 1000000.0f; return(secs); }
private void OpenMidiFile() { try { _dancerSong = new DancerSong(); OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "MIDI files (*.mid)|*.mid;", InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) }; if (openFileDialog.ShowDialog() == true) { SelectedNote = null; _dancerSong.Midi = MidiFile.Read(openFileDialog.FileName); ParsedNotes = _midiEventsTextParser.ParseFromMidFormat(_dancerSong.Midi); channelId = 0; ChannelTitles = _midiEventsTextParser.ChannelTitles; PresentedNotes = _midiEventsTextParser.NotesInChannel[0]; selectedNotes = new ObservableCollection <Note>(); SplitByChannels(); SelectedChannel = "All channels"; TicksPerQuarterNoteTimeDivision td = playback.TempoMap.TimeDivision as TicksPerQuarterNoteTimeDivision; if (td is null) { throw new NotSupportedException("Time division in this MIDI file is not supported by this application."); } MetricTimeSpan metricTime = TimeConverter.ConvertTo <MetricTimeSpan>(td.TicksPerQuarterNote, playback.TempoMap); _dancerSong.ticksPerSecond = td.TicksPerQuarterNote * 1000 / (metricTime.Seconds * 1000 + metricTime.Milliseconds); EventSystem.Publish <OnMidiLoadedMessage>( new OnMidiLoadedMessage { midiChannels = this.midiChannels, playback = this.playback, midiChannelsTitles = this.channelTitles, ticksPerSecond = _dancerSong.ticksPerSecond, ticksPerquarterNote = td.TicksPerQuarterNote }); } } catch (NotSupportedException ex) { MessageBox.Show(ex.Message); } }
static string NoteToString(Note note, TempoMap tempoMap) { string representation = "\t\t\t{"; //find the note name string name = note.NoteName.ToString(); //convert the sharp into notation that the synth recognizes if (name.Contains("Sharp")) { name = name[0] + "s"; } representation += ".note = \"" + name + "\""; //find the note octave int octave = note.Octave; representation += ", .octave = " + octave; //find the note duration MetricTimeSpan noteDuration = LengthConverter.ConvertTo <MetricTimeSpan>(note.Length, note.Time, tempoMap); double seconds = noteDuration.TotalMicroseconds * MICROSECONDS_TO_SECONDS; representation += ", .duration = " + seconds; //find the note peak and sustain intensity double peakIntensity = System.Math.Pow(INTENSITY_SENSITIVITY, note.Velocity); double sustainIntensity = peakIntensity * SUSTAINABILITY; representation += ", .peak_intensity = " + peakIntensity + ", .sustain_intensity = " + sustainIntensity; //find the adsr envelope double adPercent = AD_SLOPE * (System.Math.Abs(note.Velocity) - 127) + MIN_AD_PERCENT; double aPercent = adPercent * AD_PROPORTION; double dPercent = adPercent - aPercent; double rPercent = R_SLOPE * (System.Math.Abs(note.Velocity) - 127) + MIN_R_PERCENT; double sPercent = 1 - aPercent - dPercent - rPercent; representation += ", .adsr_envelope = (double[]) {" + aPercent + ", " + dPercent + ", " + sPercent + ", " + rPercent + "}"; representation += "}"; return(representation); }
public float SecsToBeats(float secs) { if (!_HasMidiFile()) { return(0.0f); } long microseconds = (long)(secs * 1000000.0f); MetricTimeSpan inputSpan = new MetricTimeSpan(microseconds); BeatTimeSpan outputSpan = TimeConverter.ConvertTo <BeatTimeSpan>(inputSpan, _tempoMap); float beats = (float)outputSpan.Beats; beats += ((float)outputSpan.Ticks / _TicksPerBeat()); return(beats); }
public void Run() { MidiFile file = MidiFile.Read(midiPath); IEnumerable <Note> notes = file.GetNotes(); TempoMap tempoMap = file.GetTempoMap(); foreach (var note in notes) { MetricTimeSpan metricTime = note.TimeAs <MetricTimeSpan>(tempoMap); MetricTimeSpan metricLength = note.LengthAs <MetricTimeSpan>(tempoMap); } using (StreamWriter newTask = new StreamWriter(this.txtPath, false)) { foreach (var note in notes) { MetricTimeSpan metricTime = note.TimeAs <MetricTimeSpan>(tempoMap); MetricTimeSpan metricLength = note.LengthAs <MetricTimeSpan>(tempoMap); } } }
public void MoveToFirstSnapPoint_AtZero_CheckReturnValue() { var snapPoint1Time = new MetricTimeSpan(); var snapPoint2Time = new MetricTimeSpan(0, 0, 2); using (var playback = Get10SecondsPlayback()) { var snapPoint1 = playback.Snapping.AddSnapPoint(snapPoint1Time); var snapPoint2 = playback.Snapping.AddSnapPoint(snapPoint2Time); Assert.IsTrue(playback.MoveToFirstSnapPoint(), "Failed to move to first snap point."); CheckCurrentTime(playback, snapPoint1Time, "moved first time"); Assert.IsTrue(playback.MoveToFirstSnapPoint(), "Failed to move to first snap point again."); CheckCurrentTime(playback, snapPoint1Time, "moved second time"); snapPoint1.IsEnabled = false; Assert.IsTrue(playback.MoveToFirstSnapPoint(), "Failed to move to first snap point after first one disabled."); CheckCurrentTime(playback, snapPoint2Time, "moved third time"); } }
public void MoveToFirstAnchor_Keyed_OneUnkeyedAndOneKeyed() { var anchorTime = new MetricTimeSpan(0, 0, 30); var pattern = new PatternBuilder() .MoveToTime(new MetricTimeSpan(0, 0, 10)) .StepForward(new MetricTimeSpan(0, 0, 11)) .Anchor() .MoveToTime(anchorTime) .Anchor("Test") .StepBack(new MetricTimeSpan(0, 0, 5)) .MoveToFirstAnchor("Test") .Note(Octave.Get(0).A) .Build(); TestNotes(pattern, new[] { new NoteInfo(NoteName.A, 0, anchorTime, MusicalTimeSpan.Quarter) }); }
public void CreateTxt() { MidiFile file = MidiFile.Read(midiPath); IEnumerable <Note> notes = file.GetNotes(); TempoMap tempoMap = file.GetTempoMap(); foreach (var note in notes) { MetricTimeSpan metricTime = note.TimeAs <MetricTimeSpan>(tempoMap); MetricTimeSpan metricLength = note.LengthAs <MetricTimeSpan>(tempoMap); } using (StreamWriter newTask = new StreamWriter(this.txtPath, false)) { foreach (var note in notes) { MetricTimeSpan metricTime = note.TimeAs <MetricTimeSpan>(tempoMap); MetricTimeSpan metricLength = note.LengthAs <MetricTimeSpan>(tempoMap); var name = note.NoteName; var value = note.NoteNumber; newTask.WriteLine(metricTime + "," + metricLength + "," + name + "," + value); } } }
public void MoveToFirstAnchor_Unkeyed_OneUnkeyed() { var moveTime = new MetricTimeSpan(0, 0, 10); var step = new MetricTimeSpan(0, 0, 11); var anchorTime = moveTime + step; var pattern = new PatternBuilder() .MoveToTime(moveTime) .StepForward(step) .Anchor() .MoveToTime(new MetricTimeSpan(0, 0, 30)) .StepBack(new MetricTimeSpan(0, 0, 5)) .MoveToFirstAnchor() .Note(OctaveDefinition.Get(0).A) .Build(); TestNotes(pattern, new[] { new NoteInfo(NoteName.A, 0, anchorTime, MusicalTimeSpan.Quarter) }); }
public void Note_MixedLengthAndVelocity() { var defaultNoteLength = MusicalTimeSpan.Quarter; var defaultVelocity = (SevenBitNumber)90; var specifiedLength = new MetricTimeSpan(0, 0, 10); var specifiedVelocity = (SevenBitNumber)95; var pattern = new PatternBuilder() .SetNoteLength(defaultNoteLength) .SetVelocity(defaultVelocity) .Note(Octave.Get(0).A) .Note(Octave.Get(1).C, specifiedLength, specifiedVelocity) .Build(); TestNotes(pattern, new[] { new NoteInfo(NoteName.A, 0, null, defaultNoteLength, defaultVelocity), new NoteInfo(NoteName.C, 1, MusicalTimeSpan.Quarter, specifiedLength, specifiedVelocity) }); }
public void SplitByGrid_MultipleSteps_FromZero_Mixed() { var tempoMap = TempoMap.Default; var obj1 = ObjectMethods.Create(new MidiTimeSpan(), MusicalTimeSpan.Whole, tempoMap); var obj2 = ObjectMethods.Create(new MetricTimeSpan(0, 0, 0, 210), new BarBeatTicksTimeSpan(1, 1), tempoMap); var step1 = MusicalTimeSpan.ThirtySecond; var step2 = new MetricTimeSpan(0, 0, 1); SplitByGrid_MultipleSteps( inputObjects: new[] { obj1, obj2 }, gridStart: (MidiTimeSpan)0, gridSteps: new ITimeSpan[] { step1, step2 }, expectedParts: new Dictionary <TObject, IEnumerable <TimeAndLength> > { [obj1] = new[] { new TimeAndLength(new MusicalTimeSpan(), step1), new TimeAndLength(step1, step2), new TimeAndLength(step1.Add(step2, TimeSpanMode.TimeLength), step1), new TimeAndLength((2 * step1).Add(step2, TimeSpanMode.TimeLength), (MusicalTimeSpan.Whole - 2 * step1).Subtract(step2, TimeSpanMode.LengthLength)) }, [obj2] = new[] { new TimeAndLength(new MetricTimeSpan(0, 0, 0, 210), step1.Add(step2, TimeSpanMode.TimeLength).Subtract(new MetricTimeSpan(0, 0, 0, 210), TimeSpanMode.LengthLength)), new TimeAndLength(step1.Add(step2, TimeSpanMode.TimeLength), step1), new TimeAndLength((2 * step1).Add(step2, TimeSpanMode.TimeLength), step2), new TimeAndLength((2 * step1).Add(step2 + step2, TimeSpanMode.TimeLength), step1), new TimeAndLength((3 * step1).Add(step2 + step2, TimeSpanMode.TimeLength), new MetricTimeSpan(0, 0, 0, 210).Add(new BarBeatTicksTimeSpan(1, 1), TimeSpanMode.TimeLength).Subtract((3 * step1).Add(step2 + step2, TimeSpanMode.TimeLength), TimeSpanMode.TimeTime)), } }, tempoMap: tempoMap); }
static string TrackToString(List <Chord> track, TempoMap tempoMap) { string representation = "\t{.music_chords = (struct MusicChord[]) {\n"; int num_chords = track.Count; MetricTimeSpan lastChordEndTime = new MetricTimeSpan(0); foreach (Chord chord in track) { MetricTimeSpan chordStartTime = TimeConverter.ConvertTo <MetricTimeSpan>(chord.Time, tempoMap); MetricTimeSpan chordDuration = LengthConverter.ConvertTo <MetricTimeSpan>(chord.Length, chord.Time, tempoMap); MetricTimeSpan chordEndTime = chordStartTime + chordDuration; //pad the track if necessary with rests if (lastChordEndTime < chordStartTime) { representation += RestToString(chordStartTime - lastChordEndTime) + ",\n"; num_chords++; } //add the chord's string representation representation += ChordToString(chord, tempoMap); if (track[track.Count - 1] != chord) { representation += ",\n"; } //record the end time for the next chord lastChordEndTime = chordEndTime; } representation += "\n\t}, .playback_type = PLAYBACK_MONO, .length = " + num_chords + "}"; return(representation); }
public void MoveToFirstAnchor_Unkeyed_OneUnkeyedAndOneKeyed() { var moveTime = new MetricTimeSpan(0, 0, 10); var step = new MetricTimeSpan(0, 0, 11); var anchorTime = moveTime + step; var pattern = new PatternBuilder() .MoveToTime(moveTime) .StepForward(step) .Anchor() .MoveToTime(new MetricTimeSpan(0, 0, 30)) .Anchor("Test") .StepBack(new MetricTimeSpan(0, 0, 5)) .MoveToFirstAnchor() .Note(Notes.A0) .Build(); PatternTestUtilities.TestNotes(pattern, new[] { new NoteInfo(NoteName.A, 0, anchorTime, MusicalTimeSpan.Quarter) }); }
private static float GetMetricTimeSpanTotal(MetricTimeSpan ts) { return((float)ts.TotalMicroseconds / 1000000f); }
public void ParseMetricTimeSpan_Valid(string metricTimeSpanString, MetricTimeSpan expectedTimeSpan) { TimeSpanTestUtilities.Parse(metricTimeSpanString, expectedTimeSpan); }
private static MidiTimeSpan GetDefaultMidiTimeSpan(MetricTimeSpan timeSpan) { return(new MidiTimeSpan((timeSpan.TotalMicroseconds * TimeSpanTestUtilities.TicksPerQuarterNote) / Tempo.Default.MicrosecondsPerQuarterNote)); }
private static string Format(MetricTimeSpan timeSpan) { return(timeSpan.Minutes + ":" + timeSpan.Seconds.ToString("00")); }