Browse Source

lib_synth_shed - Add **arg, struct field[3]

master
heck 4 years ago
parent
commit
e78e1109cd
  1. 10
      examples/lib/lib_synth_shed/main.c
  2. 122
      examples/lib/lib_synth_shed/synth_shed.c
  3. 18
      examples/lib/lib_synth_shed/synth_shed.h

10
examples/lib/lib_synth_shed/main.c

@ -11,15 +11,17 @@ int main() {
// synth_t* newsynth = NULL; // synth_t* newsynth = NULL;
// synth_init(newsynth); // synth_init(newsynth);
synth_t* my_synth = synth_create("Korg", "MS20", 2); synth_t *my_synth = NULL;
SYNTH_STATUS status = synth_create(&my_synth,"Korg", "MS20", 2);
if(status != SYNTH_STATUS_OK) {
printf("%s", status_to_string(status));
}
printf("brand %s\n", my_synth->brand); printf("brand %s\n", my_synth->brand);
printf("model %s\n", my_synth->model); printf("model %s\n", my_synth->model);
printf("%s", play_synth(my_synth)); printf("%s", play_synth(my_synth));
synth_set_osc_count(my_synth, 33); synth_set_osc_count(my_synth, 34);
printf("%s", play_synth(my_synth)); printf("%s", play_synth(my_synth));
return 0; return 0;
} }

122
examples/lib/lib_synth_shed/synth_shed.c

