This repository was archived by the owner on Oct 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMidiManager.cs
80 lines (62 loc) · 2.23 KB
/
MidiManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Godot;
[Tool]
public class MidiManager : Node
{
[Export]
public bool autoPlay = true;
[Export]
public bool autoPlayMusic = true;
[Signal]
delegate void NoteEvent(int note, int data, MidiEventNote.NoteType type, int track);
// Example: EmitSignal(12, 127, MidiEventNote.NoteType.NoteOn, 0);
private AnimationPlayer _animationPlayer;
private AudioStreamPlayer _audioStreamPlayer;
// ready
public override void _Ready()
{
if (Engine.EditorHint)
{
// if we don't have an animation player as a child, add one
if (GetNodeOrNull("AnimationPlayer") == null)
{
AnimationPlayer ap = new AnimationPlayer();
AddChild(ap);
// set owner
ap.Owner = GetTree().EditedSceneRoot;
_animationPlayer = ap;
}
// if we don't have an audio stream player as a child, add one
if (GetNodeOrNull("AudioStreamPlayer") == null)
{
AudioStreamPlayer asp = new AudioStreamPlayer();
AddChild(asp);
// set owner
asp.Owner = GetTree().EditedSceneRoot;
_audioStreamPlayer = asp;
}
}
if (_animationPlayer == null)
{
// get first animation player child
_animationPlayer = GetNodeOrNull<AnimationPlayer>("AnimationPlayer");
}
if (_audioStreamPlayer == null)
{
// get first audio stream player child
_audioStreamPlayer = GetNodeOrNull<AudioStreamPlayer>("AudioStreamPlayer");
}
// if auto play is enabled, play the animation
if (autoPlay)
{
_animationPlayer.Play(_animationPlayer.GetAnimationList()[0]);
if (autoPlayMusic)
{
_audioStreamPlayer.Play();
}
}
}
public void NoteEventInput(int note, int data, MidiEventNote.NoteType type, int track)
{
EmitSignal(nameof(NoteEvent), note, data, type, track);
}
}