forked from xabblll/DoomInUnityInspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiPlayer.cs
executable file
·133 lines (116 loc) · 3.84 KB
/
MidiPlayer.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.IO;
using AudioSynthesis.Bank;
using AudioSynthesis.Synthesis;
using AudioSynthesis.Sequencer;
using AudioSynthesis.Midi;
using UnityEngine;
namespace UnityMidi
{
[RequireComponent(typeof(AudioSource))]
public class MidiPlayer : MonoBehaviour
{
//TEST
public bool isPlayTestMidiAndBank;
PatchBank bank;
MidiFile midi;
private Synthesizer synthesizer;
private AudioSource audioSource;
private MidiFileSequencer sequencer;
private int bufferHead;
private float[] currentBuffer;
public string midiFilePath;
public string bankPath;
public AudioSource AudioSource { get { return audioSource; } }
public MidiFileSequencer Sequencer { get { return sequencer; } }
public PatchBank Bank { get { return bank; } }
public MidiFile MidiFile { get { return midi; } }
private bool IsPlaying => sequencer.IsPlaying;
private void Start()
{
Init();
}
public void Init()
{
var sampleRate = AudioSettings.outputSampleRate;
synthesizer = new Synthesizer(sampleRate, 2, 8, 8, 64);
sequencer = new MidiFileSequencer(synthesizer);
audioSource = GetComponent<AudioSource>();
if (isPlayTestMidiAndBank)
{
var bankData = File.ReadAllBytes(bankPath);
LoadBank(new PatchBank(new MemoryStream(bankData), Path.GetExtension(bankPath).ToLower().Remove(0, 1)));
var midiFileData = File.ReadAllBytes(midiFilePath);
LoadMidi(new MidiFile(midiFileData));
SetVolume(1f);
Play();
}
}
public void SetLoopMode(bool value)
{
if(sequencer == null)
return;
sequencer.IsLoop = value;
}
public void SetVolume(float volume)
{
synthesizer.MixGain = volume * 2.5f;
}
public void LoadBank(PatchBank bank)
{
this.bank = bank;
synthesizer.UnloadBank();
synthesizer.LoadBank(bank);
}
public void LoadMidi(MidiFile midi)
{
Stop();
this.midi = midi;
sequencer.UnloadMidi();
sequencer.LoadMidi(midi);
}
public void Play()
{
gameObject.SetActive(true);
// sequencer.Seek(sequencer.EndTime * synthesizer.SampleRate);
audioSource.volume = 1f;
sequencer.Play();
audioSource.Play();
// isPlaying = true;
}
public void Stop()
{
// isPlaying = false;
gameObject.SetActive(false);
audioSource.volume = 0f;
synthesizer.NoteOffAll(true);
audioSource.Stop();
sequencer.Stop();
}
void OnAudioFilterRead(float[] data, int channel)
{
// if (!IsPlaying)
// {
// Array.Clear(data, 0, data.Length);
// currentBuffer = null;
// bufferHead = 0;
// return;
// }
int count = 0;
while (count < data.Length)
{
if (currentBuffer == null || bufferHead >= currentBuffer.Length)
{
sequencer.FillMidiEventQueue();
synthesizer.GetNext();
currentBuffer = synthesizer.WorkingBuffer;
bufferHead = 0;
}
var length = Mathf.Min(currentBuffer.Length - bufferHead, data.Length - count);
Array.Copy(currentBuffer, bufferHead, data, count, length);
bufferHead += length;
count += length;
}
}
}
}