]> git.vomp.tv Git - vompclient.git/blob - audio.cc
Initial import
[vompclient.git] / audio.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "audio.h"
22
23 Audio* Audio::instance = NULL;
24
25 Audio::Audio()
26 {
27   if (instance) return;
28   instance = this;
29   initted = 0;
30   fdAudio = 0;
31   streamType = 0;
32   volume = 20;
33   muted = 0;
34   userMute = 0;
35   systemMute = 0;
36 }
37
38 Audio::~Audio()
39 {
40   instance = NULL;
41 }
42
43 Audio* Audio::getInstance()
44 {
45   return instance;
46 }
47
48 int Audio::init(UCHAR streamType)
49 {
50   if (initted) return 0;
51   initted = 1;
52
53 //  if ((fdAudio = open("/dev/adec_mpg", O_RDWR | O_NONBLOCK)) < 0) return 0;
54   if ((fdAudio = open("/dev/adec_mpg", O_WRONLY)) < 0) return 0;
55
56   this->streamType = streamType;
57
58   if (!initAllParams())
59   {
60     shutdown();
61     return 0;
62   }
63
64   unMute();
65
66   return 1;
67 }
68
69 int Audio::initAllParams()
70 {
71   return (setStreamType(streamType) && setChannel() && setSource());
72 }
73
74 int Audio::shutdown()
75 {
76   if (!initted) return 0;
77   initted = 0;
78   close(fdAudio);
79   return 1;
80 }
81
82 int Audio::getFD()
83 {
84   if (!initted) return 0;
85
86   return fdAudio;
87 }
88
89 int Audio::write(char *buf, int len)
90 {
91   return 0; //write(fdAudio, buf, len);
92 }
93
94 int Audio::setStreamType(UCHAR type)
95 {
96   if (!initted) return 0;
97
98   if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
99   return 1;
100 }
101
102 int Audio::setChannel()
103 {
104   if (!initted) return 0;
105
106   if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
107   return 1;
108 }
109
110 int Audio::setSource()
111 {
112   if (!initted) return 0;
113
114   if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
115   return 1;
116 }
117
118 int Audio::sync()
119 {
120   if (!initted) return 0;
121
122   if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
123   return 1;
124 }
125
126 int Audio::play()
127 {
128   if (!initted) return 0;
129
130   if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
131   return 1;
132 }
133
134 int Audio::stop()
135 {
136   if (!initted) return 0;
137
138   if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
139   return 1;
140 }
141
142 int Audio::mute()
143 {
144   if (!initted) return 0;
145
146   if (ioctl(fdAudio, AV_SET_AUD_MUTE, 1) != 0) return 0;
147   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
148
149   muted = 1;
150   return 1;
151 }
152
153 int Audio::unMute()
154 {
155   if (!initted) return 0;
156
157   if (ioctl(fdAudio, AV_SET_AUD_MUTE, 0) != 0) return 0;
158   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
159
160   muted = 0;
161   return 1;
162 }
163
164 int Audio::pause()
165 {
166   if (!initted) return 0;
167
168   if (ioctl(fdAudio, AV_SET_AUD_PAUSE, 1) != 0) return 0;
169   return 1;
170 }
171
172 int Audio::unPause()
173 {
174   if (!initted) return 0;
175
176   if (ioctl(fdAudio, AV_SET_AUD_UNPAUSE, 1) != 0) return 0;
177   return 1;
178 }
179
180 int Audio::reset()
181 {
182   if (!initted) return 0;
183 //test();
184   if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
185   if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
186
187   doMuting();
188   return 1;
189 }
190
191 int Audio::setVolume(int volume)
192 {
193   // parameter: 0 for silence, 20 for full
194
195   if ((volume < 0) || (volume > 20)) return 0;
196
197   volume = 2 * (20 - volume);
198
199   unsigned long vol = (volume << 24) | (volume << 16);
200
201   Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
202   Log::getInstance()->log("Audio", Log::DEBUG, "%i", volume);
203
204   if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
205   return 1;
206 }
207
208 int Audio::test()
209 {
210   ULLONG stc = 0;
211   return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
212 }
213
214 int Audio::volumeUp()
215 {
216   if (!initted) return 0;
217   if (volume == 20) return volume;
218   volume++;
219   setVolume(volume);
220   return volume;
221 }
222
223 int Audio::volumeDown()
224 {
225   if (!initted) return 0;
226   if (volume == 0) return 0;
227   volume--;
228   setVolume(volume);
229   return volume;
230 }
231
232 int Audio::getVolume()
233 {
234   if (!initted) return 0;
235   return volume;
236 }
237
238 int Audio::toggleUserMute()
239 {
240   if (!initted) return 0;
241
242   if (userMute)
243   {
244     userMute = 0;
245   }
246   else
247   {
248     userMute = 1;
249   }
250
251   doMuting();
252
253   return userMute;
254 }
255
256 int Audio::systemMuteOn()
257 {
258   systemMute = 1;
259   return doMuting();
260 }
261
262 int Audio::systemMuteOff()
263 {
264   systemMute = 0;
265   return doMuting();
266 }
267
268 int Audio::doMuting()
269 {
270   Log::getInstance()->log("Audio", Log::DEBUG, "doMuting: user=%i sys=%i", userMute, systemMute);
271
272
273   if (userMute || systemMute)
274   {
275     return mute();
276   }
277   else
278   {
279     return unMute();
280   }
281 }