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

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