File: ffsox_collect.c

package info (click to toggle)
bs1770gain 0.4.12-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,832 kB
  • sloc: ansic: 9,380; sh: 1,288; makefile: 212
file content (150 lines) | stat: -rwxr-xr-x 4,227 bytes parent folder | download | duplicates (2)
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
/*
 * ffsox_collect.c
 * Copyright (C) 2015 Peter Belkner <pbelkner@users.sf.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.0 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301  USA
 */
#include <ffsox_priv.h>

///////////////////////////////////////////////////////////////////////////////
#define FFMPEG_BASS 3

#define FFMPEG_CHANNEL_IN_RANGE(ch,channels) ( \
  (channels)<=LIB1770_MAX_CHANNELS \
  ?(ch)<LIB1770_MAX_CHANNELS \
  :((ch)<FFMPEG_BASS||(FFMPEG_BASS<(ch)&&(ch)<=LIB1770_MAX_CHANNELS)) \
)

///////////////////////////////////////////////////////////////////////////////
int ffsox_collect_create(collect_t *collect, collect_config_t *cc)
{
  // process LIB1770_MAX_CHANNELS at maximum
  // ignore the rest
  int channels=LIB1770_MAX_CHANNELS<cc->channels
      ?LIB1770_MAX_CHANNELS
      :cc->channels;

  collect->scale=cc->scale;
  collect->invscale=1.0/cc->scale;
  collect->aggregate=cc->aggregate;
  collect->channels=channels;
  collect->sp=collect->sample;

  if (NULL==(collect->pre=lib1770_pre_new(cc->samplerate,channels))) {
    DMESSAGE("creating pre-filter");
    goto pre;
  }

  if (NULL==cc->aggregate->momentary)
    collect->momentary=NULL;
  else {
    collect->momentary=lib1770_block_new(cc->samplerate,cc->momentary.ms,
        cc->momentary.partition);

    if (NULL==collect->momentary) {
      DMESSAGE("creating momentary block");
      goto momentary;
    }

    lib1770_block_add_stats(collect->momentary,cc->aggregate->momentary);
    lib1770_pre_add_block(collect->pre,collect->momentary);
  }
    
  if (NULL==cc->aggregate->shortterm)
    collect->shortterm=NULL;
  else {
    collect->shortterm=lib1770_block_new(cc->samplerate,cc->shortterm.ms,
        cc->shortterm.partition);

    if (NULL==collect->shortterm) {
      DMESSAGE("creating shortterm block");
      goto shortterm;
    }

    lib1770_block_add_stats(collect->shortterm,cc->aggregate->shortterm);
    lib1770_pre_add_block(collect->pre,collect->shortterm);
  }

  return 0;
// cleanup:
  if (NULL!=collect->shortterm)
    lib1770_block_close(collect->shortterm);
shortterm:
  if (NULL!=collect->momentary)
    lib1770_block_close(collect->momentary);
momentary:
  lib1770_pre_close(collect->pre);
pre:
  return -1;
}

void ffsox_collect_cleanup(collect_t *collect)
{
  if (NULL!=collect->shortterm)
    lib1770_block_close(collect->shortterm);

  if (NULL!=collect->momentary)
    lib1770_block_close(collect->momentary);

  lib1770_pre_close(collect->pre);
}

///////////////////////////////////////////////////////////////////////////////
void ffsox_collect_flush(collect_t *collect)
{
  lib1770_pre_flush(collect->pre);
}

void ffsox_collect_channel(void *data, int ch, double x)
{
  collect_t *collect=data;
  aggregate_t *aggregate=collect->aggregate;

  if (FFMPEG_CHANNEL_IN_RANGE(ch,collect->channels))
    *collect->sp++=x;

  if (0!=(AGGREGATE_SAMPLEPEAK&aggregate->flags)) {
    if (x<0)
      x=-x;

    if (aggregate->samplepeak<x)
      aggregate->samplepeak=x;
  }
}

void ffsox_collect_sample(void *data)
{
  collect_t *collect=data;

  lib1770_pre_add_sample(collect->pre,collect->sample);
  collect->sp=collect->sample;
}

void ffsox_collect_truepeak(void *data, double x)
{
  collect_t *collect=data;
  aggregate_t *aggregate=collect->aggregate;

  if (0!=(AGGREGATE_TRUEPEAK&aggregate->flags)) {
    if (x<0)
      x=-x;

    x*=collect->invscale;

    if (aggregate->truepeak<x)
      aggregate->truepeak=x;
  }
}