File: volume.c

package info (click to toggle)
gavl 1.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,912 kB
  • sloc: ansic: 437,319; sh: 10,487; makefile: 238
file content (202 lines) | stat: -rw-r--r-- 5,122 bytes parent folder | download | duplicates (3)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*****************************************************************
 * gavl - a general purpose audio/video processing library
 *
 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.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 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 program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <stdlib.h>
#include <math.h>

#include <config.h>
#include <gavl.h>
#include <volume.h>

static void set_factor_i(gavl_volume_control_t * v)
  {
  switch(v->format.sample_format)
    {
    case GAVL_SAMPLE_S8:
    case GAVL_SAMPLE_U8:
      v->factor_i = (int64_t)(v->factor_f * 0x100+0.5);
      break;
    case GAVL_SAMPLE_S16:
    case GAVL_SAMPLE_U16:
      v->factor_i = (int64_t)(v->factor_f * 0x10000+0.5);
      break;
    case GAVL_SAMPLE_S32:
      v->factor_i = (int64_t)(v->factor_f * 0x80000000LL+0.5);
      break;
    case GAVL_SAMPLE_NONE:
    case GAVL_SAMPLE_FLOAT:
    case GAVL_SAMPLE_DOUBLE:
      break;
    }
  }

static void set_volume_interleave_none(gavl_volume_control_t * v,
                                       gavl_audio_frame_t * frame)
  {
  int i;
  /* All channels separate */

  for(i = 0; i < v->format.num_channels; i++)
    {
    v->set_volume_channel(v, frame->channels.s_8[i],
                          frame->valid_samples);
    }
  }

static void set_volume_interleave_2(gavl_volume_control_t * v,
                                    gavl_audio_frame_t * frame)
  {
  int i;
  int imax;

  imax = v->format.num_channels/2;
  
  for(i = 0; i < imax; i++)
    {
    v->set_volume_channel(v, frame->channels.s_8[2*i],
                          frame->valid_samples * 2);
    }

  if(v->format.num_channels % 2)
    {
    v->set_volume_channel(v, frame->channels.s_8[2*imax],
                          frame->valid_samples);
    }
  }

static void set_volume_interleave_all(gavl_volume_control_t * v,
                                      gavl_audio_frame_t * frame)
  {
  v->set_volume_channel(v, frame->samples.s_8,
                        frame->valid_samples * v->format.num_channels);
  }


/* Create / destroy */
  
gavl_volume_control_t * gavl_volume_control_create()
  {
  gavl_volume_control_t * v;
  v = calloc(1, sizeof(*v));
  return v;
  }

void gavl_volume_control_destroy(gavl_volume_control_t * v)
  {
  free(v);
  }

/* Set format: can be called multiple times with one instance */

void gavl_volume_control_set_format(gavl_volume_control_t * v,
                                    const gavl_audio_format_t * format)
  {
  gavl_volume_funcs_t * funcs;

  gavl_audio_format_copy(&v->format, format);
  
  funcs = gavl_volume_funcs_create();
  
  switch(format->sample_format)
    {
    case GAVL_SAMPLE_S8:
      v->set_volume_channel = funcs->set_volume_s8;
      break;

    case GAVL_SAMPLE_U8:
      v->set_volume_channel = funcs->set_volume_u8;
      break;

    case GAVL_SAMPLE_S16:
      v->set_volume_channel = funcs->set_volume_s16;
      break;

    case GAVL_SAMPLE_U16:
      v->set_volume_channel = funcs->set_volume_u16;
      break;

    case GAVL_SAMPLE_S32:
      v->set_volume_channel = funcs->set_volume_s32;
      break;

    case GAVL_SAMPLE_FLOAT:
      v->set_volume_channel = funcs->set_volume_float;
      break;
    case GAVL_SAMPLE_DOUBLE:
      v->set_volume_channel = funcs->set_volume_double;
      break;

    case GAVL_SAMPLE_NONE:
      break;
    }

  gavl_volume_funcs_destroy(funcs);

  switch(format->interleave_mode)
    {
    case GAVL_INTERLEAVE_NONE:
      v->set_volume = set_volume_interleave_none;
      break;
    case GAVL_INTERLEAVE_2:
      v->set_volume = set_volume_interleave_2;
      break;
    case GAVL_INTERLEAVE_ALL:
      v->set_volume = set_volume_interleave_all;
      break;
    }
  set_factor_i(v);
  }

/* Apply the volume control to one audio frame */
  
void gavl_volume_control_apply(gavl_volume_control_t * v,
                               gavl_audio_frame_t * frame)
  {
  v->set_volume(v, frame);
  }


void gavl_volume_control_set_volume(gavl_volume_control_t * v,
                                    float volume)
  {
  v->factor_f = pow(10, volume/20.0);
  set_factor_i(v);
  }


gavl_volume_funcs_t * gavl_volume_funcs_create()
  {
  gavl_volume_funcs_t * ret;
  ret = calloc(1, sizeof(*ret));
  
  gavl_init_volume_funcs_c(ret);

#ifdef ARCH_X86
  //  gavl_init_volume_funcs_mmx(ret);
#endif
  
  return ret;
  }

void gavl_volume_funcs_destroy(gavl_volume_funcs_t * funcs)
  {
  free(funcs);
  }