📄 mp_dict.c
字号:
/*..........................................................................*//* *//* L a s t W a v e P a c k a g e 'mp' 2.1 *//* *//* Copyright (C) 2002 Remi Gribonval. *//* email : remi.gribonval@inria.fr *//* email : lastwave@cmap.polytechnique.fr *//* *//*..........................................................................*//* *//* This program is a free software, you can redistribute it and/or *//* modify it under the terms of the GNU General Public License as *//* published by the Free Software Foundation; either version 2 of the *//* License, or (at your option) any later version *//* *//* This program is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *//* GNU General Public License for more details. *//* *//* You should have received a copy of the GNU General Public License *//* along with this program (in a file named COPYRIGHT); *//* if not, write to the Free Software Foundation, Inc., *//* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* *//*..........................................................................*/#include "lastwave.h"#include "mp_book.h"/**********************************//* * DICT VARIABLES *//**********************************/char *dictType = "&dict";/* * Answers to the different print messages */void ShortPrintDict(DICT dict){ Printf("<&dict;%p>\n",dict);}char *ToStrDict(DICT dict, char flagShort){ static char str[30]; sprintf(str,"<&dict;%p>",dict); return(str);}void PrintInfoDict(DICT dict){ PrintDict(dict,NO);}DICT TNewDict(void){ DICT dict; dict = NewDict(); TempValue(dict); return(dict);}/* * Get the current dict * (generate an error if there is none) */DICT GetDictCur(void){ DICT dict; if(!ParseTypedValLevel_(levelCur,"objCur",NULL,(VALUE *)&dict,dictType)) Errorf1(""); if (dict == NULL) Errorf1(""); AddRefValue(dict); TempValue(dict); return(dict);}/*******************************************//* * Basic data management for SUBDICT */ /*******************************************/static void InitSubDict(SUBDICT subDict){ subDict->methods = NULL; subDict->flagMain = NO; subDict->channel = 0; subDict->flagUpToDate = NO; subDict->dataContainer = NULL; subDict->dict = NULL;}SUBDICT NewSubDict(){ SUBDICT subDict; #ifdef DEBUGALLOC DebugType = "SubDict";#endif subDict = (SUBDICT) Malloc(sizeof(struct subDict)); InitSubDict(subDict); return(subDict);}static void ClearSubDict(SUBDICT subDict){ if (subDict == NULL) Errorf("ClearSubDict : NULL subDict"); if(subDict->dataContainer) { DeleteValue(subDict->dataContainer); subDict->dataContainer = NULL; } InitSubDict(subDict);}static SUBDICT DeleteSubDict(SUBDICT subDict){ if (subDict == NULL) Errorf("DeleteSubDict : NULL subDict"); if(subDict->dataContainer) { DeleteValue(subDict->dataContainer); subDict->dataContainer = NULL; }#ifdef DEBUGALLOC DebugType = "SubDict";#endif Free(subDict); return(NULL);}/*******************************************//* * Basic data management for DICT */ /*******************************************/DICT NewDict(){ DICT dict; #ifdef DEBUGALLOC DebugType = "Dict";#endif dict = (DICT) Malloc(sizeof(struct dict)); InitValue(dict,&tsDict); InitTFContent(dict); dict->nChannels = 0; dict->nChannelsAlloc = 0; dict->channels = NULL; dict->signalEnergy = 0.0; dict->updateTimeIdMin = 1; dict->updateTimeIdMax = 0; dict->removedMolecule = NULL; dict->size = 0; dict->sizeAlloc = 0; dict->subDicts = NULL; return(dict);}DICT DeleteDict(DICT dict){ unsigned short i; if (dict == NULL) Errorf("DeleteDict : NULL dict"); if (dict->nRef==0) Errorf("*** Danger : trying to delete a temporary dict\n"); RemoveRefValue(dict); if (dict->nRef > 0) return(NULL); if(dict->channels) { for(i = 0; i < dict->nChannels; i++) { DeleteSignal(dict->channels[i]); dict->channels[i] = NULL; } Free(dict->channels); dict->channels = NULL; } if(dict->removedMolecule) dict->removedMolecule = DeleteMolecule(dict->removedMolecule); if(dict->subDicts) { for(i=0; i<= dict->size; i++) {if(dict->subDicts[i]) dict->subDicts[i] = DeleteSubDict(dict->subDicts[i]); } Free(dict->subDicts); dict->subDicts = NULL; } #ifdef DEBUGALLOC DebugType = "Dict";#endif Free(dict); return(NULL);}void ClearDict(DICT dict){ unsigned short i; if(dict == NULL) Errorf("ClearDict : NULL dict"); InitTFContent(dict); if(dict->channels) { for(i = 0; i < dict->nChannels; i++) { DeleteSignal(dict->channels[i]); dict->channels[i] = NULL; } dict->nChannels = 0; /* Note that we do NOT de-allocate the array of channels. */ dict->signalEnergy = 0.0; dict->updateTimeIdMin = 1; dict->updateTimeIdMin = 0; } if(dict->removedMolecule) dict->removedMolecule = DeleteMolecule(dict->removedMolecule); if(dict->subDicts) { for(i=0; i<= dict->size; i++) { if(dict->subDicts[i]) dict->subDicts[i] = DeleteSubDict(dict->subDicts[i]); } dict->size = 0; } /* Note that we do NOT delete the array of sub-dictionaries, we keep it for later use */}/* Prints the content of a dictionary, in long or short form */void PrintDict(const DICT dict,char flagShort){ unsigned short i; SUBDICT subDict; if(dict->channels == NULL) { Printf("Empty dict\n"); } else { Printf("Channels : %d\n",dict->nChannels); PrintInfoValue(GetChannel(dict,0)); Printf("Energy : %g\n",dict->signalEnergy); Printf("------------\n"); Printf("Update range : "); if(dict->updateTimeIdMin <= dict->updateTimeIdMax) Printf("[%d %d]\n",dict->updateTimeIdMin,dict->updateTimeIdMax); else Printf("empty\n"); if(dict->removedMolecule) { Printf("------------\n"); Printf("Removed &mol :\n"); PrintInfoValue(dict->removedMolecule); } for(i = 0; i < dict->size; i++) { subDict = dict->subDicts[i]; Printf("------------------\n"); Printf("Sub-dictionary[%d] : ",i); if(subDict->channel<dict->nChannels) Printf("channel %d\n",subDict->channel); else Printf("multichannel\n"); if(subDict->flagMain) Printf("main\n"); else Printf("auxiliary\n"); PrintInfoValue(subDict->dataContainer); } }}/* Function that generates an error if the dictionary does not contain any sub-dictionary */void CheckDictNotEmpty(const DICT dict){ if(dict == NULL) Errorf("CheckDictNotEmpty : NULL dict"); if(dict->size == 0) Errorf("CheckDictNotEmpty : empty dict");}/* * If 'sizeAlloc' is smaller than dict->size, an error is generated. * Else the allocation size of the array of sub-dictionaries is adjusted : * -the newly allocated part of the array is initialized to NULL sub-dictionaries; * -the previously allocated part is kept (dict->size is not changed) */void SizeDict(DICT dict,unsigned short sizeAlloc){ unsigned short i; if(sizeAlloc<dict->size) Errorf("SizeDict : cannot (re)allocate less than the number of sub-dictionaries"); if(sizeAlloc==dict->size) return; /* Case of an first allocation */ if(dict->sizeAlloc == 0) { dict->subDicts=(SUBDICT *)Calloc(sizeAlloc,sizeof(SUBDICT)); dict->sizeAlloc = sizeAlloc; } /* Case of a resize */ if(dict->size==dict->sizeAlloc) { dict->subDicts=(SUBDICT *)Realloc(dict->subDicts,sizeAlloc*sizeof(SUBDICT)); /* Initialize the newly allocated data, if necessary */ for(i = dict->sizeAlloc; i < sizeAlloc; i++) dict->subDicts[i]=NULL; dict->sizeAlloc = sizeAlloc; }}/* * Takes care of the reallocation if necessary * WARNING : should only be used with newly created subDicts! */SUBDICT PrivateAddSubDict(DICT dict,SUBDICT subDict) { if(subDict->dict!=NULL) Errorf("PrivateAddSubDict : (Weired) the sub-dictionary already belongs to a dictionary!"); /* In case we need to allocate more room */ if(dict->size==dict->sizeAlloc) { if(dict->sizeAlloc==0) SizeDict(dict,MP_DEFAULT_DICT_SIZE); else SizeDict(dict,2*dict->sizeAlloc); } subDict->dict = dict; dict->subDicts[dict->size]=subDict; dict->size++; return(subDict);}/*******************************************//* * Accessing DICT data */ /*******************************************//* If dict->channels is NULL, an error is generated */SIGNAL GetChannel(DICT dict,unsigned char channel) { if(dict->channels==NULL) Errorf("GetChannel : channels is NULL"); if(channel>=dict->nChannels) Errorf("GetChannel : bad channel number %d>%d",channel,dict->nChannels); return(dict->channels[channel]);} /* * If the subDictType is unknown, or the parameters are irrelevant for the type, an error is generated. * Else returns the first sub-dictionary that matches the type and parameters (NULL is none matches) */SUBDICT GetSubDict(DICT dict,unsigned char channel,char* subDictType,LISTV parameters){ char type; char windowShape; unsigned long windowSize; LISTV options; LWFLOAT f; /* Case of sub-dictionary of local maxima */ if(!strcmp(subDictType,maximaDictType)) { if(parameters!=NULL) Errorf("GetSubDict : irrelevant parameters for '%s' sub-dictionary",maximaDictType); if(channel!=dict->nChannels) Errorf("GetSubDict : '%s' sub-dictionary can only be multichannel",maximaDictType); return(GetMaximaDictSubDict(dict)); } /* Case of Stft sub-dictionary */ if(!strcmp(subDictType,stftType)) { if(parameters==NULL||!INRANGE(3,parameters->length,4)) Errorf("GetSubDict : bad parameters for '%s' sub-dictionary",stftType); type = Name2StftType(GetListvNthStr(parameters,0)); windowShape = Name2WindowShape(GetListvNthStr(parameters,1)); windowSize = (int) GetListvNthFloat(parameters,2); options= NULL; if(parameters->length==4) GetListvNth(parameters,3,(VALUE *)&options,&f); // TODO ?? return(GetStftSubDict(dict,channel,type,windowShape,windowSize,options)); return(GetStftSubDict(dict,channel,type,windowShape,windowSize,NULL)); } Errorf("GetSubDict : unknown sub-dictionary type '%s'",subDictType);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -