annotate libaf/af_extrastereo.c @ 14350:ab26ad5d041a

Tremor license
author henry
date Tue, 04 Jan 2005 12:01:25 +0000
parents 815f03b7cee5
children 7b7c94b5e78a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13550
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
1 /*=============================================================================
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
2 //
13602
14090f7300a8 The full name of the GPL is GNU General Public License.
diego
parents: 13550
diff changeset
3 // This software has been released under the terms of the GNU General Public
13550
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
5 //
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
6 // Copyright 2004 Alex Beregszaszi & Pierre Lombard
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
7 //
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
8 //=============================================================================
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
9 */
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
10
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
11 #include <stdio.h>
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
12 #include <stdlib.h>
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
13 #include <string.h>
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
14
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
15 #include <unistd.h>
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
16 #include <inttypes.h>
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
17 #include <math.h>
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
18 #include <limits.h>
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
19
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
20 #include "af.h"
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
21
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
22 // Data for specific instances of this filter
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
23 typedef struct af_extrastereo_s
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
24 {
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
25 float mul;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
26 }af_extrastereo_t;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
27
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
28 // Initialization and runtime control
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
29 static int control(struct af_instance_s* af, int cmd, void* arg)
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
30 {
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
31 af_extrastereo_t* s = (af_extrastereo_t*)af->setup;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
32
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
33 switch(cmd){
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
34 case AF_CONTROL_REINIT:
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
35 // Sanity check
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
36 if(!arg) return AF_ERROR;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
37
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
38 af->data->rate = ((af_data_t*)arg)->rate;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
39 af->data->nch = 2;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 14213
diff changeset
40 af->data->format = AF_FORMAT_S16_NE;
13550
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
41 af->data->bps = 2;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
42
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
43 return af_test_output(af,(af_data_t*)arg);
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
44 case AF_CONTROL_COMMAND_LINE:{
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
45 float f;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
46 sscanf((char*)arg,"%f", &f);
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
47 s->mul = f;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
48 return AF_OK;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
49 }
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
50 case AF_CONTROL_ES_MUL | AF_CONTROL_SET:
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
51 s->mul = *(float*)arg;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
52 return AF_OK;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
53 case AF_CONTROL_ES_MUL | AF_CONTROL_GET:
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
54 *(float*)arg = s->mul;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
55 return AF_OK;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
56 }
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
57 return AF_UNKNOWN;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
58 }
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
59
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
60 // Deallocate memory
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
61 static void uninit(struct af_instance_s* af)
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
62 {
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
63 if(af->data)
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
64 free(af->data);
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
65 if(af->setup)
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
66 free(af->setup);
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
67 }
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
68
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
69 // Filter data through filter
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
70 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
71 {
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
72 af_extrastereo_t *s = af->setup;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
73 register int i = 0;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
74 int16_t *a = (int16_t*)data->audio; // Audio data
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
75 int len = data->len/2; // Number of samples
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
76 int avg, l, r;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
77
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
78 for (i = 0; i < len; i+=2)
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
79 {
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
80 avg = (a[i] + a[i + 1]) / 2;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
81
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
82 l = avg + (int)(s->mul * (a[i] - avg));
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
83 r = avg + (int)(s->mul * (a[i + 1] - avg));
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
84
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
85 a[i] = clamp(l, SHRT_MIN, SHRT_MAX);
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
86 a[i + 1] = clamp(r, SHRT_MIN, SHRT_MAX);
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
87 }
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
88
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
89 return data;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
90 }
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
91
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
92 // Allocate memory and set function pointers
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
93 static int open(af_instance_t* af){
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
94 af->control=control;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
95 af->uninit=uninit;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
96 af->play=play;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
97 af->mul.n=1;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
98 af->mul.d=1;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
99 af->data=calloc(1,sizeof(af_data_t));
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
100 af->setup=calloc(1,sizeof(af_extrastereo_t));
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
101 if(af->data == NULL || af->setup == NULL)
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
102 return AF_ERROR;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
103
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
104 ((af_extrastereo_t*)af->setup)->mul = 2.5;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
105 return AF_OK;
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
106 }
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
107
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
108 // Description of this filter
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
109 af_info_t af_info_extrastereo = {
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
110 "Extra stereo",
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
111 "extrastereo",
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
112 "Alex Beregszaszi & Pierre Lombard",
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
113 "",
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
114 AF_FLAGS_NOT_REENTRANT,
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
115 open
81e62cbe57d9 reimplementation of the pl_extrastereo and pl_volnorm plugins
alex
parents:
diff changeset
116 };