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
|
|
7 #include <stdio.h>
|
|
8 #include <stdlib.h>
|
|
9 #include <unistd.h>
|
|
10 #include <inttypes.h>
|
|
11
|
|
12 #include "audio_out.h"
|
|
13 #include "audio_plugin.h"
|
|
14 #include "audio_plugin_internal.h"
|
|
15 #include "afmt.h"
|
|
16
|
|
17 static ao_info_t info =
|
|
18 {
|
|
19 "Volume control audio plugin",
|
|
20 "volume",
|
|
21 "Anders",
|
|
22 ""
|
|
23 };
|
|
24
|
|
25 LIBAO_PLUGIN_EXTERN(volume)
|
|
26
|
|
27 // local data
|
|
28 typedef struct pl_volume_s
|
|
29 {
|
|
30 uint16_t volume; // output volume level
|
|
31 int inuse; // This plugin is in use TRUE, FALSE
|
|
32 int format; // sample fomat
|
|
33 } pl_volume_t;
|
|
34
|
|
35 static pl_volume_t pl_volume={0,0,0};
|
|
36
|
|
37 // to set/get/query special features/parameters
|
|
38 static int control(int cmd,int arg){
|
|
39 switch(cmd){
|
|
40 case AOCONTROL_PLUGIN_SET_LEN:
|
|
41 return CONTROL_OK;
|
|
42 case AOCONTROL_GET_VOLUME:{
|
|
43 if(pl_volume.inuse){
|
|
44 ((ao_control_vol_t *)arg)->right=((float)pl_volume.volume)/2.55;
|
|
45 ((ao_control_vol_t *)arg)->left=((float)pl_volume.volume)/2.55;
|
|
46 return CONTROL_OK;
|
|
47 }
|
|
48 else
|
|
49 return CONTROL_ERROR;
|
|
50 }
|
|
51 case AOCONTROL_SET_VOLUME:{
|
|
52 if(pl_volume.inuse){
|
|
53 // Calculate avarage between left and right
|
|
54 float vol =2.55*((((ao_control_vol_t *)arg)->right)+(((ao_control_vol_t *)arg)->left))/2;
|
|
55 pl_volume.volume=(uint16_t)vol;
|
|
56 // Volume must be between 0 and 255
|
|
57 if(vol > 255)
|
|
58 pl_volume.volume = 0xFF;
|
|
59 if(vol < 0)
|
|
60 pl_volume.volume = 0;
|
|
61 return CONTROL_OK;
|
|
62 }
|
|
63 else
|
|
64 return CONTROL_ERROR;
|
|
65 }
|
|
66 }
|
|
67 return CONTROL_UNKNOWN;
|
|
68 }
|
|
69
|
|
70 // open & setup audio device
|
|
71 // return: 1=success 0=fail
|
|
72 static int init(){
|
|
73 // Sanity sheck this plugin supports AFMT_U8 and AFMT_S16_LE
|
|
74 switch(ao_plugin_data.format){
|
|
75 case(AFMT_U8):
|
|
76 case(AFMT_S16_LE):
|
|
77 break;
|
|
78 default:
|
|
79 fprintf(stderr,"[pl_volume] Audio format not yet suported \n");
|
|
80 return 0;
|
|
81 }
|
|
82 // Initialize volume to this value
|
|
83 pl_volume.volume=ao_plugin_cfg.pl_volume_volume;
|
|
84 pl_volume.format=ao_plugin_data.format;
|
|
85 /* The inuse flag is used in control to detremine if the return
|
|
86 value since that function always is called from ao_plugin regardless
|
|
87 of wether this plugin is in use or not. */
|
|
88 pl_volume.inuse=1;
|
|
89 // Tell the world what we are up to
|
|
90 printf("[pl_volume] Software volume control in use.\n");
|
|
91 return 1;
|
|
92 }
|
|
93
|
|
94 // close plugin
|
|
95 static void uninit(){
|
|
96 pl_volume.inuse=0;
|
|
97 }
|
|
98
|
|
99 // empty buffers
|
|
100 static void reset(){
|
|
101 }
|
|
102
|
|
103 // processes 'ao_plugin_data.len' bytes of 'data'
|
|
104 // called for every block of data
|
|
105 static int play(){
|
|
106 register int i=0;
|
|
107 // Change the volume.
|
|
108 switch(pl_volume.format){
|
|
109 case(AFMT_U8):{
|
|
110 register uint8_t* data=(uint8_t*)ao_plugin_data.data;
|
|
111 for(i=0;i<ao_plugin_data.len;i++){
|
|
112 data[i]=(((data[i]-128) * pl_volume.volume) >> 8) + 128;
|
|
113 }
|
|
114 break;
|
|
115 }
|
|
116 case(AFMT_S16_LE):{
|
|
117 register int len=ao_plugin_data.len>>1;
|
|
118 register int16_t* data=(int16_t*)ao_plugin_data.data;
|
|
119 for(i=0;i<len;i++)
|
|
120 data[i]=(data[i]* pl_volume.volume)>>8;
|
|
121 break;
|
|
122 }
|
|
123 default:
|
|
124 return 0;
|
|
125 }
|
|
126 return 1;
|
|
127
|
|
128 }
|
|
129
|
|
130
|
|
131
|
|
132
|
|
133
|