File: ffsox_analyze.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 (188 lines) | stat: -rwxr-xr-x 4,612 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * ffsox_analyze.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>
#if defined (_WIN32) // [
#include <fcntl.h>

#define W_WIN32
#endif // ]

#define FFSOX_ANALYZE_RATE 192000.0

#define FFSOX_ANALYZE_CASCADE
int ffsox_analyze(analyze_config_t *ac, int ai, int vi)
{
  int code=-1;
  aggregate_t *aggregate=ac->aggregate;
  source_cb_t progress=NULL==ac->f?NULL:ffsox_source_progress;
  source_t si;
  frame_reader_t *fr;
  collect_config_t cc;
  collect_t collect;
  intercept_t intercept;
  sox_reader_t *read;
  sox_signalinfo_t signal;
  sox_effects_chain_t *chain;
  int n;
  char *opts[2];
#if defined (FFSOX_ANALYZE_CASCADE) // {
  sox_rate_t rate;
#endif // }
  char buf[32];

#if defined (W_WIN32) // [
  if (ac->utf16) {
    fflush(ac->f);
    _setmode(_fileno(ac->f),_O_TEXT);
  }
#endif // ]

  // create a source.
  if (ffsox_source_create(&si,ac->path,ai,vi,progress,ac->f)<0) {
    DMESSAGE("creating source");
    goto si;
  }

  if (0!=ac->dump&&NULL!=ac->f)
    av_dump_format(si.f.fc,0,si.f.path,0);

  // create a frame reader.
  if (NULL==(fr=ffsox_frame_reader_new(&si,si.ai,ac->stereo,ac->drc))) {
    DMESSAGE("creating frame reader");
    goto fr;
  }

  // create a BS.1770 collector.
  memset(&cc,0,sizeof cc);
  cc.aggregate=ac->aggregate;
  cc.scale=0.25;
  cc.samplerate=fr->si.cc->sample_rate;
  cc.channels=fr->si.cc->channels;
  cc.momentary=ac->momentary;
  cc.shortterm=ac->shortterm;

  if (ffsox_collect_create(&collect,&cc)<0) {
    DMESSAGE("creating collector");
    goto collect;
  }

  // create a SoX reader.
  intercept.data=&collect;
  intercept.channel=ffsox_collect_channel;
  intercept.sample=ffsox_collect_sample;

  if (NULL==(read=ffsox_sox_reader_new(fr,collect.scale,&intercept))) {
    DMESSAGE("creating SoX reader");
    goto sa;
  }

  signal=read->signal;

  // create the SoX chain.
  if (NULL==(chain=sox_create_effects_chain(&read->encoding,NULL))) {
    DMESSAGE("creating SoX chain");
    goto chain;
  }

  // add the SoX read effect to the SoX chain.
  n=0;
  opts[n++]=(char *)read;

  if (SOX_SUCCESS!=ffsox_sox_add_effect_fn(chain,&signal,&signal,
      n,opts,ffsox_sox_read_handler)) {
    DMESSAGE("creating SoX read effect");
    goto effect;
  }

  // add the SoX rate effect to the SoX chain.
  if (0!=(AGGREGATE_TRUEPEAK&aggregate->flags)) {
#if defined (FFSOX_ANALYZE_CASCADE) // {
    rate=signal.rate;

    while ((rate*=2.0)<=FFSOX_ANALYZE_RATE) {
      //rate*=2;
      n=0;
      sprintf(buf,"%.0f",rate);
      opts[n++]=buf;

      if (SOX_SUCCESS!=ffsox_sox_add_effect_name(chain,&signal,&signal,
          n,opts,"rate")) {
        DMESSAGE("creating SoX rate effect");
        goto effect;
      }
    }
#else // } {
    n=0;
    sprintf(buf,"%.0f",FFSOX_ANALYZE_RATE);
    opts[n++]=buf;

    if (SOX_SUCCESS!=ffsox_sox_add_effect_name(chain,&signal,&signal,
        n,opts,"rate")) {
      DMESSAGE("creating SoX rate effect");
      goto effect;
    }
#endif // }
  }

  // add the SoX pull effect to the SoX chain.
  n=0;

  if (0!=(AGGREGATE_TRUEPEAK&aggregate->flags)) {
    opts[n++]=(char *)ffsox_collect_truepeak;
    opts[n++]=(char *)&collect;
  }

  if (SOX_SUCCESS!=ffsox_sox_add_effect_fn(chain,&signal,&signal,
      n,opts,ffsox_sox_pull_handler)) {
    DMESSAGE("creating SoX pull effect");
    goto effect;
  }

  // run the SoX chain.
  if (SOX_SUCCESS!=sox_flow_effects(chain,NULL,NULL)
      ||0!=read->sox_errno) {
    DMESSAGE("running SoX effects chain");
    goto flow;
  }

  ffsox_collect_flush(&collect);
  code=0;
// cleanup:
flow:
effect:
  sox_delete_effects_chain(chain);
chain:
sa:
  ffsox_collect_cleanup(&collect);
collect:
fr:
  si.vmt->cleanup(&si);
si:
#if defined (W_WIN32) // [
  if (ac->utf16) {
    fflush(ac->f);
    _setmode(_fileno(ac->f),_O_WTEXT);
  }
#if 0 // [
mode:
#endif // ]
#endif // ]
  return code;
}