@ -8,22 +8,43 @@ void init_synth_shed() {
printf("init_synth_shed() - called\n"); printf("init_synth_shed() - called\n");
} }
synth_t* synth_create(const char* brand, const char* model, int osc_count) { SYNTH_STATUS synth_create(synth_t **new_synth, const char *brand, const char *model, int osc_count) {
printf("%s::%s brand:'%s, model: %s'\n",__FILE__, __FUNCTION__, brand, model); assert(brand);
synth_t* new_synth = (synth_t*) malloc(sizeof(synth_t)); assert(model);
assert(new_synth);
if (new_synth != NULL) { if (brand == NULL || brand[0] == '\0') {
new_synth->brand = brand; return SYNTH_STATUS_ARG_ERR;
new_synth->model = model; }
new_synth->osc_count = osc_count;
new_synth->technology = ANALOG; if (model == NULL || brand[0] == '\0') {
new_synth->filter.technology = ANALOG; return SYNTH_STATUS_ARG_ERR;
new_synth->filter.type = LPF;
} }
return new_synth;
printf("%s::%s brand:'%s, model: %s'\n", __FILE__, __FUNCTION__, brand, model);
synth_t *_new_synth = (synth_t *) malloc(sizeof(synth_t));
assert(_new_synth);
if (_new_synth == NULL) {
return SYNTH_STATUS_ERR;
}
_new_synth->brand = brand;
_new_synth->model = model;
_new_synth->osc_count = osc_count;
_new_synth->technology = ANALOG;
_new_synth->filter[0].technology = ANALOG;
_new_synth->filter[0].type = LPF;
_new_synth->filter[1].technology = ANALOG;
_new_synth->filter[1].type = HPF;
_new_synth->filter[2].technology = ANALOG;
_new_synth->filter[2].type = BPF;
*new_synth = _new_synth;
return SYNTH_STATUS_OK;
} }
SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count) { SYNTH_STATUS synth_set_osc_count(synth_t *synth, int osc_count) {
assert(synth); assert(synth);
SYNTH_STATUS status = 0; SYNTH_STATUS status = 0;
if (osc_count < 0) { if (osc_count < 0) {
@ -39,9 +60,23 @@ SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count) {
//SYNTH_STATUS synth_set_tech(synth_t* synth, tech_t tech); //SYNTH_STATUS synth_set_tech(synth_t* synth, tech_t tech);
const char* tech_to_string(const enum _tech* tech) { const char *status_to_string(SYNTH_STATUS status) {
const char *ret = "unknown status";
if (status == SYNTH_STATUS_OK) {
ret = "SYNTH_STATUS_OK";
}
if (status == SYNTH_STATUS_ERR) {
ret = "SYNTH_STATUS_ERR";
}
if (status == SYNTH_STATUS_ARG_ERR) {
ret = "SYNTH_STATUS_ARG_ERR";
}
return ret;
}
const char *tech_to_string(const enum _tech *tech) {
assert(tech); assert(tech);
const char* ret = "unknown tech"; const char *ret = "unknown tech";
if (*tech == ANALOG) { if (*tech == ANALOG) {
return "ANALOG"; return "ANALOG";
} }
@ -52,36 +87,65 @@ const char* tech_to_string(const enum _tech* tech) {
} }
const char* filtertype_to_string(const filtertype_t* const filt) { SYNTH_STATUS filtertype_to_string(const filtertype_t *const filt, char **ftstr) {
assert(filt); assert(filt);
const char* ret = "unknown filtertype"; char *_ftstr = "unknown filtertype";
if (filt == NULL) {
return SYNTH_STATUS_ARG_ERR;
}
if (*filt == LPF) { if (*filt == LPF) {
ret = "LPF"; _ftstr = "LPF";
} }
if (*filt == HPF) { if (*filt == HPF) {
ret = "HPF"; _ftstr = "HPF";
} }
if (*filt == BPF) { if (*filt == BPF) {
ret = "BPF"; _ftstr = "BPF";
} }
return ret;
*ftstr = _ftstr;
return SYNTH_STATUS_OK;
} }
const char* play_synth(synth_t* synth) { const char *play_synth(synth_t *synth) {
assert(synth); assert(synth);
char ret[9999]; char ret[9999];
const char* greet = "Playing synth:"; const char *greet = "PLAYING SYNTH:";
const char* brand = synth->brand; const char *brand = synth->brand;
const char* model = synth->model; const char *model = synth->model;
char osc[4]; char osc[4];
sprintf(osc, "%i", synth->osc_count); sprintf(osc, "%i", synth->osc_count);
const char* tech = tech_to_string(&synth->technology); const char *tech = tech_to_string(&synth->technology);
const char* filt_tech = tech_to_string(&synth->filter.technology);
const char* filt_type = filtertype_to_string(&synth->filter.type);
const char *filt_tech[3] = {"", "", ""};
const char *filtertype_str[3] = {"", "", ""};
for (int i = 0; i < 3; i++) {
filt_tech[i] = tech_to_string(&synth->filter[i].technology);
SYNTH_STATUS status = filtertype_to_string(&synth->filter[i].type, &filtertype_str[i]);
if (status != SYNTH_STATUS_OK) {
printf("%s", status_to_string(status));
}
}
sprintf(ret, "%s\nbrand: %s\nmodel: %s\nosc: %s\ntech:%s\nfilt:%s / %s\n\n", greet, brand,model, osc, tech, filt_tech, filt_type); sprintf(ret, "%s\n"
"brand: %s\n"
"model: %s\n"
"osc: %s\n"
"tech:%s\n"
"filt[0]:%s / %s\n"
"filt[1]:%s / %s\n"
"filt[2]:%s / %s\n"
"\n",
greet, brand, model, osc, tech,
filt_tech[0], filtertype_str[0],
filt_tech[1], filtertype_str[1],
filt_tech[2], filtertype_str[2]);
return strdup(ret); return strdup(ret);
} }

18
examples/lib/lib_synth_shed/synth_shed.h

@ -7,12 +7,16 @@ extern "C" {
// Types (as forward declarations) // Types (as forward declarations)
// != 0 means ERR
typedef int SYNTH_STATUS;
typedef struct _synth synth_t; typedef struct _synth synth_t;
typedef synth_t synth; typedef synth_t synth;
// Enums // Enums
typedef enum _SYNTH_STATUS {
SYNTH_STATUS_OK,
SYNTH_STATUS_ERR,
SYNTH_STATUS_ARG_ERR
} SYNTH_STATUS;
typedef enum _filtertype { typedef enum _filtertype {
HPF, HPF,
LPF, LPF,
@ -42,10 +46,7 @@ struct _synth {
// Functions // Functions
void init_synth_shed(); void init_synth_shed();
synth_t* synth_create(const char* brand, const char* model, int osc_count); SYNTH_STATUS synth_create(synth_t **new_synth, const char *brand, const char *model, int osc_count);
// This function covers the problem of a pointer to pointer arg
SYNTH_STATUS synth_create_pp(synth_t** new_synth);
SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count); SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count);
@ -53,10 +54,11 @@ SYNTH_STATUS synth_set_osc_count(synth_t* synth, int osc_count);
//SYNTH_STATUS synth_set_tech(synth_t* synth, tech_t tech); //SYNTH_STATUS synth_set_tech(synth_t* synth, tech_t tech);
const char* tech_to_string(const enum _tech* tech); const char* status_to_string(SYNTH_STATUS status);
const char* filtertype_to_string(const filtertype_t* filt); const char* tech_to_string(const enum _tech* tech);
SYNTH_STATUS filtertype_to_string(const filtertype_t* const filt, char** ftstr);
const char* play_synth(synth_t* synth); const char* play_synth(synth_t* synth);

Loading…
Cancel
Save