Skip to content

Commit 7cbddfe

Browse files
committed
effect: Add buffer_frames() and drain2() to effect struct.
1 parent 564adbc commit 7cbddfe

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

effect.c

+22-12
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,23 @@ int build_effects_chain(int argc, const char *const *argv, struct effects_chain
373373
return 0;
374374
}
375375

376+
static ssize_t effect_max_out_frames(struct effect *e, ssize_t in_frames)
377+
{
378+
if (e->buffer_frames != NULL)
379+
return e->buffer_frames(e, in_frames);
380+
if (e->ostream.fs != e->istream.fs) {
381+
const int gcd = find_gcd(e->ostream.fs, e->istream.fs);
382+
return ratio_mult_ceil(in_frames, e->ostream.fs / gcd, e->istream.fs / gcd);
383+
}
384+
return in_frames;
385+
}
386+
376387
ssize_t get_effects_chain_buffer_len(struct effects_chain *chain, ssize_t in_frames, int in_channels)
377388
{
378-
int gcd;
379389
ssize_t frames = in_frames, len, max_len = in_frames * in_channels;
380390
struct effect *e = chain->head;
381391
while (e != NULL) {
382-
if (e->ostream.fs != e->istream.fs) {
383-
gcd = find_gcd(e->ostream.fs, e->istream.fs);
384-
frames = ratio_mult_ceil(frames, e->ostream.fs / gcd, e->istream.fs / gcd);
385-
}
392+
frames = effect_max_out_frames(e, frames);
386393
len = frames * e->ostream.channels;
387394
if (len > max_len) max_len = len;
388395
e = e->next;
@@ -395,10 +402,7 @@ ssize_t get_effects_chain_max_out_frames(struct effects_chain *chain, ssize_t in
395402
ssize_t frames = in_frames;
396403
struct effect *e = chain->head;
397404
while (e != NULL) {
398-
if (e->ostream.fs != e->istream.fs) {
399-
const int gcd = find_gcd(e->ostream.fs, e->istream.fs);
400-
frames = ratio_mult_ceil(frames, e->ostream.fs / gcd, e->istream.fs / gcd);
401-
}
405+
frames = effect_max_out_frames(e, frames);
402406
e = e->next;
403407
}
404408
return frames;
@@ -556,15 +560,21 @@ void plot_effects_chain(struct effects_chain *chain, int input_fs, int input_cha
556560

557561
sample_t * drain_effects_chain(struct effects_chain *chain, ssize_t *frames, sample_t *buf1, sample_t *buf2)
558562
{
559-
int gcd;
560563
ssize_t ftmp = *frames, dframes = -1;
561564
struct effect *e = chain->head;
562565
while (e != NULL && dframes == -1) {
563566
dframes = ftmp;
564-
if (e->drain != NULL) e->drain(e, &dframes, buf1);
567+
if (e->drain2 != NULL) {
568+
sample_t *rbuf = e->drain2(e, &dframes, buf1, buf2);
569+
if (rbuf == buf2) {
570+
buf2 = buf1;
571+
buf1 = rbuf;
572+
}
573+
}
574+
else if (e->drain != NULL) e->drain(e, &dframes, buf1);
565575
else dframes = -1;
566576
if (e->ostream.fs != e->istream.fs) {
567-
gcd = find_gcd(e->ostream.fs, e->istream.fs);
577+
const int gcd = find_gcd(e->ostream.fs, e->istream.fs);
568578
ftmp = ratio_mult_ceil(ftmp, e->ostream.fs / gcd, e->istream.fs / gcd);
569579
}
570580
e = e->next;

effect.h

+2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ struct effect {
4848
void (*signal)(struct effect *);
4949
void (*plot)(struct effect *, int);
5050
void (*drain)(struct effect *, ssize_t *, sample_t *);
51+
sample_t * (*drain2)(struct effect *, ssize_t *, sample_t *, sample_t *);
5152
void (*destroy)(struct effect *);
5253
struct effect * (*merge)(struct effect *, struct effect *);
54+
ssize_t (*buffer_frames)(struct effect *, ssize_t);
5355
void *data;
5456
};
5557

0 commit comments

Comments
 (0)