source: buchla-68k/ram/initi.c@ 06f6615

Last change on this file since 06f6615 was b28a12e, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Zero redundant declarations.

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