| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <alsa/asoundlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "alsa.h" |
| 8 |
#include "biquad.h" |
| 9 |
#include "dsp-state.h" |
| 10 |
#include "optional-state.h" |
| 11 |
|
| 12 |
|
| 13 |
#define FREQ_SCALE 10 // Hz * 10 (1000.5 Hz = 10005) |
| 14 |
#define Q_SCALE 1000 // Q * 1000 (0.707 = 707) |
| 15 |
#define GAIN_SCALE 10 // dB * 10 (3.5 dB = 35) |
| 16 |
|
| 17 |
|
| 18 |
static char *get_filter_elem_name( |
| 19 |
const char *filter_type, |
| 20 |
int channel, |
| 21 |
int stage, |
| 22 |
const char *param |
| 23 |
) { |
| 24 |
return g_strdup_printf( |
| 25 |
"Line In %d %s Filter %d %s", |
| 26 |
channel, filter_type, stage, param |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
static struct alsa_elem *create_dsp_elem( |
| 32 |
struct alsa_card *card, |
| 33 |
const char *elem_name, |
| 34 |
long min_val, |
| 35 |
long max_val, |
| 36 |
long default_val, |
| 37 |
GHashTable *state |
| 38 |
) { |
| 39 |
|
| 40 |
struct alsa_elem *elem = get_elem_by_name(card->elems, elem_name); |
| 41 |
if (elem) |
| 42 |
return elem; |
| 43 |
|
| 44 |
|
| 45 |
elem = alsa_create_optional_elem( |
| 46 |
card, |
| 47 |
elem_name, |
| 48 |
SND_CTL_ELEM_TYPE_INTEGER, |
| 49 |
0 |
| 50 |
); |
| 51 |
|
| 52 |
if (!elem) { |
| 53 |
fprintf(stderr, "Failed to create DSP element %s\n", elem_name); |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
elem->min_val = min_val; |
| 59 |
elem->max_val = max_val; |
| 60 |
|
| 61 |
|
| 62 |
const char *value_str = g_hash_table_lookup(state, elem_name); |
| 63 |
long value = default_val; |
| 64 |
|
| 65 |
if (value_str && *value_str) { |
| 66 |
value = strtol(value_str, NULL, 10); |
| 67 |
if (value < min_val) |
| 68 |
value = min_val; |
| 69 |
if (value > max_val) |
| 70 |
value = max_val; |
| 71 |
} |
| 72 |
|
| 73 |
elem->value = value; |
| 74 |
|
| 75 |
return elem; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
static struct alsa_elem *create_dsp_enum_elem( |
| 80 |
struct alsa_card *card, |
| 81 |
const char *elem_name, |
| 82 |
const char **item_names, |
| 83 |
int item_count, |
| 84 |
int default_val, |
| 85 |
GHashTable *state |
| 86 |
) { |
| 87 |
|
| 88 |
struct alsa_elem *elem = get_elem_by_name(card->elems, elem_name); |
| 89 |
if (elem) |
| 90 |
return elem; |
| 91 |
|
| 92 |
|
| 93 |
elem = alsa_create_optional_enum_elem( |
| 94 |
card, |
| 95 |
elem_name, |
| 96 |
item_names, |
| 97 |
item_count |
| 98 |
); |
| 99 |
|
| 100 |
if (!elem) { |
| 101 |
fprintf(stderr, "Failed to create DSP element %s\n", elem_name); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
const char *value_str = g_hash_table_lookup(state, elem_name); |
| 107 |
int value = default_val; |
| 108 |
|
| 109 |
if (value_str && *value_str) { |
| 110 |
|
| 111 |
for (int i = 0; i < item_count; i++) { |
| 112 |
if (strcmp(item_names[i], value_str) == 0) { |
| 113 |
value = i; |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
elem->value = value; |
| 120 |
|
| 121 |
return elem; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
static void create_filter_stage_elems( |
| 126 |
struct alsa_card *card, |
| 127 |
const char *filter_type, |
| 128 |
int channel, |
| 129 |
int stage, |
| 130 |
BiquadFilterType default_type, |
| 131 |
GHashTable *state |
| 132 |
) { |
| 133 |
char *name; |
| 134 |
|
| 135 |
|
| 136 |
name = get_filter_elem_name(filter_type, channel, stage, "Enable"); |
| 137 |
create_dsp_elem(card, name, 0, 1, 1, state); |
| 138 |
g_free(name); |
| 139 |
|
| 140 |
|
| 141 |
name = get_filter_elem_name(filter_type, channel, stage, "Type"); |
| 142 |
create_dsp_enum_elem( |
| 143 |
card, name, |
| 144 |
biquad_get_type_names(), BIQUAD_TYPE_COUNT, |
| 145 |
default_type, state |
| 146 |
); |
| 147 |
g_free(name); |
| 148 |
|
| 149 |
|
| 150 |
name = get_filter_elem_name(filter_type, channel, stage, "Freq"); |
| 151 |
create_dsp_elem( |
| 152 |
card, name, |
| 153 |
20 * FREQ_SCALE, |
| 154 |
20000 * FREQ_SCALE, |
| 155 |
1000 * FREQ_SCALE, |
| 156 |
state |
| 157 |
); |
| 158 |
g_free(name); |
| 159 |
|
| 160 |
|
| 161 |
name = get_filter_elem_name(filter_type, channel, stage, "Q"); |
| 162 |
create_dsp_elem( |
| 163 |
card, name, |
| 164 |
(long)(0.1 * Q_SCALE), |
| 165 |
10 * Q_SCALE, |
| 166 |
(long)(0.707 * Q_SCALE), |
| 167 |
state |
| 168 |
); |
| 169 |
g_free(name); |
| 170 |
|
| 171 |
|
| 172 |
name = get_filter_elem_name(filter_type, channel, stage, "Gain"); |
| 173 |
create_dsp_elem( |
| 174 |
card, name, |
| 175 |
(long)(-GAIN_DB_LIMIT * GAIN_SCALE), |
| 176 |
(long)(GAIN_DB_LIMIT * GAIN_SCALE), |
| 177 |
0, |
| 178 |
state |
| 179 |
); |
| 180 |
g_free(name); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
void dsp_state_init(struct alsa_card *card) { |
| 185 |
if (!card->serial || !*card->serial) |
| 186 |
return; |
| 187 |
|
| 188 |
|
| 189 |
char *test_name = g_strdup_printf("Line In 1 DSP Capture Switch"); |
| 190 |
struct alsa_elem *test_elem = get_elem_by_name(card->elems, test_name); |
| 191 |
g_free(test_name); |
| 192 |
|
| 193 |
if (!test_elem) |
| 194 |
return; |
| 195 |
|
| 196 |
|
| 197 |
GHashTable *state = optional_state_load(card, CONFIG_SECTION_CONTROLS); |
| 198 |
if (!state) { |
| 199 |
state = g_hash_table_new_full( |
| 200 |
g_str_hash, g_str_equal, g_free, g_free |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
for (int channel = 1; channel <= 2; channel++) { |
| 206 |
|
| 207 |
|
| 208 |
char *dsp_name = g_strdup_printf("Line In %d DSP Capture Switch", channel); |
| 209 |
struct alsa_elem *dsp_elem = get_elem_by_name(card->elems, dsp_name); |
| 210 |
g_free(dsp_name); |
| 211 |
|
| 212 |
if (!dsp_elem) |
| 213 |
continue; |
| 214 |
|
| 215 |
|
| 216 |
for (int stage = 1; stage <= 2; stage++) { |
| 217 |
create_filter_stage_elems( |
| 218 |
card, "Pre-Comp", channel, stage, BIQUAD_TYPE_HIGHPASS, state |
| 219 |
); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
for (int stage = 1; stage <= 3; stage++) { |
| 224 |
create_filter_stage_elems( |
| 225 |
card, "PEQ", channel, stage, BIQUAD_TYPE_PEAKING, state |
| 226 |
); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
g_hash_table_destroy(state); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
static struct alsa_elem *get_filter_param_elem( |
| 235 |
struct alsa_card *card, |
| 236 |
const char *filter_type, |
| 237 |
int channel, |
| 238 |
int stage, |
| 239 |
const char *param |
| 240 |
) { |
| 241 |
char *name = get_filter_elem_name(filter_type, channel, stage, param); |
| 242 |
struct alsa_elem *elem = get_elem_by_name(card->elems, name); |
| 243 |
g_free(name); |
| 244 |
return elem; |
| 245 |
} |
| 246 |
|
| 247 |
struct alsa_elem *dsp_state_get_enable_elem( |
| 248 |
struct alsa_card *card, |
| 249 |
const char *filter_type, |
| 250 |
int channel, |
| 251 |
int stage |
| 252 |
) { |
| 253 |
return get_filter_param_elem(card, filter_type, channel, stage, "Enable"); |
| 254 |
} |
| 255 |
|
| 256 |
struct alsa_elem *dsp_state_get_type_elem( |
| 257 |
struct alsa_card *card, |
| 258 |
const char *filter_type, |
| 259 |
int channel, |
| 260 |
int stage |
| 261 |
) { |
| 262 |
return get_filter_param_elem(card, filter_type, channel, stage, "Type"); |
| 263 |
} |
| 264 |
|
| 265 |
struct alsa_elem *dsp_state_get_freq_elem( |
| 266 |
struct alsa_card *card, |
| 267 |
const char *filter_type, |
| 268 |
int channel, |
| 269 |
int stage |
| 270 |
) { |
| 271 |
return get_filter_param_elem(card, filter_type, channel, stage, "Freq"); |
| 272 |
} |
| 273 |
|
| 274 |
struct alsa_elem *dsp_state_get_q_elem( |
| 275 |
struct alsa_card *card, |
| 276 |
const char *filter_type, |
| 277 |
int channel, |
| 278 |
int stage |
| 279 |
) { |
| 280 |
return get_filter_param_elem(card, filter_type, channel, stage, "Q"); |
| 281 |
} |
| 282 |
|
| 283 |
struct alsa_elem *dsp_state_get_gain_elem( |
| 284 |
struct alsa_card *card, |
| 285 |
const char *filter_type, |
| 286 |
int channel, |
| 287 |
int stage |
| 288 |
) { |
| 289 |
return get_filter_param_elem(card, filter_type, channel, stage, "Gain"); |
| 290 |
} |