00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
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
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
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
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282