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; }
/** * 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; }
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; }