summaryrefslogtreecommitdiff
path: root/plugins/supereq/supereq.c
blob: 368bcda303b43aa11ab91b7ba1a36569187c1255 (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
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009-2010 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 <stdio.h>
#include <string.h>
#include "../../deadbeef.h"
#include "supereq.h"

static DB_functions_t *deadbeef;

void *paramlist_alloc (void);
void paramlist_free (void *);
void equ_makeTable(float *lbc,float *rbc,void *param,float fs);
int equ_modifySamples(char *buf,int nsamples,int nch,int bps);
void equ_clearbuf(int bps,int srate);
void equ_init(int wb);
void equ_quit(void);

static float last_srate = 0;
static int last_nch = 0, last_bps = 0;
static float lbands[18] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
static float rbands[18] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
static void *paramsroot;

static int params_changed = 0;
static intptr_t tid = 0;
static uintptr_t mutex = 0;

int
supereq_plugin_start (void) {
    // load bands from config
    for (int i = 0; i < 18; i++) {
        char key[100];
        snprintf (key, sizeof (key), "eq.band%d", i);
        lbands[i] = rbands[i] = deadbeef->conf_get_float (key, 1);
    }

    equ_init (14);
    paramsroot = paramlist_alloc ();
    last_srate = 44100;
    last_nch = 2;
    last_bps = 16;
    equ_makeTable (lbands, rbands, paramsroot, last_srate);
    equ_clearbuf (last_bps,last_srate);
    mutex = deadbeef->mutex_create ();
    return 0;
}

int
supereq_plugin_stop (void) {
    if (tid) {
        deadbeef->thread_join (tid);
        deadbeef->mutex_free (mutex);
        mutex = 0;
    }
    equ_quit ();
    paramlist_free (paramsroot);
    return 0;
}

void
supereq_regen_table_thread (void *param) {
    void *params = paramlist_alloc ();

    deadbeef->mutex_lock (mutex);
    float lbands_copy[18];
    float rbands_copy[18];
    float srate = last_srate;
    memcpy (lbands_copy, lbands, sizeof (lbands));
    memcpy (rbands_copy, rbands, sizeof (rbands));
    deadbeef->mutex_unlock (mutex);

    equ_makeTable (lbands_copy, rbands_copy, params, srate);

    deadbeef->mutex_lock (mutex);
    paramlist_free (paramsroot);
    paramsroot = params;
    deadbeef->mutex_unlock (mutex);
    tid = 0;
}

int
supereq_process_int16 (int16_t *samples, int nsamples, int nch, int bps, int srate) {
	if ((nch != 1 && nch != 2) || (bps != 8 && bps != 16 && bps != 24)) return nsamples;
    if (params_changed && !tid) {
        tid = deadbeef->thread_start (supereq_regen_table_thread, NULL);
        params_changed = 0;
    }
	if (last_srate != srate) {
        deadbeef->mutex_lock (mutex);
        equ_makeTable (lbands, rbands, paramsroot, srate);
		last_srate = srate;
		last_nch = nch;
		last_bps = bps;
        deadbeef->mutex_unlock (mutex);
		equ_clearbuf(bps,srate);
    }
	else if (last_nch != nch || last_bps != bps) {
        deadbeef->mutex_lock (mutex);
		last_nch = nch;
		last_bps = bps;
        deadbeef->mutex_unlock (mutex);
		equ_clearbuf(bps,srate);
    }
	equ_modifySamples((char *)samples,nsamples,nch,bps);
	return nsamples;
}

float
supereq_get_band (int band) {
    return lbands[band];
}

void
supereq_set_band (int band, float value) {
    deadbeef->mutex_lock (mutex);
    lbands[band] = rbands[band] = value;
    deadbeef->mutex_unlock (mutex);
    params_changed = 1;
    char key[100];
    snprintf (key, sizeof (key), "eq.band%d", band);
    deadbeef->conf_set_float (key, value);
}

void
supereq_reset (void) {
    deadbeef->mutex_lock (mutex);
    equ_clearbuf(last_bps,last_srate);
    deadbeef->mutex_unlock (mutex);
}

static DB_supereq_dsp_t plugin = {
    .dsp.plugin.api_vmajor = DB_API_VERSION_MAJOR,
    .dsp.plugin.api_vminor = DB_API_VERSION_MINOR,
    .dsp.plugin.type = DB_PLUGIN_DSP,
    .dsp.plugin.id = "supereq",
    .dsp.plugin.name = "SuperEQ",
    .dsp.plugin.descr = "equalizer plugin using SuperEQ library by Naoki Shibata",
    .dsp.plugin.author = "Alexey Yakovenko",
    .dsp.plugin.email = "waker@users.sourceforge.net",
    .dsp.plugin.website = "http://deadbeef.sf.net",
    .dsp.plugin.start = supereq_plugin_start,
    .dsp.plugin.stop = supereq_plugin_stop,
//  .dsp.plugin.configdialog = settings_dlg,
    .dsp.process_int16 = supereq_process_int16,
    .dsp.reset = supereq_reset,
    .get_band = supereq_get_band,
    .set_band = supereq_set_band,
};

DB_plugin_t *
supereq_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}