Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'FMOut (./kdelibs/libkmid/fmout.h:43)

class FMOut : public MidiOut
{
  private:
    class FMOutPrivate;
    FMOutPrivate *di;

    int patchloaded[256];
    /**
     * Takes a value of 2 or 3, for FM or OPL3 support
     */
    int opl;
    int nvoices;

    VoiceManager *vm;

    void modifyPatch(char *buf, int key);
    void loadFMPatches  (void);

  public:
    /**
     * Constructor. See @ref MidiOut::MidiOut() for more information.
     */
    FMOut  ( int d=0, int total =12 );

    /**
     * Destructor. 
     */
    ~FMOut ();

    /**
     * See @ref MidiOut::openDev()
     */
    virtual void openDev	( int sqfd );

    /**
     * See @ref MidiOut::closeDev()
     */
    virtual void closeDev	( void );

    /**
     * See @ref MidiOut::initDev()
     */
    virtual void initDev	( void );

    /**
     * See @ref MidiOut::noteOn()
     */
    virtual void noteOn		( uchar chn, uchar note, uchar vel );

    /**
     * See @ref MidiOut::noteOff()
     */
    virtual void noteOff		( uchar chn, uchar note, uchar vel );

    /**
     * See @ref MidiOut::keyPressure()
     */
    virtual void keyPressure	( uchar chn, uchar note, uchar vel );

    /**
     * See @ref MidiOut::chnPatchChange()
     */
    virtual void chnPatchChange	( uchar chn, uchar patch );

    /**
     * See @ref MidiOut::chnPressure()
     */
    virtual void chnPressure	( uchar chn, uchar vel );

    /**
     * See @ref MidiOut::chnPitchBender()
     */
    virtual void chnPitchBender	( uchar chn, uchar lsb,  uchar msb );

    /**
     * See @ref MidiOut::chnController()
     */
    virtual void chnController	( uchar chn, uchar ctl , uchar v ); 

    /**
     * It's an empty function, as FM devices don't support System Exclusive
     * messages
     */
    virtual void sysex		( uchar *data,ulong size);

    /**
     * See @ref MidiOut::setVolumePercentage()
     */
    virtual void setVolumePercentage    ( int i );

    /**
     * Returns @p p if the patch p has been loaded, or another patch (already loaded)
     * if @p p hasn't been loaded. 
     */
    int patch(int p);

  private:
    static const char *FMPatchesDirectory;
    static int deleteFMPatchesDirectory;

  public:
    /**
     * Sets the directory where the FM patches are stored, that is, where the
     * std.o3, std.sb, drums.o3 and drums.sb files can be found.
     *
     * It will store a copy of the parameter, so you should delete the memory
     * used by the parameter you passed.
     */
    static void setFMPatchesDirectory(const char *dir);

};

kdelibs'FMOut::FMOut() (./kdelibs/libkmid/fmout.cc:44)

FMOut::FMOut( int d, int total )
{
  seqfd = -1;
  devicetype = KMID_FM;
  device = d;
#ifdef HANDLETIMEINDEVICES
  count = 0.0;
  lastcount = 0.0;
  rate = 100;
#endif
  _ok = 1;
  // Put opl=3 for opl/3 (better quality/ 6 voices)
  //  or opl=2 for fm output (less quality/ 18 voices, which is better imho) : 
  opl = 2;
  // But be aware that opl=3 is not intended to be fully supported by now

  nvoices = total;
  vm = new VoiceManager (nvoices);
}


kdelibs'FMOut::~FMOut() (./kdelibs/libkmid/fmout.cc:64)

FMOut::~FMOut()
{
  delete map;
  closeDev();
  if (deleteFMPatchesDirectory) 
  {
    delete FMPatchesDirectory;
    deleteFMPatchesDirectory = 0;
    FMPatchesDirectory="/etc";
  }
}


kdelibs'FMOut::openDev() (./kdelibs/libkmid/fmout.cc:76)

void FMOut::openDev (int sqfd)
{
#ifdef HAVE_OSS_SUPPORT
  _ok=1;
  seqfd = sqfd;
  //vm->clearLists();
  if ( seqfd == -1 )
  {
    printfdebug("ERROR: Could not open /dev/sequencer\n");
    return;
  }
#ifdef HANDLETIMEINDEVICES
  ioctl( seqfd, SNDCTL_SEQ_NRSYNTHS, &ndevs);
  ioctl( seqfd, SNDCTL_SEQ_NRMIDIS, &nmidiports);

  rate = 0;
  int r = ioctl( seqfd, SNDCTL_SEQ_CTRLRATE, &rate);
  if ( ( r == -1 ) || ( rate <= 0 ) ) rate = HZ;
  convertrate = 1000/rate;

  count = 0.0;
  lastcount = 0.0;

#endif
  //seqbufClean();
  //ioctl(seqfd,SNDCTL_SEQ_RESET);
  //ioctl(seqfd,SNDCTL_SEQ_PANIC);

  loadFMPatches();
#endif

}


