1 /*----------------------------------------------------------------------------
2  *
3  * File:
4  * eas_mdls.h
5  *
6  * Contents and purpose:
7  * Declarations, interfaces, and prototypes for eas_mdls.c
8  *
9  * Copyright Sonic Network Inc. 2004
10 
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  *----------------------------------------------------------------------------
24 */
25 
26 #ifndef _EAS_MDLS_H
27 #define _EAS_MDLS_H
28 
29 /*------------------------------------
30  * includes
31  *------------------------------------
32 */
33 #include "eas_data.h"
34 
35 
36 /*------------------------------------
37  * Some defines for dls.h
38  *------------------------------------
39 */
40 #ifndef DWORD
41 #define DWORD EAS_I32
42 #define FAR
43 #define SHORT EAS_I16
44 #define USHORT EAS_U16
45 #define LONG EAS_I32
46 #define ULONG EAS_U32
47 #endif
48 
49 
50 /* GUID struct (call it DLSID in case GUID is defined elsewhere) */
51 typedef struct
52 {
53     EAS_U32 Data1;
54     EAS_U16 Data2;
55     EAS_U16 Data3;
56     EAS_U8  Data4[8];
57 } DLSID;
58 
59 #define DEFINE_DLSID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) const DLSID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
60 
61 /*------------------------------------
62  * defines
63  *------------------------------------
64 */
65 
66 /* maximum sample memory for DLS query support */
67 #ifndef MAX_DLS_MEMORY
68 #define MAX_DLS_MEMORY 65536
69 #endif
70 
71 /* size of conditional chunk stack */
72 #ifndef CDL_STACK_SIZE
73 #define CDL_STACK_SIZE 8
74 #endif
75 
76 /* size of read buffer for sample conversion */
77 #ifndef SAMPLE_CONVERT_CHUNK_SIZE
78 #define SAMPLE_CONVERT_CHUNK_SIZE 32
79 #endif
80 
81 
82 #define ZERO_TIME_IN_CENTS              -32768
83 
84 /* Pan calculation macros */
85 #define PAN_CONVERSION_FACTOR       4129
86 #define MAX_PAN_VALUE               63
87 #define MIN_PAN_VALUE               -63
88 
89 /* multiplier to convert time cents to 10-bit fraction log for EAS_LogToLinear16 */
90 #define TIME_CENTS_TO_LOG2          27962
91 
92 /* conversion factor sustain level from percent to exponent for LogToLinear16 */
93 #define SUSTAIN_LOG_CONVERSION_FACTOR   536871
94 #define SUSTAIN_LOG_CONVERSION_SHIFT    15
95 
96 /* conversion factor sustain level from percent to EG full scale */
97 #define SUSTAIN_LINEAR_CONVERSION_FACTOR    1073709
98 
99 /* conversion factor to convert frame period to decay rate */
100 #define DECAY_CONVERSION_FACTOR     -16
101 
102 /*----------------------------------------------------------------------------
103  * These macros define the various characteristics of the defined sample rates
104  *----------------------------------------------------------------------------
105  * DLS_ATTACK_TIME_CONVERT      log offset for conversion from time cents to attack rate
106  * DLS_LFO_FREQUENCY_CONVERT    pitch-cents offset for LFO frequency conversion
107  *----------------------------------------------------------------------------
108 */
109 
110 #if defined (_SAMPLE_RATE_8000)
111 #define DLS_RATE_CONVERT                -9559
112 #define DLS_LFO_FREQUENCY_CONVERT       5921
113 
114 #elif defined (_SAMPLE_RATE_16000)
115 #define DLS_RATE_CONVERT                -9559
116 #define DLS_LFO_FREQUENCY_CONVERT       5921
117 
118 #elif defined (_SAMPLE_RATE_20000)
119 #define DLS_RATE_CONVERT                -8745
120 #define DLS_LFO_FREQUENCY_CONVERT       5108
121 
122 #elif defined (_SAMPLE_RATE_22050)
123 #define DLS_RATE_CONVERT                -8914
124 #define DLS_LFO_FREQUENCY_CONVERT       5277
125 
126 #elif defined (_SAMPLE_RATE_24000)
127 #define DLS_RATE_CONVERT                -9061
128 #define DLS_LFO_FREQUENCY_CONVERT       5423
129 
130 #elif defined (_SAMPLE_RATE_32000)
131 #define DLS_RATE_CONVERT                -9559
132 #define DLS_LFO_FREQUENCY_CONVERT       5921
133 
134 #elif defined (_SAMPLE_RATE_44100)
135 #define DLS_RATE_CONVERT                -8914
136 #define DLS_LFO_FREQUENCY_CONVERT       5277
137 
138 #elif defined (_SAMPLE_RATE_48000)
139 #define DLS_RATE_CONVERT                -9061
140 #define DLS_LFO_FREQUENCY_CONVERT       5423
141 
142 #else
143 #error "_SAMPLE_RATE_XXXXX must be defined to valid rate"
144 #endif
145 
146 /*
147  * FILTER_Q_CONVERSION_FACTOR convers the 0.1dB steps in the DLS
148  * file to our internal 0.75 dB steps. The value is calculated
149  * as follows:
150  *
151  * 32768 / (10 * <step-size in dB>)
152  *
153  * FILTER_RESONANCE_NUM_ENTRIES is the number of entries in the table
154 */
155 #define FILTER_Q_CONVERSION_FACTOR          4369
156 #define FILTER_RESONANCE_NUM_ENTRIES        31
157 
158 /*
159  * Multiplier to convert DLS gain units (10ths of a dB) to a
160  * power-of-two exponent for conversion to linear gain using our
161  * piece-wise linear approximator. Note that we ignore the lower
162  * 16-bits of the DLS gain value. The result is a 10-bit fraction
163  * that works with the EAS_LogToLinear16 function.
164  *
165  * DLS_GAIN_FACTOR = (2^18) / (200 * log10(2))
166  */
167 #define DLS_GAIN_FACTOR         4354
168 #define DLS_GAIN_SHIFT          8
169 
170 /*
171  * Reciprocal of 10 for quick divide by 10's
172  *
173  * DLS_GAIN_FACTOR = (2^18) / (200 * log10(2))
174  */
175 #define DLS_DIV_10_FACTOR       3277
176 #define DLS_DIV_10_SHIFT        16
177 
178 /*
179  * Multiplier to convert DLS time cents units to a power-of-two
180  * exponent for conversion to absolute time units using our
181  * piece-wise linear approximator.
182  *
183  * DLS_TIME_FACTOR = (2^22) / 1200
184  */
185 #define DLS_TIME_FACTOR         3495
186 #define DLS_TIME_SHIFT          22
187 
188 
189 /* LFO limits */
190 #define MAX_LFO_FREQUENCY_IN_HERTZ          20
191 #define MIN_LFO_FREQUENCY_IN_HERTZ          0.1
192 #define MAX_LFO_FREQUENCY_IN_PITCHCENTS     1549
193 #define MIN_LFO_FREQUENCY_IN_PITCHCENTS     -7624
194 #define MAX_LFO_AMPLITUDE_DEPTH             12  /* in dB, DLS2.1 p 31*/
195 #define MIN_LFO_AMPLITUDE_DEPTH             -12 /* in dB, DLS2.1 p 31*/
196 
197 /* add to pitch cents before pow(2.0, n) to convert to frequency */
198 #define ABSOLUTE_PITCH_BIAS                 238395828
199 
200 #define A5_PITCH_OFFSET                     6900
201 
202 /*
203 CHUNK_TYPE is a macro that converts the 4 input args into a 32-bit int
204 where
205 argument a is placed at the MSB location and
206 argument d is placed at the LSB location.
207 This is useful for determining the DLS chunk types
208 */
209 #define CHUNK_TYPE(a,b,c,d) (   \
210         ( ((EAS_U32)(a) & 0xFF) << 24 ) \
211     +   ( ((EAS_U32)(b) & 0xFF) << 16 ) \
212     +   ( ((EAS_U32)(c) & 0xFF) <<  8 ) \
213     +   ( ((EAS_U32)(d) & 0xFF)       ) )
214 
215 #define CHUNK_RIFF                  CHUNK_TYPE('R','I','F','F')
216 #define CHUNK_DLS                   CHUNK_TYPE('D','L','S',' ')
217 #define CHUNK_CDL                   CHUNK_TYPE('c','d','l',' ')
218 #define CHUNK_VERS                  CHUNK_TYPE('v','e','r','s')
219 #define CHUNK_DLID                  CHUNK_TYPE('d','l','i','d')
220 #define CHUNK_LIST                  CHUNK_TYPE('L','I','S','T')
221 #define CHUNK_COLH                  CHUNK_TYPE('c','o','l','h')
222 #define CHUNK_LINS                  CHUNK_TYPE('l','i','n','s')
223 #define CHUNK_PTBL                  CHUNK_TYPE('p','t','b','l')
224 #define CHUNK_WVPL                  CHUNK_TYPE('w','v','p','l')
225 #define CHUNK_INFO                  CHUNK_TYPE('I','N','F','O')
226 #define CHUNK_INAM                  CHUNK_TYPE('I','N','A','M')
227 #define CHUNK_INS                   CHUNK_TYPE('i','n','s',' ')
228 #define CHUNK_INSH                  CHUNK_TYPE('i','n','s','h')
229 #define CHUNK_LRGN                  CHUNK_TYPE('l','r','g','n')
230 #define CHUNK_RGN                   CHUNK_TYPE('r','g','n',' ')
231 #define CHUNK_RGN2                  CHUNK_TYPE('r','g','n','2')
232 #define CHUNK_RGNH                  CHUNK_TYPE('r','g','n','h')
233 #define CHUNK_WSMP                  CHUNK_TYPE('w','s','m','p')
234 #define CHUNK_WLNK                  CHUNK_TYPE('w','l','n','k')
235 #define CHUNK_LART                  CHUNK_TYPE('l','a','r','t')
236 #define CHUNK_LAR2                  CHUNK_TYPE('l','a','r','2')
237 #define CHUNK_ART1                  CHUNK_TYPE('a','r','t','1')
238 #define CHUNK_ART2                  CHUNK_TYPE('a','r','t','2')
239 #define CHUNK_WAVE                  CHUNK_TYPE('w','a','v','e')
240 #define CHUNK_FMT                   CHUNK_TYPE('f','m','t',' ')
241 #define CHUNK_DATA                  CHUNK_TYPE('d','a','t','a')
242 #define CHUNK_DMPR                  CHUNK_TYPE('d','m','p','r')
243 
244 
245 #define WAVE_FORMAT_PCM             0x0001 /* Microsoft PCM format, see DLS2.1 p60 */
246 #define WAVE_FORMAT_EXTENSIBLE      0xffff
247 
248 /* defines for wave table structures */
249 
250 /* initialize each articulation structure to a harmless state */
251 /* change art values after we've determined EAS internals */
252 #define DEFAULT_DLS_FILTER_CUTOFF_FREQUENCY     0x7FFF      /* DLS2.1, p 31 means leave filter off */
253 
254 /**********/
255 
256 /* define the waves that we expect to generate instead of store */
257 /* NOTE: our comparison routine converts the input string
258 to lowercase, so the following comparison values should all
259 be in lowercase.
260 */
261 #define STRING_NOISE            "noise"
262 
263 
264 /*------------------------------------
265  * type definitions
266  *------------------------------------
267 */
268 #ifdef _STANDALONE_CONVERTER
269 typedef struct s_dls_params
270 {
271     EAS_INT     sampleRate;
272     EAS_INT     samplesPerFrame;
273     EAS_INT     bitDepth;
274     double      ditherLevel;
275     double      ditherFilterCoeff;
276     EAS_BOOL    compatibility;
277     EAS_BOOL    encodeADPCM;
278 } S_DLS_PARAMS;
279 #endif
280 
281 
282 /* function prototypes */
283 EAS_RESULT DLSParser (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE fileHandle, EAS_I32 offset, S_DLS **pDLS);
284 EAS_RESULT DLSCleanup (EAS_HW_DATA_HANDLE hwInstData, S_DLS *pDLS);
285 void DLSAddRef (S_DLS *pDLS);
286 EAS_I16 ConvertDelay (EAS_I32 timeCents);
287 EAS_I16 ConvertRate (EAS_I32 timeCents);
288 
289 
290 #ifdef _STANDALONE_CONVERTER
291 void DLSConvParams (S_DLS_PARAMS *pParams, EAS_BOOL set);
292 #endif
293 
294 #endif
295 
296