private static MidiFile CreateTestFile() { const int trackChunksNumber = 100; const int chordsPerTrackChunk = 1000; const int noteLength = 100; const int notesPerChord = 10; var midiFile = new MidiFile(); for (int i = 0; i < trackChunksNumber; i++) { var trackChunk = new TrackChunk(); using (var chordsManager = trackChunk.ManageChords()) { for (int j = 0; j < chordsPerTrackChunk; j++) { var noteNumber = (SevenBitNumber)(j % SevenBitNumber.MaxValue); var notes = Enumerable.Range(0, notesPerChord).Select(_ => new Note(noteNumber, noteLength)); chordsManager.Chords.Add(new Chord(notes, j)); } } midiFile.Chunks.Add(trackChunk); } return(midiFile); }
/// <summary> /// Quantizes chords contained in the specified <see cref="TrackChunk"/>. /// </summary> /// <param name="trackChunk"><see cref="TrackChunk"/> to quantize chords in.</param> /// <param name="grid">Grid to quantize objects by.</param> /// <param name="tempoMap">Tempo map used to calculate times to quantize by.</param> /// <param name="settings">Settings according to which chords should be quantized.</param> /// <exception cref="ArgumentNullException"> /// <para>One of the following errors occured:</para> /// <list type="bullet"> /// <item> /// <description><paramref name="trackChunk"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="grid"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="tempoMap"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> /// <exception cref="InvalidOperationException"> /// <para>One of the following errors occured:</para> /// <list type="bullet"> /// <item> /// <description>Chord is going to be moved beyond zero.</description> /// </item> /// <item> /// <description>Chord's end is going to be moved beyond the chord's fixed end.</description> /// </item> /// </list> /// </exception> public static void QuantizeChords(this TrackChunk trackChunk, IGrid grid, TempoMap tempoMap, ChordsQuantizingSettings settings = null) { ThrowIfArgument.IsNull(nameof(trackChunk), trackChunk); ThrowIfArgument.IsNull(nameof(grid), grid); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); using (var chordsManager = trackChunk.ManageChords(settings?.ChordDetectionSettings)) { new ChordsQuantizer().Quantize(chordsManager.Chords, grid, tempoMap, settings); } }
/// <summary> /// Randomizes chords contained in the specified <see cref="TrackChunk"/>. /// </summary> /// <param name="trackChunk"><see cref="TrackChunk"/> to randomize chords in.</param> /// <param name="bounds">Bounds to randomize time within.</param> /// <param name="tempoMap">Tempo map used to calculate time bounds to randomize within.</param> /// <param name="settings">Settings according to which chords should be randomized.</param> /// <exception cref="ArgumentNullException"> /// <para>One of the following errors occured:</para> /// <list type="bullet"> /// <item> /// <description><paramref name="trackChunk"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="bounds"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="tempoMap"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> public static void RandomizeChords(this TrackChunk trackChunk, IBounds bounds, TempoMap tempoMap, ChordsRandomizingSettings settings = null) { ThrowIfArgument.IsNull(nameof(trackChunk), trackChunk); ThrowIfArgument.IsNull(nameof(bounds), bounds); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); using (var chordsManager = trackChunk.ManageChords(settings?.ChordDetectionSettings)) { new ChordsRandomizer().Randomize(chordsManager.Chords, bounds, tempoMap, settings); } }
/// <summary> /// Randomizes chords contained in the specified <see cref="TrackChunk"/>. /// </summary> /// <param name="trackChunk"><see cref="TrackChunk"/> to randomize chords in.</param> /// <param name="bounds">Bounds to randomize time within.</param> /// <param name="tempoMap">Tempo map used to calculate time bounds to randomize within.</param> /// <param name="settings">Settings according to which chords should be randomized.</param> /// <param name="notesTolerance">Chords tolerance that defines maximum distance of chords from the /// start of the first chord of a chord. Chords within this tolerance will be considered as a chord.</param> /// <exception cref="ArgumentNullException"> /// <para>One of the following errors occured:</para> /// <list type="bullet"> /// <item> /// <description><paramref name="trackChunk"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="bounds"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="tempoMap"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="notesTolerance"/> is negative.</exception> public static void RandomizeChords(this TrackChunk trackChunk, IBounds bounds, TempoMap tempoMap, long notesTolerance = 0, ChordsRandomizingSettings settings = null) { ThrowIfArgument.IsNull(nameof(trackChunk), trackChunk); ThrowIfArgument.IsNull(nameof(bounds), bounds); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); ThrowIfNotesTolerance.IsNegative(nameof(notesTolerance), notesTolerance); using (var chordsManager = trackChunk.ManageChords(notesTolerance)) { new ChordsRandomizer().Randomize(chordsManager.Chords, bounds, tempoMap, settings); } }
/// <summary> /// Quantizes chords contained in the specified <see cref="TrackChunk"/>. /// </summary> /// <param name="trackChunk"><see cref="TrackChunk"/> to quantize chords in.</param> /// <param name="grid">Grid to quantize objects by.</param> /// <param name="tempoMap">Tempo map used to calculate times to quantize by.</param> /// <param name="settings">Settings according to which chords should be quantized.</param> /// <param name="notesTolerance">Chords tolerance that defines maximum distance of chords from the /// start of the first chord of a chord. Chords within this tolerance will be considered as a chord.</param> /// <exception cref="ArgumentNullException"><paramref name="trackChunk"/> is null. -or- /// <paramref name="grid"/> is null. -or- <paramref name="tempoMap"/> is null.</exception> /// <exception cref="InvalidOperationException">Chord is going to be moved beyond zero. -or- /// Chord's end is going to be moved beyond the chord's fixed end.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="notesTolerance"/> is negative.</exception> public static void QuantizeChords(this TrackChunk trackChunk, IGrid grid, TempoMap tempoMap, long notesTolerance = 0, ChordsQuantizingSettings settings = null) { ThrowIfArgument.IsNull(nameof(trackChunk), trackChunk); ThrowIfArgument.IsNull(nameof(grid), grid); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); ThrowIfNotesTolerance.IsNegative(nameof(notesTolerance), notesTolerance); using (var chordsManager = trackChunk.ManageChords(notesTolerance)) { new ChordsQuantizer().Quantize(chordsManager.Chords, grid, tempoMap, settings); } }
private static void SplitTrackChunkChords(TrackChunk trackChunk, Func <ChordsSplitter, IEnumerable <Chord>, IEnumerable <Chord> > splitOperation, long notesTolerance) { using (var chordsManager = trackChunk.ManageChords(notesTolerance)) { var chords = chordsManager.Chords; var chordsSplitter = new ChordsSplitter(); var newChords = splitOperation(chordsSplitter, chords).ToList(); chords.Clear(); chords.Add(newChords); } }
private static void SplitTrackChunkChords(TrackChunk trackChunk, Func <ChordsSplitter, IEnumerable <Chord>, IEnumerable <Chord> > splitOperation, ChordDetectionSettings settings) { using (var chordsManager = trackChunk.ManageChords(settings)) { var chords = chordsManager.Chords; var chordsSplitter = new ChordsSplitter(); var newChords = splitOperation(chordsSplitter, chords).ToList(); chords.Clear(); chords.Add(newChords); } }
public static void ParseAndPlay(string text, string nameSynth = null) { if (nameSynth == null) { // nameSynth = "Microsoft GS Wavetable Synth"; var devices = AllDevices(); nameSynth = devices[R.Next(0, devices.Length)]; } var g = Enum.GetValues(typeof(NoteName)); T2MJsonObject obj = null; obj = JsonConvert.DeserializeObject <T2MJsonObject>(text); var midiFile = new MidiFile(); var cmdsplit = "-".ToCharArray(); #if DEBUG StringBuilder sb = new StringBuilder(); #endif foreach (MidiTrack track in obj.Tracks) { FourBitNumber channel = (FourBitNumber)(track.Channel & 15); int velocity = track.Velocity; var instro = new ProgramChangeEvent((SevenBitNumber)track.Instrument); instro.Channel = channel; var trackChunk = new TrackChunk(instro); #if DEBUG sb.Append("Begin of Chunk" + Environment.NewLine); #endif int length = 40; using (var cm = trackChunk.ManageChords()) { ChordsCollection chords = cm.Chords; var commands = string.Join(" ", track.Commands).Split(' ', '\t', '\r', '\n'); // NoteName tpz = (NoteName)_EXT_.AllNotes.GetValue(0); // TODO Transpose as cmd int t = track.Start; int i = track.Interval; int oct = track.Octave; string cmd; foreach (string v in commands) { if (v.StartsWith("/")) { cmd = v.TrimStart('/'); if (cmd == "_") { t += i; continue; } var n0 = cmd.Split(cmdsplit, StringSplitOptions.RemoveEmptyEntries).Select(x => GetNoteName(x)); var nn = n0; //.FromTemplate(n0); // nn = nn.FromTemplate(tpz); #if DEBUG var m = string.Join(" ", nn.Select(x => x.Name.ToString().Replace("Sharp", "#") + " " + (x.Octave == -1 ? oct : x.Octave))); sb.Append(m + " /"); sb.Append(Environment.NewLine); #endif InterNote[] ni = nn.Select(x => new InterNote(x.Name, x.Octave == -1 ? oct : x.Octave, length, t) { Channel = channel, Velocity = (SevenBitNumber)(velocity & 127) }) // .Concat(new[] { new InterNote(nn.First(), oct + 1, G, t) }) .ToArray(); var chord = new IA.Chord(ni); chords.Add(chord); t += i; } else if (v.StartsWith("*")) { cmd = v.TrimStart('*'); if (int.TryParse(cmd, out int ia)) { oct = ia; } } else if (v.StartsWith("+")) { cmd = v.TrimStart('+'); if (int.TryParse(cmd, out int ia)) { i += ia; } } else if (v.StartsWith("-")) { if (int.TryParse(v, out int ia)) { i += ia; } } else if (v.StartsWith("L")) { cmd = v.TrimStart('L'); if (int.TryParse(cmd, out int ia)) { length = ia; } } else if (v == "_") { t += i; continue; } } cm.SaveChanges(); } midiFile.Chunks.Add(trackChunk); #if DEBUG sb.Append(Environment.NewLine); sb.Append("End of Chunk"); sb.Append(Environment.NewLine); #endif } var appDir = Assembly.GetExecutingAssembly().Location; appDir = Path.GetDirectoryName(appDir); var file = "Im3" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".mid"; file = Path.Combine(appDir, file); midiFile.Write(file); Process.Start(appDir); #if DEBUG var file2 = file + ".txt"; file2 = Path.Combine(appDir, file2); File.WriteAllText(file2, sb.ToString()); Process.Start(file2); #endif using (var outputDevice = OutputDevice.GetByName(nameSynth)) using (var playback = midiFile.GetPlayback(outputDevice)) { // playback.Speed = 2.0; playback.Play(); } }
public static void FractalMusik() { var g = Enum.GetValues(typeof(NoteName)); var midiFile = new MidiFile(); var ch = midiFile.GetChannels(); StringBuilder sb = new StringBuilder(); var trackChunk = new TrackChunk(new ProgramChangeEvent((SevenBitNumber)(0))); sb.Append("Begin of Chunk" + Environment.NewLine); int G = 36; using (var cm = trackChunk.ManageChords()) { ChordsCollection chords = cm.Chords; long t, tof = 0; int oct0 = 3; bool bct = false; NoteName tpz = (NoteName)_EXT_.AllNotes.GetValue(0); for (int i = 0; i < 160; i++) { t = (i * (G + 1)) + tof; var n0 = ta[R.Next(ta.Length)]; var nn = _EXT_.Maj3.FromTemplate(n0); nn = nn.FromTemplate(tpz); int oct = bct ? oct0 : R.Next(3, 7); var m = string.Join(" ", nn.Select(x => x.ToString().Replace("Sharp", "#"))); sb.Append(m + " " + oct); sb.Append(Environment.NewLine); InterNote [] ni = nn.Select(x => new InterNote(x, oct, G, t)) .Concat(new[] { new InterNote(nn.First(), oct + 1, G, t) }) .ToArray(); chords.Add(new IA.Chord(ni)); if (i % 8 == 7) { sb.Append(Environment.NewLine); oct0 = oct; bct = R.NextDouble() > 0.5; // G = R.Next(25, 45); tof += G + 5; tpz = (NoteName)_EXT_.AllNotes.GetValue(R.Next(oct == 3? 4 : 0, _EXT_.AllNotes.Length)); } } } midiFile.Chunks.Add(trackChunk); sb.Append(Environment.NewLine); sb.Append("End of Chunk"); sb.Append(Environment.NewLine); var appDir = Assembly.GetExecutingAssembly().Location; appDir = Path.GetDirectoryName(appDir); var file = "Im3" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".mid"; file = Path.Combine(appDir, file); var file2 = file + ".txt"; file2 = Path.Combine(appDir, file2); midiFile.Write(file); File.WriteAllText(file2, sb.ToString()); Process.Start(appDir); Process.Start(file2); using (var outputDevice = OutputDevice.GetByName("Microsoft GS Wavetable Synth")) using (var playback = midiFile.GetPlayback(outputDevice)) { // playback.Speed = 2.0; playback.Play(); } }