1 #include <string.h>
2 #include "fx_man.h"
3 #include "hmpplay.h"
4 #include "opl3.h"
5 
6 opl3_chip fm_chip;
7 
8 void FMSendData(uint8_t *data);
9 int FMInit(void);
10 void FMUnInit(void);
11 void FMReset(void);
12 int FMSetBank(void* _bank);
13 
SendData(uint32_t device,uint8_t * data,uint32_t size)14 uint32_t SendData(uint32_t device, uint8_t *data, uint32_t size)
15 {
16     FMSendData(data);
17     return 0;
18 }
19 
20 static const char *hmisignature = "HMIMIDIP013195";
21 static const char *hmisignatureold = "HMIMIDIP";
22 
23 static HMIHeader *song_header[MAX_SONGS];
24 static uint8_t *song_track[MAX_SONGS][MAX_TRACKS];
25 static uint32_t (*song_branchcallback[MAX_SONGS])(uint32_t, uint8_t, uint8_t);
26 static uint32_t (*song_loopcallback[MAX_SONGS])(uint32_t, uint8_t, uint8_t, uint8_t);
27 static uint32_t (*song_triggercallback[MAX_SONGS][127])(uint32_t, uint8_t, uint8_t);
28 static TrackDevice *song_trackmap[MAX_SONGS];
29 static uint32_t song_activetracks[MAX_SONGS];
30 static uint32_t song_totaltracks[MAX_SONGS];
31 static void (*song_callback[MAX_SONGS])(uint32_t);
32 static uint32_t song_trackdeltatime[MAX_SONGS][MAX_TRACKS];
33 static uint32_t song_trackdeltacurrent[MAX_SONGS][MAX_TRACKS];
34 static HMITrack *song_trackheader[MAX_SONGS][MAX_TRACKS];
35 static HMIBranch *song_branchdata[MAX_SONGS][MAX_TRACKS];
36 static uint32_t song_active[MAX_SONGS];
37 static uint8_t song_mastervolume = 0x7f;
38 static uint32_t song_volume[MAX_SONGS] = {
39     0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
40 };
41 static uint8_t mididata[10], mididata1[10];
42 static uint8_t song_branchoccured;
43 static uint32_t song_paused[MAX_SONGS];
44 static uint32_t song_muted[MAX_SONGS];
45 static uint32_t channelmuted[5][16];
46 static uint32_t channelstealing;
47 static uint8_t song_channelremap[5][MAX_SONGS][16] = {
48     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
49     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
50     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
51     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
52     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
53     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
79     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
81     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
88 };
89 
90 static uint8_t devicechannelvolume[5][16] = {
91     0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
92     0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
93     0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
94     0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
95     0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
96 };
97 
98 static uint8_t devicechannelavailable[5][16] = {
99     1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
100     1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
101     1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
102     1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
103     1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1
104 };
105 
106 static uint8_t channelowner[5][16] = {
107     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
108     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
109     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
111     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
112 };
113 
114 static uint8_t ownersong[5][16] = {
115     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
116     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
117     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
118     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
119     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
120 };
121 
122 static uint8_t channelpriority[5][16] = {
123     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
124     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
125     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
126     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
127     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
128 };
129 
130 static uint8_t channeldataindex[5][MAX_SONGS][16] = {
131     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
132     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
133     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
134     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
135     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
136     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
137     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
138     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
139     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
140     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
141     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
142     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
143     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
144     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
145     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
146     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
147     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
148     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
149     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
150     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
151     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
152     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
153     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
154     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
155     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
156     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
157     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
158     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
159     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
160     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
161     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
162     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
163     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
164     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
165     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
166     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
167     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
168     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
169     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
170     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
171 };
172 
173 static ChannelData channeldata[5][16][4] = {
174     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
175     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
176     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
177     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
178     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
179     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
180     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
181     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
182     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
183     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
184     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
185     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
186     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
187     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
188     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
189     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
190     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
191     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
192     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
193     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
194     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
195     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
196     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
197     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
198     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
199     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
200     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
201     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
202     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
203     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
204     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
205     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
206     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
207     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
208     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
209     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
210     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
211     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
212     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
213     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
214     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
215     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
216     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
217     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
218     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
219     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
220     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
221     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
222     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
223     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
224     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
225     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
226     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
227     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
228     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
229     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
230     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
231     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
232     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
233     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
234     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
235     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
236     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
237     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
238     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
239     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
240     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
241     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
242     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
243     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
244     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
245     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
246     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
247     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
248     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
249     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
250     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
251     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
252     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
253     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
254 };
255 
256 static uint32_t song_fadedir[MAX_SONGS];
257 static uint32_t song_fadefrac[MAX_SONGS];
258 static uint32_t song_fadevol[MAX_SONGS];
259 static uint32_t song_fadeticks[MAX_SONGS];
260 static uint8_t song_handlevolumefade[MAX_SONGS];
261 
262 static uint8_t midieventsize[] = {
263     0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 2, 2, 3, 0
264 };
265 
266 static uint8_t midieventsizecontrol[] = {
267     0, 1, 2, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2
268 };
269 
270 static uint32_t song_timerrate[MAX_SONGS] = {
271     0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
272 };
273 
274 static uint32_t song_timeraccum[MAX_SONGS];
275 
276 static uint32_t song_oldhmp[MAX_SONGS];
277 
278 static uint32_t HMIGetDeltaTime(uint8_t *track, uint32_t *delta);
279 static uint32_t HMIResetChannelStealing(uint32_t handle);
280 uint32_t HMIResetSong(uint32_t handle, InitSong *song);
281 uint32_t HMISetSongVolume(uint32_t handle, uint8_t volume);
282 uint32_t HMIHandleMIDIData(uint32_t handle, uint8_t *data, uint32_t device, uint32_t size);
283 void HMISetTrackState(uint32_t handle, uint32_t track, uint32_t branch);
284 uint32_t HMISongDone(uint32_t handle);
285 
HMIInitSong(InitSong * song,TrackDevice * trackmap,uint32_t * handleptr)286 uint32_t HMIInitSong(InitSong *song, TrackDevice *trackmap, uint32_t *handleptr)
287 {
288     MV_Lock();
289     uint32_t trackoffset = 0;
290     uint32_t s, i, j, handle, size, status, dev;
291     uint8_t *branchptr, *trackptr, *branchcount;
292     uint32_t ver = 0;
293     for (s = 0; hmisignature[s]; s++)
294     {
295         if (hmisignature[s] != song->songptr[s])
296         {
297             ver = 1;
298             break;
299         }
300     }
301     if (ver)
302     {
303         for (s = 0; hmisignatureold[s]; s++)
304         {
305             if (hmisignatureold[s] != song->songptr[s])
306             {
307                 MV_Unlock();
308                 return 14;
309             }
310         }
311     }
312     for (j = 0; j < MAX_SONGS; j++)
313     {
314         if (!song_header[j])
315         {
316             handle = j;
317             break;
318         }
319     }
320     if (j == MAX_SONGS)
321     {
322         MV_Unlock();
323         return 11;
324     }
325     for (i = 0; i < MAX_TRACKS; i++)
326     {
327         song_track[handle][i] = NULL;
328     }
329     song_header[handle] = (HMIHeader *)song->songptr;
330     song_oldhmp[handle] = ver;
331     for (i = 0; i < 127; i++)
332     {
333         song_triggercallback[handle][i] = NULL;
334     }
335     song_branchcallback[handle] = NULL;
336     song_loopcallback[handle] = NULL;
337     branchptr = song->songptr + song_header[handle]->branchoffset;
338     trackptr = song->songptr + sizeof(HMIHeader);
339     if (song_oldhmp[handle])
340         trackptr = song->songptr + 0x308;
341     song_trackmap[handle] = trackmap;
342     song_activetracks[handle] = song_header[handle]->numtracks;
343     song_totaltracks[handle] = song_activetracks[handle];
344     song_callback[handle] = song->callback;
345     for (i = 0; i < song_activetracks[handle]; i++)
346     {
347         song_trackdeltatime[handle][i] = 0;
348         song_trackheader[handle][i] = (HMITrack*)(trackptr + trackoffset);
349         song_track[handle][i] = trackptr + trackoffset + sizeof(HMITrack);
350         size = HMIGetDeltaTime(song_track[handle][i], &song_trackdeltacurrent[handle][i]);
351         song_track[handle][i] += size;
352         trackoffset += song_trackheader[handle][i]->size;
353     }
354     branchcount = branchptr;
355     branchptr += song_activetracks[handle];
356     for (i = 0; i < song_activetracks[handle]; i++)
357     {
358         if (*branchcount)
359         {
360             song_branchdata[handle][i] = (HMIBranch*)branchptr;
361         }
362         branchptr += *branchcount * sizeof(HMIBranch);
363         branchcount++;
364     }
365     for (i = 0; i < song_totaltracks[handle]; i++)
366     {
367         if (song_trackmap[handle]->trackdevice[i] == 0xff)
368         {
369             status = 0;
370             dev = 0;
371             while (dev < 5 && song_header[handle]->trackdevice[i][dev] && !status)
372             {
373                 for (j = 0; j < 5; j++)
374                 {
375                     if (song_header[handle]->trackdevice[i][dev] == 0xa002) // OPL2
376                     {
377                         song_trackmap[handle]->trackdevice[i] = j;
378                         status = 1;
379                         break;
380                     }
381                 }
382                 dev++;
383             }
384             if (!song_header[handle]->trackdevice[i][0])
385             {
386                 song_trackmap[handle]->trackdevice[i] = 0;
387             }
388             else if (!status)
389             {
390                 song_track[handle][i] = NULL;
391                 song_trackmap[handle]->trackdevice[i] = 0xff;
392                 song_activetracks[handle]--;
393             }
394         }
395     }
396     if (!song_oldhmp[j])
397     for (j = 0; j < 128; j++)
398     {
399         song_header[handle]->ccrestore[j] = 1;
400     }
401     *handleptr = handle;
402     MV_Unlock();
403     return 0;
404 }
405 
HMIUnInitSong(uint32_t handle)406 uint32_t HMIUnInitSong(uint32_t handle)
407 {
408     if (handle < MAX_SONGS)
409     {
410         MV_Lock();
411         song_header[handle] = NULL;
412         MV_Unlock();
413     }
414     else
415         return 10;
416     return 0;
417 }
418 
HMIStartSong(uint32_t handle)419 uint32_t HMIStartSong(uint32_t handle)
420 {
421     MV_Lock();
422     song_timerrate[handle] = song_header[handle]->tempo;
423     song_timeraccum[handle] = 0;
424     song_active[handle] = 1;
425     MV_Unlock();
426     return 0;
427 }
428 
HMIStopSong(uint32_t handle)429 uint32_t HMIStopSong(uint32_t handle)
430 {
431     InitSong init;
432     if (handle < MAX_SONGS)
433     {
434         MV_Lock();
435         song_timerrate[handle] = 0xffffffff;
436         if (song_active[handle])
437         {
438             init.songptr = (uint8_t*)song_header[handle];
439             init.callback = song_callback[handle];
440             HMIResetChannelStealing(handle);
441             song_active[handle] = 0;
442             song_header[handle] = NULL;
443             HMIResetSong(handle, &init);
444         }
445         MV_Unlock();
446     }
447     else
448         return 10;
449     return 0;
450 }
451 
HMIResetSong(uint32_t handle,InitSong * init)452 uint32_t HMIResetSong(uint32_t handle, InitSong *init)
453 {
454     uint32_t trackoffset = 0;
455     uint32_t i, size;
456     uint8_t *trackptr;
457     MV_Lock();
458     song_header[handle] = (HMIHeader*)init->songptr;
459     trackptr = (uint8_t*)song_header[handle] + sizeof(HMIHeader);
460     if (song_oldhmp[handle])
461         trackptr = (uint8_t*)song_header[handle] + 0x308;
462     song_activetracks[handle] = song_header[handle]->numtracks;
463     song_totaltracks[handle] = song_activetracks[handle];
464     song_callback[handle] = init->callback;
465     for (i = 0; i < song_activetracks[handle]; i++)
466     {
467         song_trackdeltatime[handle][i] = 0;
468         song_trackheader[handle][i] = (HMITrack*)(trackptr + trackoffset);
469         song_track[handle][i] = trackptr + trackoffset + sizeof(HMITrack);
470         size = HMIGetDeltaTime(song_track[handle][i], &song_trackdeltacurrent[handle][i]);
471         song_track[handle][i] += size;
472         trackoffset += song_trackheader[handle][i]->size;
473     }
474     for (i = 0; i < song_totaltracks[handle]; i++)
475     {
476         if (song_trackmap[handle]->trackdevice[i] == 0xff)
477         {
478             song_track[handle][i] = NULL;
479             song_activetracks[handle]--;
480         }
481     }
482     MV_Unlock();
483     return 0;
484 }
485 
HMIGetDeltaTime(uint8_t * track,uint32_t * delta)486 static uint32_t HMIGetDeltaTime(uint8_t *track, uint32_t *delta)
487 {
488     uint8_t b;
489     uint32_t flag = 0, shift = 0, value = 0, offset = 0;
490     do
491     {
492         offset++;
493         b = *track++;
494         if (b & 0x80)
495             flag = 1;
496         b &= 0x7f;
497         value |= b << shift;
498         shift += 7;
499     } while (!flag);
500     *delta = value;
501     return offset;
502 }
503 
HMISetMasterVolume(uint8_t volume)504 uint32_t HMISetMasterVolume(uint8_t volume)
505 {
506     uint32_t i;
507     MV_Lock();
508     song_mastervolume = volume;
509     for (i = 0; i < MAX_SONGS; i++)
510     {
511         if (song_active[i])
512             HMISetSongVolume(i, song_volume[i]);
513     }
514     MV_Unlock();
515     return 0;
516 }
517 
HMIRegisterLoopFunction(uint32_t handle,uint32_t (* callback)(uint32_t,uint8_t,uint8_t,uint8_t))518 uint32_t HMIRegisterLoopFunction(uint32_t handle, uint32_t (*callback)(uint32_t, uint8_t, uint8_t, uint8_t))
519 {
520     song_loopcallback[handle] = callback;
521     return 0;
522 }
523 
HMIRegisterBranchFunction(uint32_t handle,uint32_t (* callback)(uint32_t,uint8_t,uint8_t))524 uint32_t HMIRegisterBranchFunction(uint32_t handle, uint32_t (*callback)(uint32_t, uint8_t, uint8_t))
525 {
526     song_branchcallback[handle] = callback;
527     return 0;
528 }
529 
HMIRegisterTriggerFunction(uint32_t handle,uint32_t track,uint32_t (* callback)(uint32_t,uint8_t,uint8_t))530 uint32_t HMIRegisterTriggerFunction(uint32_t handle, uint32_t track, uint32_t (*callback)(uint32_t, uint8_t, uint8_t))
531 {
532     song_triggercallback[handle][track] = callback;
533     return 0;
534 }
535 
HMIBranchToTrackLocation(uint32_t handle,uint8_t track,uint8_t id)536 uint32_t HMIBranchToTrackLocation(uint32_t handle, uint8_t track, uint8_t id)
537 {
538     uint32_t branch;
539     uint32_t trackoffset, i;
540     uint8_t *ccdata;
541 
542     branch = 0;
543     while (song_branchdata[handle][track][branch].id == id)
544     {
545         branch++;
546     }
547     trackoffset = song_branchdata[handle][track][branch].offset + sizeof(HMITrack);
548     song_track[handle][track] = (uint8_t*)song_trackheader[handle][track] + trackoffset;
549     mididata[0] = 0xb0 | song_trackheader[handle][track]->channel;
550     ccdata = (uint8_t*)song_header[handle] + song_branchdata[handle][track][branch].ccoffset;
551     for (i = 0; i < song_branchdata[handle][track][branch].cccount; i += 2)
552     {
553         mididata[1] = ccdata[i];
554         mididata[2] = ccdata[i + 1];
555         HMIHandleMIDIData(handle, mididata, song_trackmap[handle]->trackdevice[track], 3);
556     }
557     song_branchoccured = 0;
558     return 0;
559 }
560 
HMIBranchToSongLocation(uint32_t handle,uint32_t id)561 uint32_t HMIBranchToSongLocation(uint32_t handle, uint32_t id)
562 {
563     uint8_t i;
564     uint32_t branch, trackoffset, t;
565     for (i = 1; i < song_totaltracks[handle]; i++)
566     {
567         if (song_track[handle][i])
568         {
569             branch = 0;
570             while (song_branchdata[handle][i][branch].id == id)
571             {
572                 branch++;
573             }
574             trackoffset = song_branchdata[handle][i][branch].offset + sizeof(HMITrack);
575             song_track[handle][i] = (uint8_t*)song_trackheader[handle][i] + trackoffset;
576             t = HMIGetDeltaTime(song_track[handle][i], &song_trackdeltacurrent[handle][i]);
577             song_track[handle][i] += t;
578             song_trackdeltatime[handle][i] = 0;
579             HMISetTrackState(handle, i, branch);
580         }
581     }
582     song_branchoccured = 0;
583     return 0;
584 }
585 
HMISongAlterTempo(uint32_t handle,uint32_t value)586 uint32_t HMISongAlterTempo(uint32_t handle, uint32_t value)
587 {
588     uint32_t value2, rate;
589     if (HMISongDone(handle))
590         return 0;
591     value2 = (value << 16) / 100;
592     rate = (song_header[handle]->tempo * value2) >> 16;
593     if (rate == 0)
594         rate = 1;
595     song_timerrate[handle] = rate;
596     song_timeraccum[handle] = 0;
597     return song_timerrate[handle];
598 }
599 
HMIGetTimeToPlay(uint32_t handle)600 uint32_t HMIGetTimeToPlay(uint32_t handle)
601 {
602     return song_header[handle]->timetoplay;
603 }
604 
HMIPauseSong(uint32_t handle,uint32_t mute)605 uint32_t HMIPauseSong(uint32_t handle, uint32_t mute)
606 {
607     uint8_t channel, channelremap;
608     uint32_t i, dev;
609     song_paused[handle] = 1;
610     if (mute)
611     {
612         song_muted[handle] = 1;
613         for (i = 0; i < MAX_TRACKS; i++)
614         {
615             if (song_track[handle][i])
616             {
617                 channel = song_trackheader[handle][i]->channel;
618                 dev = song_trackmap[handle]->trackdevice[i];
619                 if (channelstealing)
620                     channelremap = song_channelremap[dev][handle][channel];
621                 else
622                     channelremap = channel;
623                 mididata1[0] = 0xb0 | channel;
624                 mididata1[1] = 7;
625                 mididata1[2] = devicechannelvolume[dev][channelremap];
626                 HMIHandleMIDIData(handle, mididata1, dev, 3);
627                 channelmuted[dev][channelremap] = 1;
628             }
629         }
630     }
631     return 0;
632 }
633 
HMIResumeSong(uint32_t handle)634 uint32_t HMIResumeSong(uint32_t handle)
635 {
636     uint8_t channel, channelremap;
637     uint32_t i, dev;
638     song_paused[handle] = 0;
639     if (song_muted[handle])
640     {
641         song_muted[handle] = 0;
642         for (i = 0; i < MAX_TRACKS; i++)
643         {
644             if (song_track[handle][i])
645             {
646                 dev = song_trackmap[handle]->trackdevice[i];
647                 channel = song_trackheader[handle][i]->channel;
648                 if (channelstealing)
649                     channelremap = song_channelremap[dev][handle][channel];
650                 else
651                     channelremap = channel;
652                 mididata1[0] = 0xb0 | channel;
653                 mididata1[1] = 7;
654                 mididata1[2] = devicechannelvolume[dev][channelremap];
655                 HMIHandleMIDIData(handle, mididata1, dev, 3);
656                 channelmuted[dev][channelremap] = 0;
657             }
658         }
659     }
660     return 0;
661 }
662 
HMIMuteSong(uint32_t handle)663 uint32_t HMIMuteSong(uint32_t handle)
664 {
665     uint8_t channel;
666     uint32_t i;
667     for (i = 0; i < MAX_TRACKS; i++)
668     {
669         if (song_track[handle][i])
670         {
671             channel = song_trackheader[handle][i]->channel;
672             mididata1[0] = 0xb0 | channel;
673             mididata1[1] = 7;
674             mididata1[2] = 0;
675             HMIHandleMIDIData(handle, mididata1, song_trackmap[handle]->trackdevice[i], 3);
676         }
677     }
678     song_muted[handle] = 1;
679     return 0;
680 }
681 
HMIUnMuteSong(uint32_t handle)682 uint32_t HMIUnMuteSong(uint32_t handle)
683 {
684     uint8_t channel, channelremap;
685     uint32_t i, dev;
686     song_muted[handle] = 0;
687     for (i = 0; i < 32; i++)
688     {
689         if (song_track[handle][i])
690         {
691             dev = song_trackmap[handle]->trackdevice[i];
692             channel = song_trackheader[handle][i]->channel;
693             if (channelstealing)
694                 channelremap = song_channelremap[dev][handle][channel];
695             else
696                 channelremap = channel;
697             mididata1[0] = 0xb0 | channel;
698             mididata1[1] = 7;
699             mididata1[2] = devicechannelvolume[dev][channelremap];
700             HMIHandleMIDIData(handle, mididata1, dev, 3);
701         }
702     }
703     return 0;
704 }
705 
HMISetSongVolume(uint32_t handle,uint8_t volume)706 uint32_t HMISetSongVolume(uint32_t handle, uint8_t volume)
707 {
708     uint8_t channel, channelremap;
709     uint32_t i, dev;
710     song_volume[handle] = volume;
711     for (i = 0; i < 32; i++)
712     {
713         if (song_track[handle][i])
714         {
715             channel = song_trackheader[handle][i]->channel;
716             dev = song_trackmap[handle]->trackdevice[i];
717             if (channelstealing)
718                 channelremap = song_channelremap[dev][handle][channel];
719             else
720                 channelremap = channel;
721             mididata1[0] = 0xb0 | channel;
722             mididata1[1] = 7;
723             mididata1[2] = devicechannelvolume[dev][channelremap];
724             HMIHandleMIDIData(handle, mididata1, song_trackmap[handle]->trackdevice[i], 3);
725         }
726     }
727     return 0;
728 }
729 
HMIFadeSong(uint32_t handle,uint32_t flags,uint32_t speed,uint8_t vol1,uint8_t vol2,uint32_t speed2)730 uint32_t HMIFadeSong(uint32_t handle, uint32_t flags, uint32_t speed, uint8_t vol1, uint8_t vol2, uint32_t speed2)
731 {
732     uint32_t diff, rate, div, ticks, frac;
733     MV_Lock();
734     if (flags & SONG_FADE_IN)
735     {
736         diff = vol2 - vol1;
737     }
738     else
739     {
740         diff = vol1 - vol2;
741     }
742     rate = song_timerrate[handle];
743     div = (100 << 16) / rate;
744     ticks = (speed << 16) / div;
745     ticks = ticks / speed2;
746     if (!ticks)
747     {
748         if (flags & SONG_FADE_OUT_STOP)
749         {
750             HMIStopSong(handle);
751             return 0;
752         }
753         HMISetSongVolume(handle, vol2);
754         return 0;
755     }
756     HMISetSongVolume(handle, vol1);
757     frac = (diff << 16) / ticks;
758     song_fadedir[handle] = flags;
759     song_fadefrac[handle] = frac;
760     song_fadevol[handle] = vol1 << 16;
761     song_fadeticks[handle] = ticks;
762     MV_Unlock();
763     return 0;
764 }
765 
HMISongDone(uint32_t handle)766 uint32_t HMISongDone(uint32_t handle)
767 {
768     uint32_t status;
769     MV_Lock();
770     if (!song_active[handle])
771         status = 1;
772     else
773         status = 0;
774     MV_Unlock();
775     return status;
776 }
777 
HMIHandleMIDIData(uint32_t handle,uint8_t * data,uint32_t device,uint32_t size)778 uint32_t HMIHandleMIDIData(uint32_t handle, uint8_t *data, uint32_t device, uint32_t size)
779 {
780     uint8_t priority = 0, replace = 0xff;
781     uint8_t channel, channelremap, cmd;
782     uint32_t i, vol, j, cmdsize, newvol = 0xffffffff;
783     cmd = data[0];
784     channel = cmd & 0x0f;
785     if (!channelstealing)
786     {
787         switch (data[0] & 0xf0)
788         {
789         case 0xb0:
790             switch (data[1])
791             {
792             case 7:
793                 mididata[0] = data[0];
794                 mididata[1] = 7;
795                 vol = (data[2] * song_volume[handle]) >> 7;
796                 mididata[2] = (vol * song_mastervolume) >> 7;
797                 devicechannelvolume[device][channel] = data[2];
798                 if (song_muted[handle])
799                     mididata[2] = 0;
800                 break;
801             default:
802                 mididata[0] = data[0];
803                 mididata[1] = data[1];
804                 mididata[2] = data[2];
805                 mididata[3] = data[3];
806                 break;
807             }
808             SendData(device, mididata, size);
809             break;
810         default:
811             SendData(device, data, size);
812             break;
813         }
814         return 1;
815     }
816     channelremap = song_channelremap[device][handle][channel];
817 RESTART:
818     if (channelremap != 0xff)
819     {
820         data[0] = (cmd & 0xf0) | channelremap;
821     }
822     else
823     {
824         if (channel == 9)
825         {
826             song_channelremap[device][handle][channel] = 9;
827             channelremap = 9;
828             goto RESTART;
829         }
830         for (i = 0; i < 16; i++)
831         {
832             while (i < 16 && !devicechannelavailable[device][i])
833             {
834                 i++;
835             }
836             if (i < 16 && channelowner[device][i] == 0xff)
837             {
838                 song_channelremap[device][handle][channel] = i;
839                 channelremap = i;
840                 channelowner[device][i] = channel;
841                 ownersong[device][i] = handle;
842                 channelpriority[device][i] = song_header[handle]->priority[channel];
843                 j = channeldataindex[device][handle][channel];
844                 if (j != 0xff)
845                 {
846                     devicechannelvolume[device][channelremap] = 0x7f;
847                     mididata[0] = 0xb0 | channelremap;
848                     mididata[1] = 0x79;
849                     mididata[2] = 0;
850                     cmdsize = 3;
851                     SendData(device, mididata, cmdsize);
852                     if (channeldata[device][channel][j].patch != 0xff)
853                     {
854                         mididata[0] = 0xc0 | channelremap;
855                         mididata[1] = channeldata[device][channel][j].patch;
856                         cmdsize = 2;
857                         SendData(device, mididata, cmdsize);
858                     }
859                     if (channeldata[device][channel][j].bend != 0xff)
860                     {
861                         mididata[0] = 0xe0 | channelremap;
862                         mididata[1] = 0;
863                         mididata[2] = channeldata[device][channel][j].bend;
864                         cmdsize = 2;
865                         SendData(device, mididata, cmdsize);
866                     }
867                     if (channeldata[device][channel][j].volume != 0xff)
868                     {
869                         mididata[0] = 0xb0 | channelremap;
870                         mididata[1] = 7;
871                         mididata[2] = channeldata[device][channel][j].volume;
872                         cmdsize = 3;
873                         SendData(device, mididata, cmdsize);
874                     }
875                     if (channeldata[device][channel][j].sustain != 0xff)
876                     {
877                         mididata[0] = 0xb0 | channelremap;
878                         mididata[1] = 64;
879                         mididata[2] = channeldata[device][channel][j].sustain;
880                         cmdsize = 3;
881                         SendData(device, mididata, cmdsize);
882                     }
883                 }
884                 else
885                 {
886                     for (j = 0; j < 4; j++)
887                     {
888                         if (channeldata[device][channel][j].used == 0xff)
889                         {
890                             channeldata[device][channel][j].used = 1;
891                             channeldataindex[device][handle][channel] = j;
892                             break;
893                         }
894                     }
895                 }
896                 goto RESTART;
897             }
898         }
899         for (i = 0; i < 16; i++)
900         {
901             while (i < 16 && !devicechannelavailable[device][i])
902             {
903                 i++;
904             }
905             if (i < 16)
906             {
907                 if (channelpriority[device][i] > priority && channelpriority[device][i] != 0xff)
908                 {
909                     priority = channelpriority[device][i];
910                     replace = i;
911                 }
912             }
913         }
914         if (replace != 0xff)
915         {
916             if (priority > song_header[handle]->priority[channel])
917             {
918                 song_channelremap[device][handle][channel] = replace;
919                 song_channelremap[device][ownersong[device][replace]][replace] = 0xff;
920                 channelowner[device][replace] = channel;
921                 ownersong[device][replace] = handle;
922                 channelremap = replace;
923                 channelpriority[device][replace] = song_header[handle]->priority[channel];
924                 mididata[0] = 0xb0 | channelremap;
925                 mididata[1] = 0x7b;
926                 mididata[2] = 0;
927                 cmdsize = 3;
928                 SendData(device, mididata, cmdsize);
929                 mididata[0] = 0xb0 | channelremap;
930                 mididata[1] = 0x79;
931                 mididata[2] = 0;
932                 cmdsize = 3;
933                 SendData(device, mididata, cmdsize);
934                 if (channeldataindex[device][handle][channel] == 0xff)
935                 {
936                     for (j = 0; j < 4; j++)
937                     {
938                         if (channeldata[device][channel][j].used == 0xff)
939                         {
940                             channeldata[device][channel][j].used = 1;
941                             channeldataindex[device][handle][channel] = j;
942                             break;
943                         }
944                     }
945                 }
946                 goto RESTART;
947             }
948             if (channeldataindex[device][handle][channel] == 0xff)
949             {
950                 for (j = 0; j < 4; j++)
951                 {
952                     if (channeldata[device][channel][j].used == 0xff)
953                     {
954                         channeldata[device][channel][j].used = 1;
955                         channeldataindex[device][handle][channel] = j;
956                         break;
957                     }
958                 }
959             }
960         }
961     }
962     if (channel != 9)
963     {
964         switch (cmd & 0xf0)
965         {
966         case 0xb0:
967             switch (data[1])
968             {
969             case 7:
970                 channeldata[device][channel][channeldataindex[device][handle][channel]].volume = data[2];
971                 newvol = data[2];
972                 devicechannelvolume[device][channelremap] = newvol;
973                 break;
974             case 64:
975                 channeldata[device][channel][channeldataindex[device][handle][channel]].sustain = data[2];
976                 break;
977             }
978             break;
979         case 0xc0:
980             channeldata[device][channel][channeldataindex[device][handle][channel]].patch = data[1];
981             break;
982         case 0xe0:
983             channeldata[device][channel][channeldataindex[device][handle][channel]].bend = data[2];
984             break;
985         }
986     }
987     else
988     {
989         if (cmd == 0xb9 && data[1] == 7)
990         {
991             newvol = data[2];
992             devicechannelvolume[device][channelremap] = newvol;
993         }
994     }
995 
996     if (channelremap != 0xff)
997     {
998         if (newvol != 0xffffffff)
999         {
1000             if (song_muted[handle])
1001                 data[2] = 0;
1002             else
1003             {
1004                 vol = (song_volume[handle] * newvol) >> 7;
1005                 vol = (song_mastervolume * vol) >> 7;
1006                 data[2] = vol;
1007             }
1008         }
1009         SendData(device, data, size);
1010         data[0] = (cmd & 0xf0) | channel;
1011         if (newvol != 0xffffffff)
1012             data[2] = newvol;
1013         return 0;
1014     }
1015     return -1;
1016 }
1017 
HMIResetChannelStealing(uint32_t handle)1018 uint32_t HMIResetChannelStealing(uint32_t handle)
1019 {
1020     uint8_t channel, channelremap, chandataid;
1021     uint32_t i, device, size;
1022     for (i = 1; i < song_totaltracks[handle]; i++)
1023     {
1024         device = song_trackmap[handle]->trackdevice[i];
1025         if (device != 0xffffffff && device != 0xff)
1026         {
1027             channel = song_trackheader[handle][i]->channel;
1028             if (!channelstealing)
1029             {
1030                 mididata[0] = 0xb0 | channel;
1031                 mididata[1] = 0x7b;
1032                 mididata[2] = 0;
1033                 size = 3;
1034                 SendData(device, mididata, size);
1035                 mididata[0] = 0xb0 | channel;
1036                 mididata[1] = 0x79;
1037                 mididata[2] = 0;
1038                 size = 3;
1039                 SendData(device, mididata, size);
1040                 mididata[0] = 0xe0 | channel;
1041                 mididata[1] = 0x40;
1042                 mididata[2] = 0x40;
1043                 size = 3;
1044                 SendData(device, mididata, size);
1045                 mididata[0] = 0xb0 | channel;
1046                 mididata[1] = 0x7;
1047                 mididata[2] = 0x0;
1048                 size = 3;
1049                 SendData(device, mididata, size);
1050             }
1051             else
1052             {
1053                 channelremap = song_channelremap[device][handle][channel];
1054                 song_channelremap[device][handle][channel] = 0xff;
1055                 chandataid = channeldataindex[device][handle][channel];
1056                 channelowner[device][channelremap] = 0xff;
1057                 ownersong[device][channelremap] = 0xff;
1058                 mididata[0] = 0xb0 | channelremap;
1059                 mididata[2] = 0;
1060                 size = 3;
1061                 SendData(device, mididata, size);
1062                 mididata[0] = 0xb0 | channelremap;
1063                 mididata[1] = 0x79;
1064                 mididata[2] = 0;
1065                 size = 3;
1066                 SendData(device, mididata, size);
1067                 mididata[0] = 0xe0 | channelremap;
1068                 mididata[1] = 0x40;
1069                 mididata[2] = 0x40;
1070                 size = 3;
1071                 SendData(device, mididata, size);
1072                 mididata[0] = 0xb0 | channelremap;
1073                 mididata[1] = 0x7;
1074                 mididata[2] = 0x0;
1075                 size = 3;
1076                 SendData(device, mididata, size);
1077                 if (chandataid != 0xff)
1078                 {
1079                     channeldata[device][channel][chandataid].patch = 0xff;
1080                     channeldata[device][channel][chandataid].bend = 0xff;
1081                     channeldata[device][channel][chandataid].volume = 0xff;
1082                     channeldata[device][channel][chandataid].sustain = 0xff;
1083                     channeldata[device][channel][chandataid].used = 0xff;
1084                     channeldataindex[device][handle][channel] = 0xff;
1085                 }
1086             }
1087         }
1088     }
1089     return 1;
1090 }
1091 
HMISendMIDIData(uint32_t device,uint8_t * data,uint32_t size)1092 uint32_t HMISendMIDIData(uint32_t device, uint8_t *data, uint32_t size)
1093 {
1094     return SendData(device, data, size);
1095 }
1096 
HMIEnableChannelStealing(uint32_t enable)1097 uint32_t HMIEnableChannelStealing(uint32_t enable)
1098 {
1099     uint32_t val;
1100     val = channelstealing;
1101     channelstealing = enable;
1102     return val;
1103 }
1104 
HMISongHandler(uint32_t handle)1105 void HMISongHandler(uint32_t handle)
1106 {
1107     void (*callback)(uint32_t);
1108     InitSong init;
1109     uint32_t v18 = 1, track, size, id, branch, loop, j, jbranch, trackoffset;
1110     if (!song_active[handle] || song_paused[handle])
1111         return;
1112 
1113     if (song_fadeticks[handle])
1114     {
1115         if (--song_handlevolumefade[handle] == 0)
1116         {
1117             song_handlevolumefade[handle] = 3;
1118             song_fadeticks[handle]--;
1119             switch (song_fadedir[handle])
1120             {
1121             case SONG_FADE_OUT:
1122             case SONG_FADE_OUT_STOP:
1123                 song_fadevol[handle] -= song_fadefrac[handle];
1124                 HMISetSongVolume(handle, song_fadevol[handle] >> 16);
1125                 if ((song_fadedir[handle] & SONG_FADE_OUT_STOP) && !song_fadeticks[handle])
1126                 {
1127                     song_active[handle] = 0;
1128                     HMIResetChannelStealing(handle);
1129                     song_timerrate[handle] = 0xffffffff;
1130                     callback = song_callback[handle];
1131                     init.songptr = (uint8_t*)song_header[handle];
1132                     init.callback = callback;
1133                     song_header[handle] = NULL;
1134                     HMIResetSong(handle, &init);
1135                     if (callback)
1136                         callback(handle);
1137                     return;
1138                 }
1139                 break;
1140             case SONG_FADE_IN:
1141                 song_fadevol[handle] += song_fadefrac[handle];
1142                 HMISetSongVolume(handle, song_fadevol[handle] >> 16);
1143                 break;
1144             }
1145         }
1146     }
1147     for (track = 0; track < song_totaltracks[handle]; track++)
1148     {
1149         song_trackdeltatime[handle][track]++;
1150         if (song_track[handle][track])
1151         {
1152             if (song_trackdeltacurrent[handle][track] <= song_trackdeltatime[handle][track])
1153             {
1154                 do
1155                 {
1156                     song_branchoccured = 0;
1157                     v18 = 1;
1158                     if (song_track[handle][track][0] < 0xf0)
1159                     {
1160                         size = midieventsize[song_track[handle][track][0] >> 4];
1161                     }
1162                     else
1163                     {
1164                         size = midieventsizecontrol[song_track[handle][track][0] & 0x0f];
1165                     }
1166                     if (song_track[handle][track][0] == 0xff)
1167                     {
1168                         switch (song_track[handle][track][1])
1169                         {
1170                         case 0x2f:
1171                             song_track[handle][track] = NULL;
1172                             if (song_activetracks[handle] - 1 == 1 && song_track[handle][0] != NULL)
1173                             {
1174                                 song_activetracks[handle]--;
1175                                 song_track[handle][0] = NULL;
1176                             }
1177                             song_activetracks[handle]--;
1178                             if (song_activetracks[handle] == 0)
1179                             {
1180                                 song_active[handle] = 0;
1181                                 HMIResetChannelStealing(handle);
1182                                 song_timerrate[handle] = 0xffffffff;
1183                                 callback = song_callback[handle];
1184                                 init.songptr = (uint8_t*)song_header[handle];
1185                                 init.callback = callback;
1186                                 song_header[handle] = NULL;
1187                                 HMIResetSong(handle, &init);
1188                                 if (callback)
1189                                     callback(handle);
1190                                 return;
1191                             }
1192                             size = 3;
1193                             break;
1194                         case 0x5f:
1195                             size = 5;
1196                             break;
1197                         }
1198                     }
1199                     else
1200                     {
1201                         if ((song_track[handle][track][0] & 0xf0) == 0xb0)
1202                         {
1203                             switch (song_track[handle][track][1])
1204                             {
1205                             case 103:
1206                                 if (!song_oldhmp[handle])
1207                                 song_header[handle]->ccrestore[song_track[handle][track][2]] = 0;
1208                                 break;
1209                             case 104:
1210                                 if (!song_oldhmp[handle])
1211                                 song_header[handle]->ccrestore[song_track[handle][track][2]] = 1;
1212                                 break;
1213                             case 108:
1214                                 break;
1215                             case 110:
1216                                 break;
1217                             case 109:
1218                                 id = song_track[handle][track][2];
1219                                 branch = 0;
1220                                 while (song_branchdata[handle][track][branch].id != id)
1221                                 {
1222                                     branch++;
1223                                 }
1224                                 song_branchdata[handle][track][branch].loopcount = song_track[handle][track][6];
1225                                 break;
1226                             case 111:
1227                             case 112:
1228                                 id = song_track[handle][track][2];
1229                                 branch = 0;
1230                                 while (song_branchdata[handle][track][branch].id != id)
1231                                 {
1232                                     branch++;
1233                                 }
1234                                 loop = song_branchdata[handle][track][branch].loopcount;
1235                                 if (loop != 0xff && loop != 0)
1236                                 {
1237                                     song_branchdata[handle][track][branch].loopcount--;
1238                                     loop--;
1239                                 }
1240                                 if (song_loopcallback[handle])
1241                                 {
1242                                     song_branchoccured = 1;
1243                                     if (!song_loopcallback[handle](handle, track, id, loop))
1244                                         loop = 0;
1245                                     if (!song_branchoccured)
1246                                     {
1247                                         v18 = 0;
1248                                         size = 0;
1249                                     }
1250                                     else
1251                                         song_branchoccured = 0;
1252                                 }
1253                                 if (loop)
1254                                 {
1255                                     for (j = 1; j < song_totaltracks[handle]; j++)
1256                                     {
1257                                         if (song_track[handle][j])
1258                                         {
1259                                             jbranch = 0;
1260                                             while (song_branchdata[handle][j][jbranch].id != id)
1261                                             {
1262                                                 jbranch++;
1263                                             }
1264                                             trackoffset = song_branchdata[handle][j][jbranch].offset + sizeof(HMITrack);
1265                                             song_track[handle][j] = (uint8_t*)song_trackheader[handle][j] + sizeof(HMITrack);
1266                                             size = HMIGetDeltaTime(song_track[handle][j], &song_trackdeltacurrent[handle][j]);
1267                                             song_track[handle][j] += size;
1268                                             song_trackdeltatime[handle][j] = 0;
1269                                             v18 = 0;
1270                                             HMISetTrackState(handle, j, jbranch);
1271                                         }
1272                                     }
1273                                     size = 0;
1274                                 }
1275                                 break;
1276                             case 113:
1277                                 break;
1278                             case 114:
1279                                 id = song_track[handle][track][2];
1280                                 branch = 0;
1281                                 while (song_branchdata[handle][track][branch].id != id)
1282                                 {
1283                                     branch++;
1284                                 }
1285                                 loop = 1;
1286                                 if (song_branchcallback[handle])
1287                                 {
1288                                     song_branchoccured = 1;
1289                                     if (!song_branchcallback[handle](handle, track, id))
1290                                         loop = 1;
1291                                     if (!song_branchoccured)
1292                                     {
1293                                         v18 = 0;
1294                                         size = 0;
1295                                     }
1296                                     else
1297                                         song_branchoccured = 0;
1298                                 }
1299                                 if (loop)
1300                                 {
1301                                     for (j = 1; j < song_totaltracks[handle]; j++)
1302                                     {
1303                                         if (song_track[handle][j])
1304                                         {
1305                                             jbranch = 0;
1306                                             while (song_branchdata[handle][j][jbranch].id != id)
1307                                             {
1308                                                 jbranch++;
1309                                             }
1310                                             trackoffset = song_branchdata[handle][j][jbranch].offset + sizeof(HMITrack);
1311                                             song_track[handle][j] = (uint8_t*)song_trackheader[handle][j] + sizeof(HMITrack);
1312                                             size = HMIGetDeltaTime(song_track[handle][j], &song_trackdeltacurrent[handle][j]);
1313                                             song_track[handle][j] += size;
1314                                             song_trackdeltatime[handle][j] = 0;
1315                                             v18 = 0;
1316                                             HMISetTrackState(handle, j, jbranch);
1317                                         }
1318                                     }
1319                                     size = 0;
1320                                 }
1321                                 break;
1322                             case 116:
1323                                 break;
1324                             case 115:
1325                                 id = song_track[handle][track][2];
1326                                 branch = 0;
1327                                 while (song_branchdata[handle][track][branch].id != id)
1328                                 {
1329                                     branch++;
1330                                 }
1331                                 song_branchdata[handle][track][branch].loopcount = song_track[handle][track][6];
1332                                 break;
1333                             case 117:
1334                             case 118:
1335                                 id = song_track[handle][track][2];
1336                                 branch = 0;
1337                                 while (song_branchdata[handle][track][branch].id != id)
1338                                 {
1339                                     branch++;
1340                                 }
1341                                 loop = song_branchdata[handle][track][branch].loopcount;
1342                                 if (loop != 0xff && loop != 0)
1343                                 {
1344                                     song_branchdata[handle][track][branch].loopcount--;
1345                                     loop--;
1346                                 }
1347                                 if (song_loopcallback[handle])
1348                                 {
1349                                     song_branchoccured = 1;
1350                                     if (!song_loopcallback[handle](handle, track, id, loop))
1351                                         loop = 0;
1352                                     if (!song_branchoccured)
1353                                         size = 0;
1354                                     else
1355                                         song_branchoccured = 0;
1356                                 }
1357                                 if (loop)
1358                                 {
1359                                     trackoffset = song_branchdata[handle][track][branch].offset + sizeof(HMITrack);
1360                                     song_track[handle][track] = (uint8_t*)song_trackheader[handle][track] + sizeof(HMITrack);
1361                                     HMISetTrackState(handle, track, branch);
1362                                     size = 0;
1363                                 }
1364                                 break;
1365                             case 119:
1366                                 loop = song_track[handle][track][2];
1367                                 if (song_triggercallback[handle][loop])
1368                                 {
1369                                     song_branchoccured = 1;
1370                                     song_triggercallback[handle][loop];
1371                                     if (!song_branchoccured)
1372                                     {
1373                                         v18 = 0;
1374                                         size = 0;
1375                                     }
1376                                     else
1377                                         song_branchoccured = 0;
1378                                 }
1379                                 break;
1380                             case 120:
1381                                 break;
1382                             case 121:
1383                                 id = song_track[handle][track][2];
1384                                 branch = 0;
1385                                 while (song_branchdata[handle][track][branch].id != id)
1386                                 {
1387                                     branch++;
1388                                 }
1389                                 loop = 1;
1390                                 if (song_branchcallback[handle])
1391                                 {
1392                                     song_branchoccured = 1;
1393                                     if (!song_branchcallback[handle](handle, track, id))
1394                                         loop = 0;
1395                                     if (!song_branchoccured)
1396                                         size = 0;
1397                                     else
1398                                         song_branchoccured = 0;
1399                                 }
1400                                 if (loop)
1401                                 {
1402                                     trackoffset = song_branchdata[handle][track][branch].offset + sizeof(HMITrack);
1403                                     song_track[handle][track] = (uint8_t*)song_trackheader[handle][track] + sizeof(HMITrack);
1404                                     HMISetTrackState(handle, track, branch);
1405                                     size = 0;
1406                                 }
1407                                 break;
1408                             default:
1409                                 goto default_case;
1410                                 break;
1411                             }
1412                         }
1413                         else
1414                         {
1415                     default_case:
1416                             if (track)
1417                             {
1418                                 HMIHandleMIDIData(handle, song_track[handle][track], song_trackmap[handle]->trackdevice[track], size);
1419                             }
1420                         }
1421                     }
1422                     if (!song_branchoccured)
1423                         song_trackdeltatime[handle][track] = 0;
1424                     if (!song_track[handle][track])
1425                         break;
1426                     song_track[handle][track] += size;
1427                     if (v18)
1428                     {
1429                         size = HMIGetDeltaTime(song_track[handle][track], &song_trackdeltacurrent[handle][track]);
1430                         song_track[handle][track] += size;
1431                     }
1432                 } while (!song_trackdeltacurrent[handle][track]);
1433             }
1434         }
1435     }
1436 }
1437 
HMISetTrackState(uint32_t handle,uint32_t track,uint32_t branch)1438 void HMISetTrackState(uint32_t handle, uint32_t track, uint32_t branch)
1439 {
1440     uint8_t mevent[3];
1441     uint8_t *ccdata;
1442     uint32_t i;
1443     // TODO:
1444     if (song_oldhmp[handle])
1445         return;
1446     if (song_header[handle]->ccrestore[108])
1447     {
1448         mevent[0] = 0xc0 | (song_track[handle][track][0] & 0xf);
1449         mevent[1] = song_branchdata[handle][track][branch].patch;
1450         HMIHandleMIDIData(handle, mevent, song_trackmap[handle]->trackdevice[track], 2);
1451     }
1452     mevent[0] = 0xb0 | (song_track[handle][track][0] & 0xf);
1453     ccdata = (uint8_t*)song_header[handle] + song_branchdata[handle][track][branch].ccoffset;
1454     for (i = 0; i < song_branchdata[handle][track][branch].cccount; i += 2)
1455     {
1456         mevent[1] = ccdata[i];
1457         mevent[2] = ccdata[i + 1];
1458         if (song_header[handle]->ccrestore[mevent[1]])
1459         {
1460             HMIHandleMIDIData(handle, mevent, song_trackmap[handle]->trackdevice[track], 3);
1461         }
1462     }
1463 }
1464 
1465 
1466 static int mixrate = 49716;
1467 static MV_MusicRoutineBuffer musicbuffer;
1468 static int hmiinit;
1469 extern int MV_Channels;
1470 
HMIFill(void)1471 static void HMIFill(void)
1472 {
1473     int16_t *stream16 = (int16_t*)musicbuffer.buffer;
1474     int len = musicbuffer.size / (2 * MV_Channels);
1475     for (int i = 0; i < len; i++)
1476     {
1477         Bit16s sampl[2] = {};
1478         for (int j = 0; j < MAX_SONGS; j++)
1479         {
1480             if (song_timerrate[j] == 0xffffffff)
1481                 continue;
1482             song_timeraccum[j] += song_timerrate[j];
1483             while (song_timeraccum[j] >= mixrate)
1484             {
1485                 song_timeraccum[j] -= mixrate;
1486                 HMISongHandler(j);
1487             }
1488         }
1489         OPL3_GenerateResampled(&fm_chip, sampl);
1490         if (MV_Channels == 2)
1491         {
1492             *stream16++ = sampl[0];
1493             *stream16++ = sampl[1];
1494         }
1495         else
1496         {
1497             *stream16++ = (sampl[0] + sampl[1]) / 2;
1498         }
1499     }
1500 }
1501 
HMIInit(int rate)1502 void HMIInit(int rate)
1503 {
1504     if (hmiinit)
1505         return;
1506     mixrate = rate;
1507     musicbuffer = MV_GetMusicRoutineBuffer();
1508     MV_HookMusicRoutine(HMIFill);
1509     OPL3_Reset(&fm_chip, rate);
1510     FMInit();
1511     FMReset();
1512     hmiinit = 1;
1513 }
1514 
HMIUnInit(void)1515 void HMIUnInit(void)
1516 {
1517     if (!hmiinit)
1518         return;
1519     MV_UnhookMusicRoutine();
1520     for (int i = 0; i < MAX_SONGS; i++)
1521     {
1522         if (song_header[i])
1523         {
1524             HMIStopSong(i);
1525             HMIUnInitSong(i);
1526         }
1527     }
1528     hmiinit = 0;
1529 }
1530