melanchall / drywetmidi

.NET library to read, write, process MIDI files and to work with MIDI devices

Home Page:https://melanchall.github.io/drywetmidi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HELP !!! Notes in panel to midi file??

CIRCE-EYES opened this issue · comments

commented

Hi at everybody! I m stuck in a very tiny question of sintax
I have done an example to see the midi notes from a file into a panel.
Ok!

But now just want to export the notes located in the panel to an archive.mid
In a word to make the reverse function!

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.Threading.Tasks;

using Melanchall.DryWetMidi.Devices;
using Melanchall.DryWetMidi.Common;
using Melanchall.DryWetMidi.Interaction;
using Melanchall.DryWetMidi.Core;
using [Melanchall.DryWetMidi.MusicTheory]

namespace panel_basic
{

	public partial class Form1 : Form

	{
		

		public Form1()

		{
			InitializeComponent();
		}

		private const int ALTURA_NOTA = 3;
		private const int ANCHO_NOTA = 6;

		private void Form1_Load(object sender, EventArgs e)
		{

			if (System.IO.File.Exists( "01.mid"))
			{
				Cargar_Notas( "01.mid");
			}
			else
			{
				MessageBox.Show("Not found 01.mid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

		}

		private object Cargar_Notas(string file)
		{

			NoScrollPanel1.Controls.Clear();

			MidiFile midiFile = MidiFile.Read(file);
			var notes = midiFile.GetNotes();

			foreach (var note in midiFile.GetNotes())
			{

				var x = (note.Time * ANCHO_NOTA ) + 127 ;
				var y = 480 - (note.NoteNumber) * ALTURA_NOTA ;
				var width = note.Length * ANCHO_NOTA;
				var height = ANCHO_NOTA;

				Panel rectangulo = new Panel();
				rectangulo.Location = new Point((int)(x / 10), y);
				rectangulo.Size = new Size((int)(width / 10), height);
				rectangulo.BackColor = Color.Cyan;			
				NoScrollPanel1. Controls.Add(rectangulo);

			}
			
			return null;
		}

	}

}
commented

Almost get.. I created a function called exportarnotas to get the notes..But sintaxis error in var note and var trackChunks:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.Threading.Tasks;

using Melanchall.DryWetMidi.Devices;
using Melanchall.DryWetMidi.Common;
using Melanchall.DryWetMidi.Interaction;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.MusicTheory;
using Microsoft.VisualBasic.CompilerServices;

namespace panel_basic
{

	public partial class Form1 : Form

	{
		

		public Form1()

		{
			InitializeComponent();
		}

		private const int ALTURA_NOTA = 3;
		private const int ANCHO_NOTA = 6;

