libkmid Library API Documentation

deviceman.cc

00001 /**************************************************************************
00002 
00003     deviceman.cc  - The device manager, that hides the use of midiOut
00004     This file is part of LibKMid 0.9.5
00005     Copyright (C) 1997,98,99,2000  Antonio Larrosa Jimenez
00006     LibKMid's homepage : http://www.arrakis.es/~rlarrosa/libkmid.html
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021     Boston, MA 02111-1307, USA.
00022 
00023     $Id: deviceman.cc,v 1.48 2002/10/05 10:29:15 garbanzo Exp $
00024 
00025     Send comments and bug fixes to Antonio Larrosa <larrosa@kde.org>
00026 
00027 ***************************************************************************/
00028 #include "deviceman.h"
00029 #include "midiout.h"
00030 #include <stdio.h>
00031 #include <fcntl.h>
00032 #include <unistd.h>
00033 #include <sys/ioctl.h>
00034 #include <errno.h>
00035 #include "sndcard.h"
00036 #include "synthout.h"
00037 #include "fmout.h"
00038 #include "gusout.h"
00039 #include "alsaout.h"
00040 #include "midimapper.h"
00041 #include "midispec.h"
00042 
00043 #ifdef HAVE_CONFIG_H
00044 #include <config.h>
00045 #endif
00046 
00047 #ifdef HAVE_SYS_STAT_H
00048 #include <sys/stat.h>
00049 #endif
00050 
00051 #ifdef HAVE_LIBASOUND2
00052 #   define HAVE_ALSA_SUPPORT
00053 #   include <sound/asound.h>
00054 #   include <sound/asequencer.h>
00055 #elif defined(HAVE_LIBASOUND)
00056 #   define HAVE_ALSA_SUPPORT
00057 #   include <linux/asequencer.h>
00058 #endif
00059 
00060 #ifdef HAVE_ALSA_ASOUNDLIB_H
00061 #   include <alsa/asoundlib.h>
00062 #elif defined(HAVE_SYS_ASOUNDLIB_H)
00063 #   include <sys/asoundlib.h>
00064 #endif
00065 
00066 #if 1
00067 #include <kinstance.h>
00068 #include <kglobal.h>
00069 #include <kconfig.h>
00070 #endif
00071 
00072 //#define DEVICEMANDEBUG
00073 //#define GENERAL_DEBUG_MESSAGES
00074 
00075 SEQ_DEFINEBUF (4096);
00076 
00077 #define CONTROLTIMER
00078 
00079 #ifdef GENERAL_DEBUG_MESSAGES
00080 void DEBUGPRINTF(const char *format)
00081 {
00082   printf(format);
00083 }
00084 
00085 void DEBUGPRINTF(const char *format,int i)
00086 {
00087   printf(format,i);
00088 }
00089 
00090 void DEBUGPRINTF(const char *format,const char *s)
00091 {
00092   printf(format,s);
00093 }
00094 
00095 #else
00096 
00097 void DEBUGPRINTF(const char *) { }
00098 void DEBUGPRINTF(const char *,int ) { }
00099 void DEBUGPRINTF(const char *,const char * ) { }
00100 
00101 #endif
00102 
00103 
00104 DeviceManager::DeviceManager(int def)
00105 {
00106 #if 1
00107   if (def==-1)
00108   {
00109     KInstance *tmp_instance=0L;
00110     if (!KGlobal::_instance) tmp_instance=new KInstance("nonKDEapp");
00111     KConfig *config = new KConfig("kcmmidirc", true);
00112 
00113     config->setGroup("Configuration");
00114     default_dev=config->readNumEntry("midiDevice",0);
00115     QString mapurl(config->readEntry("mapFilename",""));
00116     if ((config->readBoolEntry("useMidiMapper", false))&&(!mapurl.isEmpty()))
00117     {
00118       mapper_tmp = new MidiMapper( mapurl.mid(mapurl.find(":")+1 ).local8Bit() );
00119     }
00120     else
00121       mapper_tmp = 0L;
00122 
00123     delete config;
00124     delete tmp_instance;
00125   }
00126   else
00127 #endif
00128   {
00129     default_dev = def;
00130     mapper_tmp = 0L;
00131   }
00132 
00133   initialized=0;
00134   _ok=1;
00135   alsa=false;
00136   device = 0L;
00137   m_rate=0;
00138   convertrate=10;
00139   seqfd=-1;
00140   timerstarted=0;
00141   n_midi=0;
00142   n_synths=0;
00143   n_total=0;
00144   midiinfo=0L;
00145   synthinfo=0L;
00146   for (int i=0;i<16;i++) chn2dev[i]=default_dev;
00147 }
00148 
00149 DeviceManager::~DeviceManager(void)
00150 {
00151   closeDev();
00152   if (device)
00153   {
00154     for (int i=0;i<n_total;i++)
00155       delete device[i];
00156     delete[] device;
00157     device=0L;
00158   }
00159 #ifdef HAVE_OSS_SUPPORT
00160   delete[] midiinfo;
00161   delete[] synthinfo;
00162 #endif
00163 }
00164 
00165 int DeviceManager::ok(void)
00166 {
00167   int r=_ok;
00168   _ok=1;
00169   return r;
00170 }
00171 
00172 int DeviceManager::checkInit(void)
00173 {
00174   if (initialized==0)
00175   {
00176     int r=initManager();
00177     if (default_dev>=n_total) default_dev=0;
00178     DEBUGPRINTF("check : %d\n",r);
00179     return r;
00180   }
00181   return 0;
00182 }
00183 
00184 void DeviceManager::checkAlsa(void)
00185 {
00186 #ifdef HAVE_SYS_STAT_H
00187   struct stat buf;
00188   stat("/proc/asound", &buf);
00189   if ((stat("/proc/asound", &buf) == 0 ) && (S_ISDIR(buf.st_mode)))
00190     alsa=true;
00191   else
00192     alsa=false;
00193 #else
00194 #warning "ALSA won't be found at runtime"
00195   alsa=false;
00196 #endif
00197 }
00198 
00199 int DeviceManager::initManager(void)
00200 {
00201   checkAlsa();
00202 
00203   if (!alsa)  // We are using OSS
00204   {
00205 #ifdef HAVE_OSS_SUPPORT
00206     n_synths=0;
00207     n_midi=0;
00208     n_total=0;
00209 
00210     seqfd = open("/dev/sequencer", O_WRONLY | O_NONBLOCK, 0);
00211     if (seqfd==-1)
00212     {
00213       fprintf(stderr,"ERROR: Couldn't open /dev/sequencer to get some information\n");
00214       _ok=0;
00215       return -1;
00216     }
00217     ioctl(seqfd,SNDCTL_SEQ_NRSYNTHS,&n_synths);
00218     ioctl(seqfd,SNDCTL_SEQ_NRMIDIS,&n_midi);
00219     n_total=n_midi+n_synths;
00220 
00221 
00222     if (n_midi==0)
00223     {
00224       fprintf(stderr,"ERROR: There's no midi port\n");
00225       /* This could be a problem if the user don't have a synth neither,
00226      but not having any of both things is unusual */
00227       //    _ok=0;
00228       //    return 1;
00229     }
00230 
00231     device=new MidiOut*[n_total];
00232     midiinfo=new midi_info[n_midi];
00233     synthinfo=new synth_info[n_synths];
00234 
00235     int i;
00236     for (i=0;i<n_midi;i++)
00237     {
00238       midiinfo[i].device=i;
00239       if (ioctl(seqfd,SNDCTL_MIDI_INFO,&midiinfo[i])!=-1)
00240       {
00241 #ifdef GENERAL_DEBUG_MESSAGES
00242     printf("----\n");
00243     printf("Device : %d\n",i);
00244     printf("Name : %s\n",midiinfo[i].name);
00245     printf("Device type : %d\n",midiinfo[i].dev_type);
00246 #endif
00247       }
00248       device[i]=new MidiOut(i);
00249     }
00250 
00251     for (i=0;i<n_synths;i++)
00252     {
00253       synthinfo[i].device=i;
00254       if (ioctl(seqfd,SNDCTL_SYNTH_INFO,&synthinfo[i])!=-1)
00255       {
00256 #ifdef GENERAL_DEBUG_MESSAGES
00257     printf("----\n");
00258     printf("Device : %d\n",i);
00259     printf("Name : %s\n",synthinfo[i].name);
00260     switch (synthinfo[i].synth_type)
00261     {
00262       case (SYNTH_TYPE_FM) : printf("FM\n");break;
00263       case (SYNTH_TYPE_SAMPLE) : printf("Sample\n");break;
00264       case (SYNTH_TYPE_MIDI) : printf("Midi\n");break;
00265       default : printf("default type\n");break;
00266     };
00267     switch (synthinfo[i].synth_subtype)
00268     {
00269       case (FM_TYPE_ADLIB) : printf("Adlib\n");break;
00270       case (FM_TYPE_OPL3) : printf("Opl3\n");break;
00271       case (MIDI_TYPE_MPU401) : printf("Mpu-401\n");break;
00272       case (SAMPLE_TYPE_GUS) : printf("Gus\n");break;
00273       default : printf("default subtype\n");break;
00274     }
00275 #endif
00276     if (synthinfo[i].synth_type==SYNTH_TYPE_FM)
00277       device[i+n_midi]=new FMOut(i,synthinfo[i].nr_voices);
00278     else if ((synthinfo[i].synth_type==SYNTH_TYPE_SAMPLE)&&
00279         (synthinfo[i].synth_subtype==SAMPLE_TYPE_GUS))
00280       device[i+n_midi]=new GUSOut(i,synthinfo[i].nr_voices);
00281     else
00282       device[i+n_midi]=new SynthOut(i);
00283       }
00284     }
00285 
00286     close(seqfd);
00287 #else // There's no OSS support and ALSA wasn't detected
00288       // It must be one of those systems coolo is using :-)
00289 
00290     n_synths=0;
00291     n_midi=0;
00292     n_total=0;
00293     device=0L;
00294     midiinfo=0L;
00295     synthinfo=0L;
00296 
00297 #endif
00298 
00299   }
00300   else
00301   {  // We are using ALSA
00302 
00303 #ifdef HAVE_ALSA_SUPPORT
00304     int  client, port;
00305 #ifdef HAVE_LIBASOUND2
00306     snd_seq_t *handle=0;
00307     snd_seq_client_info_t *clienti;
00308     snd_seq_client_info_malloc(&clienti);
00309     snd_seq_port_info_t *porti;
00310     snd_seq_port_info_malloc(&porti);
00311 
00312     snd_seq_open(&handle, "hw", SND_SEQ_OPEN_DUPLEX, 0);
00313     if (!handle) { printf("handle==0\n"); return -1; };
00314     snd_seq_system_info_t *info;
00315     snd_seq_system_info_malloc(&info);
00316     if (!info) { printf("info==0\n"); return -1; };
00317     snd_seq_system_info(handle, info);
00318 
00319     n_total=0;
00320     n_midi=0;
00321     n_synths=0;
00322 
00323     device=new MidiOut*[snd_seq_system_info_get_clients(info)*snd_seq_system_info_get_ports(info)];
00324     unsigned int k=SND_SEQ_PORT_CAP_SUBS_WRITE | SND_SEQ_PORT_CAP_WRITE ;
00325     for (client=0 ; client<snd_seq_system_info_get_clients(info) ; client++)
00326     {
00327       snd_seq_get_any_client_info(handle, client, clienti);
00328       for (port=0 ; port<snd_seq_client_info_get_num_ports(clienti) ; port++)
00329       {
00330         snd_seq_get_any_port_info(handle, client, port, porti);
00331         if (( snd_seq_port_info_get_capability(porti) & k ) == k)
00332         {
00333           device[n_midi]=new AlsaOut(n_midi,client, port, snd_seq_client_info_get_name(clienti), snd_seq_port_info_get_name(porti));
00334           n_midi++;
00335         };
00336       }
00337     }
00338     snd_seq_client_info_free(clienti);
00339     snd_seq_port_info_free(porti);
00340     snd_seq_system_info_free(info);
00341 #else
00342     snd_seq_t *handle=0;
00343     snd_seq_client_info_t clienti;
00344     snd_seq_port_info_t porti;
00345 
00346     snd_seq_open(&handle, SND_SEQ_OPEN);
00347     if (!handle) { printf("handle(2)==0\n"); return -1; };
00348 
00349     snd_seq_system_info_t info;
00350     info.clients=info.ports=0;
00351     snd_seq_system_info(handle, &info);
00352 
00353     n_total=0;
00354     n_midi=0;
00355     n_synths=0;
00356 
00357     device=new MidiOut*[info.clients*info.ports];
00358     unsigned int k=SND_SEQ_PORT_CAP_SUBS_WRITE | SND_SEQ_PORT_CAP_WRITE ;
00359     for (client=0 ; client<info.clients ; client++)
00360     {
00361       snd_seq_get_any_client_info(handle, client, &clienti);
00362       for (port=0 ; port<clienti.num_ports ; port++)
00363       {
00364     snd_seq_get_any_port_info(handle, client, port, &porti);
00365     if (( porti.capability & k ) == k)
00366     {
00367       device[n_midi]=new AlsaOut(n_midi,client, port, clienti.name, porti.name);
00368       n_midi++;
00369     };
00370       }
00371     }
00372 #endif
00373     n_total=n_midi;
00374 
00375     snd_seq_close(handle);
00376 #else
00377 
00378     // Note: Please don't add i18n for the text below, thanks :)
00379 
00380     fprintf(stderr,"Sorry, this KMid version was compiled without \n");
00381     fprintf(stderr,"ALSA support but you're using ALSA . \n");
00382     fprintf(stderr,"Please compile KMid for yourself or tell the people\n");
00383     fprintf(stderr,"at your Linux distribution to compile it themselves\n");
00384 #endif
00385   }
00386 
00387   if (mapper_tmp!=0L) setMidiMap(mapper_tmp);
00388 
00389   initialized=1;
00390 
00391   return 0;
00392 }
00393 
00394 void DeviceManager::openDev(void)
00395 {
00396   if (checkInit()<0)
00397   {
00398     DEBUGPRINTF("DeviceManager::openDev : Not initialized\n");
00399     _ok = 0;
00400     return;
00401   }
00402   _ok=1;
00403 
00404   if (!alsa)
00405   {
00406 #ifdef HAVE_OSS_SUPPORT
00407     seqfd = open("/dev/sequencer", O_WRONLY | O_NONBLOCK, 0);
00408     if (seqfd==-1)
00409     {
00410       fprintf(stderr,"Couldn't open the MIDI sequencer device (/dev/sequencer)\n");
00411       _ok=0;
00412       return;
00413     }
00414     _seqbufptr = 0;
00415     ioctl(seqfd,SNDCTL_SEQ_RESET);
00416     //ioctl(seqfd,SNDCTL_SEQ_PANIC);
00417     m_rate=0;
00418     int r=ioctl(seqfd,SNDCTL_SEQ_CTRLRATE,&m_rate);
00419     if ((r==-1)||(m_rate<=0)) m_rate=HZ;
00420     
00421     convertrate=1000/m_rate;
00422 
00423 #endif
00424   }
00425   else seqfd=0L; // ALSA
00426 
00427 //  DEBUGPRINTF("Opening devices : ");
00428   for (int i=0;i<n_total;i++)
00429   {
00430     device[i]->openDev(seqfd);
00431 //    DEBUGPRINTF("%s ",device[i]->deviceName());
00432   }
00433 //  DEBUGPRINTF("\n");
00434   for (int i=0;i<n_total;i++) if (!device[i]->ok()) _ok=0;
00435   if (_ok==0)
00436   {
00437     for (int i=0;i<n_total;i++) device[i]->closeDev();
00438 //    DEBUGPRINTF("DeviceMan :: ERROR : Closing devices\n");
00439     return;
00440   }
00441 
00442 //  DEBUGPRINTF("Devices opened\n");
00443 }
00444 
00445 void DeviceManager::closeDev(void)
00446 {
00447   if (alsa)
00448   {
00449    if (device) 
00450      for (int i=0;i<n_total;i++) 
00451        if (device[i]) device[i]->closeDev();
00452 
00453    return;
00454   }
00455 
00456 #ifdef HAVE_OSS_SUPPORT
00457   if (seqfd==-1) return;
00458   tmrStop(); 
00459   if (device)
00460     for (int i=0;i<n_total;i++)
00461        if (device[i]) device[i]->closeDev();
00462   /*
00463      DEBUGPRINTF("Closing devices : ");
00464      if (device!=NULL) for (int i=0;i<n_total;i++)
00465      {
00466        device[i]->initDev();
00467        DEBUGPRINTF("%s ",device[i]->deviceName());
00468 
00469   //    device[i]->closeDev();
00470   };
00471   DEBUGPRINTF("\n");
00472    */
00473   close(seqfd);
00474   seqfd=-1;
00475 #endif
00476 }
00477 
00478 void DeviceManager::initDev(void)
00479 {
00480   if (device!=0L)
00481   {
00482 //    DEBUGPRINTF("Initializing devices :");
00483     for (int i=0;i<n_total;i++)
00484     {
00485       device[i]->initDev();
00486       DEBUGPRINTF("%s ",device[i]->deviceName());
00487     }
00488     DEBUGPRINTF("\n");
00489   }
00490 }
00491 
00492 void DeviceManager::noteOn         ( uchar chn, uchar note, uchar vel )
00493 {
00494   MidiOut *midi=chntodev(chn);
00495   if (midi) midi->noteOn(chn,note,vel);
00496 }
00497 void DeviceManager::noteOff        ( uchar chn, uchar note, uchar vel )
00498 {
00499   MidiOut *midi=chntodev(chn);
00500   if (midi) midi->noteOff(chn,note,vel);
00501 }
00502 void DeviceManager::keyPressure    ( uchar chn, uchar note, uchar vel )
00503 {
00504   MidiOut *midi=chntodev(chn);
00505   if (midi) midi->keyPressure(chn,note,vel);
00506 }
00507 void DeviceManager::chnPatchChange ( uchar chn, uchar patch )
00508 {
00509   MidiOut *midi=chntodev(chn);
00510   if (midi) midi->chnPatchChange(chn,patch);
00511 }
00512 void DeviceManager::chnPressure    ( uchar chn, uchar vel )
00513 {
00514   MidiOut *midi=chntodev(chn);
00515   if (midi) midi->chnPressure(chn,vel);
00516 }
00517 void DeviceManager::chnPitchBender ( uchar chn, uchar lsb,  uchar msb )
00518 {
00519   MidiOut *midi=chntodev(chn);
00520   if (midi) midi->chnPitchBender(chn,lsb,msb);
00521 }
00522 void DeviceManager::chnController  ( uchar chn, uchar ctl , uchar v )
00523 {
00524   MidiOut *midi=chntodev(chn);
00525   if (midi) midi->chnController(chn,ctl,v);
00526 }
00527 void DeviceManager::sysEx          ( uchar *data,ulong size)
00528 {
00529   for (int i=0;i<n_midi;i++)
00530     device[i]->sysex(data,size);
00531 }
00532 
00533 void DeviceManager::wait (double ticks)
00534 {
00535 #ifdef HAVE_ALSA_SUPPORT
00536   if (alsa) { ((AlsaOut *)device[default_dev])->wait(ticks); return; };
00537 #endif
00538 
00539 #ifdef HAVE_OSS_SUPPORT
00540   unsigned long int t=(unsigned long int)(ticks/convertrate);
00541   if (lastwaittime==t) return;
00542   lastwaittime=t;
00543   SEQ_WAIT_TIME(t);
00544   SEQ_DUMPBUF();     
00545 #endif
00546 }
00547 
00548 //void DeviceManager::tmrSetTempo(int v)
00549 void DeviceManager::tmrSetTempo(int v)
00550 {
00551 #ifdef HAVE_ALSA_SUPPORT
00552   if (alsa) { ((AlsaOut *)device[default_dev])->tmrSetTempo(v); return; }
00553 #endif
00554 
00555 #ifdef HAVE_OSS_SUPPORT
00556   SEQ_SET_TEMPO(v);
00557   SEQ_DUMPBUF();
00558 #endif
00559 }
00560 
00561 void DeviceManager::tmrStart(long int
00562 #ifdef HAVE_ALSA_SUPPORT
00563 tpcn /*name the argument only if it is used*/
00564 #endif
00565 )
00566 {
00567 #ifdef HAVE_ALSA_SUPPORT
00568   if (alsa) { ((AlsaOut *)device[default_dev])->tmrStart(tpcn); return; }
00569 #endif
00570 
00571 #ifdef HAVE_OSS_SUPPORT
00572 #ifdef CONTROLTIMER
00573   if (!timerstarted)
00574   {
00575       SEQ_START_TIMER();
00576       SEQ_DUMPBUF();
00577       timerstarted=1;
00578   }
00579   lastwaittime=0;
00580 #else
00581   SEQ_START_TIMER();
00582   SEQ_DUMPBUF();
00583 #endif          
00584 #endif
00585 }
00586 
00587 void DeviceManager::tmrStop(void)
00588 {
00589 #ifdef HAVE_ALSA_SUPPORT
00590   if (alsa) { ((AlsaOut *)device[default_dev])->tmrStop(); return; }
00591 #endif
00592 
00593 #ifdef HAVE_OSS_SUPPORT
00594 #ifdef CONTROLTIMER
00595   if (timerstarted)
00596   {
00597     SEQ_STOP_TIMER();
00598     SEQ_DUMPBUF();
00599     timerstarted=0;
00600   }
00601 #else
00602   SEQ_STOP_TIMER();
00603   SEQ_DUMPBUF();
00604 #endif          
00605 #endif
00606 }
00607 
00608 void DeviceManager::tmrContinue(void)
00609 {
00610 #ifdef HAVE_ALSA_SUPPORT
00611   if (alsa) { ((AlsaOut *)device[default_dev])->tmrContinue(); return; }
00612 #endif
00613 
00614 #ifdef HAVE_OSS_SUPPORT
00615 #ifdef CONTROLTIMER
00616   if (timerstarted)
00617   {
00618     SEQ_CONTINUE_TIMER();
00619     SEQ_DUMPBUF();
00620   }
00621 #else
00622   SEQ_CONTINUE_TIMER();
00623   SEQ_DUMPBUF();
00624 #endif        
00625 #endif
00626 }
00627 
00628 void DeviceManager::sync(bool f)
00629 {
00630 #ifdef HAVE_ALSA_SUPPORT
00631   if (alsa) { ((AlsaOut *)device[default_dev])->sync(f); return ; };
00632 #endif
00633 
00634 #ifdef HAVE_OSS_SUPPORT
00635 #ifdef DEVICEMANDEBUG
00636   printf("Sync %d\n",f);
00637 #endif
00638   if (f)
00639   {
00640     seqbuf_clean();
00641     /* If you have any problem, try removing the next 2 lines,
00642        I though they would be useful here but the may have side effects */
00643     ioctl(seqfd,SNDCTL_SEQ_RESET);
00644     ioctl(seqfd,SNDCTL_SEQ_PANIC);
00645   }
00646   else
00647   {
00648     seqbuf_dump();
00649     ioctl(seqfd, SNDCTL_SEQ_SYNC);
00650   };
00651 #endif
00652 }
00653 
00654 void DeviceManager::seqbuf_dump (void)
00655 {
00656   if (!alsa)
00657   {
00658 #ifdef HAVE_OSS_SUPPORT
00659     if (_seqbufptr)
00660     {
00661       int r=0;
00662       unsigned char *sb=_seqbuf;
00663       int w=_seqbufptr;
00664       r=write (seqfd, _seqbuf, _seqbufptr);
00665 #ifdef DEVICEMANDEBUG
00666       printf("%d == %d\n",r,w);
00667       printf("%d\n",(errno==EAGAIN)? 1 : 0);
00668 #endif
00669       while (((r == -1)&&(errno==EAGAIN))||(r != w))
00670       {
00671     if ((r==-1)&&(errno==EAGAIN))
00672     {
00673       usleep(1);
00674     }
00675     else if ((r>0)&&(r!=w))
00676     {
00677       w-=r;
00678       sb+=r;
00679     }
00680     r=write (seqfd, sb, w);
00681 #ifdef DEVICEMANDEBUG
00682     printf("%d == %d\n",r,w);
00683     printf("%d\n",(errno==EAGAIN)? 1 : 0);
00684 #endif
00685       }
00686     }
00687     /*
00688      *   if (_seqbufptr)
00689      *       if (write (seqfd, _seqbuf, _seqbufptr) == -1)
00690      *       {
00691      *           printf("Error writing to /dev/sequencer in deviceManager::seqbuf_dump\n");
00692      *           perror ("write /dev/sequencer in seqbuf_dump\n");
00693      *           exit (-1);
00694      *       }
00695      */
00696     _seqbufptr = 0;
00697 #endif
00698   }
00699 }
00700 
00701 void DeviceManager::seqbuf_clean(void)
00702 {
00703 #ifdef HAVE_ALSA_SUPPORT
00704   if (alsa)
00705     ((AlsaOut *)device[default_dev])->seqbuf_clean();
00706   else
00707 #endif
00708 #ifdef HAVE_OSS_SUPPORT
00709     _seqbufptr=0;
00710 #endif
00711 }
00712 
00713 
00714 const char *DeviceManager::name(int i)
00715 {
00716 #ifdef HAVE_OSS_SUPPORT
00717   if (checkInit()<0) {_ok = 0; return NULL;}
00718 
00719   if (alsa)
00720   {
00721     if (i<n_midi) return device[i]->deviceName(); 
00722   }
00723   else
00724   {
00725     if (i<n_midi) return midiinfo[i].name;
00726     if (i<n_midi+n_synths) return synthinfo[i-n_midi].name;
00727   };
00728 #endif
00729   return (char *)"";
00730 }
00731 
00732 const char *DeviceManager::type(int i)
00733 {
00734 #ifdef HAVE_OSS_SUPPORT
00735   if (checkInit()<0) {_ok = 0; return NULL;}
00736 
00737   if (alsa)
00738   {
00739     if (i<n_midi) return "ALSA device";
00740   }
00741   else
00742   {
00743     if (i<n_midi)
00744     {
00745       return "External Midi Port";
00746     }
00747     if (i<n_midi+n_synths)
00748     {
00749       switch (synthinfo[i-n_midi].synth_subtype)
00750       {
00751         case (FM_TYPE_ADLIB) : return "Adlib";break;
00752         case (FM_TYPE_OPL3) : return "FM";break;
00753         case (MIDI_TYPE_MPU401) : return "MPU 401";break;
00754         case (SAMPLE_TYPE_GUS) : return "GUS";break;
00755       }
00756     }
00757   }
00758 #endif
00759   return "";
00760 }
00761 
00762 int DeviceManager::defaultDevice(void)
00763 {
00764   return default_dev;
00765 }
00766 
00767 void DeviceManager::setDefaultDevice(int i)
00768 {
00769   if (i>=n_total) return;
00770   default_dev=i;
00771   for (int i=0;i<16;i++) chn2dev[i]=default_dev;
00772 }
00773 
00774 const char *DeviceManager::midiMapFilename(void)
00775 {
00776   if (device==0L) return "";
00777   if (default_dev>=n_total) return "";
00778   return (device[default_dev]!=NULL) ?
00779     device[default_dev]->midiMapFilename() : "";
00780 }
00781 
00782 void DeviceManager::setMidiMap(MidiMapper *map)
00783 {
00784   if (map==NULL) return;
00785   mapper_tmp=map;
00786   if (default_dev>=n_total) {default_dev=0;return;};
00787   if ((device==0L)||(device[default_dev]==NULL))
00788     return;
00789   device[default_dev]->setMidiMapper(map);
00790 }
00791 
00792 int DeviceManager::setPatchesToUse(int *patchesused)
00793 {
00794   if (checkInit()<0) return -1;
00795   if ((device==0L)||(device[default_dev]==NULL))
00796     return 0;
00797 
00798   if ((device[default_dev]->deviceType())==KMID_GUS)
00799   {
00800     GUSOut *gus=(GUSOut *)device[default_dev];
00801     gus->setPatchesToUse(patchesused);
00802   }
00803   return 0;
00804 }
00805 
00806 void DeviceManager::setVolumePercentage(int v)
00807 {
00808   if (device!=0L)
00809   {
00810     for (int i=0;i<n_total;i++)
00811     {
00812       device[i]->setVolumePercentage(v);
00813     }
00814   }
00815 }
00816 
00817 void DeviceManager::setDeviceNumberForChannel(int chn, int dev)
00818 {
00819   chn2dev[chn]=dev;
00820 }
00821 
00822 void DeviceManager::allNotesOff(void)
00823 {
00824   for (int i=0;i<n_midi;i++)
00825     device[i]->allNotesOff();
00826 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:59 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001