1 /*
2  * libADLMIDI is a free Software MIDI synthesizer library with OPL3 emulation
3  *
4  * Original ADLMIDI code: Copyright (c) 2010-2014 Joel Yliluoma <bisqwit@iki.fi>
5  * ADLMIDI Library API:   Copyright (c) 2017-2020 Vitaly Novichkov <admin@wohlnet.ru>
6  *
7  * Library is based on the ADLMIDI, a MIDI player for Linux and Windows with OPL3 emulation:
8  * http://iki.fi/bisqwit/source/adlmidi.html
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef ADLMIDI_PRIVATE_HPP
25 #define ADLMIDI_PRIVATE_HPP
26 
27 #define OPNMIDI_UNSTABLE_API
28 
29 // Setup compiler defines useful for exporting required public API symbols in gme.cpp
30 #ifndef OPNMIDI_EXPORT
31 #   if defined (_WIN32) && defined(OPNMIDI_BUILD_DLL)
32 #       define OPNMIDI_EXPORT __declspec(dllexport)
33 #   elif defined (LIBOPNMIDI_VISIBILITY) && defined (__GNUC__)
34 #       define OPNMIDI_EXPORT __attribute__((visibility ("default")))
35 #   else
36 #       define OPNMIDI_EXPORT
37 #   endif
38 #endif
39 
40 #ifdef _WIN32
41 #define NOMINMAX 1
42 #endif
43 
44 #ifdef _WIN32
45 #   undef NO_OLDNAMES
46 #       include <stdint.h>
47 #   ifdef _MSC_VER
48 #       ifdef _WIN64
49 typedef __int64 ssize_t;
50 #       else
51 typedef __int32 ssize_t;
52 #       endif
53 #   else
54 #       ifdef _WIN64
55 typedef int64_t ssize_t;
56 #       else
57 typedef int32_t ssize_t;
58 #       endif
59 #   endif
60 #   include <windows.h>
61 #endif
62 
63 #include <vector>
64 #include <list>
65 #include <string>
66 #include <map>
67 #include <set>
68 #include <new> // nothrow
69 #include <cstdlib>
70 #include <cstring>
71 #include <cmath>
72 #include <cstdarg>
73 #include <cstdio>
74 #include <cassert>
75 #include <vector> // vector
76 #include <deque>  // deque
77 #include <cmath>  // exp, log, ceil
78 #include <inttypes.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <limits> // numeric_limit
82 
83 #ifndef _WIN32
84 #include <errno.h>
85 #endif
86 
87 #include <deque>
88 #include <algorithm>
89 
90 /*
91  * Workaround for some compilers are has no those macros in their headers!
92  */
93 #ifndef INT8_MIN
94 #define INT8_MIN    (-0x7f - 1)
95 #endif
96 #ifndef INT16_MIN
97 #define INT16_MIN   (-0x7fff - 1)
98 #endif
99 #ifndef INT32_MIN
100 #define INT32_MIN   (-0x7fffffff - 1)
101 #endif
102 #ifndef INT8_MAX
103 #define INT8_MAX    0x7f
104 #endif
105 #ifndef INT16_MAX
106 #define INT16_MAX   0x7fff
107 #endif
108 #ifndef INT32_MAX
109 #define INT32_MAX   0x7fffffff
110 #endif
111 
112 class FileAndMemReader;
113 
114 #ifndef OPNMIDI_DISABLE_MIDI_SEQUENCER
115 // Rename class to avoid ABI collisions
116 #define BW_MidiSequencer OpnMidiSequencer
117 class BW_MidiSequencer;
118 typedef BW_MidiSequencer MidiSequencer;
119 typedef struct BW_MidiRtInterface BW_MidiRtInterface;
120 #endif//OPNMIDI_DISABLE_MIDI_SEQUENCER
121 
122 class OPN2;
123 class OPNChipBase;
124 
125 typedef class OPN2 Synth;
126 
127 #include "opnbank.h"
128 
129 #define OPNMIDI_BUILD
130 #include "opnmidi.h"    //Main API
131 
132 #include "opnmidi_ptr.hpp"
133 
134 class MIDIplay;
135 
136 #define ADL_UNUSED(x) (void)x
137 
138 #define OPN_MAX_CHIPS 100
139 #define OPN_MAX_CHIPS_STR "100"
140 
141 extern std::string OPN2MIDI_ErrorString;
142 
143 /*
144   Sample conversions to various formats
145 */
146 template <class Real>
opn2_cvtReal(int32_t x)147 inline Real opn2_cvtReal(int32_t x)
148 {
149     return static_cast<Real>(x) * (static_cast<Real>(1) / static_cast<Real>(INT16_MAX));
150 }
opn2_cvtS16(int32_t x)151 inline int32_t opn2_cvtS16(int32_t x)
152 {
153     x = (x < INT16_MIN) ? INT16_MIN : x;
154     x = (x > INT16_MAX) ? INT16_MAX : x;
155     return x;
156 }
opn2_cvtS8(int32_t x)157 inline int32_t opn2_cvtS8(int32_t x)
158 {
159     return opn2_cvtS16(x) / 256;
160 }
opn2_cvtS24(int32_t x)161 inline int32_t opn2_cvtS24(int32_t x)
162 {
163     return opn2_cvtS16(x) * 256;
164 }
opn2_cvtS32(int32_t x)165 inline int32_t opn2_cvtS32(int32_t x)
166 {
167     return opn2_cvtS16(x) * 65536;
168 }
opn2_cvtU16(int32_t x)169 inline int32_t opn2_cvtU16(int32_t x)
170 {
171     return opn2_cvtS16(x) - INT16_MIN;
172 }
opn2_cvtU8(int32_t x)173 inline int32_t opn2_cvtU8(int32_t x)
174 {
175     return opn2_cvtS8(x) - INT8_MIN;
176 }
opn2_cvtU24(int32_t x)177 inline int32_t opn2_cvtU24(int32_t x)
178 {
179     enum { int24_min = -(1 << 23) };
180     return opn2_cvtS24(x) - int24_min;
181 }
opn2_cvtU32(int32_t x)182 inline int32_t opn2_cvtU32(int32_t x)
183 {
184     // unsigned operation because overflow on signed integers is undefined
185     return (uint32_t)opn2_cvtS32(x) - (uint32_t)INT32_MIN;
186 }
187 
188 #if defined(ADLMIDI_AUDIO_TICK_HANDLER)
189 extern void opn2_audioTickHandler(void *instance, uint32_t chipId, uint32_t rate);
190 #endif
191 
192 #endif // ADLMIDI_PRIVATE_HPP
193