예제 #1
0
        public static void Test()
        {
            var      midiFile = new MidiFile();
            TempoMap tempoMap = midiFile.GetTempoMap();

            var trackChunk = new TrackChunk();

            using (var notesManager = trackChunk.ManageNotes())
            {
                NotesCollection notes = notesManager.Notes;

                var      g = Enum.GetValues(typeof(NoteName));
                NoteName n = (NoteName)g.GetValue(MainWindow._rnd.Next(g.Length));
                notes.Add(new InterNote(n, 4, LC.ConvertFrom(
                                            new MetricTimeSpan(hours: 0, minutes: 0, seconds: 2), 0, tempoMap)));

                n = (NoteName)g.GetValue(MainWindow._rnd.Next(g.Length));
                notes.Add(new InterNote(n, 3, LC.ConvertFrom(
                                            new MetricTimeSpan(hours: 0, minutes: 0, seconds: 1, milliseconds: 500), 0, tempoMap)));
                n = (NoteName)g.GetValue(MainWindow._rnd.Next(g.Length));

                notes.Add(new InterNote(n, 5, LC.ConvertFrom(
                                            new MetricTimeSpan(hours: 0, minutes: 0, seconds: 3), 0, tempoMap)));
            }

            midiFile.Chunks.Add(trackChunk);

            using (var outputDevice = OutputDevice.GetByName("Microsoft GS Wavetable Synth"))
                using (var playback = midiFile.GetPlayback(outputDevice))
                {
                    // playback.Speed = 2.0;
                    playback.Play();
                }
        }
예제 #2
0
        private static void ResizeNotesByRatio(IEnumerable <Note> notes,
                                               double ratio,
                                               TimeSpanType distanceCalculationType,
                                               TempoMap tempoMap,
                                               ITimeSpan startTime)
        {
            foreach (var note in notes)
            {
                var noteLength = note.LengthAs(distanceCalculationType, tempoMap);
                var noteTime   = note.TimeAs(distanceCalculationType, tempoMap);

                var scaledShiftFromStart = noteTime.Subtract(startTime, TimeSpanMode.TimeTime).Multiply(ratio);
                note.Time = TimeConverter.ConvertFrom(startTime.Add(scaledShiftFromStart, TimeSpanMode.TimeLength), tempoMap);

                var scaledLength = noteLength.Multiply(ratio);
                note.Length = LengthConverter.ConvertFrom(scaledLength, note.Time, tempoMap);
            }
        }
예제 #3
0
        public void InitPlay()
        {
            _midiFile = new MidiFile();
            TempoMap tempoMap = _midiFile.GetTempoMap();

            var trackChunk = new TrackChunk();

            using (var notesManager = trackChunk.ManageNotes())
            {
                NotesCollection notes = notesManager.Notes;
                var             len   = LC.ConvertFrom(new MetricTimeSpan(hours: 0, minutes: 0, seconds: 1), 0, tempoMap);

                notes.Add(new InterNote(Note, Octave, len));
            }


            _midiFile.Chunks.Add(trackChunk);
        }
예제 #4
0
        private static long ConvertFromTimeLength(MathTimeSpan mathTimeSpan, long time, TempoMap tempoMap)
        {
            var convertedTimeSpan1 = TimeConverter.ConvertFrom(mathTimeSpan.TimeSpan1, tempoMap);

            switch (mathTimeSpan.Operation)
            {
            case MathOperation.Add:
                return(convertedTimeSpan1 + LengthConverter.ConvertFrom(mathTimeSpan.TimeSpan2,
                                                                        convertedTimeSpan1,
                                                                        tempoMap));

            case MathOperation.Subtract:
                return(convertedTimeSpan1 - LengthConverter.ConvertFrom(mathTimeSpan.TimeSpan2,
                                                                        convertedTimeSpan1,
                                                                        tempoMap.Flip(convertedTimeSpan1)));

            default:
                throw new ArgumentException($"{mathTimeSpan.Operation} is not supported by the converter.", nameof(mathTimeSpan));
            }
        }
        /// <summary>
        /// Gets points in time of the current grid.
        /// </summary>
        /// <param name="tempoMap">Tempo map used to get grid's times.</param>
        /// <returns>Collection of points in time of the current grid.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="tempoMap"/> is <c>null</c>.</exception>
        public IEnumerable <long> GetTimes(TempoMap tempoMap)
        {
            ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap);

            if (!Steps.Any())
            {
                yield break;
            }

            var time = TimeConverter.ConvertFrom(Start, tempoMap);

            yield return(time);

            while (true)
            {
                foreach (var step in Steps)
                {
                    time += LengthConverter.ConvertFrom(step, time, tempoMap);
                    yield return(time);
                }
            }
        }