summaryrefslogtreecommitdiff
path: root/plugins/mono2stereo/mono2stereo.c
blob: 18bcc074ec0bba3f00b6cb7e7d84d840e1581110 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009-2013 Alexey Yakovenko <waker@users.sourceforge.net>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "../../deadbeef.h"

enum {
    M2S_PARAM_LEFTMIX,
    M2S_PARAM_RIGHTMIX,
    M2S_PARAM_COUNT
};

static DB_functions_t *deadbeef;
static DB_dsp_t plugin;

typedef struct {
    ddb_dsp_context_t ctx;
    float leftmix;
    float rightmix;
} ddb_m2s_t;

ddb_dsp_context_t*
m2s_open (void) {
    ddb_m2s_t *m2s = malloc (sizeof (ddb_m2s_t));
    DDB_INIT_DSP_CONTEXT (m2s,ddb_m2s_t,&plugin);

    // initialize
    m2s->leftmix = 1;
    m2s->rightmix = 1;

    return (ddb_dsp_context_t *)m2s;
}

void
m2s_close (ddb_dsp_context_t *ctx) {
    ddb_m2s_t *m2s = (ddb_m2s_t *)ctx;

    // free instance-specific allocations

    free (m2s);
}

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

int
m2s_process (ddb_dsp_context_t *ctx, float *samples, int nframes, int maxframes, ddb_waveformat_t *fmt, float *r) {
    if (fmt->channels >= 2) {
        return nframes;
    }
    ddb_m2s_t *m2s = (ddb_m2s_t *)ctx;

    for (int i = nframes-1; i >= 0; i--) {
        samples[i*2+1] = samples[i] * m2s->rightmix;
        samples[i*2+0] = samples[i] * m2s->leftmix;
    }
    fmt->channels = 2;
    fmt->channelmask = 3;
    return nframes;
}

const char *
m2s_get_param_name (int p) {
    switch (p) {
    case M2S_PARAM_LEFTMIX:
        return "Left mix";
    case M2S_PARAM_RIGHTMIX:
        return "Right mix";
    default:
        fprintf (stderr, "m2s_param_name: invalid param index (%d)\n", p);
    }
    return NULL;
}

int
m2s_num_params (void) {
    return M2S_PARAM_COUNT;
}

void
m2s_set_param (ddb_dsp_context_t *ctx, int p, const char *val) {
    ddb_m2s_t *m2s = (ddb_m2s_t *)ctx;
    switch (p) {
    case M2S_PARAM_LEFTMIX:
        m2s->leftmix = atof (val);
        break;
    case M2S_PARAM_RIGHTMIX:
        m2s->rightmix = atof (val);
        break;
    default:
        fprintf (stderr, "m2s_set_param: invalid param index (%d)\n", p);
    }
}

void
m2s_get_param (ddb_dsp_context_t *ctx, int p, char *val, int sz) {
    ddb_m2s_t *m2s = (ddb_m2s_t *)ctx;
    switch (p) {
    case M2S_PARAM_LEFTMIX:
        snprintf (val, sz, "%f", m2s->leftmix);
        break;
    case M2S_PARAM_RIGHTMIX:
        snprintf (val, sz, "%f", m2s->rightmix);
        break;
    default:
        fprintf (stderr, "m2s_get_param: invalid param index (%d)\n", p);
    }
}

static const char settings_dlg[] =
    "property \"Left mix:\" hscale[0,1,0.001] 0 1;\n"
    "property \"Right mix:\" hscale[0,1,0.001] 1 1;\n"
;

static DB_dsp_t plugin = {
    .plugin.api_vmajor = 1,
    .plugin.api_vminor = 0,
    .open = m2s_open,
    .close = m2s_close,
    .process = m2s_process,
    .plugin.version_major = 1,
    .plugin.version_minor = 0,
    .plugin.type = DB_PLUGIN_DSP,
    .plugin.id = "m2s",
    .plugin.name = "Mono to stereo",
    .plugin.descr = "DSP plugin to convert mono to stereo",
    .plugin.copyright = 
        "Copyright (C) 2009-2013 Alexey Yakovenko <waker@users.sourceforge.net>\n"
        "\n"
        "This program is free software; you can redistribute it and/or\n"
        "modify it under the terms of the GNU General Public License\n"
        "as published by the Free Software Foundation; either version 2\n"
        "of the License, or (at your option) any later version.\n"
        "\n"
        "This program is distributed in the hope that it will be useful,\n"
        "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
        "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
        "GNU General Public License for more details.\n"
        "\n"
        "You should have received a copy of the GNU General Public License\n"
        "along with this program; if not, write to the Free Software\n"
        "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n"
    ,
    .plugin.website = "http://deadbeef.sf.net",
    .num_params = m2s_num_params,
    .get_param_name = m2s_get_param_name,
    .set_param = m2s_set_param,
    .get_param = m2s_get_param,
    .reset = m2s_reset,
    .configdialog = settings_dlg,
};

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