/** * Allocate nbytes bytes of memory that can be accessed later by calling * csoundQueryGlobalVariable() with the specified name; the space is * cleared to zero. * Returns CSOUND_SUCCESS on success, CSOUND_ERROR in case of invalid * parameters (zero nbytes, invalid or already used name), or * CSOUND_MEMORY if there is not enough memory. */ PUBLIC int csoundCreateGlobalVariable(CSOUND *csound, const char *name, size_t nbytes) { void* p; /* create new empty database if it does not exist yet */ if (UNLIKELY(csound->namedGlobals == NULL)) { csound->namedGlobals = cs_hash_table_create(csound); if (UNLIKELY(csound->namedGlobals == NULL)) return CSOUND_MEMORY; } /* check for valid parameters */ if (UNLIKELY(name == NULL)) return CSOUND_ERROR; if (UNLIKELY(name[0] == '\0')) return CSOUND_ERROR; if (UNLIKELY(nbytes < (size_t) 1 || nbytes >= (size_t) 0x7F000000L)) return CSOUND_ERROR; if (cs_hash_table_get(csound, csound->namedGlobals, (char*)name) != NULL) return CSOUND_ERROR; p = csound->Calloc(csound, nbytes); if (UNLIKELY(p == NULL)) return CSOUND_MEMORY; cs_hash_table_put(csound, csound->namedGlobals, (char*)name, p); return CSOUND_SUCCESS; }
OENTRY* csound_find_internal_oentry(CSOUND* csound, OENTRY* oentry) { CONS_CELL *items; char *shortName; OENTRY *ep, *retVal = NULL; if (oentry == NULL) { return NULL; } shortName = get_opcode_short_name(csound, oentry->opname); items = cs_hash_table_get(csound, csound->opcodes, shortName); while (items != NULL) { ep = items->value; if (oentry->iopadr == ep->iopadr && oentry->kopadr == ep->kopadr && oentry->aopadr == ep->aopadr && strcmp(oentry->opname, ep->opname) == 0 && strcmp(oentry->outypes, ep->outypes) == 0 && strcmp(oentry->intypes, ep->intypes) == 0) { retVal = ep; break; } items = items->next; } if (shortName != oentry->opname) { csound->Free(csound, shortName); } return retVal; }
ORCTOKEN *add_token(CSOUND *csound, char *s, int type) { //printf("Hash value for %s: %i\n", s, h); ORCTOKEN *a = cs_hash_table_get(csound, csound->symbtab, s); ORCTOKEN *ans; if (a!=NULL) { if (type == a->type) return a; if ((type!=T_FUNCTION || a->type!=T_OPCODE)) csound->Warning(csound, Str("Type confusion for %s (%d,%d), replacing\n"), s, type, a->type); a->type = type; return a; } ans = new_token(csound, T_IDENT); ans->lexeme = (char*)csound->Malloc(csound, 1+strlen(s)); strcpy(ans->lexeme, s); ans->type = type; cs_hash_table_put(csound, csound->symbtab, s, ans); return ans; }
/** * Free memory allocated for "name" and remove "name" from the database. * Return value is CSOUND_SUCCESS on success, or CSOUND_ERROR if the name is * not defined. */ PUBLIC int csoundDestroyGlobalVariable(CSOUND *csound, const char *name) { void *p = cs_hash_table_get(csound, csound->namedGlobals, (char*)name); if (UNLIKELY(p == NULL)) return CSOUND_ERROR; csound->Free(csound, p); cs_hash_table_remove(csound, csound->namedGlobals, (char*) name); return CSOUND_SUCCESS; }
/** * Get pointer to space allocated with the name "name". * Returns NULL if the specified name is not defined. */ PUBLIC void *csoundQueryGlobalVariable(CSOUND *csound, const char *name) { /* check if there is an actual database to search */ if (csound->namedGlobals == NULL) return NULL; /* check for a valid name */ if (UNLIKELY(name == NULL)) return NULL; if (UNLIKELY(name[0] == '\0')) return NULL; return cs_hash_table_get(csound, csound->namedGlobals, (char*) name); }
int32 named_instr_find(CSOUND *csound, char *s) { INSTRNAME *inm; if (!csound->engineState.instrumentNames) return 0L; /* no named instruments defined */ /* now find instrument */ inm = cs_hash_table_get(csound, csound->engineState.instrumentNames, s); return (inm == NULL) ? 0L : inm->instno; }
ORCTOKEN *lookup_token(CSOUND *csound, char *s, void *yyscanner) { int type = T_IDENT; ORCTOKEN *a; ORCTOKEN *ans; if (PARSER_DEBUG) csound->Message(csound, "Looking up token for: %s\n", s); if (udoflag == 0) { if (isUDOAnsList(s)) { ans = new_token(csound, UDO_ANS_TOKEN); ans->lexeme = (char*)csound->Malloc(csound, 1+strlen(s)); strcpy(ans->lexeme, s); return ans; } } if (udoflag == 1) { if (csound->oparms->odebug) printf("Found UDO Arg List\n"); if (isUDOArgList(s)) { ans = new_token(csound, UDO_ARGS_TOKEN); ans->lexeme = (char*)csound->Malloc(csound, 1+strlen(s)); strcpy(ans->lexeme, s); return ans; } } a = cs_hash_table_get(csound, csound->symbtab, s); if (a != NULL) { ans = (ORCTOKEN*)csound->Malloc(csound, sizeof(ORCTOKEN)); memcpy(ans, a, sizeof(ORCTOKEN)); ans->next = NULL; ans->lexeme = (char *)csound->Malloc(csound, strlen(a->lexeme) + 1); strcpy(ans->lexeme, a->lexeme); return ans; } ans = new_token(csound, T_IDENT); ans->lexeme = (char*)csound->Malloc(csound, 1+strlen(s)); strcpy(ans->lexeme, s); if (udoflag == -2 || namedInstrFlag == 1) { return ans; } ans->type = type; return ans; }
/** * This function is the same as csoundQueryGlobalVariable(), except the * variable is assumed to exist and no error checking is done. * Faster, but may crash or return an invalid pointer if 'name' is * not defined. */ PUBLIC void *csoundQueryGlobalVariableNoCheck(CSOUND *csound, const char *name) { return cs_hash_table_get(csound, csound->namedGlobals, (char*) name); }
SNDMEMFILE *csoundLoadSoundFile(CSOUND *csound, const char *fileName, void *sfi) { SF_INFO *sfinfo = sfi; SNDFILE *sf; void *fd; SNDMEMFILE *p = NULL; SF_INFO tmp; if (UNLIKELY(fileName == NULL || fileName[0] == '\0')) return NULL; /* check if file is already loaded */ if (csound->sndmemfiles != NULL) { p = cs_hash_table_get(csound, csound->sndmemfiles, (char*)fileName); } else { csound->sndmemfiles = cs_hash_table_create(csound); } if (p != NULL) { /* if file was loaded earlier: */ if (sfinfo != NULL) { memset(sfinfo, 0, sizeof(SF_INFO)); sfinfo->frames = (sf_count_t) p->nFrames; sfinfo->samplerate = ((int) p->sampleRate + 0.5); sfinfo->channels = p->nChannels; sfinfo->format = FORMAT2SF(p->sampleFormat) | TYPE2SF(p->fileType); } return p; } /* open file */ if (sfinfo == NULL) { memset(&tmp, 0, sizeof(SF_INFO)); sfinfo = &tmp; } fd = csound->FileOpen2(csound, &sf, CSFILE_SND_R, fileName, sfinfo, "SFDIR;SSDIR", CSFTYPE_UNKNOWN_AUDIO, 0); if (UNLIKELY(fd == NULL)) { csound->ErrorMsg(csound, Str("csoundLoadSoundFile(): failed to open '%s'"), fileName); return NULL; } p = (SNDMEMFILE*) csound->Malloc(csound, sizeof(SNDMEMFILE) + (size_t) sfinfo->frames * sizeof(float)); /* set parameters */ p->name = (char*) csound->Malloc(csound, strlen(fileName) + 1); strcpy(p->name, fileName); p->fullName = (char*) csound->Malloc(csound, strlen(csound->GetFileName(fd)) + 1); strcpy(p->fullName, csound->GetFileName(fd)); p->sampleRate = (double) sfinfo->samplerate; p->nFrames = (size_t) sfinfo->frames; p->nChannels = sfinfo->channels; p->sampleFormat = SF2FORMAT(sfinfo->format); p->fileType = SF2TYPE(sfinfo->format); /* set defaults for sampler information */ p->loopMode = 0; p->startOffs = 0.0; p->loopStart = 0.0; p->loopEnd = 0.0; p->baseFreq = 1.0; p->scaleFac = 1.0; { SF_INSTRUMENT lpd; if (sf_command(sf, SFC_GET_INSTRUMENT, &lpd, sizeof(SF_INSTRUMENT)) != 0) { if (lpd.loop_count > 0 && lpd.loops[0].mode != SF_LOOP_NONE) { /* set loop mode and loop points */ p->loopMode = (lpd.loops[0].mode == SF_LOOP_FORWARD ? 2 : (lpd.loops[0].mode == SF_LOOP_BACKWARD ? 3 : 4)); p->loopStart = (double) lpd.loops[0].start; p->loopEnd = (double) lpd.loops[0].end; } else { /* loop mode: off */ p->loopMode = 1; } p->baseFreq = pow(2.0, (double) (((int) lpd.basenote - 69) * 100 + (int) lpd.detune) / 1200.0) * 440.0; p->scaleFac = pow(10.0, (double) lpd.gain * 0.05); } } if ((size_t) sf_readf_float(sf, &(p->data[0]), (sf_count_t) p->nFrames) != p->nFrames) { csound->FileClose(csound, fd); csound->Free(csound, p->name); csound->Free(csound, p->fullName); csound->Free(csound, p); csound->ErrorMsg(csound, Str("csoundLoadSoundFile(): error reading '%s'"), fileName); return NULL; } p->data[p->nFrames] = 0.0f; csound->FileClose(csound, fd); csound->Message(csound, Str("File '%s' (sr = %d Hz, %d channel(s), %lu " "sample frames) loaded into memory\n"), p->fullName, (int) sfinfo->samplerate, (int) sfinfo->channels, (uint32) sfinfo->frames); /* link into database */ cs_hash_table_put(csound, csound->sndmemfiles, (char*)fileName, p); /* return with pointer to file structure */ return p; }