libkmid Library API Documentation

voiceman.cc

00001 /**************************************************************************
00002 
00003     voiceman.cc - The VoiceManager class handles a set of voices for synths
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     Send comments and bug fixes to Antonio Larrosa <larrosa@kde.org>
00024 
00025 ***************************************************************************/
00026 
00027 #include "voiceman.h"
00028 #include <stdio.h>
00029 #ifdef HAVE_CONFIG_H
00030 #include <config.h>
00031 #endif
00032 
00033 VoiceManager::VoiceManager(int totalvoices)
00034 {
00035   nvoices=totalvoices;
00036 
00037   FirstVoice=new voice;
00038   FirstVoice->id=0;
00039   FirstVoice->channel=0;
00040   FirstVoice->note=0;
00041   FirstVoice->used=0;
00042   FirstVoice->prev=NULL;
00043 
00044   voice *ptrb=FirstVoice;
00045   voice *ptr=NULL;
00046   int i;
00047   for (i=1;i<nvoices;i++)
00048   {
00049     ptr=new voice;
00050     ptrb->next=ptr;
00051     ptr->id=i;
00052     ptr->channel=0;
00053     ptr->note=0;
00054     ptr->used=0;
00055     ptr->prev=ptrb;    
00056     ptrb=ptr;
00057   }
00058   LastVoice=ptr;
00059   LastVoice->next=NULL;
00060   LastnotusedVoice=LastVoice;
00061 
00062   VoiceList=new voice *[nvoices];
00063   ptr=FirstVoice;
00064   for (i=0;i<nvoices;i++)
00065   {
00066     VoiceList[i]=ptr;
00067     ptr=ptr->next; 
00068   }
00069   searcher_aid=new voice;
00070 }
00071 
00072 VoiceManager::~VoiceManager()
00073 {
00074   voice *ptr=FirstVoice;
00075   voice *ptr2;
00076   while (ptr!=NULL)
00077   {
00078     ptr2=ptr->next;
00079     delete ptr;
00080     ptr=ptr2;
00081   }
00082   FirstVoice=NULL;
00083   LastVoice=NULL;
00084   LastnotusedVoice=NULL;
00085 
00086   if (VoiceList!=NULL) 
00087   {
00088     delete VoiceList;
00089     VoiceList=NULL;
00090   }
00091 
00092   delete searcher_aid;
00093 }
00094 
00095 void VoiceManager::clearLists(void)
00096 {
00097 #ifdef VOICEMANDEBUG
00098   printf("voicemanager::cleanLists\n");
00099 #endif
00100   voice *ptr=FirstVoice;
00101   voice *ptr2=FirstVoice;
00102   while (ptr!=NULL)
00103   {
00104     ptr->used=0;
00105     ptr2=ptr;
00106     ptr=ptr->next;
00107   }
00108   LastVoice=ptr2;
00109   LastnotusedVoice=ptr2;
00110 
00111 }
00112 
00113 int VoiceManager::allocateVoice(int chn,int key)
00114 {
00115   // First, we take the allocated voice out of the first place of the list
00116   if ((LastnotusedVoice!=NULL)&&(LastnotusedVoice->id==FirstVoice->id))
00117   {
00118 #ifdef VOICEMANDEBUG
00119     printf("Used last voice !\n");
00120 #endif
00121     LastnotusedVoice=NULL;
00122   }
00123   voice *newvoice=FirstVoice;
00124   FirstVoice=FirstVoice->next;
00125   FirstVoice->prev=NULL;
00126 
00127 #ifdef VOICEMANDEBUG
00128   printf("Allocating id :%d\n",newvoice->id);
00129 #endif
00130   // then we put the allocated voice at the end of the list
00131   LastVoice->next=newvoice;
00132   newvoice->prev=LastVoice;
00133   LastVoice=newvoice;
00134   LastVoice->next=NULL;
00135 
00136   newvoice->channel=chn;
00137   newvoice->note=key;
00138 
00139 #ifdef VOICEMANDEBUG
00140   if (newvoice->used==1) 
00141   {
00142     printf("Replacing voice : %d\n",newvoice->id);
00143   }
00144 #endif
00145   newvoice->used=1;
00146 
00147   //dispStat();
00148   return newvoice->id;
00149 }
00150 
00151 void VoiceManager::deallocateVoice(int id)
00152 {
00153   voice *delvoice=VoiceList[id];
00154 #ifdef VOICEMANDEBUG
00155   printf("Deallocating id :%d\n",id);
00156 #endif
00157   if (delvoice->id==LastVoice->id)
00158   {
00159     LastVoice=delvoice->prev;
00160     LastVoice->next=NULL;
00161 
00162     if (LastnotusedVoice==NULL)
00163     {
00164       delvoice->next=FirstVoice;
00165       FirstVoice->prev=delvoice;
00166       FirstVoice=delvoice;
00167       FirstVoice->prev=NULL;
00168       LastnotusedVoice=FirstVoice;
00169     }
00170     else
00171     {
00172       if (LastnotusedVoice->next==NULL)
00173       {
00174     LastnotusedVoice->next=delvoice;
00175     delvoice->prev=LastnotusedVoice;
00176     delvoice->next=NULL;
00177     LastnotusedVoice=delvoice;
00178     LastVoice=delvoice;
00179       }
00180       else
00181       {
00182     delvoice->next=LastnotusedVoice->next;
00183     delvoice->next->prev=delvoice;
00184     delvoice->prev=LastnotusedVoice;
00185     LastnotusedVoice->next=delvoice;
00186     LastnotusedVoice=delvoice;
00187       }
00188     }
00189   }
00190   else
00191   {
00192     if (delvoice->prev!=NULL)
00193     {
00194       delvoice->prev->next=delvoice->next;
00195       delvoice->next->prev=delvoice->prev;
00196       if (LastnotusedVoice==NULL)
00197       {
00198     delvoice->next=FirstVoice;
00199     FirstVoice->prev=delvoice;
00200     FirstVoice=delvoice;
00201     FirstVoice->prev=NULL;
00202     LastnotusedVoice=FirstVoice;                                                    }
00203       else
00204       {
00205     if (LastnotusedVoice->next==NULL)
00206     {
00207       LastnotusedVoice->next=delvoice;
00208       delvoice->prev=LastnotusedVoice;
00209       delvoice->next=NULL;
00210       LastnotusedVoice=delvoice;
00211       LastVoice=delvoice;
00212     }
00213     else
00214     {
00215       delvoice->next=LastnotusedVoice->next;
00216       delvoice->next->prev=delvoice;
00217       delvoice->prev=LastnotusedVoice;
00218       LastnotusedVoice->next=delvoice;
00219       LastnotusedVoice=delvoice;
00220     }
00221       }
00222     }
00223   }
00224   delvoice->used=0;
00225 
00226   //  dispStat();
00227 }
00228 
00229 void VoiceManager::initSearch(void)
00230 {
00231   searcher=searcher_aid;
00232   searcher_aid->prev=LastVoice;
00233 }
00234 
00235 int VoiceManager::search(int chn)
00236 {
00237   if (searcher==NULL) return -1;
00238   searcher=searcher->prev;
00239 
00240   while (searcher!=NULL)
00241   {
00242     if (searcher->used==0) return -1;
00243     if (searcher->channel==chn) 
00244     {   
00245       return searcher->id;
00246     }
00247     searcher=searcher->prev;
00248   }
00249   return -1;
00250 }
00251 
00252 int VoiceManager::search(int chn,int note)
00253 {
00254   if (searcher==NULL) return -1;
00255   searcher=searcher->prev;
00256   while ((searcher!=NULL))
00257   {
00258     if (searcher->used==0) return -1;
00259     if ((searcher->channel==chn)&&(searcher->note==note)) 
00260     {
00261       return searcher->id;
00262     }
00263     searcher=searcher->prev;
00264   }
00265   return -1;
00266 }
00267 
00268 /*
00269 void VoiceManager::dispStat(void)
00270 {
00271 #ifdef VOICEMANDEBUG
00272   printf("Stats\n");
00273   voice *ptr=FirstVoice;
00274   while (ptr!=NULL)
00275   {
00276     printf("Voice %d is %s\n",ptr->id,(ptr->used==0)?("off"):("on"));
00277     ptr=ptr->next;
00278   }
00279   if (LastnotusedVoice!=NULL) printf("LnuV = %d\n",LastnotusedVoice->id);
00280 #endif
00281 }
00282 */
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:59 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001