		private void Form1_Load(object sender, EventArgs e)
		{

			if (System.IO.File.Exists( "01.mid"))
			{
				Cargar_Notas( "01.mid");
			}
			else
			{
				MessageBox.Show("Not found 01.mid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

		}

		private object Cargar_Notas(string file)
		{

			NoScrollPanel1.Controls.Clear();

			MidiFile midiFile = MidiFile.Read(file);
			var notes = midiFile.GetNotes();

			foreach (var note in midiFile.GetNotes())
			{

				var x = (note.Time * ANCHO_NOTA ) + 127 ;
				var y = 480 - (note.NoteNumber) * ALTURA_NOTA ;
				var width = note.Length * ANCHO_NOTA;
				var height = ANCHO_NOTA;

				Panel rectangulo = new Panel();
				rectangulo.Location = new Point((int)(x / 10), y);
				rectangulo.Size = new Size((int)(width / 10), height);
				rectangulo.BackColor = Color.Cyan;			
				NoScrollPanel1. Controls.Add(rectangulo);

			}
			
			
			return null;
		}

		private void ExportarNotas(string file)
		{
			var notes = new List<Melanchall.DryWetMidi.Interaction.Note>();

			foreach (var control in NoScrollPanel1.Controls)
			{
				if (control is Panel rectangulo)
				{
					var x = rectangulo.Location.X * 10;
					var y = (480 - rectangulo.Location.Y) / ALTURA_NOTA;
					var width = rectangulo.Size.Width * 10;
					var length = width / ANCHO_NOTA;

					var note = new Melanchall.DryWetMidi.Interaction.Note((SevenBitNumber)y, (MusicalTimeSpan)x, (MusicalTimeSpan)length);
					notes.Add(note);
				}
			}

			var trackChunks = new TrackChunk(notes).ToTrackChunks();
			var midiFile = new MidiFile(trackChunks);
			midiFile.Write(file);
		}




		private void exportmidi_Click(object sender, EventArgs e)
		{
			var saveDialog = new SaveFileDialog();
			saveDialog.Filter = "MIDI Files|*.mid";
			saveDialog.Title = "Export to MIDI";
			var result = saveDialog.ShowDialog();

			if (result != DialogResult.Cancel)
			{
				ExportarNotas(saveDialog.FileName);
				MessageBox.Show("File export correct.", "Exportar a MIDI",
					MessageBoxButtons.OK, MessageBoxIcon.Information);
			}


		}
	}

}


Hi,

First of all,

var note = new Melanchall.DryWetMidi.Interaction.Note((SevenBitNumber)y, (MusicalTimeSpan)x, (MusicalTimeSpan)length);

Where you saw this syntax? DryWetMIDI has complete documentation. So regarding Note: https://melanchall.github.io/drywetmidi/api/Melanchall.DryWetMidi.Interaction.Note.html#Melanchall_DryWetMidi_Interaction_Note__ctor_Melanchall_DryWetMidi_Common_SevenBitNumber_System_Int64_System_Int64_. Please take a look at parameters types. There are no MusicalTimeSpan there, so I unfortunately can't understand what you want.

Well, as for saving notes to a file, it's supereasy. Instead of this:

var trackChunks = new TrackChunk(notes).ToTrackChunks();
var midiFile = new MidiFile(trackChunks);

just write

var midiFile = notes.ToFile();

Use IDE autocomplete please. If you went to ToTrackChunks, you probably could see that Visual Studio suggests you ToFile.

Thanks,
Max

commented

I replaced the correct syntax... But When click export gives me "Overflow"

Whats wrong in this sentence?

var note = new Melanchall.DryWetMidi.Interaction.Note((SevenBitNumber)y, (SevenBitNumber)x, (SevenBitNumber)length);

The whole code..

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.Threading.Tasks;

using Melanchall.DryWetMidi.Devices;
using Melanchall.DryWetMidi.Common;
using Melanchall.DryWetMidi.Interaction;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.MusicTheory;
using Microsoft.VisualBasic.CompilerServices;

namespace panel_basic
{

	public partial class Form1 : Form

	{
		

		public Form1()

		{
			InitializeComponent();
		}

		private const int ALTURA_NOTA = 3;
		private const int ANCHO_NOTA = 6;

