Audio SDL plugin. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / mupen64plus-audio-sdl / src / volume.c
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus-sdl-audio - volume.c                                      *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2007-2008 Richard42 Ebenblues                           *
5  *   Copyright (C) 2002 Hacktarux                                          *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
21  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22
23 #if defined(HAS_OSS_SUPPORT)
24
25 /* Sound volume functions. */
26 #include <sys/soundcard.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <unistd.h> /* close() */
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <errno.h>
34
35 #include "volume.h"
36
37 /* volSet
38  *  Sets volume of left and right PCM channels to given percentage (0-100) value.
39  */
40 void volSet(int percent)
41 {
42     int ret, vol;
43     int mixerfd = open("/dev/mixer", O_RDONLY);
44
45     if(mixerfd < 0)
46     {
47         perror("/dev/mixer");
48         return;
49     }
50
51     if(percent > 100)
52         percent = 100;
53     else if(percent < 0)
54         percent = 0;
55
56     vol = (percent << 8) + percent; // set both left/right channels to same vol
57     ret = ioctl(mixerfd, MIXER_WRITE(SOUND_MIXER_PCM), &vol);
58     if(ret < 0)
59         perror("Setting PCM volume");
60
61     close(mixerfd);
62 }
63
64 /* volGet
65  *  Returns volume of PCM channel as a percentage (0-100).
66  *  Returns 0 on error.
67  */
68 int volGet(void)
69 {
70     int vol, ret;
71     int mixerfd = open("/dev/mixer", O_RDONLY);
72
73     if(mixerfd < 0)
74     {
75         perror("/dev/mixer");
76         return 0;
77     }
78
79     ret = ioctl(mixerfd, MIXER_READ(SOUND_MIXER_PCM), &vol);
80     if(ret < 0)
81         perror("Reading PCM volume");
82
83     close(mixerfd);
84
85     return vol & 0xff; // just return the left channel
86 }
87
88 #endif /* defined(HAS_OSS_SUPPORT) */