# libkmid Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# libkmid.cc

    00001 /**************************************************************************
    00002
    00003     libkmid.cc  - class KMidSimpleAPI that makes it easy to use libkmid
    00004                         and a C wrapper.
    00005     This file is part of LibKMid 0.9.5
    00006     Copyright (C) 2000  Antonio Larrosa Jimenez
    00007     LibKMid's homepage : http://www.arrakis.es/~rlarrosa/libkmid.html
    00008
    00009     This library is free software; you can redistribute it and/or
    00010     modify it under the terms of the GNU Library General Public
    00011     License as published by the Free Software Foundation; either
    00012     version 2 of the License, or (at your option) any later version.
    00013
    00014     This library is distributed in the hope that it will be useful,
    00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00017     Library General Public License for more details.
    00018
    00019     You should have received a copy of the GNU Library General Public License
    00020     along with this library; see the file COPYING.LIB.  If not, write to
    00021     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00022     Boston, MA 02111-1307, USA.
    00023
    00024     Send comments and bug fixes to Antonio Larrosa <larrosa@kde.org>
    00025
    00026 ***************************************************************************/
    00027
    00028 #include "libkmid.h"
    00029 #include <stdlib.h>
    00030 #include <unistd.h>
    00031 #include <signal.h>
    00032 #include <sys/types.h>
    00033 #include <sys/wait.h>
    00034 #include <sys/shm.h>
    00035
    00036 #include "deviceman.h"
    00037 #include "player.h"
    00038 #include "midimapper.h"
    00039
    00040 struct kMidData kMid;
    00041
    00042 int KMidSimpleAPI::kMidInit(void)
    00043 {
    00044   kMid.midi = new DeviceManager();
    00045   if ( kMid.midi == 0L ) return 1;
    00046   kMid.midi->initManager();
    00047   if (!kMid.midi->ok()) return 1;
    00048
    00049   kMid.pctlsmID=shmget(getpid(),sizeof(PlayerController),0600 | IPC_CREAT);
    00050   if (kMid.pctlsmID==-1) return 1;
    00051   kMid.pctl=(PlayerController *)shmat(kMid.pctlsmID,NULL,0);
    00052   if (kMid.pctl==NULL) return 1;
    00053
    00054   kMid.player=new MidiPlayer(kMid.midi,kMid.pctl);
    00055   if ( kMid.player == 0L )
    00056   {
    00057     delete kMid.midi;
    00058     return 1;
    00059   }
    00060
    00061   kMid.player->setParseSong(false);
    00062
    00063   kMid.pctl->message=0;
    00064   kMid.pctl->gm=1;
    00065   kMid.pctl->error=0;
    00066   kMid.pctl->ratioTempo=1.0;
    00067   kMid.pctl->tempo=500000;
    00068   kMid.pctl->volumepercentage=100;
    00069   for (int i=0;i<16;i++)
    00070   {
    00071     kMid.pctl->forcepgm[i]=0;
    00072     kMid.pctl->pgm[i]=0;
    00073   }
    00074
    00075   return 0;
    00076 }
    00077
    00078 int KMidSimpleAPI::kMidLoad(const char *filename)
    00079 {
    00080   if (kMidDevices()==0) return 0;
    00081   return kMid.player->loadSong(filename);
    00082 }
    00083
    00084 int KMidSimpleAPI::kMidPlay(int loop)
    00085 {
    00086   if (kMidDevices()==0) return 4;
    00087   if (!kMid.player->isSongLoaded()) return 1;
    00088   if (kMid.pctl->playing==1) return 2;
    00089   if (kMid.midi->checkInit()==-1) return 3;
    00090   kMid.pctl->message=0;
    00091   kMid.pctl->playing=0;
    00092   kMid.pctl->finished=0;
    00093   kMid.pctl->error=0;
    00094   kMid.pctl->SPEVplayed=0;
    00095   kMid.pctl->SPEVprocessed=0;
    00096   kMid.pctl->millisecsPlayed=0;
    00097   if ((kMid.pid=fork())==0)
    00098   {
    00099     if (loop)
    00100     {
    00101       while (1)
    00102       {
    00103         kMid.player->play();
    00104         if (kMid.pctl->error) return 5;
    00105         kMid.pctl->message=0;
    00106         kMid.pctl->playing=0;
    00107         kMid.pctl->finished=0;
    00108         kMid.pctl->error=0;
    00109         kMid.pctl->SPEVplayed=0;
    00110         kMid.pctl->SPEVprocessed=0;
    00111         kMid.pctl->millisecsPlayed=0;
    00112       }
    00113
    00114     } else {
    00115       kMid.player->play();
    00116       if (kMid.pctl->error) return 5;
    00117     }
    00118     _exit(0);
    00119   } else return 4;
    00120   return 0;
    00121 }
    00122
    00123 int KMidSimpleAPI::kMidStop(void)
    00124 {
    00125   if (kMidDevices()==0) return 4;
    00126   if (kMid.pctl->playing==0) return 1;
    00127   if (kMid.pid!=0)
    00128   {
    00129     kill(kMid.pid,SIGTERM);
    00130     waitpid(kMid.pid, NULL, 0);
    00131     kMid.pid=0;
    00132   } else return 2;
    00133
    00134   kMid.pctl->playing=0;
    00135   return 0;
    00136 }
    00137
    00138 void KMidSimpleAPI::kMidDestruct(void)
    00139 {
    00140   delete kMid.midi;
    00141   kMid.midi=0L;
    00142   delete kMid.player;
    00143   kMid.player=0L;
    00144   delete kMid.map;
    00145   shmdt((char *)kMid.pctl);
    00146   shmctl(kMid.pctlsmID, IPC_RMID, 0L);
    00147 }
    00148
    00149 int KMidSimpleAPI::kMidIsPlaying(void)
    00150 {
    00151    return kMid.pctl->playing;
    00152 }
    00153
    00154 int KMidSimpleAPI::kMidDevices(void)
    00155 {
    00156    return kMid.midi->midiPorts()+kMid.midi->synthDevices();
    00157 }
    00158
    00159 const char * KMidSimpleAPI::kMidName(int i)
    00160 {
    00161    return kMid.midi->name(i);
    00162 }
    00163
    00164 const char * KMidSimpleAPI::kMidType(int i)
    00165 {
    00166    return kMid.midi->type(i);
    00167 }
    00168
    00169 void KMidSimpleAPI::kMidSetDevice(int i)
    00170 {
    00171    kMid.midi->setDefaultDevice(i);
    00172 }
    00173
    00174 void KMidSimpleAPI::kMidSetMidiMapper(const char *mapfilename)
    00175 {
    00176   if (kMidDevices()==0) return;
    00177    kMid.map=new MidiMapper(mapfilename);
    00178    if ((kMid.map->ok() == 0L)||(!kMid.map->ok())) return;
    00179    kMid.midi->setMidiMap(kMid.map);
    00180 }
    00181
    00182 const char *KMidSimpleAPI::kMidVersion(void)
    00183 {
    00184    return "0.9.5";
    00185 }
    00186
    00187 const char *KMidSimpleAPI::kMidCopyright(void)
    00188 {
    00189    return "LibKMid 0.9.5 (C)1997-2000 Antonio Larrosa Jimenez <larrosa@kde.org>.Malaga(es)";
    00190 }
    00191
    00192 /* * * * * *
    00193
    00194  Under this line (------) there's only a C wrapper for the KMidSimpleAPI class
    00195
    00196 * * * * * */
    00197
    00198
    00199 int kMidInit(void)
    00200 {
    00201   return KMidSimpleAPI::kMidInit();
    00202 }
    00203
    00204 int kMidLoad(const char *filename)
    00205 {
    00206   return KMidSimpleAPI::kMidLoad(filename);
    00207 }
    00208
    00209 int kMidPlay(void)
    00210 {
    00211   return KMidSimpleAPI::kMidPlay();
    00212 }
    00213
    00214 int kMidStop(void)
    00215 {
    00216   return KMidSimpleAPI::kMidStop();
    00217 }
    00218
    00219 void kMidDestruct(void)
    00220 {
    00221   KMidSimpleAPI::kMidDestruct();
    00222 }
    00223
    00224 int kMidIsPlaying(void)
    00225 {
    00226   return KMidSimpleAPI::kMidIsPlaying();
    00227 }
    00228
    00229 int kMidDevices(void)
    00230 {
    00231   return KMidSimpleAPI::kMidDevices();
    00232 }
    00233
    00234 const char *kMidName(int i)
    00235 {
    00236   return KMidSimpleAPI::kMidName(i);
    00237 }
    00238
    00239 const char *kMidType(int i)
    00240 {
    00241   return KMidSimpleAPI::kMidType(i);
    00242 }
    00243
    00244 void kMidSetDevice(int i)
    00245 {
    00246   KMidSimpleAPI::kMidSetDevice(i);
    00247 }
    00248
    00249 void kMidSetMidiMapper(const char *mapfilename)
    00250 {
    00251   KMidSimpleAPI::kMidSetMidiMapper(mapfilename);
    00252 }
    00253
    00254 const char *kMidVersion(void)
    00255 {
    00256   return KMidSimpleAPI::kMidVersion();
    00257 }
    00258
    00259 const char *kMidCopyright(void)
    00260 {
    00261   return KMidSimpleAPI::kMidCopyright();
    00262 }
    00263

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:29 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001