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_aftertouch
  20. opl3_controller
  21. opl3_patchmgr
  22. opl3_init

   1 /*
   2  * linux/kernel/chr_drv/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 /* Major improvements to the FM handling 30AUG92 by Rob Hooft, */
  31 /* hooft@chem.ruu.nl */
  32 
  33 #include "sound_config.h"
  34 
  35 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_YM3812)
  36 
  37 #include "opl3.h"
  38 
  39 #define MAX_VOICE       18
  40 #define OFFS_4OP        11      /* Definitions for the operators OP3 and OP4
  41                                  * begin here */
  42 
  43 static int      opl3_enabled = 0;
  44 static int      left_address = 0x388, right_address = 0x388, both_address = 0;
  45 
  46 static int      nr_voices = 9;
  47 static int      logical_voices[MAX_VOICE] =
  48 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
  49 
  50 struct voice_info
  51   {
  52     unsigned char   keyon_byte;
  53     long            bender;
  54     long            bender_range;
  55     unsigned long   orig_freq;
  56     unsigned long   current_freq;
  57     int             mode;
  58   };
  59 
  60 static struct voice_info voices[MAX_VOICE];
  61 
  62 typedef struct sbi_instrument instr_array[SBFM_MAXINSTR];
  63 static instr_array instrmap;
  64 static struct sbi_instrument *active_instrument[MAX_VOICE] =
  65 {NULL};
  66 
  67 static struct synth_info fm_info =
  68 {"AdLib", 0, SYNTH_TYPE_FM, FM_TYPE_ADLIB, 0, 9, 0, SBFM_MAXINSTR, 0};
  69 
  70 static int      already_initialized = 0;
  71 
  72 static int      opl3_ok = 0;
  73 static int      opl3_busy = 0;
  74 static int      fm_model = 0;   /* 0=no fm, 1=mono, 2=SB Pro 1, 3=SB Pro 2       */
  75 
  76 static int      store_instr (int instr_no, struct sbi_instrument *instr);
  77 static void     freq_to_fnum (int freq, int *block, int *fnum);
  78 static void     opl3_command (int io_addr, const unsigned char addr, const unsigned char val);
  79 static int      opl3_kill_note (int dev, int voice, int velocity);
  80 static unsigned char connection_mask = 0x00;
  81 
  82 void
  83 enable_opl3_mode (int left, int right, int both)
     /* [previous][next][first][last][top][bottom][index][help] */
  84 {
  85   opl3_enabled = 1;
  86   left_address = left;
  87   right_address = right;
  88   both_address = both;
  89   fm_info.capabilities = SYNTH_CAP_OPL3;
  90   fm_info.synth_subtype = FM_TYPE_OPL3;
  91 }
  92 
  93 static void
  94 enter_4op_mode (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96   int             i;
  97   static int      voices_4op[MAX_VOICE] =
  98   {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
  99 
 100   connection_mask = 0x3f;
 101   opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x3f);       /* Select all 4-OP
 102                                                                          * voices */
 103   for (i = 0; i < 3; i++)
 104     physical_voices[i].voice_mode = 4;
 105   for (i = 3; i < 6; i++)
 106     physical_voices[i].voice_mode = 0;
 107 
 108   for (i = 9; i < 12; i++)
 109     physical_voices[i].voice_mode = 4;
 110   for (i = 12; i < 15; i++)
 111     physical_voices[i].voice_mode = 0;
 112 
 113   for (i = 0; i < 12; i++)
 114     logical_voices[i] = voices_4op[i];
 115   nr_voices = 12;
 116 }
 117 
 118 static int
 119 opl3_ioctl (int dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 120             unsigned int cmd, unsigned int arg)
 121 {
 122   switch (cmd)
 123     {
 124 
 125     case SNDCTL_FM_LOAD_INSTR:
 126       {
 127         struct sbi_instrument ins;
 128 
 129         IOCTL_FROM_USER ((char *) &ins, (char *) arg, 0, sizeof (ins));
 130 
 131         if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
 132           {
 133             printk ("FM Error: Invalid instrument number %d\n", ins.channel);
 134             return RET_ERROR (EINVAL);
 135           }
 136 
 137         pmgr_inform (dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
 138         return store_instr (ins.channel, &ins);
 139       }
 140       break;
 141 
 142     case SNDCTL_SYNTH_INFO:
 143       fm_info.nr_voices = (nr_voices == 12) ? 6 : nr_voices;
 144 
 145       IOCTL_TO_USER ((char *) arg, 0, &fm_info, sizeof (fm_info));
 146       return 0;
 147       break;
 148 
 149     case SNDCTL_SYNTH_MEMAVL:
 150       return 0x7fffffff;
 151       break;
 152 
 153     case SNDCTL_FM_4OP_ENABLE:
 154       if (opl3_enabled)
 155         enter_4op_mode ();
 156       return 0;
 157       break;
 158 
 159     default:
 160       return RET_ERROR (EINVAL);
 161     }
 162 
 163 }
 164 
 165 int
 166 opl3_detect (int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168   /*
 169    * This function returns 1 if the FM chicp is present at the given I/O port
 170    * The detection algorithm plays with the timer built in the FM chip and
 171    * looks for a change in the status register.
 172    * 
 173    * Note! The timers of the FM chip are not connected to AdLib (and compatible)
 174    * boards.
 175    * 
 176    * Note2! The chip is initialized if detected.
 177    */
 178 
 179   unsigned char   stat1, stat2;
 180   int             i;
 181 
 182   if (already_initialized)
 183     {
 184       return 0;                 /* Do avoid duplicate initializations */
 185     }
 186 
 187   if (opl3_enabled)
 188     ioaddr = left_address;
 189 
 190   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);     /* Reset timers 1 and 2 */
 191   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);     /* Reset the IRQ of FM
 192                                                                  * chicp */
 193 
 194   stat1 = INB (ioaddr);         /* Read status register */
 195 
 196   if ((stat1 & 0xE0) != 0x00)
 197     {
 198       return 0;                 /* Should be 0x00        */
 199     }
 200 
 201   opl3_command (ioaddr, TIMER1_REGISTER, 0xff); /* Set timer 1 to 0xff */
 202   opl3_command (ioaddr, TIMER_CONTROL_REGISTER,
 203                 TIMER2_MASK | TIMER1_START);    /* Unmask and start timer 1 */
 204 
 205   /*
 206    * Now we have to delay at least 80 msec
 207    */
 208 
 209   for (i = 0; i < 50; i++)
 210     tenmicrosec ();             /* To be sure */
 211 
 212   stat2 = INB (ioaddr);         /* Read status after timers have expired */
 213 
 214   /* Stop the timers */
 215 
 216   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);     /* Reset timers 1 and 2 */
 217   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);     /* Reset the IRQ of FM
 218                                                                  * chicp */
 219 
 220   if ((stat2 & 0xE0) != 0xc0)
 221     {
 222       return 0;                 /* There is no YM3812 */
 223     }
 224 
 225   /* There is a FM chicp in this address. Now set some default values. */
 226 
 227   for (i = 0; i < 9; i++)
 228     opl3_command (ioaddr, KEYON_BLOCK + i, 0);  /* Note off */
 229 
 230   opl3_command (ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
 231   opl3_command (ioaddr, PERCUSSION_REGISTER, 0x00);     /* Melodic mode. */
 232 
 233   return 1;
 234 }
 235 
 236 static int
 237 opl3_kill_note (int dev, int voice, int velocity)
     /* [previous][next][first][last][top][bottom][index][help] */
 238 {
 239   struct physical_voice_info *map;
 240 
 241   if (voice < 0 || voice >= nr_voices)
 242     return 0;
 243 
 244   map = &physical_voices[logical_voices[voice]];
 245 
 246   DEB (printk ("Kill note %d\n", voice));
 247 
 248   if (map->voice_mode == 0)
 249     return 0;
 250 
 251   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, voices[voice].keyon_byte & ~0x20);
 252 
 253   voices[voice].keyon_byte = 0;
 254   voices[voice].bender = 0;
 255   voices[voice].bender_range = 200;     /* 200 cents = 2 semitones */
 256   voices[voice].orig_freq = 0;
 257   voices[voice].current_freq = 0;
 258   voices[voice].mode = 0;
 259 
 260   return 0;
 261 }
 262 
 263 #define HIHAT                   0
 264 #define CYMBAL                  1
 265 #define TOMTOM                  2
 266 #define SNARE                   3
 267 #define BDRUM                   4
 268 #define UNDEFINED               TOMTOM
 269 #define DEFAULT                 TOMTOM
 270 
 271 static int
 272 store_instr (int instr_no, struct sbi_instrument *instr)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274 
 275   if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || !opl3_enabled))
 276     printk ("FM warning: Invalid patch format field (key) 0x%04x\n", instr->key);
 277   memcpy ((char *) &(instrmap[instr_no]), (char *) instr, sizeof (*instr));
 278 
 279   return 0;
 280 }
 281 
 282 static int
 283 opl3_set_instr (int dev, int voice, int instr_no)
     /* [previous][next][first][last][top][bottom][index][help] */
 284 {
 285   if (voice < 0 || voice >= nr_voices)
 286     return 0;
 287 
 288   if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
 289     return 0;
 290 
 291   active_instrument[voice] = &instrmap[instr_no];
 292   return 0;
 293 }
 294 
 295 /*
 296  * The next table looks magical, but it certainly is not. Its values have
 297  * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
 298  * for i=0. This log-table converts a linear volume-scaling (0..127) to a
 299  * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
 300  * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
 301  * volume -8 it was implemented as a table because it is only 128 bytes and
 302  * it saves a lot of log() calculations. (RH)
 303  */
 304 char            fm_volume_table[128] =
 305 {-64, -48, -40, -35, -32, -29, -27, -26,        /* 0 -   7 */
 306  -24, -23, -21, -20, -19, -18, -18, -17,        /* 8 -  15 */
 307  -16, -15, -15, -14, -13, -13, -12, -12,        /* 16 -  23 */
 308  -11, -11, -10, -10, -10, -9, -9, -8,   /* 24 -  31 */
 309  -8, -8, -7, -7, -7, -6, -6, -6,/* 32 -  39 */
 310  -5, -5, -5, -5, -4, -4, -4, -4,/* 40 -  47 */
 311  -3, -3, -3, -3, -2, -2, -2, -2,/* 48 -  55 */
 312  -2, -1, -1, -1, -1, 0, 0, 0,   /* 56 -  63 */
 313  0, 0, 0, 1, 1, 1, 1, 1,        /* 64 -  71 */
 314  1, 2, 2, 2, 2, 2, 2, 2,        /* 72 -  79 */
 315  3, 3, 3, 3, 3, 3, 3, 4,        /* 80 -  87 */
 316  4, 4, 4, 4, 4, 4, 4, 5,        /* 88 -  95 */
 317  5, 5, 5, 5, 5, 5, 5, 5,        /* 96 - 103 */
 318  6, 6, 6, 6, 6, 6, 6, 6,        /* 104 - 111 */
 319  6, 7, 7, 7, 7, 7, 7, 7,        /* 112 - 119 */
 320  7, 7, 7, 8, 8, 8, 8, 8};       /* 120 - 127 */
 321 
 322 static void
 323 calc_vol (unsigned char *regbyte, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 324 {
 325   int             level = (~*regbyte & 0x3f);
 326 
 327   if (level)
 328     level += fm_volume_table[volume];
 329 
 330   if (level > 0x3f)
 331     level = 0x3f;
 332   if (level < 0)
 333     level = 0;
 334 
 335   *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
 336 }
 337 
 338 static void
 339 set_voice_volume (int voice, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 340 {
 341   unsigned char   vol1, vol2, vol3, vol4;
 342   struct sbi_instrument *instr;
 343   struct physical_voice_info *map;
 344 
 345   if (voice < 0 || voice >= nr_voices)
 346     return;
 347 
 348   map = &physical_voices[logical_voices[voice]];
 349 
 350   instr = active_instrument[voice];
 351 
 352   if (!instr)
 353     instr = &instrmap[0];
 354 
 355   if (instr->channel < 0)
 356     return;
 357 
 358   if (voices[voice].mode == 0)
 359     return;
 360 
 361   if (voices[voice].mode == 2)
 362     {                           /* 2 OP voice */
 363 
 364       vol1 = instr->operators[2];
 365       vol2 = instr->operators[3];
 366 
 367       if ((instr->operators[10] & 0x01))
 368         {                       /* Additive synthesis    */
 369           calc_vol (&vol1, volume);
 370           calc_vol (&vol2, volume);
 371         }
 372       else
 373         {                       /* FM synthesis */
 374           calc_vol (&vol2, volume);
 375         }
 376 
 377       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1); /* Modulator volume */
 378       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2); /* Carrier volume */
 379     }
 380   else
 381     {                           /* 4 OP voice */
 382       int             connection;
 383 
 384       vol1 = instr->operators[2];
 385       vol2 = instr->operators[3];
 386       vol3 = instr->operators[OFFS_4OP + 2];
 387       vol4 = instr->operators[OFFS_4OP + 3];
 388 
 389       /*
 390        * The connection method for 4 OP voices is defined by the rightmost
 391        * bits at the offsets 10 and 10+OFFS_4OP
 392        */
 393 
 394       connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
 395 
 396       switch (connection)
 397         {
 398         case 0:
 399           calc_vol (&vol4, volume);     /* Just the OP 4 is carrier */
 400           break;
 401 
 402         case 1:
 403           calc_vol (&vol2, volume);
 404           calc_vol (&vol4, volume);
 405           break;
 406 
 407         case 2:
 408           calc_vol (&vol1, volume);
 409           calc_vol (&vol4, volume);
 410           break;
 411 
 412         case 3:
 413           calc_vol (&vol1, volume);
 414           calc_vol (&vol3, volume);
 415           calc_vol (&vol4, volume);
 416           break;
 417 
 418         default:/* Why ?? */ ;
 419         }
 420 
 421       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
 422       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
 423       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], vol3);
 424       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], vol4);
 425     }
 426 }
 427 
 428 static int
 429 opl3_start_note (int dev, int voice, int note, int volume)
     /* [previous][next][first][last][top][bottom][index][help] */
 430 {
 431   unsigned char   data, fpc;
 432   int             block, fnum, freq, voice_mode;
 433   struct sbi_instrument *instr;
 434   struct physical_voice_info *map;
 435 
 436   if (voice < 0 || voice >= nr_voices)
 437     return 0;
 438 
 439   map = &physical_voices[logical_voices[voice]];
 440 
 441   if (map->voice_mode == 0)
 442     return 0;
 443 
 444   if (note == 255)              /* Just change the volume */
 445     {
 446       set_voice_volume (voice, volume);
 447       return 0;
 448     }
 449 
 450   /* Kill previous note before playing */
 451   opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], 0xff);     /* Carrier volume to min */
 452   opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], 0xff);     /* Modulator volume to */
 453 
 454   if (map->voice_mode == 4)
 455     {
 456       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
 457       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
 458     }
 459 
 460   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);       /* Note off */
 461 
 462   instr = active_instrument[voice];
 463 
 464   if (!instr)
 465     instr = &instrmap[0];
 466 
 467   if (instr->channel < 0)
 468     {
 469       printk (
 470                "OPL3: Initializing voice %d with undefined instrument\n",
 471                voice);
 472       return 0;
 473     }
 474 
 475   if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
 476     return 0;                   /* Cannot play */
 477 
 478   voice_mode = map->voice_mode;
 479 
 480   if (voice_mode == 4)
 481     {
 482       int             voice_shift;
 483 
 484       voice_shift = (map->ioaddr == left_address) ? 0 : 3;
 485       voice_shift += map->voice_num;
 486 
 487       if (instr->key != OPL3_PATCH)     /* Just 2 OP patch */
 488         {
 489           voice_mode = 2;
 490           connection_mask &= ~(1 << voice_shift);
 491         }
 492       else
 493         {
 494           connection_mask |= (1 << voice_shift);
 495         }
 496 
 497       opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
 498     }
 499 
 500   /* Set Sound Characteristics */
 501   opl3_command (map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
 502   opl3_command (map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
 503 
 504   /* Set Attack/Decay */
 505   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
 506   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
 507 
 508   /* Set Sustain/Release */
 509   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
 510   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
 511 
 512   /* Set Wave Select */
 513   opl3_command (map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
 514   opl3_command (map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
 515 
 516   /* Set Feedback/Connection */
 517   fpc = instr->operators[10];
 518   if (!(fpc & 0x30))
 519     fpc |= 0x30;                /* Ensure that at least one chn is enabled */
 520   opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num,
 521                 fpc);
 522 
 523   /*
 524    * If the voice is a 4 OP one, initialize the operators 3 and 4 also
 525    */
 526 
 527   if (voice_mode == 4)
 528     {
 529 
 530       /* Set Sound Characteristics */
 531       opl3_command (map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
 532       opl3_command (map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
 533 
 534       /* Set Attack/Decay */
 535       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
 536       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
 537 
 538       /* Set Sustain/Release */
 539       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
 540       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
 541 
 542       /* Set Wave Select */
 543       opl3_command (map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
 544       opl3_command (map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
 545 
 546       /* Set Feedback/Connection */
 547       fpc = instr->operators[OFFS_4OP + 10];
 548       if (!(fpc & 0x30))
 549         fpc |= 0x30;            /* Ensure that at least one chn is enabled */
 550       opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
 551     }
 552 
 553   voices[voice].mode = voice_mode;
 554 
 555   set_voice_volume (voice, volume);
 556 
 557   freq = voices[voice].orig_freq = note_to_freq (note) / 1000;
 558 
 559   /*
 560    * Since the pitch bender may have been set before playing the note, we
 561    * have to calculate the bending now.
 562    */
 563 
 564   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
 565   voices[voice].current_freq = freq;
 566 
 567   freq_to_fnum (freq, &block, &fnum);
 568 
 569   /* Play note */
 570 
 571   data = fnum & 0xff;           /* Least significant bits of fnumber */
 572   opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
 573 
 574   data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
 575   voices[voice].keyon_byte = data;
 576   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
 577   if (voice_mode == 4)
 578     opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
 579 
 580   return 0;
 581 }
 582 
 583 static void
 584 freq_to_fnum (int freq, int *block, int *fnum)
     /* [previous][next][first][last][top][bottom][index][help] */
 585 {
 586   int             f, octave;
 587 
 588   /* Converts the note frequency to block and fnum values for the FM chip */
 589   /* First try to compute the block -value (octave) where the note belongs */
 590 
 591   f = freq;
 592 
 593   octave = 5;
 594 
 595   if (f == 0)
 596     octave = 0;
 597   else if (f < 261)
 598     {
 599       while (f < 261)
 600         {
 601           octave--;
 602           f <<= 1;
 603         }
 604     }
 605   else if (f > 493)
 606     {
 607       while (f > 493)
 608         {
 609           octave++;
 610           f >>= 1;
 611         }
 612     }
 613 
 614   if (octave > 7)
 615     octave = 7;
 616 
 617   *fnum = freq * (1 << (20 - octave)) / 49716;
 618   *block = octave;
 619 }
 620 
 621 static void
 622 opl3_command (int io_addr, const unsigned char addr, const unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 623 {
 624   int             i;
 625 
 626   /*
 627    * The original 2-OP synth requires a quite long delay after writing to a
 628    * register. The OPL-3 survives with just two INBs
 629    */
 630 
 631   OUTB (addr, io_addr);         /* Select register       */
 632 
 633   if (!opl3_enabled)
 634     tenmicrosec ();
 635   else
 636     for (i = 0; i < 2; i++)
 637       INB (io_addr);
 638 
 639   OUTB (val, io_addr + 1);      /* Write to register     */
 640 
 641   if (!opl3_enabled)
 642     {
 643       tenmicrosec ();
 644       tenmicrosec ();
 645       tenmicrosec ();
 646     }
 647   else
 648     for (i = 0; i < 2; i++)
 649       INB (io_addr);
 650 }
 651 
 652 static void
 653 opl3_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 654 {
 655   int             i;
 656 
 657   for (i = 0; i < nr_voices; i++)
 658     {
 659       opl3_command (physical_voices[logical_voices[i]].ioaddr,
 660                 KSL_LEVEL + physical_voices[logical_voices[i]].op[0], 0xff);    /* OP1 volume to min */
 661 
 662       opl3_command (physical_voices[logical_voices[i]].ioaddr,
 663                 KSL_LEVEL + physical_voices[logical_voices[i]].op[1], 0xff);    /* OP2 volume to min */
 664 
 665       if (physical_voices[logical_voices[i]].voice_mode == 4)   /* 4 OP voice */
 666         {
 667           opl3_command (physical_voices[logical_voices[i]].ioaddr,
 668                 KSL_LEVEL + physical_voices[logical_voices[i]].op[2], 0xff);    /* OP3 volume to min */
 669 
 670           opl3_command (physical_voices[logical_voices[i]].ioaddr,
 671                 KSL_LEVEL + physical_voices[logical_voices[i]].op[3], 0xff);    /* OP4 volume to min */
 672         }
 673 
 674       opl3_kill_note (dev, i, 64);
 675     }
 676 
 677   if (opl3_enabled)
 678     {
 679       nr_voices = 18;
 680 
 681       for (i = 0; i < 18; i++)
 682         logical_voices[i] = i;
 683 
 684       for (i = 0; i < 18; i++)
 685         physical_voices[i].voice_mode = 2;
 686 
 687     }
 688 
 689 }
 690 
 691 static int
 692 opl3_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 693 {
 694   if (!opl3_ok)
 695     return RET_ERROR (ENXIO);
 696   if (opl3_busy)
 697     return RET_ERROR (EBUSY);
 698   opl3_busy = 1;
 699 
 700   connection_mask = 0x00;       /* Just 2 OP voices */
 701   if (opl3_enabled)
 702     opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
 703   return 0;
 704 }
 705 
 706 static void
 707 opl3_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 708 {
 709   opl3_busy = 0;
 710   nr_voices = opl3_enabled ? 18 : 9;
 711   fm_info.nr_drums = 0;
 712   fm_info.perc_mode = 0;
 713 
 714   opl3_reset (dev);
 715 }
 716 
 717 static void
 718 opl3_hw_control (int dev, unsigned char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
 719 {
 720 }
 721 
 722 static int
 723 opl3_load_patch (int dev, int format, snd_rw_buf * addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 724                  int offs, int count, int pmgr_flag)
 725 {
 726   struct sbi_instrument ins;
 727 
 728   if (count < sizeof (ins))
 729     {
 730       printk ("FM Error: Patch record too short\n");
 731       return RET_ERROR (EINVAL);
 732     }
 733 
 734   COPY_FROM_USER (&((char *) &ins)[offs], (char *) addr, offs, sizeof (ins) - offs);
 735 
 736   if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
 737     {
 738       printk ("FM Error: Invalid instrument number %d\n", ins.channel);
 739       return RET_ERROR (EINVAL);
 740     }
 741   ins.key = format;
 742 
 743   return store_instr (ins.channel, &ins);
 744 }
 745 
 746 static void
 747 opl3_panning (int dev, int voice, int pressure)
     /* [previous][next][first][last][top][bottom][index][help] */
 748 {
 749 }
 750 
 751 #define SET_VIBRATO(cell) { \
 752       tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
 753       if (pressure > 110) \
 754         tmp |= 0x40;            /* Vibrato on */ \
 755       opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
 756 
 757 static void
 758 opl3_aftertouch (int dev, int voice, int pressure)
     /* [previous][next][first][last][top][bottom][index][help] */
 759 {
 760   int             tmp;
 761   struct sbi_instrument *instr;
 762   struct physical_voice_info *map;
 763 
 764   if (voice < 0 || voice >= nr_voices)
 765     return;
 766 
 767   map = &physical_voices[logical_voices[voice]];
 768 
 769   DEB (printk ("Aftertouch %d\n", voice));
 770 
 771   if (map->voice_mode == 0)
 772     return;
 773 
 774   /*
 775    * Adjust the amount of vibrato depending the pressure
 776    */
 777 
 778   instr = active_instrument[voice];
 779 
 780   if (!instr)
 781     instr = &instrmap[0];
 782 
 783   if (voices[voice].mode == 4)
 784     {
 785       int             connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
 786 
 787       switch (connection)
 788         {
 789         case 0:
 790           SET_VIBRATO (4);
 791           break;
 792 
 793         case 1:
 794           SET_VIBRATO (2);
 795           SET_VIBRATO (4);
 796           break;
 797 
 798         case 2:
 799           SET_VIBRATO (1);
 800           SET_VIBRATO (4);
 801           break;
 802 
 803         case 3:
 804           SET_VIBRATO (1);
 805           SET_VIBRATO (3);
 806           SET_VIBRATO (4);
 807           break;
 808 
 809         }
 810       /* Not implemented yet */
 811     }
 812   else
 813     {
 814       SET_VIBRATO (1);
 815 
 816       if ((instr->operators[10] & 0x01))        /* Additive synthesis */
 817         SET_VIBRATO (2);
 818     }
 819 }
 820 
 821 #undef SET_VIBRATO
 822 
 823 static void
 824 opl3_controller (int dev, int voice, int ctrl_num, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
 825 {
 826   unsigned char   data;
 827   int             block, fnum, freq;
 828   struct physical_voice_info *map;
 829 
 830   if (voice < 0 || voice >= nr_voices)
 831     return;
 832 
 833   map = &physical_voices[logical_voices[voice]];
 834 
 835   if (map->voice_mode == 0)
 836     return;
 837 
 838   switch (ctrl_num)
 839     {
 840     case CTRL_PITCH_BENDER:
 841       voices[voice].bender = value;
 842       if (!value)
 843         return;
 844       if (!(voices[voice].keyon_byte & 0x20))
 845         return;                 /* Not keyed on */
 846 
 847       freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
 848       voices[voice].current_freq = freq;
 849 
 850       freq_to_fnum (freq, &block, &fnum);
 851 
 852       data = fnum & 0xff;       /* Least significant bits of fnumber */
 853       opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
 854 
 855       data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3); /* KEYON|OCTAVE|MS bits
 856                                                                  * of f-num */
 857       voices[voice].keyon_byte = data;
 858       opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
 859       break;
 860 
 861     case CTRL_PITCH_BENDER_RANGE:
 862       voices[voice].bender_range = value;
 863       break;
 864     }
 865 }
 866 
 867 static int
 868 opl3_patchmgr (int dev, struct patmgr_info *rec)
     /* [previous][next][first][last][top][bottom][index][help] */
 869 {
 870   return RET_ERROR (EINVAL);
 871 }
 872 
 873 static struct synth_operations opl3_operations =
 874 {
 875   &fm_info,
 876   SYNTH_TYPE_FM,
 877   FM_TYPE_ADLIB,
 878   opl3_open,
 879   opl3_close,
 880   opl3_ioctl,
 881   opl3_kill_note,
 882   opl3_start_note,
 883   opl3_set_instr,
 884   opl3_reset,
 885   opl3_hw_control,
 886   opl3_load_patch,
 887   opl3_aftertouch,
 888   opl3_controller,
 889   opl3_panning,
 890   opl3_patchmgr
 891 };
 892 
 893 long
 894 opl3_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
 895 {
 896   int             i;
 897 
 898   synth_devs[num_synths++] = &opl3_operations;
 899   fm_model = 0;
 900   opl3_ok = 1;
 901   if (opl3_enabled)
 902     {
 903       printk (" <Yamaha OPL-3 FM>");
 904       fm_model = 2;
 905       nr_voices = 18;
 906       fm_info.nr_drums = 0;
 907       fm_info.capabilities |= SYNTH_CAP_OPL3;
 908 #ifndef SCO
 909       strcpy (fm_info.name, "Yamaha OPL-3");
 910 #endif
 911 
 912       for (i = 0; i < 18; i++)
 913         if (physical_voices[i].ioaddr == USE_LEFT)
 914           physical_voices[i].ioaddr = left_address;
 915         else
 916           physical_voices[i].ioaddr = right_address;
 917 
 918 
 919       opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE);    /* Enable OPL-3 mode */
 920       opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x00);   /* Select all 2-OP
 921                                                                          * voices */
 922     }
 923   else
 924     {
 925       printk (" <Yamaha 2-OP FM>");
 926       fm_model = 1;
 927       nr_voices = 9;
 928       fm_info.nr_drums = 0;
 929 
 930       for (i = 0; i < 18; i++)
 931         physical_voices[i].ioaddr = left_address;
 932     };
 933 
 934   already_initialized = 1;
 935   for (i = 0; i < SBFM_MAXINSTR; i++)
 936     instrmap[i].channel = -1;
 937 
 938   return mem_start;
 939 }
 940 
 941 #endif

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