void Start()
    {
        laser = GetComponentInParent <LaserBehaviour>();

        chart            = laser.chart;
        trackToVisualize = laser.trackIndex;
        keysToVisualize  = laser.assignedPitches;

        tiltAngle = Vector3.Angle(transform.rotation * Vector3.up, Vector3.up);

        factory   = GetComponent <NoteFactory>();
        particles = GetComponentInChildren <ParticleSystem>();

        if (keysToVisualize == null || keysToVisualize.Length == 0)
        {
            foreach (var note in chart.tracks[trackToVisualize].notes)
            {
                pendingNotes.Enqueue(note);
            }
        }
        else
        {
            var keySet = new HashSet <int>(keysToVisualize);
            foreach (var note in chart.tracks[trackToVisualize].notes)
            {
                if (keySet.Contains(note.noteNum))
                {
                    pendingNotes.Enqueue(note);
                }
            }
        }
    }
Example #2
0
    public override void OnImportAsset(AssetImportContext ctx)
    {
        MIDIChart chart = ScriptableObject.CreateInstance <MIDIChart>();

        MidiFile midi = MidiFile.Read(ctx.assetPath);

        float beatTicks = ((TicksPerQuarterNoteTimeDivision)midi.TimeDivision).TicksPerQuarterNote;

        var trackList = new List <MIDIChart.Track>();

        foreach (var chunk in midi.Chunks)
        {
            var trackChunk = chunk as TrackChunk;
            if (trackChunk == null)
            {
                continue;
            }

            var noteList = new List <MIDIChart.Note>();

            var notesManager = new NotesManager(trackChunk.Events);
            foreach (var midiNote in notesManager.Notes)
            {
                float noteBeginBeat = midiNote.Time / beatTicks;
                float noteEndBeat   = noteBeginBeat + midiNote.Length / beatTicks;
                noteList.Add(new MIDIChart.Note
                {
                    noteNum      = midiNote.NoteNumber,
                    beginBeat    = noteBeginBeat,
                    endBeat      = noteEndBeat,
                    audioEndBeat = float.PositiveInfinity
                });
            }

            noteList.Sort((MIDIChart.Note x, MIDIChart.Note y) => x.beginBeat.CompareTo(y.beginBeat));

            for (int i = 0; i < noteList.Count; ++i)
            {
                for (int j = i + 1; j < noteList.Count; ++j)
                {
                    if (noteList[j].beginBeat >= noteList[i].endBeat)
                    {
                        noteList[i].audioEndBeat = noteList[j].beginBeat;
                        break;
                    }
                }
            }

            trackList.Add(new MIDIChart.Track {
                notes = noteList
            });
        }

        chart.tracks = trackList;

        ctx.AddObjectToAsset("chart", chart);
        ctx.SetMainObject(chart);
    }