summaryrefslogtreecommitdiff
path: root/examples/dsp_template.c
blob: 24b53759481599e99f90d1d70315a28a49b16cc0 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// this is a dsp plugin skeleton/example
// use to create new dsp plugins

// usage:
// 1. copy to your plugin folder
// 2. s/example/plugname/g
// 3. s/EXAMPLE/PLUGNAME/g

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <deadbeef/deadbeef.h>

enum {
    EXAMPLE_PARAM_LEVEL,
    EXAMPLE_PARAM_COUNT
};

static DB_functions_t *deadbeef;
static DB_dsp_t plugin;

typedef struct {
    ddb_dsp_context_t ctx;
    // instance-specific variables here
    float level; // this is example
} ddb_example_t;

ddb_dsp_context_t*
example_open (void) {
    ddb_example_t *example = malloc (sizeof (ddb_example_t));
    DDB_INIT_DSP_CONTEXT (example,ddb_example_t,&plugin);

    // initialize
    example->level = 0.5;

    return (ddb_dsp_context_t *)example;
}

void
example_close (ddb_dsp_context_t *ctx) {
    ddb_example_t *example = (ddb_example_t *)ctx;

    // free instance-specific allocations

    free (example);
}

void
example_reset (ddb_dsp_context_t *ctx) {
    // use this method to flush dsp buffers, reset filters, etc
}

int
example_process (ddb_dsp_context_t *ctx, float *samples, int nframes, ddb_waveformat_t *fmt) {
    ddb_example_t *example = (ddb_example_t *)ctx;
    for (int i = 0; i < nframes*fmt->channels; i++) {
        samples[i] *= example->level;
    }
    return nframes;
}

const char *
example_get_param_name (int p) {
    switch (p) {
    case EXAMPLE_PARAM_LEVEL:
        return "Volume level";
    default:
        fprintf (stderr, "example_param_name: invalid param index (%d)\n", p);
    }
    return NULL;
}

int
example_num_params (void) {
    return EXAMPLE_PARAM_COUNT;
}

void
example_set_param (ddb_dsp_context_t *ctx, int p, const char *val) {
    ddb_example_t *example = (ddb_example_t *)ctx;
    switch (p) {
    case EXAMPLE_PARAM_LEVEL:
        example->level = atof (val);
        break;
    default:
        fprintf (stderr, "example_param: invalid param index (%d)\n", p);
    }
}

void
example_get_param (ddb_dsp_context_t *ctx, int p, char *val, int sz) {
    ddb_example_t *example = (ddb_example_t *)ctx;
    switch (p) {
    case EXAMPLE_PARAM_LEVEL:
        snprintf (val, sz, "%f", example->level);
        break;
    default:
        fprintf (stderr, "example_get_param: invalid param index (%d)\n", p);
    }
}

static const char settings_dlg[] =
    "property \"Volume Level\" spinbtn[0,2,0.1] 0 0.5;\n"
;

static DB_dsp_t plugin = {
    .plugin.api_vmajor = DB_API_VERSION_MAJOR,
    .plugin.api_vminor = DB_API_VERSION_MINOR,
    .open = example_open,
    .close = example_close,
    .process = example_process,
    .plugin.version_major = 0,
    .plugin.version_minor = 1,
    .plugin.type = DB_PLUGIN_DSP,
    .plugin.id = "example",
    .plugin.name = "example",
    .plugin.descr = "example DSP Plugin",
    .plugin.author = "",
    .plugin.email = "",
    .plugin.website = "",
    .num_params = example_num_params,
    .get_param_name = example_get_param_name,
    .set_param = example_set_param,
    .get_param = example_get_param,
    .reset = example_reset,
    .configdialog = settings_dlg,
};

DB_plugin_t *
example_load (DB_functions_t *f) {
    deadbeef = f;
    return &plugin.plugin;
}