Mercurial > mplayer.hg
annotate libao2/pl_volume.c @ 10211:4bc481804519
warning fixes by Dominik Mierzejewski <dominik@rangers.eu.org>
author | alex |
---|---|
date | Fri, 30 May 2003 17:57:41 +0000 |
parents | 12b1790038b0 |
children | 815f03b7cee5 |
rev | line source |
---|---|
4859 | 1 /* This audio output plugin changes the volume of the sound, and can |
2 be used when the mixer doesn't support the PCM channel. The volume | |
3 is set in fixed steps between 0 - 2^8. */ | |
4 | |
5 #define PLUGIN | |
6 | |
5063 | 7 // Some limits |
8 #define MIN_S16 -32650 | |
9 #define MAX_S16 32650 | |
10 #define MIN_U8 0 | |
11 #define MAX_U8 255 | |
12 #define MIN_S8 -128 | |
13 #define MAX_S8 127 | |
14 | |
4859 | 15 #include <stdio.h> |
16 #include <stdlib.h> | |
17 #include <unistd.h> | |
18 #include <inttypes.h> | |
19 | |
20 #include "audio_out.h" | |
21 #include "audio_plugin.h" | |
22 #include "audio_plugin_internal.h" | |
23 #include "afmt.h" | |
24 | |
25 static ao_info_t info = | |
26 { | |
27 "Volume control audio plugin", | |
28 "volume", | |
29 "Anders", | |
30 "" | |
31 }; | |
32 | |
33 LIBAO_PLUGIN_EXTERN(volume) | |
34 | |
35 // local data | |
36 typedef struct pl_volume_s | |
37 { | |
38 uint16_t volume; // output volume level | |
39 int inuse; // This plugin is in use TRUE, FALSE | |
40 int format; // sample fomat | |
41 } pl_volume_t; | |
42 | |
43 static pl_volume_t pl_volume={0,0,0}; | |
44 | |
45 // to set/get/query special features/parameters | |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
8741
diff
changeset
|
46 static int control(int cmd,void *arg){ |
4859 | 47 switch(cmd){ |
48 case AOCONTROL_PLUGIN_SET_LEN: | |
49 return CONTROL_OK; | |
50 case AOCONTROL_GET_VOLUME:{ | |
51 if(pl_volume.inuse){ | |
52 ((ao_control_vol_t *)arg)->right=((float)pl_volume.volume)/2.55; | |
53 ((ao_control_vol_t *)arg)->left=((float)pl_volume.volume)/2.55; | |
54 return CONTROL_OK; | |
55 } | |
56 else | |
57 return CONTROL_ERROR; | |
58 } | |
59 case AOCONTROL_SET_VOLUME:{ | |
60 if(pl_volume.inuse){ | |
61 // Calculate avarage between left and right | |
62 float vol =2.55*((((ao_control_vol_t *)arg)->right)+(((ao_control_vol_t *)arg)->left))/2; | |
63 pl_volume.volume=(uint16_t)vol; | |
64 // Volume must be between 0 and 255 | |
65 if(vol > 255) | |
66 pl_volume.volume = 0xFF; | |
67 if(vol < 0) | |
68 pl_volume.volume = 0; | |
69 return CONTROL_OK; | |
70 } | |
71 else | |
72 return CONTROL_ERROR; | |
73 } | |
74 } | |
75 return CONTROL_UNKNOWN; | |
76 } | |
77 | |
78 // open & setup audio device | |
79 // return: 1=success 0=fail | |
80 static int init(){ | |
81 // Sanity sheck this plugin supports AFMT_U8 and AFMT_S16_LE | |
82 switch(ao_plugin_data.format){ | |
83 case(AFMT_U8): | |
8741
46d21c0f36aa
(nicer) endianness fix for every plugin except pl_format
colin
parents:
5063
diff
changeset
|
84 case(AFMT_S16_NE): |
4859 | 85 break; |
86 default: | |
87 fprintf(stderr,"[pl_volume] Audio format not yet suported \n"); | |
88 return 0; | |
89 } | |
90 // Initialize volume to this value | |
91 pl_volume.volume=ao_plugin_cfg.pl_volume_volume; | |
92 pl_volume.format=ao_plugin_data.format; | |
93 /* The inuse flag is used in control to detremine if the return | |
94 value since that function always is called from ao_plugin regardless | |
95 of wether this plugin is in use or not. */ | |
96 pl_volume.inuse=1; | |
97 // Tell the world what we are up to | |
5063 | 98 printf("[pl_volume] Software volume control in use%s.\n",ao_plugin_cfg.pl_volume_softclip?", soft clipping enabled":""); |
4859 | 99 return 1; |
100 } | |
101 | |
102 // close plugin | |
103 static void uninit(){ | |
104 pl_volume.inuse=0; | |
105 } | |
106 | |
107 // empty buffers | |
108 static void reset(){ | |
109 } | |
110 | |
5063 | 111 #define SIGN(x) (x>0?1:-1) |
4859 | 112 // processes 'ao_plugin_data.len' bytes of 'data' |
113 // called for every block of data | |
114 static int play(){ | |
115 register int i=0; | |
5063 | 116 register int vol=pl_volume.volume; // Logarithmic control sounds more natural |
117 vol=(vol*vol*vol)>>12; | |
4859 | 118 // Change the volume. |
119 switch(pl_volume.format){ | |
120 case(AFMT_U8):{ | |
121 register uint8_t* data=(uint8_t*)ao_plugin_data.data; | |
5063 | 122 register int x; |
123 for(i=0;i<ao_plugin_data.len;i++){ | |
124 x=((data[i]-128) * vol) >> 8; | |
125 if(x>MAX_S8) | |
126 data[i]=MAX_U8; | |
127 else if(x<MIN_S8) | |
128 data[i]=MIN_U8; | |
129 else{ | |
130 if(ao_plugin_cfg.pl_volume_softclip) | |
131 data[i] = ((3*x - ((x*x*x) >> 14)) >> 1) + 128; | |
132 else | |
133 data[i] = x + 128; | |
134 } | |
135 } | |
136 break; | |
4859 | 137 } |
8741
46d21c0f36aa
(nicer) endianness fix for every plugin except pl_format
colin
parents:
5063
diff
changeset
|
138 case(AFMT_S16_NE):{ |
4859 | 139 register int len=ao_plugin_data.len>>1; |
140 register int16_t* data=(int16_t*)ao_plugin_data.data; | |
5063 | 141 register int x; |
142 for(i=0;i<len;i++){ | |
143 x=(data[i] * vol) >> 8; | |
144 if(x>MAX_S16) | |
145 data[i]=MAX_S16; | |
146 else if(x<MIN_S16) | |
147 data[i]=MIN_S16; | |
148 else{ | |
149 if(ao_plugin_cfg.pl_volume_softclip){ | |
150 int64_t t=x*x; | |
151 t=(t*x) >> 30; | |
152 data[i] = (3*x - (int)t) >> 1; | |
153 //data[i] = 2*x - SIGN(x)*((x*x)>>15); | |
154 } | |
155 else | |
156 data[i] = x; | |
157 } | |
158 } | |
4859 | 159 break; |
160 } | |
161 default: | |
162 return 0; | |
163 } | |
164 return 1; | |
165 | |
166 } |