1 /* $NetBSD: opl.c,v 1.26 2006/06/30 13:56:25 chap Exp $ */ 2 3 /* 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (augustss@NetBSD.org). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * The OPL3 (YMF262) manual can be found at 41 * ftp://ftp.yamahayst.com/Fax_Back_Doc/sound/YMF262.PDF 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: opl.c,v 1.26 2006/06/30 13:56:25 chap Exp $"); 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/errno.h> 50 #include <sys/ioctl.h> 51 #include <sys/syslog.h> 52 #include <sys/device.h> 53 #include <sys/select.h> 54 #include <sys/malloc.h> 55 56 #include <machine/cpu.h> 57 #include <machine/bus.h> 58 59 #include <sys/audioio.h> 60 #include <sys/midiio.h> 61 #include <dev/audio_if.h> 62 63 #include <dev/midi_if.h> 64 #include <dev/midivar.h> 65 #include <dev/midisynvar.h> 66 67 #include <dev/ic/oplreg.h> 68 #include <dev/ic/oplvar.h> 69 70 #ifdef AUDIO_DEBUG 71 #define DPRINTF(x) if (opldebug) printf x 72 #define DPRINTFN(n,x) if (opldebug >= (n)) printf x 73 int opldebug = 0; 74 #else 75 #define DPRINTF(x) 76 #define DPRINTFN(n,x) 77 #endif 78 79 struct real_voice { 80 u_int8_t voice_num; 81 u_int8_t voice_mode; /* 0=unavailable, 2=2 OP, 4=4 OP */ 82 u_int8_t iooffs; /* I/O port (left or right side) */ 83 u_int8_t op[4]; /* Operator offsets */ 84 }; 85 86 const struct opl_voice voicetab[] = { 87 /* No I/O offs OP1 OP2 OP3 OP4 */ 88 /* --------------------------------------------- */ 89 { 0, OPL_L, {0x00, 0x03, 0x08, 0x0b}}, 90 { 1, OPL_L, {0x01, 0x04, 0x09, 0x0c}}, 91 { 2, OPL_L, {0x02, 0x05, 0x0a, 0x0d}}, 92 93 { 3, OPL_L, {0x08, 0x0b, 0x00, 0x00}}, 94 { 4, OPL_L, {0x09, 0x0c, 0x00, 0x00}}, 95 { 5, OPL_L, {0x0a, 0x0d, 0x00, 0x00}}, 96 97 { 6, OPL_L, {0x10, 0x13, 0x00, 0x00}}, 98 { 7, OPL_L, {0x11, 0x14, 0x00, 0x00}}, 99 { 8, OPL_L, {0x12, 0x15, 0x00, 0x00}}, 100 101 { 0, OPL_R, {0x00, 0x03, 0x08, 0x0b}}, 102 { 1, OPL_R, {0x01, 0x04, 0x09, 0x0c}}, 103 { 2, OPL_R, {0x02, 0x05, 0x0a, 0x0d}}, 104 { 3, OPL_R, {0x08, 0x0b, 0x00, 0x00}}, 105 { 4, OPL_R, {0x09, 0x0c, 0x00, 0x00}}, 106 { 5, OPL_R, {0x0a, 0x0d, 0x00, 0x00}}, 107 108 { 6, OPL_R, {0x10, 0x13, 0x00, 0x00}}, 109 { 7, OPL_R, {0x11, 0x14, 0x00, 0x00}}, 110 { 8, OPL_R, {0x12, 0x15, 0x00, 0x00}} 111 }; 112 113 static void opl_command(struct opl_softc *, int, int, int); 114 void opl_reset(struct opl_softc *); 115 void opl_freq_to_fnum (int freq, int *block, int *fnum); 116 117 int oplsyn_open(midisyn *ms, int); 118 void oplsyn_close(midisyn *); 119 void oplsyn_reset(void *); 120 void oplsyn_attackv(midisyn *, uint_fast16_t, midipitch_t, int16_t); 121 static void oplsyn_repitchv(midisyn *, uint_fast16_t, midipitch_t); 122 static void oplsyn_relevelv(midisyn *, uint_fast16_t, int16_t); 123 static void oplsyn_setv(midisyn *, uint_fast16_t, midipitch_t, int16_t, int); 124 void oplsyn_releasev(midisyn *, uint_fast16_t, uint_fast8_t); 125 int oplsyn_ctlnotice(midisyn *, midictl_evt, uint_fast8_t, uint_fast16_t); 126 void oplsyn_programchange(midisyn *, uint_fast8_t, uint_fast8_t); 127 void oplsyn_loadpatch(midisyn *, struct sysex_info *, struct uio *); 128 static void oplsyn_panhandler(midisyn *, uint_fast8_t); 129 130 void opl_set_op_reg(struct opl_softc *, int, int, int, u_char); 131 void opl_set_ch_reg(struct opl_softc *, int, int, u_char); 132 void opl_load_patch(struct opl_softc *, int); 133 u_int32_t opl_get_block_fnum(midipitch_t mp); 134 int opl_calc_vol(int regbyte, int16_t level_cB); 135 136 struct midisyn_methods opl3_midi = { 137 .open = oplsyn_open, 138 .close = oplsyn_close, 139 .attackv = oplsyn_attackv, 140 .repitchv = oplsyn_repitchv, 141 .relevelv = oplsyn_relevelv, 142 .releasev = oplsyn_releasev, 143 .pgmchg = oplsyn_programchange, 144 .ctlnotice = oplsyn_ctlnotice, 145 }; 146 147 void 148 opl_attach(sc) 149 struct opl_softc *sc; 150 { 151 int i; 152 153 if (!opl_find(sc)) { 154 printf("\nopl: find failed\n"); 155 return; 156 } 157 158 sc->syn.mets = &opl3_midi; 159 snprintf(sc->syn.name, sizeof(sc->syn.name), "%sYamaha OPL%d", 160 sc->syn.name, sc->model); 161 sc->syn.data = sc; 162 sc->syn.nvoice = sc->model == OPL_2 ? OPL2_NVOICE : OPL3_NVOICE; 163 midisyn_attach(&sc->mididev, &sc->syn); 164 165 /* Set up voice table */ 166 for (i = 0; i < OPL3_NVOICE; i++) 167 sc->voices[i] = voicetab[i]; 168 169 opl_reset(sc); 170 171 printf(": model OPL%d", sc->model); 172 173 /* Set up panpot */ 174 sc->panl = OPL_VOICE_TO_LEFT; 175 sc->panr = OPL_VOICE_TO_RIGHT; 176 if (sc->model == OPL_3 && 177 device_cfdata(&sc->mididev.dev)->cf_flags & OPL_FLAGS_SWAP_LR) { 178 sc->panl = OPL_VOICE_TO_RIGHT; 179 sc->panr = OPL_VOICE_TO_LEFT; 180 printf(": LR swapped"); 181 } 182 183 printf("\n"); 184 185 sc->sc_mididev = 186 midi_attach_mi(&midisyn_hw_if, &sc->syn, &sc->mididev.dev); 187 } 188 189 int 190 opl_detach(sc, flags) 191 struct opl_softc *sc; 192 int flags; 193 { 194 int rv = 0; 195 196 if (sc->sc_mididev != NULL) 197 rv = config_detach(sc->sc_mididev, flags); 198 199 return(rv); 200 } 201 202 static void 203 opl_command(sc, offs, addr, data) 204 struct opl_softc *sc; 205 int offs; 206 int addr, data; 207 { 208 DPRINTFN(4, ("opl_command: sc=%p, offs=%d addr=0x%02x data=0x%02x\n", 209 sc, offs, addr, data)); 210 offs += sc->offs; 211 bus_space_write_1(sc->iot, sc->ioh, OPL_ADDR+offs, addr); 212 if (sc->model == OPL_2) 213 delay(10); 214 else 215 delay(6); 216 bus_space_write_1(sc->iot, sc->ioh, OPL_DATA+offs, data); 217 if (sc->model == OPL_2) 218 delay(30); 219 else 220 delay(6); 221 } 222 223 int 224 opl_match(bus_space_tag_t iot, bus_space_handle_t ioh, int offs) 225 { 226 struct opl_softc *sc; 227 int rv; 228 229 sc = malloc(sizeof(*sc), M_TEMP, M_WAITOK|M_ZERO); 230 sc->iot = iot; 231 sc->ioh = ioh; 232 sc->offs = offs; 233 rv = opl_find(sc); 234 free(sc, M_TEMP); 235 return rv; 236 } 237 238 int 239 opl_find(sc) 240 struct opl_softc *sc; 241 { 242 u_int8_t status1, status2; 243 244 DPRINTFN(2,("opl_find: ioh=0x%x\n", (int)sc->ioh)); 245 sc->model = OPL_2; /* worst case assumption */ 246 247 /* Reset timers 1 and 2 */ 248 opl_command(sc, OPL_L, OPL_TIMER_CONTROL, 249 OPL_TIMER1_MASK | OPL_TIMER2_MASK); 250 /* Reset the IRQ of the FM chip */ 251 opl_command(sc, OPL_L, OPL_TIMER_CONTROL, OPL_IRQ_RESET); 252 253 /* get status bits */ 254 status1 = bus_space_read_1(sc->iot,sc->ioh,OPL_STATUS+OPL_L+sc->offs); 255 256 opl_command(sc, OPL_L, OPL_TIMER1, -2); /* wait 2 ticks */ 257 opl_command(sc, OPL_L, OPL_TIMER_CONTROL, /* start timer1 */ 258 OPL_TIMER1_START | OPL_TIMER2_MASK); 259 delay(1000); /* wait for timer to expire */ 260 261 /* get status bits again */ 262 status2 = bus_space_read_1(sc->iot,sc->ioh,OPL_STATUS+OPL_L+sc->offs); 263 264 opl_command(sc, OPL_L, OPL_TIMER_CONTROL, 265 OPL_TIMER1_MASK | OPL_TIMER2_MASK); 266 opl_command(sc, OPL_L, OPL_TIMER_CONTROL, OPL_IRQ_RESET); 267 268 DPRINTFN(2,("opl_find: %02x %02x\n", status1, status2)); 269 270 if ((status1 & OPL_STATUS_MASK) != 0 || 271 (status2 & OPL_STATUS_MASK) != (OPL_STATUS_IRQ | OPL_STATUS_FT1)) 272 return (0); 273 274 switch(status1) { 275 case 0x00: 276 case 0x0f: 277 sc->model = OPL_3; 278 break; 279 case 0x06: 280 sc->model = OPL_2; 281 break; 282 default: 283 return (0); 284 } 285 286 DPRINTFN(2,("opl_find: OPL%d at 0x%x detected\n", 287 sc->model, (int)sc->ioh)); 288 return (1); 289 } 290 291 /* 292 * idea: opl_command does a lot of busywaiting, and the driver typically sets 293 * a lot of registers each time a voice-attack happens. some kind of 294 * caching to remember what was last written to each register could save 295 * a lot of cpu. It would have to be smart enough not to interfere with 296 * any necessary sequences of register access expected by the hardware... 297 */ 298 void 299 opl_set_op_reg(sc, base, voice, op, value) 300 struct opl_softc *sc; 301 int base; 302 int voice; 303 int op; 304 u_char value; 305 { 306 struct opl_voice *v = &sc->voices[voice]; 307 opl_command(sc, v->iooffs, base + v->op[op], value); 308 } 309 310 void 311 opl_set_ch_reg(sc, base, voice, value) 312 struct opl_softc *sc; 313 int base; 314 int voice; 315 u_char value; 316 { 317 struct opl_voice *v = &sc->voices[voice]; 318 opl_command(sc, v->iooffs, base + v->voiceno, value); 319 } 320 321 322 void 323 opl_load_patch(sc, v) 324 struct opl_softc *sc; 325 int v; 326 { 327 const struct opl_operators *p = sc->voices[v].patch; 328 329 opl_set_op_reg(sc, OPL_AM_VIB, v, 0, p->ops[OO_CHARS+0]); 330 opl_set_op_reg(sc, OPL_AM_VIB, v, 1, p->ops[OO_CHARS+1]); 331 opl_set_op_reg(sc, OPL_KSL_LEVEL, v, 0, p->ops[OO_KSL_LEV+0]); 332 opl_set_op_reg(sc, OPL_KSL_LEVEL, v, 1, p->ops[OO_KSL_LEV+1]); 333 opl_set_op_reg(sc, OPL_ATTACK_DECAY, v, 0, p->ops[OO_ATT_DEC+0]); 334 opl_set_op_reg(sc, OPL_ATTACK_DECAY, v, 1, p->ops[OO_ATT_DEC+1]); 335 opl_set_op_reg(sc, OPL_SUSTAIN_RELEASE, v, 0, p->ops[OO_SUS_REL+0]); 336 opl_set_op_reg(sc, OPL_SUSTAIN_RELEASE, v, 1, p->ops[OO_SUS_REL+1]); 337 opl_set_op_reg(sc, OPL_WAVE_SELECT, v, 0, p->ops[OO_WAV_SEL+0]); 338 opl_set_op_reg(sc, OPL_WAVE_SELECT, v, 1, p->ops[OO_WAV_SEL+1]); 339 opl_set_ch_reg(sc, OPL_FEEDBACK_CONNECTION, v, p->ops[OO_FB_CONN]); 340 } 341 342 uint32_t 343 opl_get_block_fnum(midipitch_t mp) 344 { 345 midihz18_t hz18; 346 uint32_t block; 347 uint32_t f_num; 348 349 /* 350 * We can get to about note 30 before needing to switch from block 0. 351 * Thereafter, switch block every octave; that will keep f_num in the 352 * upper end of its range, making the most bits available for 353 * resolution. 354 */ 355 block = ( mp - MIDIPITCH_FROM_KEY(19) ) / MIDIPITCH_OCTAVE; 356 if ( block > 7 ) /* subtract wrapped */ 357 block = 0; 358 /* 359 * Could subtract block*MIDIPITCH_OCTAVE here, or >>block later. Later. 360 */ 361 362 hz18 = MIDIPITCH_TO_HZ18(mp); 363 hz18 >>= block; 364 365 /* 366 * The formula in the manual is f_num = ((hz<<19)/fs)>>(block-1) (though 367 * block==0 implies >>-1 which is a C unspecified result). As we already 368 * have hz<<18 and I omitted the -1 when shifting above, what's left to 369 * do now is multiply by 4 and divide by fs, the sampling frequency of 370 * the chip. fs is the master clock frequency fM / 288, fM is 14.32 MHz 371 * so fs is a goofy number around 49.7kHz. The 5th convergent of the 372 * continued fraction matches 4/fs to 9+ significant figures. Doing the 373 * shift first (above) ensures there's room in hz18 to multiply by 9. 374 */ 375 376 f_num = (9 * hz18) / 111875; 377 return ((block << 10) | f_num); 378 } 379 380 381 void 382 opl_reset(sc) 383 struct opl_softc *sc; 384 { 385 int i; 386 387 for (i = 1; i <= OPL_MAXREG; i++) 388 opl_command(sc, OPL_L, OPL_KEYON_BLOCK + i, 0); 389 390 opl_command(sc, OPL_L, OPL_TEST, OPL_ENABLE_WAVE_SELECT); 391 opl_command(sc, OPL_L, OPL_PERCUSSION, 0); 392 if (sc->model == OPL_3) { 393 opl_command(sc, OPL_R, OPL_MODE, OPL3_ENABLE); 394 opl_command(sc, OPL_R,OPL_CONNECTION_SELECT,OPL_NOCONNECTION); 395 } 396 397 for (i = 0; i < MIDI_MAX_CHANS; i++) 398 sc->pan[i] = OPL_VOICE_TO_LEFT | OPL_VOICE_TO_RIGHT; 399 } 400 401 int 402 oplsyn_open(ms, flags) 403 midisyn *ms; 404 int flags; 405 { 406 struct opl_softc *sc = ms->data; 407 408 DPRINTFN(2, ("oplsyn_open: %d\n", flags)); 409 410 #ifndef AUDIO_NO_POWER_CTL 411 if (sc->powerctl) 412 sc->powerctl(sc->powerarg, 1); 413 #endif 414 opl_reset(ms->data); 415 if (sc->spkrctl) 416 sc->spkrctl(sc->spkrarg, 1); 417 return (0); 418 } 419 420 void 421 oplsyn_close(ms) 422 midisyn *ms; 423 { 424 struct opl_softc *sc = ms->data; 425 426 DPRINTFN(2, ("oplsyn_close:\n")); 427 428 /*opl_reset(ms->data);*/ 429 if (sc->spkrctl) 430 sc->spkrctl(sc->spkrarg, 0); 431 #ifndef AUDIO_NO_POWER_CTL 432 if (sc->powerctl) 433 sc->powerctl(sc->powerarg, 0); 434 #endif 435 } 436 437 #if 0 438 void 439 oplsyn_getinfo(addr, sd) 440 void *addr; 441 struct synth_dev *sd; 442 { 443 struct opl_softc *sc = addr; 444 445 sd->name = sc->model == OPL_2 ? "Yamaha OPL2" : "Yamaha OPL3"; 446 sd->type = SYNTH_TYPE_FM; 447 sd->subtype = sc->model == OPL_2 ? SYNTH_SUB_FM_TYPE_ADLIB 448 : SYNTH_SUB_FM_TYPE_OPL3; 449 sd->capabilities = 0; 450 } 451 #endif 452 453 void 454 oplsyn_reset(addr) 455 void *addr; 456 { 457 struct opl_softc *sc = addr; 458 DPRINTFN(3, ("oplsyn_reset:\n")); 459 opl_reset(sc); 460 } 461 462 int 463 opl_calc_vol(int regbyte, int16_t level_cB) 464 { 465 int level = regbyte & OPL_TOTAL_LEVEL_MASK; 466 467 /* 468 * level is a six-bit attenuation, from 0 (full output) 469 * to -48dB (but without the minus sign) in steps of .75 dB. 470 * We'll just add level_cB, after scaling it because it's 471 * in centibels instead and has the customary minus sign. 472 */ 473 474 level += ( -4 * level_cB ) / 30; 475 476 if (level > OPL_TOTAL_LEVEL_MASK) 477 level = OPL_TOTAL_LEVEL_MASK; 478 if (level < 0) 479 level = 0; 480 481 return level & OPL_TOTAL_LEVEL_MASK; 482 } 483 484 #define OPLACT_ARTICULATE 1 485 #define OPLACT_PITCH 2 486 #define OPLACT_LEVEL 4 487 488 void 489 oplsyn_attackv(midisyn *ms, 490 uint_fast16_t voice, midipitch_t mp, int16_t level_cB) 491 { 492 oplsyn_setv(ms, voice, mp, level_cB, 493 OPLACT_ARTICULATE | OPLACT_PITCH | OPLACT_LEVEL); 494 } 495 496 static void 497 oplsyn_repitchv(midisyn *ms, uint_fast16_t voice, midipitch_t mp) 498 { 499 oplsyn_setv(ms, voice, mp, 0, OPLACT_PITCH); 500 } 501 502 static void 503 oplsyn_relevelv(midisyn *ms, uint_fast16_t voice, int16_t level_cB) 504 { 505 oplsyn_setv(ms, voice, 0, level_cB, OPLACT_LEVEL); 506 } 507 508 static void 509 oplsyn_setv(midisyn *ms, 510 uint_fast16_t voice, midipitch_t mp, int16_t level_cB, int act) 511 { 512 struct opl_softc *sc = ms->data; 513 struct opl_voice *v; 514 const struct opl_operators *p; 515 u_int32_t block_fnum; 516 int mult; 517 int c_mult, m_mult; 518 u_int32_t chan; 519 u_int8_t chars0, chars1, ksl0, ksl1, fbc; 520 u_int8_t r20m, r20c, r40m, r40c, rA0, rB0; 521 u_int8_t vol0, vol1; 522 523 DPRINTFN(3, ("%s: %p %d %u %d\n", __func__, sc, voice, 524 mp, level_cB)); 525 526 #ifdef DIAGNOSTIC 527 if (voice < 0 || voice >= sc->syn.nvoice) { 528 printf("%s: bad voice %d\n", __func__, voice); 529 return; 530 } 531 #endif 532 v = &sc->voices[voice]; 533 534 if ( act & OPLACT_ARTICULATE ) { 535 /* Turn off old note */ 536 opl_set_op_reg(sc, OPL_KSL_LEVEL, voice, 0, 0xff); 537 opl_set_op_reg(sc, OPL_KSL_LEVEL, voice, 1, 0xff); 538 opl_set_ch_reg(sc, OPL_KEYON_BLOCK, voice, 0); 539 540 chan = MS_GETCHAN(&ms->voices[voice]); 541 p = &opl2_instrs[ms->pgms[chan]]; 542 v->patch = p; 543 opl_load_patch(sc, voice); 544 545 fbc = p->ops[OO_FB_CONN]; 546 if (sc->model == OPL_3) { 547 fbc &= ~OPL_STEREO_BITS; 548 fbc |= sc->pan[chan]; 549 } 550 opl_set_ch_reg(sc, OPL_FEEDBACK_CONNECTION, voice, fbc); 551 } else 552 p = v->patch; 553 554 if ( act & OPLACT_LEVEL ) { 555 /* 2 voice */ 556 ksl0 = p->ops[OO_KSL_LEV+0]; 557 ksl1 = p->ops[OO_KSL_LEV+1]; 558 if (p->ops[OO_FB_CONN] & 0x01) { 559 vol0 = opl_calc_vol(ksl0, level_cB); 560 vol1 = opl_calc_vol(ksl1, level_cB); 561 } else { 562 vol0 = ksl0; 563 vol1 = opl_calc_vol(ksl1, level_cB); 564 } 565 r40m = (ksl0 & OPL_KSL_MASK) | vol0; 566 r40c = (ksl1 & OPL_KSL_MASK) | vol1; 567 568 opl_set_op_reg(sc, OPL_KSL_LEVEL, voice, 0, r40m); 569 opl_set_op_reg(sc, OPL_KSL_LEVEL, voice, 1, r40c); 570 } 571 572 if ( act & OPLACT_PITCH ) { 573 mult = 1; 574 if ( mp > MIDIPITCH_FROM_KEY(114) ) { /* out of mult 1 range */ 575 mult = 4; /* will cover remaining MIDI range */ 576 mp -= 2*MIDIPITCH_OCTAVE; 577 } 578 579 block_fnum = opl_get_block_fnum(mp); 580 581 chars0 = p->ops[OO_CHARS+0]; 582 chars1 = p->ops[OO_CHARS+1]; 583 m_mult = (chars0 & OPL_MULTIPLE_MASK) * mult; 584 c_mult = (chars1 & OPL_MULTIPLE_MASK) * mult; 585 586 if ( 4 == mult ) { 587 if ( 0 == m_mult ) /* The OPL uses 0 to represent .5 */ 588 m_mult = 2; /* but of course 0*mult above did */ 589 if ( 0 == c_mult ) /* not DTRT */ 590 c_mult = 2; 591 } 592 593 if ((m_mult > 15) || (c_mult > 15)) { 594 printf("%s: frequency out of range %u (mult %d)\n", 595 __func__, mp, mult); 596 return; 597 } 598 r20m = (chars0 &~ OPL_MULTIPLE_MASK) | m_mult; 599 r20c = (chars1 &~ OPL_MULTIPLE_MASK) | c_mult; 600 601 rA0 = block_fnum & 0xFF; 602 rB0 = (block_fnum >> 8) | OPL_KEYON_BIT; 603 604 v->rB0 = rB0; 605 606 opl_set_op_reg(sc, OPL_AM_VIB, voice, 0, r20m); 607 opl_set_op_reg(sc, OPL_AM_VIB, voice, 1, r20c); 608 609 opl_set_ch_reg(sc, OPL_FNUM_LOW, voice, rA0); 610 opl_set_ch_reg(sc, OPL_KEYON_BLOCK, voice, rB0); 611 } 612 } 613 614 void 615 oplsyn_releasev(midisyn *ms, uint_fast16_t voice, uint_fast8_t vel) 616 { 617 struct opl_softc *sc = ms->data; 618 struct opl_voice *v; 619 620 DPRINTFN(3, ("%s: %p %d %d\n", __func__, sc, voice, 621 MIDISYN_FREQ_TO_HZ(note))); 622 623 #ifdef DIAGNOSTIC 624 if (voice < 0 || voice >= sc->syn.nvoice) { 625 printf("oplsyn_noteoff: bad voice %d\n", voice); 626 return; 627 } 628 #endif 629 v = &sc->voices[voice]; 630 opl_set_ch_reg(sc, 0xB0, voice, v->rB0 & ~OPL_KEYON_BIT); 631 } 632 633 int 634 oplsyn_ctlnotice(midisyn *ms, 635 midictl_evt evt, uint_fast8_t chan, uint_fast16_t key) 636 { 637 638 DPRINTFN(1, ("%s: %p %d\n", __func__, sc, chan)); 639 640 switch (evt) { 641 case MIDICTL_RESET: 642 oplsyn_panhandler(ms, chan); 643 return 1; 644 645 case MIDICTL_CTLR: 646 switch (key) { 647 case MIDI_CTRL_PAN_MSB: 648 oplsyn_panhandler(ms, chan); 649 return 1; 650 } 651 return 0; 652 default: 653 return 0; 654 } 655 } 656 657 /* PROGRAM CHANGE midi event: */ 658 void 659 oplsyn_programchange(midisyn *ms, uint_fast8_t chan, uint_fast8_t prog) 660 { 661 /* sanity checks */ 662 if (chan >= MIDI_MAX_CHANS) 663 return; 664 665 ms->pgms[chan] = prog; 666 } 667 668 void 669 oplsyn_loadpatch(ms, sysex, uio) 670 midisyn *ms; 671 struct sysex_info *sysex; 672 struct uio *uio; 673 { 674 #if 0 675 struct opl_softc *sc = ms->data; 676 struct sbi_instrument ins; 677 678 DPRINTFN(1, ("oplsyn_loadpatch: %p\n", sc)); 679 680 memcpy(&ins, sysex, sizeof *sysex); 681 if (uio->uio_resid >= sizeof ins - sizeof *sysex) 682 return EINVAL; 683 uiomove((char *)&ins + sizeof *sysex, sizeof ins - sizeof *sysex, uio); 684 /* XXX */ 685 #endif 686 } 687 688 static void 689 oplsyn_panhandler(midisyn *ms, uint_fast8_t chan) 690 { 691 struct opl_softc *sc = ms->data; 692 uint_fast16_t setting; 693 694 setting = midictl_read(&ms->ctl, chan, MIDI_CTRL_PAN_MSB, 8192); 695 setting >>= 7; /* we used to treat it as MSB only */ 696 sc->pan[chan] = 697 (setting <= OPL_MIDI_CENTER_MAX ? sc->panl : 0) | 698 (setting >= OPL_MIDI_CENTER_MIN ? sc->panr : 0); 699 } 700