Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: How to read MIDI notes properly

$
0
0
Hello,

I'm trying to read the MIDI notes of one track with 6 channels properly, so I can export it to C++ code which can be read by another device (Arduino).

The first few notes sound right, but at some point it starts sounding weird. The start and end times are being calculated wrong, but I don't know why.

This is the full code, it's not much tho.

This is the MIDI file, which I edited to have 6 channels: http://dev-ch.com/files/818e6e15-8e92-1b1b-1a18-d0d8ffedcf23/piratesofthecarribean.mid

Any help is very welcome!
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using NAudio.Midi;

namespace MidiToCode
{
    public static class Program
    {
        public static void Main()
        {
            foreach (FileInfo midiFile in new DirectoryInfo(Path.Combine(Application.StartupPath, "Input")).GetFiles("*.mid"))
            {
                string name = Path.GetFileNameWithoutExtension(midiFile.Name);

                StreamWriter file = File.CreateText(Path.Combine(Application.StartupPath, "Output", "Song_" + name + ".h"));
                List<string>[] notes = new List<string>[6];

                for (int channel = 0; channel < 6; channel++)
                {
                    notes[channel] = new List<string>();
                    MidiFile midi = new MidiFile(midiFile.FullName);
                    int noteStart = 0, noteEnd = 0, noteNumber = 0;
                    bool recording = false;

                    foreach (MidiEvent note in midi.Events[0])
                    {
                        if (note.Channel - 1 == channel)
                        {
                            if (note.CommandCode == MidiCommandCode.NoteOff)
                            {
                                noteStart = (int)(note.AbsoluteTime - note.DeltaTime);
                                if (noteStart > 65535 || noteEnd > 65535) throw new Exception();
                            }
                            else if (note.CommandCode == MidiCommandCode.NoteOn)
                            {
                                noteEnd = (int)(note.AbsoluteTime - note.DeltaTime);
                                noteNumber = (note as NoteOnEvent).NoteNumber;

                                if (recording)
                                {
                                    notes[channel].Add
                                    (
                                        "\t" +
                                        "0x" + (noteStart >> 8).ToString("x2") + ", " +
                                        "0x" + (noteStart & 0xff).ToString("x2") + ", " +
                                        "0x" + (noteEnd >> 8).ToString("x2") + ", " +
                                        "0x" + (noteEnd & 0xff).ToString("x2") + ", " +
                                        "0x" + noteNumber.ToString("x2") + ","
                                    );
                                }
                                recording = true;
                            }
                        }
                    }
                }

                file.WriteLine("CreateSong(" + name + ")");
                file.WriteLine("{");
                for (int i = 0, count = 24; i < 6; i++)
                {
                    file.WriteLine("\t" +
                        "0x" + (count >> 8).ToString("x2") + ", " +
                        "0x" + (count & 0xff).ToString("x2") + ", " +
                        "0x" + (notes[i].Count >> 8).ToString("x2") + ", " +
                        "0x" + (notes[i].Count & 0xff).ToString("x2") + ",");
                    count += notes[i].Count * 5;
                }
                file.WriteLine();
                for (int i = 0; i < 6; i++)
                {
                    notes[i].ForEach(itm => file.WriteLine(itm));
                }
                file.Write("};");
                file.Close();
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>