summaryrefslogtreecommitdiff
path: root/plugins/mono2stereo/mono2stereo.c
blob: 9a259fa6b8294e73deb0921ce69826e42e335bd6 (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
178
179
180
181
182
183
184
185
186
187
/*
    Mono to stereo converter DSP plugin for DeaDBeeF Player
    Copyright (C) 2009-2014 Alexey Yakovenko

    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.

    2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.
*/

#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 = "Mono to stereo converter DSP",
    .plugin.copyright = 
        "Mono to stereo converter DSP plugin for DeaDBeeF Player\n"
        "Copyright (C) 2009-2014 Alexey Yakovenko\n"
        "\n"
        "This software is provided 'as-is', without any express or implied\n"
        "warranty.  In no event will the authors be held liable for any damages\n"
        "arising from the use of this software.\n"
        "\n"
        "Permission is granted to anyone to use this software for any purpose,\n"
        "including commercial applications, and to alter it and redistribute it\n"
        "freely, subject to the following restrictions:\n"
        "\n"
        "1. The origin of this software must not be misrepresented; you must not\n"
        " claim that you wrote the original software. If you use this software\n"
        " in a product, an acknowledgment in the product documentation would be\n"
        " appreciated but is not required.\n"
        "\n"
        "2. Altered source versions must be plainly marked as such, and must not be\n"
        " misrepresented as being the original software.\n"
        "\n"
        "3. This notice may not be removed or altered from any source distribution.\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;
}