| # libkmid Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

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

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:28:28 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001