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

Class Index

kmid'MidiOut (./kdemultimedia/kmid/player/midiout.h:50)

class MidiOut
{
//friend class DeviceManager; 
protected:
// This is the /dev/sequencer file handler. 
// Remember NOT TO CLOSE it on MidiOut, but just on DeviceManager
    int                 seqfd; 

    int			device;
#ifdef HANDLETIMEINDEVICES
    int			ndevs; // Total number of devices
    int			nmidiports; // Total number of midi ports

    double		count;
    double		lastcount;
    double		lasttime;
    double		begintime;
    
    int			rate;
    double		convertrate; // A "constant" used to convert from
				// milliseconds to the computer rate
#endif
  int	devicetype; 

  int 		volumepercentage;

  MidiMapper	*map;

  uchar	chnpatch [N_CHANNELS];
  int  	chnbender [N_CHANNELS];
  uchar	chnpressure [N_CHANNELS];
  uchar	chncontroller [N_CHANNELS][N_CTL];
  int	chnmute [N_CHANNELS];

  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
 * 
 */
  int          deviceType () const { return devicetype; };

/**
 * @return the name and type of this MIDI device. 
 */
  const char * deviceName (void) const;

#ifdef HANDLETIMEINDEVICES
  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; };

/**
 * @return 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
  void wait        (double ticks);
  void tmrSetTempo (int v);
  void tmrStart    ();
  void tmrStop     ();
  void tmrContinue ();
  void sync        (int i=0);  // if i==1 syncronizes by cleaning the buffer
			// instead of sending it (in fact, this is what
			// syncronizing really means :-)
#endif

/**
 * @return 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 ();

};

kmid'MidiOut::MidiOut() (./kdemultimedia/kmid/player/midiout.cc:42)

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;
};


kmid'MidiOut::~MidiOut() (./kdemultimedia/kmid/player/midiout.cc:59)

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


kmid'MidiOut::openDev() (./kdemultimedia/kmid/player/midiout.cc:65)

void MidiOut::openDev (int sqfd)
{
  _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

}


kmid'MidiOut::closeDev() (./kdemultimedia/kmid/player/midiout.cc:146)

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


kmid'MidiOut::initDev() (./kdemultimedia/kmid/player/midiout.cc:157)

void MidiOut::initDev (void)
{
  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);
  }
}


kmid'MidiOut::setMidiMapper() (./kdemultimedia/kmid/player/midiout.cc:180)

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


kmid'MidiOut::noteOn() (./kdemultimedia/kmid/player/midiout.cc:186)

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
}


kmid'MidiOut::noteOff() (./kdemultimedia/kmid/player/midiout.cc:203)

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
}


kmid'MidiOut::keyPressure() (./kdemultimedia/kmid/player/midiout.cc:213)

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);
}


kmid'MidiOut::chnPatchChange() (./kdemultimedia/kmid/player/midiout.cc:220)

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;
}


kmid'MidiOut::chnPressure() (./kdemultimedia/kmid/player/midiout.cc:231)

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

  chnpressure[chn]=vel;
}


kmid'MidiOut::chnPitchBender() (./kdemultimedia/kmid/player/midiout.cc:239)

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);
}


kmid'MidiOut::chnController() (./kdemultimedia/kmid/player/midiout.cc:259)

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;
}


kmid'MidiOut::sysex() (./kdemultimedia/kmid/player/midiout.cc:278)

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
}


kmid'MidiOut::channelSilence() (./kdemultimedia/kmid/player/midiout.cc:293)

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


kmid'MidiOut::channelMute() (./kdemultimedia/kmid/player/midiout.cc:303)

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 procedure */
}


kmid'MidiOut::seqbuf_dump() (./kdemultimedia/kmid/player/midiout.cc:317)

void MidiOut::seqbuf_dump (void)
{
  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;
}


kmid'MidiOut::seqbuf_clean() (./kdemultimedia/kmid/player/midiout.cc:329)

void MidiOut::seqbuf_clean(void)
{
  _seqbufptr=0;
}

kmid'MidiOut::wait() (./kdemultimedia/kmid/player/midiout.cc:335)

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

kmid'MidiOut::tmrSetTempo() (./kdemultimedia/kmid/player/midiout.cc:344)

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();
}


kmid'MidiOut::sync() (./kdemultimedia/kmid/player/midiout.cc:357)

void MidiOut::sync(int i)
{
#ifdef MIDIOUTDEBUG
  printfdebug("Sync %d\n",i);
#endif
  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);
}


kmid'MidiOut::tmrStart() (./kdemultimedia/kmid/player/midiout.cc:374)

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


kmid'MidiOut::tmrStop() (./kdemultimedia/kmid/player/midiout.cc:380)

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


kmid'MidiOut::tmrContinue() (./kdemultimedia/kmid/player/midiout.cc:386)

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

kmid'MidiOut::midiMapFilename() (./kdemultimedia/kmid/player/midiout.cc:394)

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


kmid'MidiOut::deviceName() (./kdemultimedia/kmid/player/midiout.cc:399)

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";
}