Debian Codegraph @

BambooTracker/format/wopn_file.h

raw
1 /*
2 * Wohlstand's OPN2 Bank File - a bank format to store OPN2 timbre data and setup
3 *
4 * Copyright (c) 2018 Vitaly Novichkov <[email protected]>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef WOPN_FILE_H
26 #define WOPN_FILE_H
27
28 #include <stdint.h>
29 #include <stddef.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #if !defined(__STDC_VERSION__) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ < 199901L)) \
36 || defined(__STRICT_ANSI__) || !defined(__cplusplus)
37 typedef signed char int8_t;
38 typedef unsigned char uint8_t;
39 typedef signed short int int16_t;
40 typedef unsigned short int uint16_t;
41 #endif
42
43 /* Type of chip for which a bank has been designed */
44 typedef enum WOPN_ChipType
45 {
46 /* The Yamaha OPN2 chip, alias YM2612 YM3438 */
47 WOPN_Chip_OPN2 = 0,
48 /* The Yamaha OPNA chip, alias YM2608 */
49 WOPN_Chip_OPNA = 1
50 } WOPN_ChipType;
51
52 /* Volume scaling model implemented in the libOPNMIDI */
53 typedef enum WOPN_VolumeModel
54 {
55 WOPN_VM_Generic = 0
56 } WOPN_VolumeModel;
57
58 typedef enum WOPN_InstrumentFlags
59 {
60 /* Is pseudo eight-operator (two 4-operator voices) instrument */
61 WOPN_Ins_Pseudo8op = 0x01,
62 /* Is a blank instrument entry */
63 WOPN_Ins_IsBlank = 0x02,
64
65 /* Mask of the flags range */
66 WOPN_Ins_ALL_MASK = 0x03
67 } WOPN_InstrumentFlags;
68
69 /* Error codes */
70 typedef enum WOPN_ErrorCodes
71 {
72 WOPN_ERR_OK = 0,
73 /* Magic number is not maching */
74 WOPN_ERR_BAD_MAGIC,
75 /* Too short file */
76 WOPN_ERR_UNEXPECTED_ENDING,
77 /* Zero banks count */
78 WOPN_ERR_INVALID_BANKS_COUNT,
79 /* Version of file is newer than supported by current version of library */
80 WOPN_ERR_NEWER_VERSION,
81 /* Out of memory */
82 WOPN_ERR_OUT_OF_MEMORY,
83 /* Given null pointer memory data */
84 WOPN_ERR_NULL_POINTER
85 } WOPN_ErrorCodes;
86
87 /* OPN2 Oerators data */
88 typedef struct WOPNOperator
89 {
90 /* Detune and frequency multiplication register data */
91 uint8_t dtfm_30;
92 /* Total level register data */
93 uint8_t level_40;
94 /* Rate scale and attack register data */
95 uint8_t rsatk_50;
96 /* Amplitude modulation enable and Decay-1 register data */
97 uint8_t amdecay1_60;
98 /* Decay-2 register data */
99 uint8_t decay2_70;
100 /* Sustain and Release register data */
101 uint8_t susrel_80;
102 /* SSG-EG register data */
103 uint8_t ssgeg_90;
104 } WOPNOperator;
105
106 /* Instrument entry */
107 typedef struct WOPNInstrument
108 {
109 /* Title of the instrument */
110 char inst_name[34];
111 /* MIDI note key (half-tone) offset for an instrument (or a first voice in pseudo-4-op mode) */
112 int16_t note_offset;
113 /* Reserved */
114 int8_t midi_velocity_offset;
115 /* Percussion MIDI base tone number at which this drum will be played */
116 uint8_t percussion_key_number;
117 /* Enum WOPN_InstrumentFlags */
118 uint8_t inst_flags;
119 /* Feedback and Algorithm register data */
120 uint8_t fbalg;
121 /* LFO Sensitivity register data */
122 uint8_t lfosens;
123 /* Operators register data */
124 WOPNOperator operators[4];
125 /* Millisecond delay of sounding while key is on */
126 uint16_t delay_on_ms;
127 /* Millisecond delay of sounding after key off */
128 uint16_t delay_off_ms;
129 } WOPNInstrument;
130
131 /* Bank entry */
132 typedef struct WOPNBank
133 {
134 /* Name of bank */
135 char bank_name[33];
136 /* MIDI Bank LSB code */
137 uint8_t bank_midi_lsb;
138 /* MIDI Bank MSB code */
139 uint8_t bank_midi_msb;
140 /* Instruments data of this bank */
141 WOPNInstrument ins[128];
142 } WOPNBank;
143
144 /* Instrument data file */
145 typedef struct OPNIFile
146 {
147 /* Version of instrument file */
148 uint16_t version;
149 /* Is this a percussion instrument */
150 uint8_t is_drum;
151 /* Instrument data */
152 WOPNInstrument inst;
153 } OPNIFile;
154
155 /* Bank data file */
156 typedef struct WOPNFile
157 {
158 /* Version of bank file */
159 uint16_t version;
160 /* Count of melodic banks in this file */
161 uint16_t banks_count_melodic;
162 /* Count of percussion banks in this file */
163 uint16_t banks_count_percussion;
164 /* Chip global LFO enable flag and frequency register data */
165 uint8_t lfo_freq;
166 /* Chip type this bank is designed for */
167 uint8_t chip_type;
168 /* Reserved (Enum WOPN_VolumeModel) */
169 uint8_t volume_model;
170 /* dynamically allocated data Melodic banks array */
171 WOPNBank *banks_melodic;
172 /* dynamically allocated data Percussive banks array */
173 WOPNBank *banks_percussive;
174 } WOPNFile;
175
176
177 /**
178 * @brief Initialize blank WOPN data structure with allocated bank data
179 * @param melodic_banks Count of melodic banks
180 * @param percussive_banks Count of percussive banks
181 * @return pointer to heap-allocated WOPN data structure or NULL when out of memory or incorrectly given banks counts
182 */
183 extern WOPNFile *WOPN_Init(uint16_t melodic_banks, uint16_t percussive_banks);
184
185 /**
186 * @brief Clean up WOPN data file (all allocated bank arrays will be fried too)
187 * @param file pointer to heap-allocated WOPN data structure
188 */
189 extern void WOPN_Free(WOPNFile *file);
190
191 /**
192 * @brief Compare two bank entries
193 * @param bank1 First bank
194 * @param bank2 Second bank
195 * @return 1 if banks are equal or 0 if there are different
196 */
197 extern int WOPN_BanksCmp(const WOPNFile *bank1, const WOPNFile *bank2);
198
199
200 /**
201 * @brief Load WOPN bank file from the memory.
202 * WOPN data structure will be allocated. (don't forget to clear it with WOPN_Free() after use!)
203 * @param mem Pointer to memory block contains raw WOPN bank file data
204 * @param length Length of given memory block
205 * @param error pointer to integer to return an error code. Pass NULL if you don't want to use error codes.
206 * @return Heap-allocated WOPN file data structure or NULL if any error has occouped
207 */
208 extern WOPNFile *WOPN_LoadBankFromMem(void *mem, size_t length, int *error);
209
210 /**
211 * @brief Load WOPI instrument file from the memory.
212 * You must allocate OPNIFile structure by yourself and give the pointer to it.
213 * @param file Pointer to destination OPNIFile structure to fill it with parsed data.
214 * @param mem Pointer to memory block contains raw WOPI instrument file data
215 * @param length Length of given memory block
216 * @return 0 if no errors occouped, or an error code of WOPN_ErrorCodes enumeration
217 */
218 extern int WOPN_LoadInstFromMem(OPNIFile *file, void *mem, size_t length);
219
220 /**
221 * @brief Calculate the size of the output memory block
222 * @param file Heap-allocated WOPN file data structure
223 * @param version Destination version of the file
224 * @return Size of the raw WOPN file data
225 */
226 extern size_t WOPN_CalculateBankFileSize(WOPNFile *file, uint16_t version);
227
228 /**
229 * @brief Calculate the size of the output memory block
230 * @param file Pointer to WOPI file data structure
231 * @param version Destination version of the file
232 * @return Size of the raw WOPI file data
233 */
234 extern size_t WOPN_CalculateInstFileSize(OPNIFile *file, uint16_t version);
235
236 /**
237 * @brief Write raw WOPN into given memory block
238 * @param file Heap-allocated WOPN file data structure
239 * @param dest_mem Destination memory block pointer
240 * @param length Length of destination memory block
241 * @param version Wanted WOPN version
242 * @param force_gm Force GM set in saved bank file
243 * @return Error code or 0 on success
244 */
245 extern int WOPN_SaveBankToMem(WOPNFile *file, void *dest_mem, size_t length, uint16_t version, uint16_t force_gm);
246
247 /**
248 * @brief Write raw WOPI into given memory block
249 * @param file Pointer to WOPI file data structure
250 * @param dest_mem Destination memory block pointer
251 * @param length Length of destination memory block
252 * @param version Wanted WOPI version
253 * @return Error code or 0 on success
254 */
255 extern int WOPN_SaveInstToMem(OPNIFile *file, void *dest_mem, size_t length, uint16_t version);
256
257 #ifdef __cplusplus
258 }
259 #endif
260
261 #endif /* WOPN_FILE_H */