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

Class Index

kdelibs'MidiOut (./kdelibs/libkmid/midiout.h:51)

class MidiOut
{
  private:
    class MidiOutPrivate;
    MidiOutPrivate *d;

  protected:

  /**
   * @internal
   * This is the /dev/sequencer file handler. 
   * Remember _not_to_close_ it on MidiOut, but just on DeviceManager
   */
  int seqfd; 

  int device;
#ifdef HANDLETIMEINDEVICES
/**
 * @internal
 * Total number of devices.
 */
  int ndevs;

/**
 * @internal
 * Total number of midi ports
 */
  int nmidiports;

  double count;
  double lastcount;
  double lasttime;
  double begintime;

  int    rate;
/**
 * @internal
 * A "constant" used to convert from milliseconds to the computer rate
 */
  double convertrate; 
#endif

  int devicetype; 

  int volumepercentage;

  MidiMapper *map;

  uchar	chnpatch [16];
  int  	chnbender [16];
  uchar	chnpressure [16];
  uchar	chncontroller [16][256];
  int	chnmute [16];

  int	_ok;

  void seqbuf_dump (void);
  void seqbuf_clean(void);

  public:

  /**
   * Constructor. After constructing a MidiOut device, you must open it
   * (using @ref #openDev() ). Additionally you may want to initialize it
   * (with @ref #initDev() ),
   */
  MidiOut(int d=0);

  /**
   * Destructor. It doesn't matter if you close the device (@ref closeDev() )
   * before you destruct the object because in other case, it will be closed
   * here.
   */
  virtual ~MidiOut();

  /**
   * Opens the device. This is generally called from @ref DeviceManager , so you
   * shouldn't call this yourself (except if you created the MidiOut object
   * yourself.
   * @param sqfd a file descriptor of /dev/sequencer
   * @see #closeDev
   * @see #initDev
   */
  virtual void openDev	(int sqfd);

  /**
   * Closes the device. It basically tells the device (the file descriptor)
   * is going to be closed.
   * @see #openDev
   */
  virtual void closeDev	();

  /**
   * Initializes the device sending generic standard midi events and controllers,
   * such as changing the patches of each channel to an Acoustic Piano (000),
   * setting the volume to a normal value, etc.
   */
  virtual void initDev	();

  /**
   * @return the device type of the object. This is to identify the 
   * inherited class that a given object is polymorphed to.
   * The returned value is one of these :
   *
   * @li KMID_EXTERNAL_MIDI if it's a @ref MidiOut object
   * @li KMID_SYNTH if it's a @ref SynthOut object (as an AWE device)
   * @li KMID_FM if it's a @ref FMOut object
   * @li KMID_GUS if it's a @ref GUSOut object
   *
   * which are defined in midispec.h
   * 
   * @see #deviceName
   */
  int          deviceType () const { return devicetype; };

  /**
   * Returns the name and type of this MIDI device. 
   * @see #deviceType
   */
  const char * deviceName (void) const;

#ifdef HANDLETIMEINDEVICES
  /**
   * @internal
   */
  int  rate	(void) { return rate; };
#endif

  /**
   * Sets a @ref MidiMapper object to be used to modify the midi events before
   * sending them.
   *
   * @param map the MidiMapper to use.
   *
   * @see MidiMapper
   * @see #midiMapFilename
   */
  void setMidiMapper	( MidiMapper *map );

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

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

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

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

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

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

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

  /**
   * See @ref DeviceManager::sysex()
   */
  virtual void sysex		( uchar *data,ulong size);

  /**
   * Mutes all notes being played on a given channel.
   */
  virtual void channelSilence	( uchar chn );

  /**
   * Mute or "unmute" a given channel .
   * @param chn channel to work on
   * @param b if true, the device will ignore subsequent notes played on the chn
   * channel, and mute all notes being played on it. If b is false, the channel
   * is back to work.
   */
  virtual void channelMute	( uchar chn, int a );

  /**
   * Change all channel volume events multiplying it by this percentage correction
   * Instead of forcing a channel to a fixed volume, this method allows to 
   * music to fade out even when it was being played softly.
   * @param volper is an integer value, where 0 is quiet, 100 is used to send
   * an unmodified value, 200 play music twice louder than it should, etc.
   */ 
  virtual void setVolumePercentage ( int volper ) 
  { volumepercentage = volper; };

