1 /*
2     Mono to stereo converter DSP plugin for DeaDBeeF Player
3     Copyright (C) 2009-2014 Alexey Yakovenko
4 
5     This software is provided 'as-is', without any express or implied
6     warranty.  In no event will the authors be held liable for any damages
7     arising from the use of this software.
8 
9     Permission is granted to anyone to use this software for any purpose,
10     including commercial applications, and to alter it and redistribute it
11     freely, subject to the following restrictions:
12 
13     1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17 
18     2. Altered source versions must be plainly marked as such, and must not be
19      misrepresented as being the original software.
20 
21     3. This notice may not be removed or altered from any source distribution.
22 */
23 
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include "../../deadbeef.h"
28 
29 enum {
30     M2S_PARAM_LEFTMIX,
31     M2S_PARAM_RIGHTMIX,
32     M2S_PARAM_COUNT
33 };
34 
35 static DB_functions_t *deadbeef;
36 static DB_dsp_t plugin;
37 
38 typedef struct {
39     ddb_dsp_context_t ctx;
40     float leftmix;
41     float rightmix;
42 } ddb_m2s_t;
43 
44 ddb_dsp_context_t*
m2s_open(void)45 m2s_open (void) {
46     ddb_m2s_t *m2s = malloc (sizeof (ddb_m2s_t));
47     DDB_INIT_DSP_CONTEXT (m2s,ddb_m2s_t,&plugin);
48 
49     // initialize
50     m2s->leftmix = 1;
51     m2s->rightmix = 1;
52 
53     return (ddb_dsp_context_t *)m2s;
54 }
55 
56 void
m2s_close(ddb_dsp_context_t * ctx)57 m2s_close (ddb_dsp_context_t *ctx) {
58     ddb_m2s_t *m2s = (ddb_m2s_t *)ctx;
59 
60     // free instance-specific allocations
61 
62     free (m2s);
63 }
64 
65 void
m2s_reset(ddb_dsp_context_t * ctx)66 m2s_reset (ddb_dsp_context_t *ctx) {
67     // use this method to flush dsp buffers, reset filters, etc
68 }
69 
70 int
m2s_process(ddb_dsp_context_t * ctx,float * samples,int nframes,int maxframes,ddb_waveformat_t * fmt,float * r)71 m2s_process (ddb_dsp_context_t *ctx, float *samples, int nframes, int maxframes, ddb_waveformat_t *fmt, float *r) {
72     if (fmt->channels >= 2) {
73         return nframes;
74     }
75     ddb_m2s_t *m2s = (ddb_m2s_t *)ctx;
76 
77     for (int i = nframes-1; i >= 0; i--) {
78         samples[i*2+1] = samples[i] * m2s->rightmix;
79         samples[i*2+0] = samples[i] * m2s->leftmix;
80     }
81     fmt->channels = 2;
82     fmt->channelmask = 3;
83     return nframes;
84 }
85 
86 const char *
m2s_get_param_name(int p)87 m2s_get_param_name (int p) {
88     switch (p) {
89     case M2S_PARAM_LEFTMIX:
90         return "Left mix";
91     case M2S_PARAM_RIGHTMIX:
92         return "Right mix";
93     default:
94         fprintf (stderr, "m2s_param_name: invalid param index (%d)\n", p);
95     }
96     return NULL;
97 }
98 
99 int
m2s_num_params(void)100 m2s_num_params (void) {
101     return M2S_PARAM_COUNT;
102 }
103 
104 void
m2s_set_param(ddb_dsp_context_t * ctx,int p,const char * val)105 m2s_set_param (ddb_dsp_context_t *ctx, int p, const char *val) {
106     ddb_m2s_t *m2s = (ddb_m2s_t *)ctx;
107     switch (p) {
108     case M2S_PARAM_LEFTMIX:
109         m2s->leftmix = atof (val);
110         break;
111     case M2S_PARAM_RIGHTMIX:
112         m2s->rightmix = atof (val);
113         break;
114     default:
115         fprintf (stderr, "m2s_set_param: invalid param index (%d)\n", p);
116     }
117 }
118 
119 void
m2s_get_param(ddb_dsp_context_t * ctx,int p,char * val,int sz)120 m2s_get_param (ddb_dsp_context_t *ctx, int p, char *val, int sz) {
121     ddb_m2s_t *m2s = (ddb_m2s_t *)ctx;
122     switch (p) {
123     case M2S_PARAM_LEFTMIX:
124         snprintf (val, sz, "%f", m2s->leftmix);
125         break;
126     case M2S_PARAM_RIGHTMIX:
127         snprintf (val, sz, "%f", m2s->rightmix);
128         break;
129     default:
130         fprintf (stderr, "m2s_get_param: invalid param index (%d)\n", p);
131     }
132 }
133 
134 static const char settings_dlg[] =
135     "property \"Left mix:\" hscale[0,1,0.001] 0 1;\n"
136     "property \"Right mix:\" hscale[0,1,0.001] 1 1;\n"
137 ;
138 
139 static DB_dsp_t plugin = {
140     .plugin.api_vmajor = 1,
141     .plugin.api_vminor = 0,
142     .open = m2s_open,
143     .close = m2s_close,
144     .process = m2s_process,
145     .plugin.version_major = 1,
146     .plugin.version_minor = 0,
147     .plugin.type = DB_PLUGIN_DSP,
148     .plugin.id = "m2s",
149     .plugin.name = "Mono to stereo",
150     .plugin.descr = "Mono to stereo converter DSP",
151     .plugin.copyright =
152         "Mono to stereo converter DSP plugin for DeaDBeeF Player\n"
153         "Copyright (C) 2009-2014 Alexey Yakovenko\n"
154         "\n"
155         "This software is provided 'as-is', without any express or implied\n"
156         "warranty.  In no event will the authors be held liable for any damages\n"
157         "arising from the use of this software.\n"
158         "\n"
159         "Permission is granted to anyone to use this software for any purpose,\n"
160         "including commercial applications, and to alter it and redistribute it\n"
161         "freely, subject to the following restrictions:\n"
162         "\n"
163         "1. The origin of this software must not be misrepresented; you must not\n"
164         " claim that you wrote the original software. If you use this software\n"
165         " in a product, an acknowledgment in the product documentation would be\n"
166         " appreciated but is not required.\n"
167         "\n"
168         "2. Altered source versions must be plainly marked as such, and must not be\n"
169         " misrepresented as being the original software.\n"
170         "\n"
171         "3. This notice may not be removed or altered from any source distribution.\n"
172     ,
173     .plugin.website = "http://deadbeef.sf.net",
174     .num_params = m2s_num_params,
175     .get_param_name = m2s_get_param_name,
176     .set_param = m2s_set_param,
177     .get_param = m2s_get_param,
178     .reset = m2s_reset,
179     .configdialog = settings_dlg,
180 };
181 
182 DB_plugin_t *
ddb_mono2stereo_load(DB_functions_t * f)183 ddb_mono2stereo_load (DB_functions_t *f) {
184     deadbeef = f;
185     return &plugin.plugin;
186 }
187 
188