		private void Form1_Load(object sender, EventArgs e)
		{

			if (System.IO.File.Exists( "01.mid"))
			{
				Cargar_Notas( "01.mid");
			}
			else
			{
				MessageBox.Show("Not found 01.mid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

		}

		private object Cargar_Notas(string file)
		{

			NoScrollPanel1.Controls.Clear();

			MidiFile midiFile = MidiFile.Read(file);
			var notes = midiFile.GetNotes();

			foreach (var note in midiFile.GetNotes())
			{

				var x = (note.Time * ANCHO_NOTA ) + 127 ;
				var y = 480 - (note.NoteNumber) * ALTURA_NOTA ;
				var width = note.Length * ANCHO_NOTA;
				var height = ANCHO_NOTA;

				Panel rectangulo = new Panel();
				rectangulo.Location = new Point((int)(x / 10), y);
				rectangulo.Size = new Size((int)(width / 10), height);
				rectangulo.BackColor = Color.Cyan;			
				NoScrollPanel1. Controls.Add(rectangulo);

			}
			
			
			return null;
		}

		private void ExportarNotas(string file)
		{
			var notes = new List<Melanchall.DryWetMidi.Interaction.Note>();

			foreach (var control in NoScrollPanel1.Controls)
			{
				if (control is Panel rectangulo)
				{
					var x = rectangulo.Location.X * 10;
					var y = (480 - rectangulo.Location.Y) / ALTURA_NOTA;
					var width = rectangulo.Size.Width * 10;
					var length = width / ANCHO_NOTA;

					var note = new Melanchall.DryWetMidi.Interaction.Note((SevenBitNumber)y, (SevenBitNumber)x, (SevenBitNumber)length);
					notes.Add(note);
				}
			}
			var midifile = notes.ToFile();
			midifile.Write(file,true);	
			}


		private void exportmidi_Click(object sender, EventArgs e)
		{
			var saveDialog = new SaveFileDialog();
			saveDialog.Filter = "MIDI Files|*.mid";
			saveDialog.Title = "Export to MIDI";
			var result = saveDialog.ShowDialog();

			if (result != DialogResult.Cancel)
			{
				ExportarNotas(saveDialog.FileName);
				MessageBox.Show("File export correct.", "Export to MIDI",
					MessageBoxButtons.OK, MessageBoxIcon.Information);
			}


		}
	}

}




Hi,

Please read this article first − Support. This sentence

When click export gives me "Overflow"

tells me nothing. Do you know how to use debugger in Visual Studio? If not, you should definitely learn it.

I can just say some methods you can use. Create a note:

var note = new Note(noteNumber);

To set time and length in terms of ITimeSpan (read this article first):

var note = new Note(noteNumber).SetTime(time).SetLength(length);

I don't know what x, y and so on mean and I have no time to dive into your algorithm, so debug your code by yourself please. Looks like you don't want to learn the library because I can't imagine why you write this:

var note = new Melanchall.DryWetMidi.Interaction.Note((SevenBitNumber)y, (SevenBitNumber)x, (SevenBitNumber)length);

I've provided you a link to the documentation in my previous message where you can see that parameters are of long type. I don't know why you cast your values to SevenBitNumber.

commented

Hi Max.

You ask me a lot and I ask you so little! If I had to learn a whole library of functions for just one function, I think I would go crazy! For you who are the creator of this wonderful library, it is not so much to lend a hand to those who do not know the shortcuts and especially the syntax of it.

As if that were not enough, I have to transfer the functions that I use in some programs from C# to vb.net, it is not my fault that I did not learn C#, I just know vb.net. With respect to the aforementioned function of var notes=..... I

I'll tell you that I'm sorry I can't finish it because I don't know its correct syntax and it's because you think the solution is in the documentation, and sometimes it doesn't seem so simple for the one behind the screen... After all this stuff that I've released... you can summarize it in one thing: I needed your help, because you're the one who created its syntax....

Thank you.

If someone else is following/reading this thread, please I need to solve the damn line,

oh and if I use the debbuger...

Just try to remove casting to SevenBitNumber for x and length:

var note = new Melanchall.DryWetMidi.Interaction.Note((SevenBitNumber)y, x, length);

Also be sure y is between 0 and 127. Casting a value outside of this range can cause an exception.

And ALWAYS send a stacktrace and screenshot if an exception occurred. I can't help you without it. This info also specified in the Support article I've shown above. It's just a little sad that you don't read it :-(

Also length should be the second argument in the constructor, not the third one. And this also is specified in the documentation:

public Note(SevenBitNumber noteNumber, long length, long time)
commented

Yeah! Now IT runs good, but needs to replace the ppq to 960.
I use your : TicksPerQuarterNoteTimeDivision in the write function.
And the result its very good.
Thanks a lot.
I Will post the final result as a very aceptable semi-piano roll viewer and editor notes here in Github