diff options
author | Richard <q@1bpm.net> | 2025-09-13 16:37:04 +0100 |
---|---|---|
committer | Richard <q@1bpm.net> | 2025-09-13 16:37:04 +0100 |
commit | 1376945b3d351ceeb3ac38210f66627d91f413fd (patch) | |
tree | d20593c6287ea024ce36c854c5a9244f20c1d917 /src/opcodes.cpp | |
download | csound-cueextract-1376945b3d351ceeb3ac38210f66627d91f413fd.tar.gz csound-cueextract-1376945b3d351ceeb3ac38210f66627d91f413fd.tar.bz2 csound-cueextract-1376945b3d351ceeb3ac38210f66627d91f413fd.zip |
Diffstat (limited to 'src/opcodes.cpp')
-rw-r--r-- | src/opcodes.cpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/opcodes.cpp b/src/opcodes.cpp new file mode 100644 index 0000000..abd51cd --- /dev/null +++ b/src/opcodes.cpp @@ -0,0 +1,100 @@ +#include <plugin.h> +#include <sndfile.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +STRINGDAT* arrayInit(csnd::Csound* csound, ARRAYDAT* array, int rows) { + int totalResults = rows; + size_t totalAllocated; + + if (array->data == NULL) { + array->sizes = (int32_t*) csound->calloc(sizeof(int32_t)); + array->sizes[0] = rows; + array->dimensions = 1; + CS_VARIABLE *var = array->arrayType->createVariable(csound->get_csound(), NULL); + array->arrayMemberSize = var->memBlockSize; + totalAllocated = array->arrayMemberSize * totalResults; + array->data = (MYFLT*) csound->calloc(totalAllocated); + } else if ((totalAllocated = array->arrayMemberSize * totalResults) > array->allocated) { + array->data = (MYFLT*) csound->realloc(array->data, totalAllocated); + memset((char*)(array->data)+array->allocated, '\0', totalAllocated - array->allocated); + array->allocated = totalAllocated; + } + + // convenience return to be used if it is a string array + return (STRINGDAT*) array->data; +} + + +void insertArrayStringItem(csnd::Csound* csound, STRINGDAT* strings, int index, char* item) { + strings[index].size = strlen(item) + 1; + if (strings[index].data != NULL) { + csound->free(strings[index].data); + } + strings[index].data = csound->strdup(item); +} + +// SFC_GET_CUE doesn't give us the names, at least from audition +struct cueextract : csnd::Plugin<1, 1> { + static constexpr char const *otypes = "i[]"; + static constexpr char const *itypes = "S"; + + int init() { + STRINGDAT &file = inargs.str_data(0); + int result; + SNDFILE* infile = NULL; + SF_INFO sfinfo; + infile = sf_open(file.data, SFM_READ, &sfinfo); + if (!infile) { + return csound->init_error("cannot open file"); + } + + + typedef struct { + uint32_t cue_count; + SF_CUE_POINT cue_points[100]; + } SF_CUESX_T; + SF_CUESX_T cues; + + int nrCues; + result = sf_command(infile, SFC_GET_CUE, &cues, sizeof(cues)); + if (result != SF_TRUE) { + char* error = csound->strdup((char*) sf_strerror(infile)); + sf_close(infile); + return csound->init_error(error); + } else { + nrCues = cues.cue_count; + } + + + + if (nrCues > 0) { + ARRAYDAT* positions = (ARRAYDAT*) outargs(0); + arrayInit(csound, positions, nrCues); + + //ARRAYDAT* names = (ARRAYDAT*) outargs(1); + //STRINGDAT* strings = arrayInit(csound, names, nrCues); + + for (int index = 0; index < nrCues; index ++) { + positions->data[index] = cues.cue_points[index].position; + //insertArrayStringItem(csound, strings, index, cues.cue_points[index].name); + } + } else { + + } + + sf_close(infile); + return OK; + } + +}; + + +#include <modload.h> + +void csnd::on_load(csnd::Csound *csound) { + csnd::plugin<cueextract>(csound, "cueextract", csnd::thread::i); +} + |