  /**
   * Returns true if everything's ok and false if there has been any problem
   */
  int ok (void) 
  { if (seqfd<0) return 0;
    return (_ok>0);
  };

#ifdef HANDLETIMEINDEVICES
  virtual void wait        (double ticks);
  virtual void tmrSetTempo (int v);
  virtual void tmrStart    (long int tpcn);
  virtual void tmrStop     ();
  virtual void tmrContinue ();
  /**
   * @internal
   * If i==1 syncronizes by cleaning the buffer instead of sending it (in fact,
   * this is what syncronizing really means :-) )
   */
  virtual void sync        (int i=0);  
#endif

  /**
   * Returns the path to the file where the current used @ref MidiMapper object
   * reads the configuration from, or an empty string if there's no MidiMapper.
   */
  char *midiMapFilename ();

};

kdelibs'MidiOut::MidiOut() (./kdelibs/libkmid/midiout.cc:47)

MidiOut::MidiOut(int d)
{
  seqfd = -1;
  devicetype=KMID_EXTERNAL_MIDI;
  device= d;
#ifdef HANDLETIMEINDEVICES
  count=0.0;
  lastcount=0.0;
  rate=100;
  convertrate=10;
#endif
  volumepercentage=100;
  map=new MidiMapper(NULL);
  if (map==NULL) { printfdebug("ERROR : midiOut : Map is NULL\n"); return; };
  _ok=1;
};


kdelibs'MidiOut::~MidiOut() (./kdelibs/libkmid/midiout.cc:64)

MidiOut::~MidiOut()
{
  delete map;
  closeDev();
}


kdelibs'MidiOut::openDev() (./kdelibs/libkmid/midiout.cc:70)

void MidiOut::openDev (int sqfd)
{
#ifdef HAVE_OSS_SUPPORT
  _ok=1;
  seqfd=sqfd;
  if (seqfd==-1)
  {
    printfdebug("ERROR: Could not open /dev/sequencer\n");
    _ok=0;
    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;

  midi_info midiinfo;
  midiinfo.device=device;
  convertrate=1000/rate;

#ifdef MIDIOUTDEBUG
  printfdebug("Number of synth devices : %d\n",ndevs);
  printfdebug("Number of midi ports : %d\n",nmidiports);
  printfdebug("Rate : %d\n",rate);

  int i;
  synth_info synthinfo;
  for (i=0;i<ndevs;i++)
  {
    synthinfo.device=i;
    if (ioctl(seqfd,SNDCTL_SYNTH_INFO,&synthinfo)!=-1)
    {
      printfdebug("----");
      printfdebug("Device : %d\n",i);
      printfdebug("Name : %s\n",synthinfo.name);
      switch (synthinfo.synth_type)
      {
	case (SYNTH_TYPE_FM) : printfdebug("FM\n");break;
	case (SYNTH_TYPE_SAMPLE) : printfdebug("Sample\n");break;
	case (SYNTH_TYPE_MIDI) : printfdebug("Midi\n");break;
	default : printfdebug("default type\n");break;
      }
      switch (synthinfo.synth_subtype)
      {
	case (FM_TYPE_ADLIB) : printfdebug("Adlib\n");break;
	case (FM_TYPE_OPL3) : printfdebug("Opl3\n");break;
	case (MIDI_TYPE_MPU401) : printfdebug("Mpu-401\n");break;
	case (SAMPLE_TYPE_GUS) : printfdebug("Gus\n");break;
	default : printfdebug("default subtype\n");break;
      }
    }
  }

  for (i=0;i<nmidiports;i++)
  {
    midiinfo.device=i;
    if (ioctl(seqfd,SNDCTL_MIDI_INFO,&midiinfo)!=-1)
    {
      printfdebug("----");
      printfdebug("Device : %d\n",i);
      printfdebug("Name : %s\n",midiinfo.name);
      printfdebug("Device type : %d\n",midiinfo.dev_type);
    }
  }

#endif

  count=0.0;
  lastcount=0.0;
  if (nmidiports<=0)
  {
    printfdebug("ERROR: There is no midi port !!\n");
    _ok=0;
    return;
  }
#endif
#endif

}


kdelibs'MidiOut::closeDev() (./kdelibs/libkmid/midiout.cc:153)

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


kdelibs'MidiOut::initDev() (./kdelibs/libkmid/midiout.cc:164)

void MidiOut::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,110*volumepercentage);
    chnController(chn, CTL_EXT_EFF_DEPTH, 0);
    chnController(chn, CTL_CHORUS_DEPTH, 0);
    chnController(chn, 0x4a, 127);
  }
#endif
}


kdelibs'MidiOut::setMidiMapper() (./kdelibs/libkmid/midiout.cc:189)

void MidiOut::setMidiMapper(MidiMapper *_map)
{
  delete map;
  map=_map;
}


