annotate libao2/pl_volume.c @ 4884:c758cf9360d9

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