⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opl3.c

📁 freebsd v4.4内核源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  opl3_command (map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);  opl3_command (map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);  /*   * Set Attack/Decay   */  opl3_command (map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);  opl3_command (map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);  /*   * Set Sustain/Release   */  opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);  opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);  /*   * Set Wave Select   */  opl3_command (map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);  opl3_command (map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);  /*   * Set Feedback/Connection   */  fpc = instr->operators[10];  if (!(fpc & 0x30))    fpc |= 0x30;		/*				 * Ensure that at least one chn is enabled				 */  opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num,		fpc);  /*   * If the voice is a 4 OP one, initialize the operators 3 and 4 also   */  if (voice_mode == 4)    {      /*       * Set Sound Characteristics       */      opl3_command (map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);      opl3_command (map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);      /*       * Set Attack/Decay       */      opl3_command (map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);      opl3_command (map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);      /*       * Set Sustain/Release       */      opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);      opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);      /*       * Set Wave Select       */      opl3_command (map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);      opl3_command (map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);      /*       * Set Feedback/Connection       */      fpc = instr->operators[OFFS_4OP + 10];      if (!(fpc & 0x30))	fpc |= 0x30;		/*				 * Ensure that at least one chn is enabled				 */      opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);    }  voices[voice].mode = voice_mode;  set_voice_volume (voice, volume);  freq = voices[voice].orig_freq = note_to_freq (note) / 1000;  /*   * Since the pitch bender may have been set before playing the note, we   * have to calculate the bending now.   */  freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);  voices[voice].current_freq = freq;  freq_to_fnum (freq, &block, &fnum);  /*   * Play note   */  data = fnum & 0xff;		/*				 * Least significant bits of fnumber				 */  opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);  data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);  voices[voice].keyon_byte = data;  opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);  if (voice_mode == 4)    opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);  return 0;}static voidfreq_to_fnum (int freq, int *block, int *fnum){  int             f, octave;  /*   * Converts the note frequency to block and fnum values for the FM chip   */  /*   * First try to compute the block -value (octave) where the note belongs   */  f = freq;  octave = 5;  if (f == 0)    octave = 0;  else if (f < 261)    {      while (f < 261)	{	  octave--;	  f <<= 1;	}    }  else if (f > 493)    {      while (f > 493)	{	  octave++;	  f >>= 1;	}    }  if (octave > 7)    octave = 7;  *fnum = freq * (1 << (20 - octave)) / 49716;  *block = octave;}static voidopl3_command (int io_addr, unsigned int addr, unsigned int val){  int             i;  /*   * The original 2-OP synth requires a quite long delay after writing to a   * register. The OPL-3 survives with just two INBs   */  OUTB ((unsigned char) (addr & 0xff), io_addr);	/*							 * Select register							 *							 */  if (!opl3_enabled)    tenmicrosec ();  else    for (i = 0; i < 2; i++)      INB (io_addr);#ifdef PC98  OUTB ((unsigned char) (val & 0xff), io_addr + 0x100);#else  OUTB ((unsigned char) (val & 0xff), io_addr + 1);	/*							 * Write to register							 *							 */#endif  if (!opl3_enabled)    {      tenmicrosec ();      tenmicrosec ();      tenmicrosec ();    }  else    for (i = 0; i < 2; i++)      INB (io_addr);}static voidopl3_reset (int dev){  int             i;  for (i = 0; i < nr_voices; i++)    {      opl3_command (physical_voices[logical_voices[i]].ioaddr,		KSL_LEVEL + physical_voices[logical_voices[i]].op[0], 0xff);      opl3_command (physical_voices[logical_voices[i]].ioaddr,		KSL_LEVEL + physical_voices[logical_voices[i]].op[1], 0xff);      if (physical_voices[logical_voices[i]].voice_mode == 4)	{	  opl3_command (physical_voices[logical_voices[i]].ioaddr,		KSL_LEVEL + physical_voices[logical_voices[i]].op[2], 0xff);	  opl3_command (physical_voices[logical_voices[i]].ioaddr,		KSL_LEVEL + physical_voices[logical_voices[i]].op[3], 0xff);	}      opl3_kill_note (dev, i, 0, 64);    }  if (opl3_enabled)    {      voice_alloc->max_voice = nr_voices = 18;      for (i = 0; i < 18; i++)	logical_voices[i] = i;      for (i = 0; i < 18; i++)	physical_voices[i].voice_mode = 2;    }}static intopl3_open (int dev, int mode){  int             i;  if (!opl3_ok)    return RET_ERROR (ENXIO);  if (opl3_busy)    return RET_ERROR (EBUSY);  opl3_busy = 1;  voice_alloc->max_voice = nr_voices = opl3_enabled ? 18 : 9;  voice_alloc->timestamp = 0;  for (i = 0; i < 18; i++)    {      voice_alloc->map[i] = 0;      voice_alloc->alloc_times[i] = 0;    }  connection_mask = 0x00;	/*				 * Just 2 OP voices				 */  if (opl3_enabled)    opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);  return 0;}static voidopl3_close (int dev){  opl3_busy = 0;  voice_alloc->max_voice = nr_voices = opl3_enabled ? 18 : 9;  fm_info.nr_drums = 0;  fm_info.perc_mode = 0;  opl3_reset (dev);}static voidopl3_hw_control (int dev, unsigned char *event){}static intopl3_load_patch (int dev, int format, snd_rw_buf * addr,		 int offs, int count, int pmgr_flag){  struct sbi_instrument ins;  if (count < sizeof (ins))    {      printk ("FM Error: Patch record too short\n");      return RET_ERROR (EINVAL);    }  COPY_FROM_USER (&((char *) &ins)[offs], (char *) addr, offs, sizeof (ins) - offs);  if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)    {      printk ("FM Error: Invalid instrument number %d\n", ins.channel);      return RET_ERROR (EINVAL);    }  ins.key = format;  return store_instr (ins.channel, &ins);}static voidopl3_panning (int dev, int voice, int pressure){}static voidopl3_volume_method (int dev, int mode){}#define SET_VIBRATO(cell) { \      tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \      if (pressure > 110) \	tmp |= 0x40;		/* Vibrato on */ \      opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}static voidopl3_aftertouch (int dev, int voice, int pressure){  int             tmp;  struct sbi_instrument *instr;  struct physical_voice_info *map;  if (voice < 0 || voice >= nr_voices)    return;  map = &physical_voices[logical_voices[voice]];  DEB (printk ("Aftertouch %d\n", voice));  if (map->voice_mode == 0)    return;  /*   * Adjust the amount of vibrato depending the pressure   */  instr = active_instrument[voice];  if (!instr)    instr = &instrmap[0];  if (voices[voice].mode == 4)    {      int             connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);      switch (connection)	{	case 0:	  SET_VIBRATO (4);	  break;	case 1:	  SET_VIBRATO (2);	  SET_VIBRATO (4);	  break;	case 2:	  SET_VIBRATO (1);	  SET_VIBRATO (4);	  break;	case 3:	  SET_VIBRATO (1);	  SET_VIBRATO (3);	  SET_VIBRATO (4);	  break;	}      /*       * Not implemented yet       */    }  else    {      SET_VIBRATO (1);      if ((instr->operators[10] & 0x01))	/*						 * Additive synthesis						 */	SET_VIBRATO (2);    }}#undef SET_VIBRATOstatic voidbend_pitch (int dev, int voice, int value){  unsigned char   data;  int             block, fnum, freq;  struct physical_voice_info *map;  map = &physical_voices[logical_voices[voice]];  if (map->voice_mode == 0)    return;  voices[voice].bender = value;  if (!value)    return;  if (!(voices[voice].keyon_byte & 0x20))    return;			/*				 * Not keyed on				 */  freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);  voices[voice].current_freq = freq;  freq_to_fnum (freq, &block, &fnum);  data = fnum & 0xff;		/*				 * Least significant bits of fnumber				 */  opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);  data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);	/*								 * *								 * KEYON|OCTAVE|MS								 *								 * * bits * *								 * of * f-num								 *								 */  voices[voice].keyon_byte = data;  opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);}static voidopl3_controller (int dev, int voice, int ctrl_num, int value){  if (voice < 0 || voice >= nr_voices)    return;  switch (ctrl_num)    {    case CTRL_PITCH_BENDER:      bend_pitch (dev, voice, value);      break;    case CTRL_PITCH_BENDER_RANGE:      voices[voice].bender_range = value;      break;    }}static intopl3_patchmgr (int dev, struct patmgr_info *rec){  return RET_ERROR (EINVAL);}static voidopl3_bender (int dev, int voice, int value){  if (voice < 0 || voice >= nr_voices)    return;  bend_pitch (dev, voice, value);}static intopl3_alloc_voice (int dev, int chn, int note, struct voice_alloc_info *alloc){  int             i, p, best, first, avail_voices, best_time = 0x7fffffff;  struct sbi_instrument *instr;  int             is4op;  int             instr_no;  if (chn < 0 || chn > 15)    instr_no = 0;  else    instr_no = chn_info[chn].pgm_num;  instr = &instrmap[instr_no];  if (instr->channel < 0 ||	/* Instrument not loaded */      nr_voices != 12)		/* Not in 4 OP mode */    is4op = 0;  else if (nr_voices == 12)	/* 4 OP mode */    is4op = (instr->key == OPL3_PATCH);  else    is4op = 0;  if (is4op)    {      first = p = 0;      avail_voices = 6;    }  else    {      if (nr_voices == 12)	/* 4 OP mode. Use the '2 OP only' voices first */	first = p = 6;      else	first = p = 0;      avail_voices = nr_voices;    }  /*     *    Now try to find a free voice   */  best = first;  for (i = 0; i < avail_voices; i++)    {      if (alloc->map[p] == 0)	{	  return p;	}      if (alloc->alloc_times[p] < best_time)	/* Find oldest playing note */	{	  best_time = alloc->alloc_times[p];	  best = p;	}      p = (p + 1) % avail_voices;    }  /*     *    Insert some kind of priority mechanism here.   */  if (best < 0)    best = 0;  if (best > nr_voices)    best -= nr_voices;  return best;			/* All voices in use. Select the first one. */}static voidopl3_setup_voice (int dev, int voice, int chn){  struct channel_info *info =  &synth_devs[dev]->chn_info[chn];  opl3_set_instr (dev, voice,		  info->pgm_num);  voices[voice].bender = info->bender_value;}static struct synth_operations opl3_operations ={  &fm_info,  0,  SYNTH_TYPE_FM,  FM_TYPE_ADLIB,  opl3_open,  opl3_close,  opl3_ioctl,  opl3_kill_note,  opl3_start_note,  opl3_set_instr,  opl3_reset,  opl3_hw_control,  opl3_load_patch,  opl3_aftertouch,  opl3_controller,  opl3_panning,  opl3_volume_method,  opl3_patchmgr,  opl3_bender,  opl3_alloc_voice,  opl3_setup_voice};longopl3_init (long mem_start){  int             i;  PERMANENT_MALLOC (struct sbi_instrument *, instrmap,		    SBFM_MAXINSTR * sizeof (*instrmap), mem_start);  if (num_synths >= MAX_SYNTH_DEV)    printk ("OPL3 Error: Too many synthesizers\n");  else    {      synth_devs[num_synths++] = &opl3_operations;      voice_alloc = &opl3_operations.alloc;      chn_info = &opl3_operations.chn_info[0];    }  fm_model = 0;  opl3_ok = 1;  if (opl3_enabled)    {      if (opl4_enabled)#if defined(__FreeBSD__)	printk ("opl0: <Yamaha OPL4/OPL3 FM>");      else	printk ("opl0: <Yamaha OPL-3 FM>");#else	printk (" <Yamaha OPL4/OPL3 FM>");      else	printk (" <Yamaha OPL-3 FM>");#endif      fm_model = 2;      voice_alloc->max_voice = nr_voices = 18;      fm_info.nr_drums = 0;      fm_info.capabilities |= SYNTH_CAP_OPL3;      strcpy (fm_info.name, "Yamaha OPL-3");      for (i = 0; i < 18; i++)	if (physical_voices[i].ioaddr == USE_LEFT)	  physical_voices[i].ioaddr = left_address;	else	  physical_voices[i].ioaddr = right_address;      opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE);	/*									 * Enable									 * OPL-3									 * mode									 */      opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x00);	/*									 * Select									 * all									 * 2-OP									 * *									 * voices									 */    }  else    {#if defined(__FreeBSD__)      printk ("opl0: <Yamaha 2-OP FM>");#else      printk (" <Yamaha 2-OP FM>");#endif      fm_model = 1;      voice_alloc->max_voice = nr_voices = 9;      fm_info.nr_drums = 0;      for (i = 0; i < 18; i++)	physical_voices[i].ioaddr = left_address;    };  already_initialized = 1;  for (i = 0; i < SBFM_MAXINSTR; i++)    instrmap[i].channel = -1;  return mem_start;}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -