root/drivers/sound/opl3.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. enable_opl3_mode
  2. enter_4op_mode
  3. opl3_ioctl
  4. opl3_detect
  5. opl3_kill_note
  6. store_instr
  7. opl3_set_instr
  8. calc_vol
  9. set_voice_volume
  10. opl3_start_note
  11. freq_to_fnum
  12. opl3_command
  13. opl3_reset
  14. opl3_open
  15. opl3_close
  16. opl3_hw_control
  17. opl3_load_patch
  18. opl3_panning
  19. opl3_volume_method
  20. opl3_aftertouch
  21. bend_pitch
  22. opl3_controller
  23. opl3_patchmgr
  24. opl3_bender
  25. opl3_alloc_voice
  26. opl3_setup_voice
  27. opl3_init

   1 /*
   2  * sound/opl3.c
   3  *
   4  * A low level driver for Yamaha YM3812 and OPL-3 -chips
   5  *
   6  * Copyright by Hannu Savolainen 1993
   7  *
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions are
  10  * met: 1. Redistributions of source code must retain the above copyright
  11  * notice, this list of conditions and the following disclaimer. 2.
  12  * Redistributions in binary form must reproduce the above copyright notice,
  13  * this list of conditions and the following disclaimer in the documentation
  14  * and/or other materials provided with the distribution.
  15  *
  16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26  * SUCH DAMAGE.
  27  *
  28  */
  29 
  30 /*
  31  * Major improvements to the FM handling 30AUG92 by Rob Hooft,
  32  */
  33 /*
  34  * hooft@chem.ruu.nl
  35  */
  36 
  37 #include "sound_config.h"
  38 
  39 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_YM3812)
  40 
  41 #include "opl3.h"
  42 
  43 #define MAX_VOICE       18
  44 #define OFFS_4OP        11      /*
  45                                    * * * Definitions for the operators OP3 and
  46                                    * * OP4 * * begin here   */
  47 
  48 static int      opl3_enabled = 0;
  49 static int      opl4_enabled = 0;
  50 static int      left_address = 0x388, right_address = 0x388, both_address = 0;
  51 
  52 static int      nr_voices = 9;
  53 static int      logical_voices[MAX_VOICE] =
  54 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
  55 
  56 struct voice_info
  57   {
  58     unsigned char   keyon_byte;
  59     long            bender;
  60     long            bender_range;
  61     unsigned long   orig_freq;
  62     unsigned long   current_freq;
  63     int             mode;
  64   };
  65 
  66 static struct voice_info voices[MAX_VOICE];
  67 static struct voice_alloc_info *voice_alloc;
  68 static struct channel_info *chn_info;
  69 
  70 static struct sbi_instrument *instrmap;
  71 static struct sbi_instrument *active_instrument[MAX_VOICE] =
  72 {NULL};
  73 
  74 static struct synth_info fm_info =
  75 {"OPL-2", 0, SYNTH_TYPE_FM, FM_TYPE_ADLIB, 0, 9, 0, SBFM_MAXINSTR, 0};
  76 
  77 static int      already_initialized = 0;
  78 
  79 static int      opl3_ok = 0;
  80 static int      opl3_busy = 0;
  81 static int      fm_model = 0;   /*
  82 
  83 
  84                                  * *  * * 0=no fm, 1=mono, 2=SB Pro 1, 3=SB
  85                                  * Pro 2 * *    */
  86 
  87 static int      store_instr (int instr_no, struct sbi_instrument *instr);
  88 static void     freq_to_fnum (int freq, int *block, int *fnum);
  89 static void     opl3_command (int io_addr, unsigned int addr, unsigned int val);
  90 static int      opl3_kill_note (int dev, int voice, int note, int velocity);
  91 static unsigned char connection_mask = 0x00;
  92 
  93 void
  94 enable_opl3_mode (int left, int right, int both)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96   if (opl3_enabled)
  97     return;
  98 
  99   opl3_enabled = 1;
 100   left_address = left;
 101   right_address = right;
 102   both_address = both;
 103   fm_info.capabilities = SYNTH_CAP_OPL3;
 104   fm_info.synth_subtype = FM_TYPE_OPL3;
 105 }
 106 
 107 static void
 108 enter_4op_mode (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 109 {
 110   int             i;
 111   static int      voices_4op[MAX_VOICE] =
 112   {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
 113 
 114   connection_mask = 0x3f;       /* Connect all possible 4 OP voices */
 115   opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x3f);
 116 
 117   for (i = 0; i < 3; i++)
 118     physical_voices[i].voice_mode = 4;
 119   for (i = 3; i < 6; i++)
 120     physical_voices[i].voice_mode = 0;
 121 
 122   for (i = 9; i < 12; i++)
 123     physical_voices[i].voice_mode = 4;
 124   for (i = 12; i < 15; i++)
 125     physical_voices[i].voice_mode = 0;
 126 
 127   for (i = 0; i < 12; i++)
 128     logical_voices[i] = voices_4op[i];
 129   voice_alloc->max_voice = nr_voices = 12;
 130 }
 131 
 132 static int
 133 opl3_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 134             unsigned int cmd, unsigned int arg)
 135 {
 136   switch (cmd)
 137     {
 138 
 139     case SNDCTL_FM_LOAD_INSTR:
 140       {
 141         struct sbi_instrument ins;
 142 
 143         IOCTL_FROM_USER ((char *) &ins, (char *) arg, 0, sizeof (ins));
 144 
 145         if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
 146           {
 147             printk ("FM Error: Invalid instrument number %d\n", ins.channel);
 148             return RET_ERROR (EINVAL);
 149           }
 150 
 151         pmgr_inform (dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
 152         return store_instr (ins.channel, &ins);
 153       }
 154       break;
 155 
 156     case SNDCTL_SYNTH_INFO:
 157       fm_info.nr_voices = (nr_voices == 12) ? 6 : nr_voices;
 158 
 159       IOCTL_TO_USER ((char *) arg, 0, &fm_info, sizeof (fm_info));
 160       return 0;
 161       break;
 162 
 163     case SNDCTL_SYNTH_MEMAVL:
 164       return 0x7fffffff;
 165       break;
 166 
 167     case SNDCTL_FM_4OP_ENABLE:
 168       if (opl3_enabled)
 169         enter_4op_mode ();
 170       return 0;
 171       break;
 172 
 173     default:
 174       return RET_ERROR (EINVAL);
 175     }
 176 
 177 }
 178 
 179 int
 180 opl3_detect (int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 181 {
 182   /*
 183    * This function returns 1 if the FM chicp is present at the given I/O port
 184    * The detection algorithm plays with the timer built in the FM chip and
 185    * looks for a change in the status register.
 186    *
 187    * Note! The timers of the FM chip are not connected to AdLib (and compatible)
 188    * boards.
 189    *
 190    * Note2! The chip is initialized if detected.
 191    */
 192 
 193   unsigned char   stat1, stat2, signature;
 194   int             i;
 195 
 196   if (already_initialized)
 197     {
 198       return 0;                 /*
 199                                  * Do avoid duplicate initializations
 200                                  */
 201     }
 202 
 203   if (opl3_enabled)
 204     ioaddr = left_address;
 205 
 206   /* Reset timers 1 and 2 */
 207   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
 208 
 209   /* Reset the IRQ of the FM chip */
 210   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
 211 
 212   signature = stat1 = INB (ioaddr);     /* Status register */
 213 
 214   if ((stat1 & 0xE0) != 0x00)
 215     {
 216       return 0;                 /*
 217                                  * Should be 0x00
 218                                  */
 219     }
 220 
 221   opl3_command (ioaddr, TIMER1_REGISTER, 0xff);         /* Set timer1 to 0xff */
 222 
 223   opl3_command (ioaddr, TIMER_CONTROL_REGISTER,
 224                 TIMER2_MASK | TIMER1_START);    /*
 225                                                  * Unmask and start timer 1
 226                                                  */
 227 
 228   /*
 229    * Now we have to delay at least 80 usec
 230    */
 231 
 232   for (i = 0; i < 50; i++)
 233     tenmicrosec ();
 234 
 235   stat2 = INB (ioaddr);         /*
 236                                  * Read status after timers have expired
 237                                  */
 238 
 239   /*
 240    * Stop the timers
 241    */
 242 
 243   /* Reset timers 1 and 2 */
 244   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
 245   /* Reset the IRQ of the FM chip */
 246   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
 247 
 248   if ((stat2 & 0xE0) != 0xc0)
 249     {
 250       return 0;                 /*
 251                                  * There is no YM3812
 252                                  */
 253     }
 254 
 255   /*
 256    * There is a FM chicp in this address. Detect the type (OPL2 to OPL4)
 257    */
 258 
 259   if (signature == 0x06)        /* OPL2 */
 260     {
 261       opl3_enabled = 0;
 262     }
 263   else if (signature == 0x00)   /* OPL3 or OPL4 */
 264     {
 265       unsigned char   tmp;
 266 
 267       if (!opl3_enabled)        /* Was not already enabled */
 268         {
 269           left_address = ioaddr;
 270           right_address = ioaddr + 2;
 271           opl3_enabled = 1;
 272         }
 273 
 274       /*
 275        * Detect availability of OPL4 (_experimental_). Works propably
 276        * only after a cold boot. In addition the OPL4 port
 277        * of the chip may not be connected to the PC bus at all.
 278        */
 279 
 280       opl3_command (right_address, OPL3_MODE_REGISTER, 0x00);
 281       opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
 282 
 283       if ((tmp = INB (ioaddr)) == 0x02)         /* Have a OPL4 */
 284         {
 285           opl4_enabled = 1;
 286         }
 287       opl3_command (right_address, OPL3_MODE_REGISTER, 0);
 288 
 289     }
 290 
 291   for (i = 0; i < 9; i++)
 292     opl3_command (ioaddr, KEYON_BLOCK + i, 0);  /*
 293                                                  * Note off
 294                                                  */
 295 
 296   opl3_command (ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
 297   opl3_command (ioaddr, PERCUSSION_REGISTER, 0x00);     /*
 298                                                          * Melodic mode.
 299                                                          */
 300 
 301   return 1;
 302 }
 303 
 304 static int
 305 opl3_kill_note (int dev, int voice, int note, int velocity)
     /* [previous][next][first][last][top][bottom][index][help] */
 306 {
 307   struct physical_voice_info *map;
 308 
 309   if (voice < 0 || voice >= nr_voices)
 310     return 0;
 311 
 312   voice_alloc->map[voice] = 0;
 313 
 314   map = &physical_voices[logical_voices[voice]];
 315 
 316   DEB (printk ("Kill note %d\n", voice));
 317 
 318   if (map->voice_mode == 0)
 319     return 0;
 320 
 321   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, voices[voice].keyon_byte & ~0x20);
 322 
 323   voices[voice].keyon_byte = 0;
 324   voices[voice].bender = 0;
 325   voices[voice].bender_range = 200;     /*
 326                                          * 200 cents = 2 semitones
 327                                          */
 328   voices[voice].orig_freq = 0;
 329   voices[voice].current_freq = 0;
 330   voices[voice].mode = 0;
 331 
 332   return 0;
 333 }
 334 
 335 #define HIHAT                   0
 336 #define CYMBAL                  1
 337 #define TOMTOM                  2
 338 #define SNARE                   3
 339 #define BDRUM                   4
 340 #define UNDEFINED               TOMTOM
 341 #define DEFAULT                 TOMTOM
 342 
 343 static int
 344 store_instr (int instr_no, struct sbi_instrument *instr)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346 
 347   if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || !opl3_enabled))
 348     printk ("FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
 349   memcpy ((char *) &(instrmap[instr_no]), (char *) instr, sizeof (*instr));
 350 
 351   return 0;
 352 }
 353 
 354 static int
 355 opl3_set_instr (int dev, int voice, int instr_no)
     /* [previous][next][first][last][top][bottom][index][help] */
 356 {
 357   if (voice < 0 || voice >= nr_voices)
 358     return 0;
 359 
 360   if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
 361     return 0;
 362 
 363   active_instrument[voice] = &instrmap[instr_no];
 364   return 0;
 365 }
 366 
 367 /*
 368  * The next table looks magical, but it certainly is not. Its values have
 369  * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
 370  * for i=0. This log-table converts a linear volume-scaling (0..127) to a
 371  * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
 372  * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
 373  * volume -8 it was implemented as a table because it is only 128 bytes and
 374  * it saves a lot of log() calculations. (RH)
 375  */
 376 char            fm_volume_table[128] =
 377 {-64, -48, -40, -35, -32, -29, -27, -26,        /*
 378                                                  * 0 -   7
 379                                                  */
 380  -24, -23, -21, -20, -19, -18, -18, -17,        /*
 381                                                  * 8 -  15
 382                                                  */
 383  -16, -15, -15, -14, -13, -13, -12, -12,        /*
 384                                                  * 16 -  23
 385                                                  */
 386  -11, -11, -10, -10, -10, -9, -9, -8,   /*
 387                                          * 24 -  31
 388                                          */
 389  -8, -8, -7, -7, -7, -6, -6, -6,        /*
 390                                            * 32 -  39
 391                                          */
 392  -5, -5, -5, -5, -4, -4, -4, -4,        /*
 393                                            * 40 -  47
 394                                          */
 395  -3, -3, -3, -3, -2, -2, -2, -2,        /*
 396                                            * 48 -  55
 397                                          */
 398  -2, -1, -1, -1, -1, 0, 0, 0,   /*
 399                                  * 56 -  63
 400                                  */
 401  0, 0, 0, 1, 1, 1, 1, 1,        /*
 402                                  * 64 -  71
 403                                  */
 404  1, 2, 2, 2, 2, 2, 2, 2,        /*
 405                                  * 72 -  79
 406                                  */
 407  3, 3, 3, 3, 3, 3, 3, 4,        /*
 408                                  * 80 -  87
 409                                  */
 410  4, 4, 4, 4, 4, 4, 4, 5,        /*
 411                                  * 88 -  95
 412                                  */
 413  5, 5, 5, 5, 5, 5, 5, 5,        /*
 414                                  * 96 - 103
 415                                  */
 416  6, 6, 6, 6, 6, 6, 6, 6,        /*
 417                                  * 104 - 111
 418                                  */
 419  6, 7, 7, 7, 7, 7, 7, 7,        /*
 420                                  * 112 - 119
 421                                  */
 422  7, 7, 7, 8, 8, 8, 8, 8};       /*
 423 
 424 
 425                                  * *  * * 120 - 127   */
 426 
 427 static void
 428 calc_vol (unsigned char *regbyte, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 429 {
 430   int             level = (~*regbyte & 0x3f);
 431 
 432   if (level)
 433     level += fm_volume_table[volume];
 434 
 435   if (level > 0x3f)
 436     level = 0x3f;
 437   if (level < 0)
 438     level = 0;
 439 
 440   *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
 441 }
 442 
 443 static void
 444 set_voice_volume (int voice, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 445 {
 446   unsigned char   vol1, vol2, vol3, vol4;
 447   struct sbi_instrument *instr;
 448   struct physical_voice_info *map;
 449 
 450   if (voice < 0 || voice >= nr_voices)
 451     return;
 452 
 453   map = &physical_voices[logical_voices[voice]];
 454 
 455   instr = active_instrument[voice];
 456 
 457   if (!instr)
 458     instr = &instrmap[0];
 459 
 460   if (instr->channel < 0)
 461     return;
 462 
 463   if (voices[voice].mode == 0)
 464     return;
 465 
 466   if (voices[voice].mode == 2)
 467     {                           /*
 468                                  * 2 OP voice
 469                                  */
 470 
 471       vol1 = instr->operators[2];
 472       vol2 = instr->operators[3];
 473 
 474       if ((instr->operators[10] & 0x01))
 475         {                       /*
 476                                  * Additive synthesis
 477                                  */
 478           calc_vol (&vol1, volume);
 479           calc_vol (&vol2, volume);
 480         }
 481       else
 482         {                       /*
 483                                  * FM synthesis
 484                                  */
 485           calc_vol (&vol2, volume);
 486         }
 487 
 488       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);         /*
 489                                                                            * Modulator
 490                                                                            * volume
 491                                                                          */
 492       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);         /*
 493                                                                            * Carrier
 494                                                                            * volume
 495                                                                          */
 496     }
 497   else
 498     {                           /*
 499                                  * 4 OP voice
 500                                  */
 501       int             connection;
 502 
 503       vol1 = instr->operators[2];
 504       vol2 = instr->operators[3];
 505       vol3 = instr->operators[OFFS_4OP + 2];
 506       vol4 = instr->operators[OFFS_4OP + 3];
 507 
 508       /*
 509        * The connection method for 4 OP voices is defined by the rightmost
 510        * bits at the offsets 10 and 10+OFFS_4OP
 511        */
 512 
 513       connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
 514 
 515       switch (connection)
 516         {
 517         case 0:
 518           calc_vol (&vol4, volume);     /*
 519                                          * Just the OP 4 is carrier
 520                                          */
 521           break;
 522 
 523         case 1:
 524           calc_vol (&vol2, volume);
 525           calc_vol (&vol4, volume);
 526           break;
 527 
 528         case 2:
 529           calc_vol (&vol1, volume);
 530           calc_vol (&vol4, volume);
 531           break;
 532 
 533         case 3:
 534           calc_vol (&vol1, volume);
 535           calc_vol (&vol3, volume);
 536           calc_vol (&vol4, volume);
 537           break;
 538 
 539         default:                /*
 540                                  * Why ??
 541                                  */ ;
 542         }
 543 
 544       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
 545       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
 546       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], vol3);
 547       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], vol4);
 548     }
 549 }
 550 
 551 static int
 552 opl3_start_note (int dev, int voice, int note, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 553 {
 554   unsigned char   data, fpc;
 555   int             block, fnum, freq, voice_mode;
 556   struct sbi_instrument *instr;
 557   struct physical_voice_info *map;
 558 
 559   if (voice < 0 || voice >= nr_voices)
 560     return 0;
 561 
 562   map = &physical_voices[logical_voices[voice]];
 563 
 564   if (map->voice_mode == 0)
 565     return 0;
 566 
 567   if (note == 255)              /*
 568                                  * Just change the volume
 569                                  */
 570     {
 571       set_voice_volume (voice, volume);
 572       return 0;
 573     }
 574 
 575   /*
 576    * Kill previous note before playing
 577    */
 578   opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], 0xff);     /*
 579                                                                  * Carrier
 580                                                                  * volume to
 581                                                                  * min
 582                                                                  */
 583   opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], 0xff);     /*
 584                                                                  * Modulator
 585                                                                  * volume to
 586                                                                  */
 587 
 588   if (map->voice_mode == 4)
 589     {
 590       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
 591       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
 592     }
 593 
 594   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);       /*
 595                                                                          * Note
 596                                                                          * off
 597                                                                          */
 598 
 599   instr = active_instrument[voice];
 600 
 601   if (!instr)
 602     instr = &instrmap[0];
 603 
 604   if (instr->channel < 0)
 605     {
 606       printk (
 607                "OPL3: Initializing voice %d with undefined instrument\n",
 608                voice);
 609       return 0;
 610     }
 611 
 612   if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
 613     return 0;                   /*
 614                                  * Cannot play
 615                                  */
 616 
 617   voice_mode = map->voice_mode;
 618 
 619   if (voice_mode == 4)
 620     {
 621       int             voice_shift;
 622 
 623       voice_shift = (map->ioaddr == left_address) ? 0 : 3;
 624       voice_shift += map->voice_num;
 625 
 626       if (instr->key != OPL3_PATCH)     /*
 627                                          * Just 2 OP patch
 628                                          */
 629         {
 630           voice_mode = 2;
 631           connection_mask &= ~(1 << voice_shift);
 632         }
 633       else
 634         {
 635           connection_mask |= (1 << voice_shift);
 636         }
 637 
 638       opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
 639     }
 640 
 641   /*
 642    * Set Sound Characteristics
 643    */
 644   opl3_command (map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
 645   opl3_command (map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
 646 
 647   /*
 648    * Set Attack/Decay
 649    */
 650   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
 651   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
 652 
 653   /*
 654    * Set Sustain/Release
 655    */
 656   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
 657   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
 658 
 659   /*
 660    * Set Wave Select
 661    */
 662   opl3_command (map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
 663   opl3_command (map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
 664 
 665   /*
 666    * Set Feedback/Connection
 667    */
 668   fpc = instr->operators[10];
 669   if (!(fpc & 0x30))
 670     fpc |= 0x30;                /*
 671                                  * Ensure that at least one chn is enabled
 672                                  */
 673   opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num,
 674                 fpc);
 675 
 676   /*
 677    * If the voice is a 4 OP one, initialize the operators 3 and 4 also
 678    */
 679 
 680   if (voice_mode == 4)
 681     {
 682 
 683       /*
 684        * Set Sound Characteristics
 685        */
 686       opl3_command (map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
 687       opl3_command (map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
 688 
 689       /*
 690        * Set Attack/Decay
 691        */
 692       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
 693       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
 694 
 695       /*
 696        * Set Sustain/Release
 697        */
 698       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
 699       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
 700 
 701       /*
 702        * Set Wave Select
 703        */
 704       opl3_command (map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
 705       opl3_command (map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
 706 
 707       /*
 708        * Set Feedback/Connection
 709        */
 710       fpc = instr->operators[OFFS_4OP + 10];
 711       if (!(fpc & 0x30))
 712         fpc |= 0x30;            /*
 713                                  * Ensure that at least one chn is enabled
 714                                  */
 715       opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
 716     }
 717 
 718   voices[voice].mode = voice_mode;
 719 
 720   set_voice_volume (voice, volume);
 721 
 722   freq = voices[voice].orig_freq = note_to_freq (note) / 1000;
 723 
 724   /*
 725    * Since the pitch bender may have been set before playing the note, we
 726    * have to calculate the bending now.
 727    */
 728 
 729   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
 730   voices[voice].current_freq = freq;
 731 
 732   freq_to_fnum (freq, &block, &fnum);
 733 
 734   /*
 735    * Play note
 736    */
 737 
 738   data = fnum & 0xff;           /*
 739                                  * Least significant bits of fnumber
 740                                  */
 741   opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
 742 
 743   data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
 744   voices[voice].keyon_byte = data;
 745   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
 746   if (voice_mode == 4)
 747     opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
 748 
 749   return 0;
 750 }
 751 
 752 static void
 753 freq_to_fnum (int freq, int *block, int *fnum)
     /* [previous][next][first][last][top][bottom][index][help] */
 754 {
 755   int             f, octave;
 756 
 757   /*
 758    * Converts the note frequency to block and fnum values for the FM chip
 759    */
 760   /*
 761    * First try to compute the block -value (octave) where the note belongs
 762    */
 763 
 764   f = freq;
 765 
 766   octave = 5;
 767 
 768   if (f == 0)
 769     octave = 0;
 770   else if (f < 261)
 771     {
 772       while (f < 261)
 773         {
 774           octave--;
 775           f <<= 1;
 776         }
 777     }
 778   else if (f > 493)
 779     {
 780       while (f > 493)
 781         {
 782           octave++;
 783           f >>= 1;
 784         }
 785     }
 786 
 787   if (octave > 7)
 788     octave = 7;
 789 
 790   *fnum = freq * (1 << (20 - octave)) / 49716;
 791   *block = octave;
 792 }
 793 
 794 static void
 795 opl3_command (int io_addr, unsigned int addr, unsigned int val)
     /* [previous][next][first][last][top][bottom][index][help] */
 796 {
 797   int             i;
 798 
 799   /*
 800    * The original 2-OP synth requires a quite long delay after writing to a
 801    * register. The OPL-3 survives with just two INBs
 802    */
 803 
 804   OUTB ((unsigned char) (addr & 0xff), io_addr);        /*
 805                                                          * Select register
 806                                                          *
 807                                                          */
 808 
 809   if (!opl3_enabled)
 810     tenmicrosec ();
 811   else
 812     for (i = 0; i < 2; i++)
 813       INB (io_addr);
 814 
 815   OUTB ((unsigned char) (val & 0xff), io_addr + 1);     /*
 816                                                          * Write to register
 817                                                          *
 818                                                          */
 819 
 820   if (!opl3_enabled)
 821     {
 822       tenmicrosec ();
 823       tenmicrosec ();
 824       tenmicrosec ();
 825     }
 826   else
 827     for (i = 0; i < 2; i++)
 828       INB (io_addr);
 829 }
 830 
 831 static void
 832 opl3_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 833 {
 834   int             i;
 835 
 836   for (i = 0; i < nr_voices; i++)
 837     {
 838       opl3_command (physical_voices[logical_voices[i]].ioaddr,
 839                 KSL_LEVEL + physical_voices[logical_voices[i]].op[0], 0xff);
 840 
 841       opl3_command (physical_voices[logical_voices[i]].ioaddr,
 842                 KSL_LEVEL + physical_voices[logical_voices[i]].op[1], 0xff);
 843 
 844       if (physical_voices[logical_voices[i]].voice_mode == 4)
 845         {
 846           opl3_command (physical_voices[logical_voices[i]].ioaddr,
 847                 KSL_LEVEL + physical_voices[logical_voices[i]].op[2], 0xff);
 848 
 849           opl3_command (physical_voices[logical_voices[i]].ioaddr,
 850                 KSL_LEVEL + physical_voices[logical_voices[i]].op[3], 0xff);
 851         }
 852 
 853       opl3_kill_note (dev, i, 0, 64);
 854     }
 855 
 856   if (opl3_enabled)
 857     {
 858       voice_alloc->max_voice = nr_voices = 18;
 859 
 860       for (i = 0; i < 18; i++)
 861         logical_voices[i] = i;
 862 
 863       for (i = 0; i < 18; i++)
 864         physical_voices[i].voice_mode = 2;
 865 
 866     }
 867 
 868 }
 869 
 870 static int
 871 opl3_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 872 {
 873   int             i;
 874 
 875   if (!opl3_ok)
 876     return RET_ERROR (ENXIO);
 877   if (opl3_busy)
 878     return RET_ERROR (EBUSY);
 879   opl3_busy = 1;
 880 
 881   voice_alloc->max_voice = nr_voices = opl3_enabled ? 18 : 9;
 882   voice_alloc->timestamp = 0;
 883 
 884   for (i = 0; i < 18; i++)
 885     {
 886       voice_alloc->map[i] = 0;
 887       voice_alloc->alloc_times[i] = 0;
 888     }
 889 
 890   connection_mask = 0x00;       /*
 891                                  * Just 2 OP voices
 892                                  */
 893   if (opl3_enabled)
 894     opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
 895   return 0;
 896 }
 897 
 898 static void
 899 opl3_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 900 {
 901   opl3_busy = 0;
 902   voice_alloc->max_voice = nr_voices = opl3_enabled ? 18 : 9;
 903 
 904   fm_info.nr_drums = 0;
 905   fm_info.perc_mode = 0;
 906 
 907   opl3_reset (dev);
 908 }
 909 
 910 static void
 911 opl3_hw_control (int dev, unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 912 {
 913 }
 914 
 915 static int
 916 opl3_load_patch (int dev, int format, snd_rw_buf * addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 917                  int offs, int count, int pmgr_flag)
 918 {
 919   struct sbi_instrument ins;
 920 
 921   if (count < sizeof (ins))
 922     {
 923       printk ("FM Error: Patch record too short\n");
 924       return RET_ERROR (EINVAL);
 925     }
 926 
 927   COPY_FROM_USER (&((char *) &ins)[offs], (char *) addr, offs, sizeof (ins) - offs);
 928 
 929   if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
 930     {
 931       printk ("FM Error: Invalid instrument number %d\n", ins.channel);
 932       return RET_ERROR (EINVAL);
 933     }
 934   ins.key = format;
 935 
 936   return store_instr (ins.channel, &ins);
 937 }
 938 
 939 static void
 940 opl3_panning (int dev, int voice, int pressure)
     /* [previous][next][first][last][top][bottom][index][help] */
 941 {
 942 }
 943 
 944 static void
 945 opl3_volume_method (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 946 {
 947 }
 948 
 949 #define SET_VIBRATO(cell) { \
 950       tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
 951       if (pressure > 110) \
 952         tmp |= 0x40;            /* Vibrato on */ \
 953       opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
 954 
 955 static void
 956 opl3_aftertouch (int dev, int voice, int pressure)
     /* [previous][next][first][last][top][bottom][index][help] */
 957 {
 958   int             tmp;
 959   struct sbi_instrument *instr;
 960   struct physical_voice_info *map;
 961 
 962   if (voice < 0 || voice >= nr_voices)
 963     return;
 964 
 965   map = &physical_voices[logical_voices[voice]];
 966 
 967   DEB (printk ("Aftertouch %d\n", voice));
 968 
 969   if (map->voice_mode == 0)
 970     return;
 971 
 972   /*
 973    * Adjust the amount of vibrato depending the pressure
 974    */
 975 
 976   instr = active_instrument[voice];
 977 
 978   if (!instr)
 979     instr = &instrmap[0];
 980 
 981   if (voices[voice].mode == 4)
 982     {
 983       int             connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
 984 
 985       switch (connection)
 986         {
 987         case 0:
 988           SET_VIBRATO (4);
 989           break;
 990 
 991         case 1:
 992           SET_VIBRATO (2);
 993           SET_VIBRATO (4);
 994           break;
 995 
 996         case 2:
 997           SET_VIBRATO (1);
 998           SET_VIBRATO (4);
 999           break;
1000 
1001         case 3:
1002           SET_VIBRATO (1);
1003           SET_VIBRATO (3);
1004           SET_VIBRATO (4);
1005           break;
1006 
1007         }
1008       /*
1009        * Not implemented yet
1010        */
1011     }
1012   else
1013     {
1014       SET_VIBRATO (1);
1015 
1016       if ((instr->operators[10] & 0x01))        /*
1017                                                  * Additive synthesis
1018                                                  */
1019         SET_VIBRATO (2);
1020     }
1021 }
1022 
1023 #undef SET_VIBRATO
1024 
1025 static void
1026 bend_pitch (int dev, int voice, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
1027 {
1028   unsigned char   data;
1029   int             block, fnum, freq;
1030   struct physical_voice_info *map;
1031 
1032   map = &physical_voices[logical_voices[voice]];
1033 
1034   if (map->voice_mode == 0)
1035     return;
1036 
1037   voices[voice].bender = value;
1038   if (!value)
1039     return;
1040   if (!(voices[voice].keyon_byte & 0x20))
1041     return;                     /*
1042                                  * Not keyed on
1043                                  */
1044 
1045   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
1046   voices[voice].current_freq = freq;
1047 
1048   freq_to_fnum (freq, &block, &fnum);
1049 
1050   data = fnum & 0xff;           /*
1051                                  * Least significant bits of fnumber
1052                                  */
1053   opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
1054 
1055   data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);     /*
1056                                                                  * *
1057                                                                  * KEYON|OCTAVE|MS
1058                                                                  *
1059                                                                  * * bits * *
1060                                                                  * of * f-num
1061                                                                  *
1062                                                                  */
1063   voices[voice].keyon_byte = data;
1064   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
1065 }
1066 
1067 static void
1068 opl3_controller (int dev, int voice, int ctrl_num, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
1069 {
1070   if (voice < 0 || voice >= nr_voices)
1071     return;
1072 
1073   switch (ctrl_num)
1074     {
1075     case CTRL_PITCH_BENDER:
1076       bend_pitch (dev, voice, value);
1077       break;
1078 
1079     case CTRL_PITCH_BENDER_RANGE:
1080       voices[voice].bender_range = value;
1081       break;
1082     }
1083 }
1084 
1085 static int
1086 opl3_patchmgr (int dev, struct patmgr_info *rec)
     /* [previous][next][first][last][top][bottom][index][help] */
1087 {
1088   return RET_ERROR (EINVAL);
1089 }
1090 
1091 static void
1092 opl3_bender (int dev, int voice, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
1093 {
1094   if (voice < 0 || voice >= nr_voices)
1095     return;
1096 
1097   bend_pitch (dev, voice, value - 8192);
1098 }
1099 
1100 static int
1101 opl3_alloc_voice (int dev, int chn, int note, struct voice_alloc_info *alloc)
     /* [previous][next][first][last][top][bottom][index][help] */
1102 {
1103   int             i, p, best, first, avail_voices, best_time = 0x7fffffff;
1104   struct sbi_instrument *instr;
1105   int             is4op;
1106   int             instr_no;
1107 
1108   if (chn < 0 || chn > 15)
1109     instr_no = 0;
1110   else
1111     instr_no = chn_info[chn].pgm_num;
1112 
1113   instr = &instrmap[instr_no];
1114   if (instr->channel < 0 ||     /* Instrument not loaded */
1115       nr_voices != 12)          /* Not in 4 OP mode */
1116     is4op = 0;
1117   else if (nr_voices == 12)     /* 4 OP mode */
1118     is4op = (instr->key == OPL3_PATCH);
1119   else
1120     is4op = 0;
1121 
1122   if (is4op)
1123     {
1124       first = p = 0;
1125       avail_voices = 6;
1126     }
1127   else
1128     {
1129       if (nr_voices == 12)      /* 4 OP mode. Use the '2 OP only' voices first */
1130         first = p = 6;
1131       else
1132         first = p = 0;
1133       avail_voices = nr_voices;
1134     }
1135 
1136   /*
1137      *    Now try to find a free voice
1138    */
1139   best = first;
1140 
1141   for (i = 0; i < avail_voices; i++)
1142     {
1143       if (alloc->map[p] == 0)
1144         {
1145           return p;
1146         }
1147       if (alloc->alloc_times[p] < best_time)    /* Find oldest playing note */
1148         {
1149           best_time = alloc->alloc_times[p];
1150           best = p;
1151         }
1152       p = (p + 1) % avail_voices;
1153     }
1154 
1155   /*
1156      *    Insert some kind of priority mechanism here.
1157    */
1158 
1159   if (best < 0)
1160     best = 0;
1161   if (best > nr_voices)
1162     best -= nr_voices;
1163 
1164   return best;                  /* All voices in use. Select the first one. */
1165 }
1166 
1167 static void
1168 opl3_setup_voice (int dev, int voice, int chn)
     /* [previous][next][first][last][top][bottom][index][help] */
1169 {
1170   struct channel_info *info =
1171   &synth_devs[dev]->chn_info[chn];
1172 
1173   opl3_set_instr (dev, voice,
1174                   info->pgm_num);
1175 
1176   voices[voice].bender = info->bender_value;
1177 }
1178 
1179 static struct synth_operations opl3_operations =
1180 {
1181   &fm_info,
1182   0,
1183   SYNTH_TYPE_FM,
1184   FM_TYPE_ADLIB,
1185   opl3_open,
1186   opl3_close,
1187   opl3_ioctl,
1188   opl3_kill_note,
1189   opl3_start_note,
1190   opl3_set_instr,
1191   opl3_reset,
1192   opl3_hw_control,
1193   opl3_load_patch,
1194   opl3_aftertouch,
1195   opl3_controller,
1196   opl3_panning,
1197   opl3_volume_method,
1198   opl3_patchmgr,
1199   opl3_bender,
1200   opl3_alloc_voice,
1201   opl3_setup_voice
1202 };
1203 
1204 long
1205 opl3_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1206 {
1207   int             i;
1208 
1209   PERMANENT_MALLOC (struct sbi_instrument *, instrmap,
1210                     SBFM_MAXINSTR * sizeof (*instrmap), mem_start);
1211 
1212   if (num_synths >= MAX_SYNTH_DEV)
1213     printk ("OPL3 Error: Too many synthesizers\n");
1214   else
1215     {
1216       synth_devs[num_synths++] = &opl3_operations;
1217       voice_alloc = &opl3_operations.alloc;
1218       chn_info = &opl3_operations.chn_info[0];
1219     }
1220 
1221   fm_model = 0;
1222   opl3_ok = 1;
1223   if (opl3_enabled)
1224     {
1225       if (opl4_enabled)
1226         printk (" <Yamaha OPL4/OPL3 FM>");
1227       else
1228         printk (" <Yamaha OPL-3 FM>");
1229 
1230       fm_model = 2;
1231       voice_alloc->max_voice = nr_voices = 18;
1232       fm_info.nr_drums = 0;
1233       fm_info.capabilities |= SYNTH_CAP_OPL3;
1234       strcpy (fm_info.name, "Yamaha OPL-3");
1235 
1236       for (i = 0; i < 18; i++)
1237         if (physical_voices[i].ioaddr == USE_LEFT)
1238           physical_voices[i].ioaddr = left_address;
1239         else
1240           physical_voices[i].ioaddr = right_address;
1241 
1242 
1243       opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE);    /*
1244                                                                          * Enable
1245                                                                          * OPL-3
1246                                                                          * mode
1247                                                                          */
1248       opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x00);   /*
1249                                                                          * Select
1250                                                                          * all
1251                                                                          * 2-OP
1252                                                                          * *
1253                                                                          * voices
1254                                                                          */
1255     }
1256   else
1257     {
1258       printk (" <Yamaha 2-OP FM>");
1259       fm_model = 1;
1260       voice_alloc->max_voice = nr_voices = 9;
1261       fm_info.nr_drums = 0;
1262 
1263       for (i = 0; i < 18; i++)
1264         physical_voices[i].ioaddr = left_address;
1265     };
1266 
1267   already_initialized = 1;
1268   for (i = 0; i < SBFM_MAXINSTR; i++)
1269     instrmap[i].channel = -1;
1270 
1271   return mem_start;
1272 }
1273 
1274 #endif

/* [previous][next][first][last][top][bottom][index][help] */