1 | /*
|
---|
2 | =============================================================================
|
---|
3 | initi.c -- MIDAS-VII - instrument editor support functions
|
---|
4 | Version 2 -- 1989-12-19 -- D.N. Lynx Crowe
|
---|
5 | =============================================================================
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define DEBUGPS 0 /* define non-zero to debug pntsel() */
|
---|
9 |
|
---|
10 | #include "ram.h"
|
---|
11 |
|
---|
12 | #include "dfltins.h" /* short dfltins[]; default instrument */
|
---|
13 | #include "ptoftab.h" /* int16_t ptoftab[]; pitch to frequency table */
|
---|
14 |
|
---|
15 | int16_t finival[NFINST] = { /* initial function values */
|
---|
16 |
|
---|
17 | FPUVAL(500), /* 0: Freq 1 */
|
---|
18 | FPUVAL(500), /* 1: Freq 2 */
|
---|
19 | FPUVAL(500), /* 2: Freq 3 */
|
---|
20 | FPUVAL(500), /* 3: Freq 4 */
|
---|
21 | FPUVAL(500), /* 4: Filter / Resonance */
|
---|
22 | FPUVAL(500), /* 5: Location */
|
---|
23 |
|
---|
24 | FPUVAL(0), /* 6: Index 1 */
|
---|
25 | FPUVAL(0), /* 7: Index 2 */
|
---|
26 | FPUVAL(0), /* 8: Index 3 */
|
---|
27 | FPUVAL(0), /* 9: Index 4 */
|
---|
28 | FPUVAL(0), /* 10: Index 5 */
|
---|
29 | FPUVAL(0), /* 11: Index 6 */
|
---|
30 |
|
---|
31 | FPUVAL(0) /* 12: Level */
|
---|
32 | };
|
---|
33 |
|
---|
34 | uint16_t expbit[16] = { /* FPU time exponent bit expansion table */
|
---|
35 |
|
---|
36 | 0x0001, /* 0 */
|
---|
37 | 0x0002, /* 1 */
|
---|
38 | 0x0004, /* 2 */
|
---|
39 | 0x0008, /* 3 */
|
---|
40 | 0x0010, /* 4 */
|
---|
41 | 0x0020, /* 5 */
|
---|
42 | 0x0040, /* 6 */
|
---|
43 | 0x0080, /* 7 */
|
---|
44 | 0x0100, /* 8 */
|
---|
45 | 0x0200, /* 9 */
|
---|
46 | 0x0400, /* 10 */
|
---|
47 | 0x0800, /* 11 */
|
---|
48 | 0x1000, /* 12 */
|
---|
49 | 0x2000, /* 13 */
|
---|
50 | 0x4000, /* 14 */
|
---|
51 | 0x8000 /* 15 */
|
---|
52 | };
|
---|
53 |
|
---|
54 | /*
|
---|
55 | =============================================================================
|
---|
56 | segtime() -- determine time to a point using a new segment time
|
---|
57 | =============================================================================
|
---|
58 | */
|
---|
59 |
|
---|
60 | uint16_t segtime(int16_t pn, uint16_t ptime)
|
---|
61 | {
|
---|
62 | register struct instpnt *pp;
|
---|
63 | register int16_t i;
|
---|
64 | register uint16_t tacc;
|
---|
65 |
|
---|
66 | pp = &vbufs[curvce].idhpnt[vbufs[curvce].idhfnc[curfunc].idfpt1];
|
---|
67 | tacc = 0;
|
---|
68 |
|
---|
69 | for (i = 0; i < pn; i++)
|
---|
70 | tacc += fromfpu(pp++->iptim);
|
---|
71 |
|
---|
72 | tacc += fromfpu(ptime);
|
---|
73 |
|
---|
74 | return(tacc);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /*
|
---|
78 | =============================================================================
|
---|
79 | setseg() -- set the time to a segment
|
---|
80 | =============================================================================
|
---|
81 | */
|
---|
82 |
|
---|
83 | void setseg(int16_t pn, uint16_t ptime)
|
---|
84 | {
|
---|
85 | register int16_t i;
|
---|
86 | register uint16_t tacc;
|
---|
87 | register struct instpnt *pp;
|
---|
88 |
|
---|
89 | pp = &vbufs[curvce].idhpnt[vbufs[curvce].idhfnc[curfunc].idfpt1];
|
---|
90 | tacc = 0;
|
---|
91 |
|
---|
92 | for (i = 0; i < pn; i++)
|
---|
93 | tacc += fromfpu(pp++->iptim);
|
---|
94 |
|
---|
95 | pp->iptim = tofpu(ptime - tacc);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /*
|
---|
99 | =============================================================================
|
---|
100 | timeto() -- find the time at a point in a function
|
---|
101 | =============================================================================
|
---|
102 | */
|
---|
103 |
|
---|
104 | uint16_t timeto(int16_t fn, int16_t pj)
|
---|
105 | {
|
---|
106 | register struct instdef *ip;
|
---|
107 | register struct instpnt *pt;
|
---|
108 | register int16_t pm, pn;
|
---|
109 | uint16_t tf;
|
---|
110 |
|
---|
111 | ip = &vbufs[curvce];
|
---|
112 | pn = ip->idhfnc[fn].idfpt1;
|
---|
113 | pt = &ip->idhpnt[pn];
|
---|
114 | pm = pn + pj;
|
---|
115 | tf = 0;
|
---|
116 |
|
---|
117 | while (pn LE pm) {
|
---|
118 |
|
---|
119 | tf += fromfpu(pt->iptim);
|
---|
120 | ++pt;
|
---|
121 | ++pn;
|
---|
122 | }
|
---|
123 |
|
---|
124 | return(tf);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*
|
---|
128 | =============================================================================
|
---|
129 | pntsel() -- setup editing limits for subj in the current function
|
---|
130 | =============================================================================
|
---|
131 | */
|
---|
132 |
|
---|
133 | void pntsel(void)
|
---|
134 | {
|
---|
135 | register struct idfnhdr *fp;
|
---|
136 |
|
---|
137 | fp = &vbufs[curvce].idhfnc[curfunc];
|
---|
138 |
|
---|
139 | curpnt = fp->idfpt1 + subj;
|
---|
140 | pntptr = &vbufs[curvce].idhpnt[curpnt];
|
---|
141 |
|
---|
142 | npts = fp->idfpif;
|
---|
143 |
|
---|
144 | if (npts EQ 1) { /* detemine editing case */
|
---|
145 |
|
---|
146 | pecase = 0; /* single point */
|
---|
147 | temin = 1;
|
---|
148 | temax = 32767;
|
---|
149 |
|
---|
150 | } else if (subj EQ (npts - 1)) {
|
---|
151 |
|
---|
152 | pecase = 1; /* last point */
|
---|
153 | temin = timeto(curfunc, subj - 1);
|
---|
154 | temax = 32767;
|
---|
155 |
|
---|
156 | } else {
|
---|
157 |
|
---|
158 | pecase = 2; /* interior point */
|
---|
159 | temin = timeto(curfunc, subj - 1);
|
---|
160 | temax = timeto(curfunc, subj + 1);
|
---|
161 | }
|
---|
162 |
|
---|
163 | #if DEBUGPS
|
---|
164 | printf("pntsel(): curpnt=%d, subj=%d, fp=0x%08lx, pntptr=0x%08lx\r\n",
|
---|
165 | curpnt, subj, fp, pntptr);
|
---|
166 | printf("pntsel(): npts=%d, pecase=%d, temin=%d, temax=%d\r\n",
|
---|
167 | npts, pecase, temin, temax);
|
---|
168 | #endif
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*
|
---|
172 | =============================================================================
|
---|
173 | vtoy() -- convert a value to a y coordinate for display
|
---|
174 | =============================================================================
|
---|
175 | */
|
---|
176 |
|
---|
177 | int16_t vtoy(int16_t val, int16_t window)
|
---|
178 | {
|
---|
179 | register int16_t yval;
|
---|
180 |
|
---|
181 | if (val GT 1000)
|
---|
182 | val = 1000;
|
---|
183 |
|
---|
184 | yval = 56 + (((1000 - val) * 14) / 100);
|
---|
185 |
|
---|
186 | if (window < 12)
|
---|
187 | return((int16_t)(idbox[window][1] + (((yval - 56) * 100L) / 540L)));
|
---|
188 | else
|
---|
189 | return(yval);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /*
|
---|
193 | =============================================================================
|
---|
194 | ttox() -- convert a time to an x coordinate for display
|
---|
195 | =============================================================================
|
---|
196 | */
|
---|
197 |
|
---|
198 | int16_t ttox(uint16_t time, int16_t window)
|
---|
199 | {
|
---|
200 | register int16_t xval;
|
---|
201 |
|
---|
202 | if (time < 128) /* 0..127 */
|
---|
203 | xval = 8 + (int16_t)(((time * 1000L) / 2309L));
|
---|
204 | else if (time < 256) /* 128..255 */
|
---|
205 | xval = 64 + (int16_t)((((time - 128) * 1000L) / 2309L));
|
---|
206 | else if (time < 512) /* 256..511 */
|
---|
207 | xval = 120 + (int16_t)((((time - 256) * 1000L) / 4636L));
|
---|
208 | else if (time < 1024) /* 512..1023 */
|
---|
209 | xval = 176 + (int16_t)((((time - 512) * 1000L) / 9290L));
|
---|
210 | else if (time < 2048) /* 1024..2047 */
|
---|
211 | xval = 232 + (int16_t)((((time - 1024) * 1000L) / 18600L));
|
---|
212 | else if (time < 4096) /* 2048..4095 */
|
---|
213 | xval = 288 + (int16_t)((((time - 2048) * 1000L) / 37218L));
|
---|
214 | else if (time < 8192) /* 4096..8191 */
|
---|
215 | xval = 344 + (int16_t)((((time - 4096) * 1000L)/ 74454L));
|
---|
216 | else if (time < 16384) /* 8192..16383 */
|
---|
217 | xval = 400 + (int16_t)((((time - 8192) * 1000L) / 148927L));
|
---|
218 | else if (time < (uint16_t)32768) /* 16384..32767 */
|
---|
219 | xval = 456 + (int16_t)((((time - 16384) * 1000L) / 309113L));
|
---|
220 | else
|
---|
221 | xval = 509;
|
---|
222 |
|
---|
223 | if (xval > 509)
|
---|
224 | xval = 509;
|
---|
225 |
|
---|
226 | if (window < 12)
|
---|
227 | return((int16_t)(idbox[window][0] + (((xval - 8) * 100L) / 599L)));
|
---|
228 | else
|
---|
229 | return(xval);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /*
|
---|
233 | =============================================================================
|
---|
234 | selpnt() -- select a point for editing
|
---|
235 | =============================================================================
|
---|
236 | */
|
---|
237 |
|
---|
238 | int16_t selpnt(void)
|
---|
239 | {
|
---|
240 | register struct idfnhdr *fp;
|
---|
241 |
|
---|
242 | register int16_t i, xv, np;
|
---|
243 |
|
---|
244 | int16_t lo_x, lo_x_pt, hi_x, hi_x_pt;
|
---|
245 |
|
---|
246 | fp = &vbufs[curvce].idhfnc[curfunc];
|
---|
247 | np = fp->idfpif;
|
---|
248 | lo_x_pt = 0;
|
---|
249 | lo_x = ttox(timeto(curfunc, lo_x_pt), 12);
|
---|
250 | hi_x_pt = np - 1;
|
---|
251 | hi_x = ttox(timeto(curfunc, hi_x_pt), 12);
|
---|
252 |
|
---|
253 | for (i = 0; i < np; i++) {
|
---|
254 |
|
---|
255 | xv = ttox(timeto(curfunc, i), 12);
|
---|
256 |
|
---|
257 | if (xv LE cxval) /* largest x LE cxval */
|
---|
258 | if (xv GE lo_x) {
|
---|
259 |
|
---|
260 | lo_x = xv;
|
---|
261 | lo_x_pt = i;
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (xv GE cxval) /* smallest x GE cxval*/
|
---|
265 | if (xv LT hi_x) {
|
---|
266 |
|
---|
267 | hi_x = xv;
|
---|
268 | hi_x_pt = i;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | if (lo_x EQ hi_x)
|
---|
273 | return(lo_x_pt);
|
---|
274 |
|
---|
275 | if ((cxval - lo_x) LT (hi_x - cxval))
|
---|
276 | return(lo_x_pt);
|
---|
277 | else
|
---|
278 | return(hi_x_pt);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*
|
---|
282 | =============================================================================
|
---|
283 | setinst() -- setup for editing a new instrument
|
---|
284 | =============================================================================
|
---|
285 | */
|
---|
286 |
|
---|
287 | void setinst(void)
|
---|
288 | {
|
---|
289 | register struct instdef *ip;
|
---|
290 | register struct idfnhdr *fp;
|
---|
291 |
|
---|
292 | curfunc = 12; /* current function = 12 = level */
|
---|
293 | subj = 0; /* current edited point = 0 (first point) */
|
---|
294 | pntsv = 0; /* point selection state = unselected */
|
---|
295 | pecase = 0; /* point selection case = 0 (first point) */
|
---|
296 |
|
---|
297 | ip = &vbufs[curvce]; /* instrument definition pointer */
|
---|
298 | fp = &ip->idhfnc[curfunc]; /* function pointer */
|
---|
299 |
|
---|
300 | curpnt = subj + fp->idfpt1; /* current point in function */
|
---|
301 | pntptr = &ip->idhpnt[curpnt]; /* current point pointer */
|
---|
302 |
|
---|
303 | newws(); /* setup waveshape variables */
|
---|
304 | }
|
---|
305 |
|
---|
306 | /*
|
---|
307 | =============================================================================
|
---|
308 | newinst() -- select a new instrument
|
---|
309 | =============================================================================
|
---|
310 | */
|
---|
311 |
|
---|
312 | void newinst(int16_t inst)
|
---|
313 | {
|
---|
314 | curinst = inst;
|
---|
315 | s_inst[curvce] = inst;
|
---|
316 | setinst();
|
---|
317 | }
|
---|
318 |
|
---|
319 | /*
|
---|
320 | =============================================================================
|
---|
321 | newvce() -- select a new voice
|
---|
322 | =============================================================================
|
---|
323 | */
|
---|
324 |
|
---|
325 | void newvce(int16_t voice)
|
---|
326 | {
|
---|
327 | curvce = voice;
|
---|
328 | curinst = s_inst[curvce];
|
---|
329 | setinst();
|
---|
330 | }
|
---|
331 |
|
---|
332 | /*
|
---|
333 | =============================================================================
|
---|
334 | setivce() -- set instrument definition for a voice
|
---|
335 | =============================================================================
|
---|
336 | */
|
---|
337 |
|
---|
338 | void setivce(int16_t voice)
|
---|
339 | {
|
---|
340 | register int16_t j, k, oldi;
|
---|
341 | register struct instdef *ip;
|
---|
342 |
|
---|
343 | ip = &vbufs[voice];
|
---|
344 |
|
---|
345 | oldi = setipl(FPU_DI); /* disable FPU interrupts */
|
---|
346 |
|
---|
347 | /* +++++++++++++++++++++++ FPU interrupts disabled ++++++++++++++++++++++++++ */
|
---|
348 |
|
---|
349 | for (j = 0; j < NFINST; j++) { /* set funcndx[] entries */
|
---|
350 |
|
---|
351 | k = (voice << 4) + fnoff[j];
|
---|
352 | funcndx[k][0] = &ip->idhfnc[j];
|
---|
353 | funcndx[k][1] = ip->idhpnt;
|
---|
354 | }
|
---|
355 |
|
---|
356 | /* set waveshapes */
|
---|
357 |
|
---|
358 | memcpyw((io_fpu + FPU_OWST + (voice << 9) + 0x0100 + 1),
|
---|
359 | ip->idhwvaf, NUMWPNT);
|
---|
360 |
|
---|
361 | memcpyw((io_fpu + FPU_OWST + (voice << 9) + 1),
|
---|
362 | ip->idhwvbf, NUMWPNT);
|
---|
363 |
|
---|
364 | setipl(oldi); /* restore interrupts */
|
---|
365 |
|
---|
366 | /* ++++++++++++++++++++++++++ Interrupts restored +++++++++++++++++++++++++++ */
|
---|
367 |
|
---|
368 | instmod[voice] = FALSE; /* instrument matches library */
|
---|
369 | }
|
---|
370 |
|
---|
371 | /*
|
---|
372 | =============================================================================
|
---|
373 | modinst() -- handle instrument modifications
|
---|
374 | =============================================================================
|
---|
375 | */
|
---|
376 |
|
---|
377 | void modinst(void)
|
---|
378 | {
|
---|
379 | int16_t f, i, grp, oldi;
|
---|
380 |
|
---|
381 | if (NOT instmod[curvce]) {
|
---|
382 |
|
---|
383 | instmod[curvce] = TRUE;
|
---|
384 | dswin(19);
|
---|
385 | }
|
---|
386 |
|
---|
387 | if (NOT editsw) {
|
---|
388 |
|
---|
389 | if ((grp = vce2grp[curvce]) > 0) {
|
---|
390 |
|
---|
391 | oldi = setipl(FPU_DI); /* +++++ disable FPU interrupts */
|
---|
392 |
|
---|
393 | for (i = 0; i < 12; i++) {
|
---|
394 |
|
---|
395 | if (vce2grp[i] EQ grp) {
|
---|
396 |
|
---|
397 | memcpyw(&vbufs[i], &vbufs[curvce],
|
---|
398 | sizeof (struct instdef) / 2);
|
---|
399 |
|
---|
400 | for (f = 0; f < 12; f++)
|
---|
401 | vbufs[i].idhfnc[f].idftmd
|
---|
402 | &= ~I_ACTIVE;
|
---|
403 |
|
---|
404 | s_inst[i] = curinst;
|
---|
405 | clrvce(i);
|
---|
406 | setivce(i);
|
---|
407 | dosync(i);
|
---|
408 | execkey(0, 0, i, 1);
|
---|
409 | vce2trg[i] = -1;
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | setipl(oldi); /* +++++ enable FPU interrupts */
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | /*
|
---|
419 | =============================================================================
|
---|
420 | initi() -- initialize an instrument definition
|
---|
421 | =============================================================================
|
---|
422 | */
|
---|
423 |
|
---|
424 | void initi(struct instdef *ip)
|
---|
425 | {
|
---|
426 | int16_t i, rb;
|
---|
427 | uint16_t mintfpu;
|
---|
428 |
|
---|
429 | memsetw(ip, 0, sizeof (struct instdef) / 2);
|
---|
430 |
|
---|
431 | memcpy(ip->idhname, " ", MAXIDLN+1);
|
---|
432 | memcpy(ip->idhcom1, " ", MAXIDLN+1);
|
---|
433 | memcpy(ip->idhcom2, " ", MAXIDLN+1);
|
---|
434 | memcpy(ip->idhcom3, " ", MAXIDLN+1);
|
---|
435 |
|
---|
436 | ip->idhplft = NIPNTS - NFINST;
|
---|
437 | ip->idhcfg = (int8_t)((initcfg >> 8) & 0x00FF);
|
---|
438 | mintfpu = tofpu(1);
|
---|
439 |
|
---|
440 | for (i = 0; i < NFINST; i++) { /* for each function ... */
|
---|
441 |
|
---|
442 | /* initialize the function header */
|
---|
443 |
|
---|
444 | rb = ((i < 4) AND (i NE 0)) ? 0 : I_NRATIO;
|
---|
445 | ip->idhfnc[i].idfpif = 1;
|
---|
446 | ip->idhfnc[i].idfpt1 = (int8_t)i;
|
---|
447 | ip->idhfnc[i].idftmd = (int8_t)((I_TM_KEY | 0x0010) | rb);
|
---|
448 |
|
---|
449 | /* initialize the first point in the function */
|
---|
450 |
|
---|
451 | ip->idhpnt[i].iptim = mintfpu;
|
---|
452 | ip->idhpnt[i].ipval = finival[i];
|
---|
453 | }
|
---|
454 |
|
---|
455 | for (i = 0; i < NUMWPNT; i++) {
|
---|
456 |
|
---|
457 | ip->idhwvaf[i] = (ip->idhwvao[i] = (int16_t)0x8000 ^ ((i + 1) << 8));
|
---|
458 | ip->idhwvbf[i] = (ip->idhwvbo[i] = (int16_t)0x8000 ^ ((i + 1) << 8));
|
---|
459 | }
|
---|
460 | }
|
---|
461 |
|
---|
462 | /*
|
---|
463 | =============================================================================
|
---|
464 | initil() -- initialize instrument library
|
---|
465 | =============================================================================
|
---|
466 | */
|
---|
467 |
|
---|
468 | void initil(void)
|
---|
469 | {
|
---|
470 | register int16_t i;
|
---|
471 |
|
---|
472 | setipl(FPU_DI); /* disable FPU interrupts */
|
---|
473 |
|
---|
474 | /* +++++++++++++++++++++++ FPU interrupts disabled ++++++++++++++++++++++++++ */
|
---|
475 |
|
---|
476 | fpuclr(); /* clear the FPU */
|
---|
477 |
|
---|
478 | memcpyw(&idefs[0], dfltins, sizeof (struct instdef) / 2);
|
---|
479 |
|
---|
480 | for (i = 1; i < NINST; i++) /* initialize each instrument */
|
---|
481 | initi(&idefs[i]);
|
---|
482 |
|
---|
483 | memset(funcndx, 0, sizeof funcndx);
|
---|
484 |
|
---|
485 | for (i = 0; i < 12; i++) { /* initialize voices */
|
---|
486 |
|
---|
487 | memcpyw(&vbufs[i], &idefs[0], sizeof (struct instdef) / 2);
|
---|
488 | setivce(i);
|
---|
489 | }
|
---|
490 |
|
---|
491 | setipl(FPU_EI); /* enable FPU interrupts */
|
---|
492 |
|
---|
493 | /* ++++++++++++++++++++++++++ Interrupts enabled ++++++++++++++++++++++++++++ */
|
---|
494 |
|
---|
495 | newvce(0); /* setup editing for instrument 0, voice 0 */
|
---|
496 | newinst(0);
|
---|
497 | }
|
---|
498 |
|
---|
499 |
|
---|