kdelibs'FMOut::closeDev() (./kdelibs/libkmid/fmout.cc:109)

void FMOut::closeDev (void)
{
  if (!ok()) return;
#ifdef HANDLETIMEINDEVICES
  SEQ_STOP_TIMER();
  SEQ_DUMPBUF();
#endif
  vm->clearLists();
  //if (seqfd>=0) close(seqfd);
  seqfd = -1;
}


kdelibs'FMOut::initDev() (./kdelibs/libkmid/fmout.cc:121)

void FMOut::initDev (void)
{
#ifdef HAVE_OSS_SUPPORT
  int chn;
  if (!ok()) return;
#ifdef HANDLETIMEINDEVICES
  count=0.0;
  lastcount=0.0;
#endif
  uchar gm_reset[5]={0x7e, 0x7f, 0x09, 0x01, 0xf7};
  sysex(gm_reset, sizeof(gm_reset));
  for (chn=0;chn<16;chn++)
  {
    chnmute[chn]=0;
    chnPatchChange(chn,0);
    chnPressure(chn,127);
    chnPitchBender(chn, 0x00, 0x40);
    chnController(chn, CTL_MAIN_VOLUME,127);
    chnController(chn, CTL_EXT_EFF_DEPTH, 0);
    chnController(chn, CTL_CHORUS_DEPTH, 0);
    chnController(chn, 0x4a, 127);
  }

  if (opl==3) ioctl(seqfd, SNDCTL_FM_4OP_ENABLE, &device);
  SEQ_VOLUME_MODE(device,VOL_METHOD_LINEAR);

  for (int i = 0; i < nvoices; i++)
  {
    SEQ_CONTROL(device, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
    SEQ_STOP_NOTE(device, i, vm->note(i), 64);
  }
#endif
}


kdelibs'FMOut::loadFMPatches() (./kdelibs/libkmid/fmout.cc:155)

void FMOut::loadFMPatches(void)
{
#ifdef HAVE_OSS_SUPPORT
  char patchesfile[120];
  char drumsfile[120];
  int size;
  struct sbi_instrument instr;
  char tmp[60];
  int i,j;
  for ( i=0; i<256; i++ )
    patchloaded[i] = 0;
  int stereoeffect=rand()%3;
  FILE *fh;
  int datasize;

  if (opl==3)
  {
    sprintf(patchesfile,"%s/std.o3",FMPatchesDirectory);
    size=60;
  }
  else
  {
    sprintf(patchesfile,"%s/std.sb",FMPatchesDirectory);
    size=52;
  }
  fh=fopen(patchesfile,"rb");
  if (fh==NULL) return;

  for (i=0;i<128;i++)
  {
    fread(tmp,size,1,fh);
    patchloaded[i]=1;
    instr.key = ((strncmp(tmp, "4OP", 3) == 0))? OPL3_PATCH : FM_PATCH;
    datasize = (strncmp(tmp, "4OP", 3) == 0)? 22 : 11;
    instr.device=device;
    instr.channel = i;
    // Let's get some stereo effect ...
    tmp[46] = (tmp[46] & 0xcf) | ((++stereoeffect)<<4);
    stereoeffect=stereoeffect%3;
    for (j=0; j<22; j++)
      instr.operators[j] = tmp[j+36];
    SEQ_WRPATCH(&instr,sizeof(instr));
  }
  fclose(fh);

  if (opl==3)
  {
    sprintf(drumsfile,"%s/drums.o3",FMPatchesDirectory);
  }
  else
  {
    sprintf(drumsfile,"%s/drums.sb",FMPatchesDirectory);
  }

  fh=fopen(drumsfile,"rb");
  if (fh==NULL) return;

  for (i=128;i<175;i++)
  {
    fread(tmp,size,1,fh);
    patchloaded[i]=1;
    instr.key = (strncmp(tmp, "4OP", 3) == 0)? OPL3_PATCH : FM_PATCH;
    datasize = (strncmp(tmp, "4OP", 3) == 0)? 22 : 11;
    instr.device=device;
    instr.channel = i;
    // Let's get some stereo effect ...
    tmp[46] = (tmp[46] & 0xcf) | ((++stereoeffect)<<4);
    stereoeffect=stereoeffect%3;
    for (j=0; j<22; j++)
      instr.operators[j] = tmp[j+36];
    SEQ_WRPATCH(&instr,sizeof(instr));
  }
  fclose(fh);

#ifdef FMOUTDEBUG
  printfdebug("Patches loaded\n");
#endif
#endif
}


kdelibs'FMOut::patch() (./kdelibs/libkmid/fmout.cc:235)

int FMOut::patch(int p)
{
  if (patchloaded[p]==1) return p;
#ifdef FMOUTDEBUG
  printfdebug("Not loaded %d!\n",p);
#endif
  p=0;
  while ((p<256)&&(patchloaded[p]==0)) p++;
  return p;
}


kdelibs'FMOut::noteOn() (./kdelibs/libkmid/fmout.cc:246)

void FMOut::noteOn  (uchar chn, uchar note, uchar vel)
{
  if (vel==0)
  {
    noteOff(chn,note,vel);
  }
  else
  {
    if (chn==PERCUSSION_CHANNEL)
    {
      if (patchloaded[note+128]==0) return;
      else
	if (patchloaded[chnpatch[chn]]==0) return;
    }
    int v=vm->allocateVoice(chn,note);
    int p;
    if (chn==PERCUSSION_CHANNEL)
      SEQ_SET_PATCH(device,v ,p=patch(note+128))
    else
      SEQ_SET_PATCH(device,v ,p=map->patch(chn,chnpatch[chn])); 
    SEQ_BENDER(device, v, chnbender[chn]);

    SEQ_START_NOTE(device, v, note, vel);
    //    SEQ_CONTROL(device, v, CTL_MAIN_VOLUME, chncontroller[chn][CTL_MAIN_VOLUME]);

    SEQ_CHN_PRESSURE(device, v , chnpressure[chn]);
  }

#ifdef FMOUTDEBUG
  printfdebug("Note ON >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
#endif
}


kdelibs'FMOut::noteOff() (./kdelibs/libkmid/fmout.cc:279)

void FMOut::noteOff (uchar chn, uchar note, uchar vel)
{
  int i;
  vm->initSearch();
  while ((i=vm->search(chn,note))!=-1)
  {
    SEQ_STOP_NOTE(device, i, note, vel);
    vm->deallocateVoice(i);
  }

#ifdef FMOUTDEBUG
  printfdebug("Note OFF >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
#endif
}


kdelibs'FMOut::keyPressure() (./kdelibs/libkmid/fmout.cc:294)

void FMOut::keyPressure (uchar chn, uchar note, uchar vel)
{
  int i;
  vm->initSearch();
  while ((i=vm->search(chn,note))!=-1)
    SEQ_KEY_PRESSURE(device, i, note,vel);
}


kdelibs'FMOut::chnPatchChange() (./kdelibs/libkmid/fmout.cc:302)

void FMOut::chnPatchChange (uchar chn, uchar patch)
{
  if (chn==PERCUSSION_CHANNEL) return;
  int i;
  vm->initSearch();
  while ((i=vm->search(chn))!=-1)
    SEQ_SET_PATCH(device,i,map->patch(chn,patch)); 

  chnpatch[chn]=patch;
}


kdelibs'FMOut::chnPressure() (./kdelibs/libkmid/fmout.cc:313)

void FMOut::chnPressure (uchar chn, uchar vel)
{
  int i;
  vm->initSearch();
  while ((i=vm->search(chn))!=-1)
    SEQ_CHN_PRESSURE(device, i , vel);

  chnpressure[chn]=vel;
}


kdelibs'FMOut::chnPitchBender() (./kdelibs/libkmid/fmout.cc:323)

void FMOut::chnPitchBender(uchar chn,uchar lsb, uchar msb)
{
  chnbender[chn]=((int)msb<<7) | (lsb & 0x7F);

  int i;
  vm->initSearch();
  while ((i=vm->search(chn))!=-1)
    SEQ_BENDER(device, i, chnbender[chn]);

}


kdelibs'FMOut::chnController() (./kdelibs/libkmid/fmout.cc:334)

void FMOut::chnController (uchar chn, uchar ctl, uchar v) 
{
  if ((ctl==11)||(ctl==7))
  {
    v=(v*volumepercentage)/100;
    if (v>127) v=127;
  }
  int i;
  vm->initSearch();
  while ((i=vm->search(chn))!=-1)
    SEQ_CONTROL(device, i, ctl, v);

  chncontroller[chn][ctl]=v;
}


kdelibs'FMOut::sysex() (./kdelibs/libkmid/fmout.cc:349)

void FMOut::sysex(uchar *, ulong )
{

}


kdelibs'FMOut::setFMPatchesDirectory() (./kdelibs/libkmid/fmout.cc:354)

void FMOut::setFMPatchesDirectory(const char *dir)
{
  if ((dir==NULL)||(dir[0]==0)) return;
  if (deleteFMPatchesDirectory) delete FMPatchesDirectory;
  char *FMPatchesDirectory2=new char[strlen(dir)+1];
  strcpy(FMPatchesDirectory2,dir);
  FMPatchesDirectory = FMPatchesDirectory2;
  deleteFMPatchesDirectory=1;
}


kdelibs'FMOut::setVolumePercentage() (./kdelibs/libkmid/fmout.cc:364)

void FMOut::setVolumePercentage    ( int i )
{
#ifdef HAVE_OSS_SUPPORT
  int fd=open("/dev/mixer0",O_RDWR,0);
  if (fd==-1) return;
  int a=i*255/100;
  if (a>255) a=255;
  a=(a<<8) | a;
  if (ioctl(fd,MIXER_WRITE(SOUND_MIXER_SYNTH),&a) == -1) 
    printfdebug("ERROR writing to mixer\n");
  close(fd);
#endif
  volumepercentage=i;
}