kdelibs'MidiOut::noteOn() (./kdelibs/libkmid/midiout.cc:195)

void MidiOut::noteOn  (uchar chn, uchar note, uchar vel)
{
  if (vel==0)
  {
    noteOff(chn,note,vel);
  }
  else
  {
    SEQ_MIDIOUT(device, MIDI_NOTEON + map->channel(chn));
    SEQ_MIDIOUT(device, map->key(chn,chnpatch[chn],note));
    SEQ_MIDIOUT(device, vel);
  }
#ifdef MIDIOUTDEBUG
  printfdebug("Note ON >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
#endif
}


kdelibs'MidiOut::noteOff() (./kdelibs/libkmid/midiout.cc:212)

void MidiOut::noteOff (uchar chn, uchar note, uchar vel)
{
  SEQ_MIDIOUT(device, MIDI_NOTEOFF + map->channel(chn));
  SEQ_MIDIOUT(device, map->key(chn,chnpatch[chn],note));
  SEQ_MIDIOUT(device, vel);
#ifdef MIDIOUTDEBUG
  printfdebug("Note OFF >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
#endif
}


kdelibs'MidiOut::keyPressure() (./kdelibs/libkmid/midiout.cc:222)

void MidiOut::keyPressure (uchar chn, uchar note, uchar vel)
{
  SEQ_MIDIOUT(device, MIDI_KEY_PRESSURE + map->channel(chn));
  SEQ_MIDIOUT(device, map->key(chn,chnpatch[chn],note));
  SEQ_MIDIOUT(device, vel);
}


kdelibs'MidiOut::chnPatchChange() (./kdelibs/libkmid/midiout.cc:229)

void MidiOut::chnPatchChange (uchar chn, uchar patch)
{
#ifdef MIDIOUTDEBUG
  printfdebug("PATCHCHANGE [%d->%d] %d -> %d\n",
      chn,map->channel(chn),patch,map->patch(chn,patch));
#endif
  SEQ_MIDIOUT(device, MIDI_PGM_CHANGE + map->channel(chn));
  SEQ_MIDIOUT(device, map->patch(chn,patch));
  chnpatch[chn]=patch;
}


kdelibs'MidiOut::chnPressure() (./kdelibs/libkmid/midiout.cc:240)

void MidiOut::chnPressure (uchar chn, uchar vel)
{
  SEQ_MIDIOUT(device, MIDI_CHN_PRESSURE + map->channel(chn));
  SEQ_MIDIOUT(device, vel);

  chnpressure[chn]=vel;
}


kdelibs'MidiOut::chnPitchBender() (./kdelibs/libkmid/midiout.cc:248)

void MidiOut::chnPitchBender(uchar chn,uchar lsb, uchar msb)
{
  SEQ_MIDIOUT(device, MIDI_PITCH_BEND + map->channel(chn));
  /*
#ifdef AT_HOME
  short pbs=((short)msb<<7) | (lsb & 0x7F);
  pbs=pbs-0x2000;
  short pbs2=(((long)pbs*672)/4096);
  printfdebug("Pitch Bender (%d): %d -> %d \n",chn,pbs,pbs2);
  pbs2=pbs2+0x2000;
  lsb=pbs2 & 0x7F;
  msb=(pbs2 >> 7)&0x7F;
#endif
   */
  map->pitchBender(chn,lsb,msb);
  SEQ_MIDIOUT(device, lsb);
  SEQ_MIDIOUT(device, msb);
  chnbender[chn]=(msb << 8) | (lsb & 0xFF);
}


kdelibs'MidiOut::chnController() (./kdelibs/libkmid/midiout.cc:268)

void MidiOut::chnController (uchar chn, uchar ctl, uchar v) 
{
  SEQ_MIDIOUT(device, MIDI_CTL_CHANGE + map->channel(chn));
#ifdef AT_HOME
  if (ctl==11) ctl=7;
#endif
  map->controller(chn,ctl,v);
  if ((ctl==11)||(ctl==7))
  {
    v=(v*volumepercentage)/100;
    if (v>127) v=127;
  }

  SEQ_MIDIOUT(device, ctl);
  SEQ_MIDIOUT(device, v);

  chncontroller[chn][ctl]=v;
}


kdelibs'MidiOut::sysex() (./kdelibs/libkmid/midiout.cc:287)

void MidiOut::sysex(uchar *data, ulong size)
{
  ulong i=0;
  SEQ_MIDIOUT(device, MIDI_SYSTEM_PREFIX);
  while (i<size)
  {
    SEQ_MIDIOUT(device, *data);
    data++;
    i++;
  }
#ifdef MIDIOUTDEBUG
  printfdebug("sysex\n");
#endif
}


kdelibs'MidiOut::channelSilence() (./kdelibs/libkmid/midiout.cc:302)

void MidiOut::channelSilence (uchar chn)
{
  uchar i;
  for ( i=0; i<127; i++)
  {
    noteOff(chn,i,0);
  };
  SEQ_DUMPBUF();
}


kdelibs'MidiOut::channelMute() (./kdelibs/libkmid/midiout.cc:312)

void MidiOut::channelMute(uchar chn, int a)
{
  if (a==1)
  {
    chnmute[chn]=a;
    channelSilence(chn);
  }
  else if (a==0)
  {
    chnmute[chn]=a;
  }
  /*  else ignore the call to this function */
}


kdelibs'MidiOut::seqbuf_dump() (./kdelibs/libkmid/midiout.cc:326)

void MidiOut::seqbuf_dump (void)
{
#ifdef HAVE_OSS_SUPPORT
  if (_seqbufptr)
    if (write (seqfd, _seqbuf, _seqbufptr) == -1)
    {
      printfdebug("Error writing to /dev/sequencer in MidiOut::seq_buf_dump\n");
      perror ("write /dev/sequencer in seqBufDump\n");
      exit (-1);
    }
  _seqbufptr = 0;
#endif
}


kdelibs'MidiOut::seqbuf_clean() (./kdelibs/libkmid/midiout.cc:340)

void MidiOut::seqbuf_clean(void)
{
#ifdef HAVE_OSS_SUPPORT
  _seqbufptr=0;
#endif
}

kdelibs'MidiOut::wait() (./kdelibs/libkmid/midiout.cc:348)

void MidiOut::wait(double ticks)
{
  SEQ_WAIT_TIME(((int)(ticks/convertrate)));
#ifdef MIDIOUTDEBUG
  printfdebug("Wait  >\t ticks: %g\n",ticks);
#endif
}

kdelibs'MidiOut::tmrSetTempo() (./kdelibs/libkmid/midiout.cc:357)

void MidiOut::tmrSetTempo(int v)
#else
void MidiOut::tmrSetTempo(int)
#endif
{
#ifdef MIDIOUTDEBUG
  printfdebug("SETTEMPO  >\t tempo: %d\n",v);
#endif

  //SEQ_SET_TEMPO(v);
  //SEQ_DUMPBUF();
}


kdelibs'MidiOut::sync() (./kdelibs/libkmid/midiout.cc:370)

void MidiOut::sync(int i)
{
#ifdef MIDIOUTDEBUG
  printfdebug("Sync %d\n",i);
#endif
#ifdef HAVE_OSS_SUPPORT
  if (i==1) 
  {    
    seqbuf_clean();
    /* If you have any problem, try removing the next 2 lines, 
       I though they would be useful here, but I don't know
       what they exactly do :-) */
    ioctl(seqfd,SNDCTL_SEQ_RESET);
    ioctl(seqfd,SNDCTL_SEQ_PANIC);
  }
  ioctl(seqfd, SNDCTL_SEQ_SYNC);
#endif
}


kdelibs'MidiOut::tmrStart() (./kdelibs/libkmid/midiout.cc:389)

void MidiOut::tmrStart(void)
{
  SEQ_START_TIMER();
  SEQ_DUMPBUF();
}


kdelibs'MidiOut::tmrStop() (./kdelibs/libkmid/midiout.cc:395)

void MidiOut::tmrStop(void)
{
  SEQ_STOP_TIMER();
  SEQ_DUMPBUF();
}


kdelibs'MidiOut::tmrContinue() (./kdelibs/libkmid/midiout.cc:401)

void MidiOut::tmrContinue(void)
{
  SEQ_CONTINUE_TIMER();
  SEQ_DUMPBUF();
}

kdelibs'MidiOut::midiMapFilename() (./kdelibs/libkmid/midiout.cc:409)

char *MidiOut::midiMapFilename(void)
{
  return (map!=NULL) ? map->filename() : (char *)"";
}


kdelibs'MidiOut::deviceName() (./kdelibs/libkmid/midiout.cc:414)

const char * MidiOut::deviceName(void) const
{
  switch (deviceType())
  {
    case (KMID_EXTERNAL_MIDI) : return "External Midi";
    case (KMID_SYNTH) : return "Synth";
    case (KMID_FM) : return "FM";
    case (KMID_GUS) : return "GUS";
    case (KMID_AWE) : return "AWE";
  }
  return "Unknown";
}