Skip to content

Commit 3d2d351

Browse files
committed
!* Equalizer: specify parameters by their name
1 parent ec9c9aa commit 3d2d351

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ phiola http://server/stream -tee "@artist - @title.mp3"
130130
phiola http://server/music.mp3 -dup @stdout.wav | phiola convert @stdin -aac_q 64 -o output.m4a
131131

132132
# Play audio with 2-band Equalizer (at 600Hz, 2.0 Q-factor width, -6 dB gain; at 10KHz, +3 dB gain)
133-
phiola file.mp3 -equalizer "600 2.0q -6.0 10000 2.0q 3.0"
133+
phiola file.mp3 -equalizer "f 600 w 2.0q g -6.0, f 10000 w 2.0q g 3.0"
134134
```
135135

136136
While audio is playing, you can control phiola via keyboard. The most commonly used commands are:

src/afilter/sox.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <sox/sox-phi.h>
66
#include <util/aformat.h>
77
#include <ffsys/globals.h>
8+
#include <ffbase/args.h>
89

910
static const phi_core *core;
1011
#define errlog(t, ...) phi_errlog(core, NULL, t, __VA_ARGS__)
@@ -39,24 +40,44 @@ static int request_input_conversion(phi_track *t)
3940
t->aconv.out = t->audio.format;
4041
t->aconv.out.format = PHI_PCM_32;
4142
t->aconv.out.interleaved = 1;
42-
if (!t->conf.oaudio.format.format)
43-
t->conf.oaudio.format.format = PHI_PCM_FLOAT64;
4443
t->oaudio.format = t->aconv.out;
4544
t->data_out = t->data_in;
4645
return PHI_BACK;
4746
}
4847

49-
static int argv_extract(ffstr *s, char **argv, uint n)
48+
struct sox_eq_conf {
49+
const char *frequency, *width, *gain;
50+
};
51+
52+
#define O(m) (void*)(ffsize)FF_OFF(struct sox_eq_conf, m)
53+
static const struct ffarg sox_eq_args[] = {
54+
{ "frequency", 's', O(frequency) },
55+
{ "gain", 's', O(gain) },
56+
{ "width", 's', O(width) },
57+
{}
58+
};
59+
#undef O
60+
61+
static int sox_argv_extract(struct sox *c, phi_track *t, ffstr *s, char **argv, uint n)
5062
{
51-
for (uint i = 0; i < n; i++) {
52-
ffstr a;
53-
ffstr_splitby(s, ' ', &a, s);
54-
if (!a.len)
55-
return -1;
56-
a.ptr[a.len] = '\0';
57-
argv[i] = a.ptr;
58-
}
63+
struct sox_eq_conf eqc = {};
64+
ffstr sc = {};
65+
ffstr_splitby(s, ',', &sc, s);
5966
ffstr_skipchar(s, ' ');
67+
sc.ptr[sc.len] = '\0';
68+
69+
struct ffargs a = {};
70+
if (ffargs_process_line(&a, sox_eq_args, &eqc, FFARGS_O_PARTIAL | FFARGS_O_DUPLICATES, sc.ptr)) {
71+
errlog(t, "%s", a.error);
72+
return -1;
73+
}
74+
75+
if (!eqc.frequency || !eqc.width || !eqc.gain)
76+
return -1;
77+
78+
argv[0] = (char*)eqc.frequency;
79+
argv[1] = (char*)eqc.width;
80+
argv[2] = (char*)eqc.gain;
6081
return 0;
6182
}
6283

@@ -94,7 +115,7 @@ static int sox_process(struct sox *c, phi_track *t)
94115
uint i = 1;
95116
while (s.len) {
96117
char *argv[3];
97-
if (argv_extract(&s, argv, 3)) {
118+
if (sox_argv_extract(c, t, &s, argv, 3)) {
98119
errlog(t, "Equalizer: incorrect parameters");
99120
t->error = PHI_E_FILTER_CONF;
100121
return PHI_ERR;
@@ -103,7 +124,7 @@ static int sox_process(struct sox *c, phi_track *t)
103124
errlog(t, "phi_sox_filter");
104125
return PHI_ERR;
105126
}
106-
dbglog(t, "added filter #%u 'equalizer'", i);
127+
dbglog(t, "added filter #%u 'equalizer': %s %s %s", i, argv[0], argv[1], argv[2]);
107128
i++;
108129
}
109130

src/exe/play.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ Options:\n\
4343
`attenuate` Integer\n\
4444
`gain` Integer\n\
4545
`-equalizer` \"OPTIONS\" SoX Equalizer (doesn't work with `-norm`). Options:\n\
46-
frequency width gain ...\n\
47-
Refer to the official SoX documentation for more info.\n\
46+
`frequency` Hz\n\
47+
`width`q Q-factor value plus 'q' character (larger = narrower)\n\
48+
`gain` dB\n\
49+
[, ...]\n\
50+
Add more parameters after comma for multi-band equalizer.\n\
51+
Refer to the official SoX documentation for more info.\n\
4852
\n\
4953
`-audio` STRING Audio library name (e.g. alsa)\n\
5054
`-device` NUMBER Playback device number\n\

src/jni/queue.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,27 @@ enum {
458458
QC_EQUALIZER = 1,
459459
};
460460

461+
/* "F W G ..." -> "f F w W g G, ..." */
462+
static char* eq_convert(const char *sz)
463+
{
464+
ffvec buf = {};
465+
ffstr s = FFSTR_INITZ(sz), f, w, g;
466+
ffstr_skipchar(&s, ' ');
467+
while (s.len) {
468+
ffstr_splitby(&s, ' ', &f, &s);
469+
ffstr_splitby(&s, ' ', &w, &s);
470+
ffstr_splitby(&s, ' ', &g, &s);
471+
ffstr_skipchar(&s, ' ');
472+
if (!f.len || !w.len || !g.len)
473+
break;
474+
if (buf.len)
475+
ffvec_addchar(&buf, ',');
476+
ffvec_addfmt(&buf, "f %S w %S g %S", &f, &w, &g);
477+
}
478+
ffvec_addchar(&buf, '\0');
479+
return buf.ptr;
480+
}
481+
461482
JNIEXPORT void JNICALL
462483
Java_com_github_stsaz_phiola_Phiola_quConfStr(JNIEnv *env, jobject thiz, int setting, jstring jval)
463484
{
@@ -467,7 +488,7 @@ Java_com_github_stsaz_phiola_Phiola_quConfStr(JNIEnv *env, jobject thiz, int set
467488
switch (setting) {
468489
case QC_EQUALIZER:
469490
ffmem_free(x->play.equalizer);
470-
x->play.equalizer = (*val) ? ffsz_dup(val) : NULL; break;
491+
x->play.equalizer = (*val) ? eq_convert(val) : NULL; break;
471492
}
472493

473494
// Apply settings for the active playlist

test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,15 @@ test_norm() {
546546
./phiola pl pl.wav -norm ""
547547
}
548548

549+
test_equalizer() {
550+
if ! test -f pl.wav ; then
551+
./phiola rec -rate 48000 -o pl.wav -f -u 2
552+
fi
553+
./phiola pl -equ " f 1000 w 1.0q" pl.wav || true # missing parameter
554+
./phiola pl -equ " f 1000 unknown 1.0q g -6.0" pl.wav || true # unknown parameter
555+
./phiola -D pl -equ " w 1.0q g -6.0 f 1000 , w 1.0q f 10000 g -6.0 " pl.wav | grep equalizer
556+
}
557+
549558
test_dir_read() {
550559
./phiola i -inc '*.wav' .
551560
./phiola i -inc '*.wav' -exc 'co*.wav' .
@@ -931,6 +940,7 @@ TESTS=(
931940
rename
932941
server
933942
# http
943+
equalizer
934944
clean
935945
# rec_play_alsa
936946
# wasapi_exclusive

0 commit comments

Comments
 (0)