Files should be opened in binary mode on OS/2.
[mplayer/glamo.git] / mixer.c
blob3e66b78cfcb5cd3bf877819440b33de1f36f2810
1 #include <string.h>
2 #ifndef __MINGW32__
3 #include <sys/ioctl.h>
4 #endif
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <unistd.h>
9 #include "config.h"
10 #include "libao2/audio_out.h"
11 #include "libaf/af.h"
12 #include "mixer.h"
14 #include "help_mp.h"
16 char * mixer_device=NULL;
17 char * mixer_channel=NULL;
18 int soft_vol = 0;
19 float soft_vol_max = 110.0;
21 void mixer_getvolume(mixer_t *mixer, float *l, float *r)
23 ao_control_vol_t vol;
24 *l=0; *r=0;
25 if(mixer->audio_out){
26 if(soft_vol ||
27 CONTROL_OK != mixer->audio_out->control(AOCONTROL_GET_VOLUME,&vol)) {
28 if (!mixer->afilter)
29 return;
30 else {
31 float db_vals[AF_NCH];
32 if (!af_control_any_rev(mixer->afilter,
33 AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_GET, db_vals))
34 db_vals[0] = db_vals[1] = 1.0;
35 else
36 af_from_dB (2, db_vals, db_vals, 20.0, -200.0, 60.0);
37 vol.left = (db_vals[0] / (soft_vol_max / 100.0)) * 100.0;
38 vol.right = (db_vals[1] / (soft_vol_max / 100.0)) * 100.0;
41 *r=vol.right;
42 *l=vol.left;
46 void mixer_setvolume(mixer_t *mixer, float l, float r)
48 ao_control_vol_t vol;
49 vol.right=r; vol.left=l;
50 if(mixer->audio_out){
51 if(soft_vol ||
52 CONTROL_OK != mixer->audio_out->control(AOCONTROL_SET_VOLUME,&vol)) {
53 if (!mixer->afilter)
54 return;
55 else {
56 // af_volume uses values in dB
57 float db_vals[AF_NCH];
58 int i;
59 db_vals[0] = (l / 100.0) * (soft_vol_max / 100.0);
60 db_vals[1] = (r / 100.0) * (soft_vol_max / 100.0);
61 for (i = 2; i < AF_NCH; i++) {
62 db_vals[i] = ((l + r) / 100.0) * (soft_vol_max / 100.0) / 2.0;
64 af_to_dB (AF_NCH, db_vals, db_vals, 20.0);
65 if (!af_control_any_rev(mixer->afilter,
66 AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, db_vals)) {
67 mp_msg(MSGT_GLOBAL, MSGL_INFO, MSGTR_InsertingAfVolume);
68 if (af_add(mixer->afilter, "volume")) {
69 if (!af_control_any_rev(mixer->afilter,
70 AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, db_vals)) {
71 mp_msg(MSGT_GLOBAL, MSGL_ERR, MSGTR_NoVolume);
72 return;
79 mixer->muted=0;
82 void mixer_incvolume(mixer_t *mixer)
84 float mixer_l, mixer_r;
85 mixer_getvolume(mixer, &mixer_l, &mixer_r);
86 mixer_l += mixer->volstep;
87 if ( mixer_l > 100 ) mixer_l = 100;
88 mixer_r += mixer->volstep;
89 if ( mixer_r > 100 ) mixer_r = 100;
90 mixer_setvolume(mixer, mixer_l, mixer_r);
93 void mixer_decvolume(mixer_t *mixer)
95 float mixer_l, mixer_r;
96 mixer_getvolume(mixer, &mixer_l, &mixer_r);
97 mixer_l -= mixer->volstep;
98 if ( mixer_l < 0 ) mixer_l = 0;
99 mixer_r -= mixer->volstep;
100 if ( mixer_r < 0 ) mixer_r = 0;
101 mixer_setvolume(mixer, mixer_l, mixer_r);
104 void mixer_getbothvolume(mixer_t *mixer, float *b)
106 float mixer_l, mixer_r;
107 mixer_getvolume(mixer, &mixer_l, &mixer_r);
108 *b = ( mixer_l + mixer_r ) / 2;
111 void mixer_mute(mixer_t *mixer)
113 if (mixer->muted) mixer_setvolume(mixer, mixer->last_l, mixer->last_r);
114 else
116 mixer_getvolume(mixer, &mixer->last_l, &mixer->last_r);
117 mixer_setvolume(mixer, 0, 0);
118 mixer->muted=1;
122 void mixer_getbalance(mixer_t *mixer, float *val)
124 *val = 0.f;
125 if(!mixer->afilter)
126 return;
127 af_control_any_rev(mixer->afilter,
128 AF_CONTROL_PAN_BALANCE | AF_CONTROL_GET, val);
131 void mixer_setbalance(mixer_t *mixer, float val)
133 float level[AF_NCH];
134 int i;
135 af_control_ext_t arg_ext = { .arg = level };
136 af_instance_t* af_pan_balance;
138 if(!mixer->afilter)
139 return;
140 if (af_control_any_rev(mixer->afilter,
141 AF_CONTROL_PAN_BALANCE | AF_CONTROL_SET, &val))
142 return;
144 if (!(af_pan_balance = af_add(mixer->afilter, "pan"))) {
145 mp_msg(MSGT_GLOBAL, MSGL_ERR, MSGTR_NoBalance);
146 return;
149 af_init(mixer->afilter);
150 /* make all other channels pass thru since by default pan blocks all */
151 memset(level, 0, sizeof(level));
152 for (i = 2; i < AF_NCH; i++) {
153 arg_ext.ch = i;
154 level[i] = 1.f;
155 af_pan_balance->control(af_pan_balance,
156 AF_CONTROL_PAN_LEVEL | AF_CONTROL_SET, &arg_ext);
157 level[i] = 0.f;
160 af_pan_balance->control(af_pan_balance,
161 AF_CONTROL_PAN_BALANCE | AF_CONTROL_SET, &val);