1 /*
2 ** midisource_mids.cpp
3 ** Code to let ZMusic play MIDS MIDI music through the MIDI streaming API.
4 **
5 **---------------------------------------------------------------------------
6 ** Copyright 2020 Cacodemon345
7 ** All rights reserved.
8 **
9 ** Redistribution and use in source and binary forms, with or without
10 ** modification, are permitted provided that the following conditions
11 ** are met:
12 **
13 ** 1. Redistributions of source code must retain the above copyright
14 **    notice, this list of conditions and the following disclaimer.
15 ** 2. Redistributions in binary form must reproduce the above copyright
16 **    notice, this list of conditions and the following disclaimer in the
17 **    documentation and/or other materials provided with the distribution.
18 ** 3. The name of the author may not be used to endorse or promote products
19 **    derived from this software without specific prior written permission.
20 **
21 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 **---------------------------------------------------------------------------
32 **
33 */
34 
35 // HEADER FILES ------------------------------------------------------------
36 
37 #include <algorithm>
38 #include "midisource.h"
39 #include "zmusic/m_swap.h"
40 
41 // MACROS ------------------------------------------------------------------
42 
43 // TYPES -------------------------------------------------------------------
44 
45 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
46 
47 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
48 
49 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
50 
51 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
52 
53 // PRIVATE DATA DEFINITIONS ------------------------------------------------
54 
55 // PUBLIC DATA DEFINITIONS -------------------------------------------------
56 
57 // CODE --------------------------------------------------------------------
58 
59 //==========================================================================
60 //
61 //  MIDSSong constructor
62 //
63 //  Reads the buffers from the file and validates the MIDS header
64 //
65 //==========================================================================
66 
MIDSSong(const uint8_t * data,size_t len)67 MIDSSong::MIDSSong(const uint8_t* data, size_t len)
68 {
69     if (len <= 52)
70         return;
71 
72     if ((len % 4) != 0)
73         return;
74 
75     // Validate the header first.
76     if (data[12] != 'f' || data[13] != 'm' || data[14] != 't' || data[15] != ' ')
77     {
78         return;
79     }
80     int headerSize = LittleLong(GetInt(&data[16]));
81     if (headerSize != 12) return;
82     Division = LittleLong(GetInt(&data[20]));
83     FormatFlags = LittleLong(GetInt(&data[28]));
84     // Validate the data chunk.
85     if (data[32] != 'd' || data[33] != 'a' || data[34] != 't' || data[35] != 'a')
86     {
87         return;
88     }
89     int NumBlocks = LittleLong(GetInt(&data[40]));
90     const uint32_t* midiData = (const uint32_t*)&data[44];
91     uint32_t tkStart = 0;
92     uint32_t cbBuffer = 0;
93     while (NumBlocks-- > 0)
94     {
95         tkStart = LittleLong(*midiData);
96         cbBuffer = LittleLong(*(midiData + 1));
97         midiData += 2;
98         if ((cbBuffer % (FormatFlags ? 8 : 12)) != 0) return;
99         MidsBuffer.insert(MidsBuffer.end(), midiData, midiData + (cbBuffer / 4));
100         midiData += cbBuffer / 4;
101     }
102     MidsP = 0;
103     MaxMidsP = MidsBuffer.size() - 1;
104     for (auto& curMidiData : MidsBuffer)
105     {
106         curMidiData = LittleLong(curMidiData);
107     }
108 }
109 
110 //==========================================================================
111 //
112 // MIDSSong :: DoInitialSetup
113 //
114 // Sets the starting channel volumes.
115 //
116 //==========================================================================
117 
DoInitialSetup()118 void MIDSSong::DoInitialSetup()
119 {
120 	for (int i = 0; i < 16; ++i)
121 	{
122 		ChannelVolumes[i] = 100;
123 	}
124 }
125 
126 //==========================================================================
127 //
128 // MIDSSong :: CheckDone
129 //
130 //==========================================================================
131 
CheckDone()132 bool MIDSSong::CheckDone()
133 {
134     return MidsP >= MaxMidsP;
135 }
136 
137 //==========================================================================
138 //
139 // MIDSSong :: DoRestart()
140 //
141 // Rewinds the song
142 //
143 //==========================================================================
144 
DoRestart()145 void MIDSSong::DoRestart()
146 {
147     MidsP = 0;
148     ProcessInitialTempoEvents();
149 }
150 
151 //==========================================================================
152 //
153 // MIDSSong :: ProcessInitialTempoEvents()
154 //
155 // Process initial tempo events at the start of the song.
156 //
157 //==========================================================================
158 
ProcessInitialTempoEvents()159 void MIDSSong::ProcessInitialTempoEvents()
160 {
161     if (MEVENT_EVENTTYPE(MidsBuffer[FormatFlags ? 1 : 2]) == MEVENT_TEMPO)
162     {
163         SetTempo(MEVENT_EVENTPARM(MidsBuffer[FormatFlags ? 1 : 2]));
164     }
165 }
166 
167 //==========================================================================
168 //
169 // MUSSong2 :: MakeEvents
170 //
171 // Puts MIDS events into a MIDI stream
172 // buffer. Returns the new position in the buffer.
173 //
174 //==========================================================================
175 
MakeEvents(uint32_t * events,uint32_t * max_event_p,uint32_t max_time)176 uint32_t* MIDSSong::MakeEvents(uint32_t* events, uint32_t* max_event_p, uint32_t max_time)
177 {
178     uint32_t time = 0;
179     uint32_t tot_time = 0;
180 
181     max_time = max_time * Division / Tempo;
182     while (events < max_event_p && tot_time <= max_time)
183     {
184         events[0] = time = MidsBuffer[MidsP++];
185         events[1] = FormatFlags ? 0 : MidsBuffer[MidsP++];
186         events[2] = MidsBuffer[MidsP++];
187         events += 3;
188         tot_time += time;
189         if (MidsP >= MaxMidsP) break;
190     }
191     return events;
192 }
193