aboutsummaryrefslogtreecommitdiff
path: root/src/opcodes.cpp
blob: abd51cdc95bbe6918065b0374c005937aa1635ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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);
}