]> git.vomp.tv Git - vompclient.git/blob - audio.cc
wshadow
[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 tstreamType)
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   streamType = tstreamType;
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 tvolume)
192 {
193   // parameter: 0 for silence, 20 for full
194   if ((tvolume < 0) || (tvolume > 20)) return 0;
195
196 // volume = 2 * (20 - volume);
197 // Right, that one was rubbish... 0-10 were almost
198 // inaudible, 11-20 did what should have been done
199 // over the whole 0-20 range
200
201   tvolume = 20 - tvolume;
202
203   unsigned long vol = (tvolume << 24) | (tvolume << 16);
204
205   Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
206   Log::getInstance()->log("Audio", Log::DEBUG, "%i", tvolume);
207
208   if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
209   return 1;
210 }
211
212 #ifdef DEV
213 int Audio::test()
214 {
215 //  ULLONG stc = 0;
216 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
217
218   aud_sync_parms_t a;
219   a.parm1 = 0;
220   a.parm2 = 0;
221
222   int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
223
224
225   printf("Audio sync disable = %i\n", b);
226
227   return 1;
228
229
230 }
231 #endif
232
233 int Audio::volumeUp()
234 {
235   if (!initted) return 0;
236   if (volume == 20) return volume;
237   volume++;
238   setVolume(volume);
239   return volume;
240 }
241
242 int Audio::volumeDown()
243 {
244   if (!initted) return 0;
245   if (volume == 0) return 0;
246   volume--;
247   setVolume(volume);
248   return volume;
249 }
250
251 int Audio::getVolume()
252 {
253   if (!initted) return 0;
254   return volume;
255 }
256
257 int Audio::toggleUserMute()
258 {
259   if (!initted) return 0;
260
261   if (userMute)
262   {
263     userMute = 0;
264   }
265   else
266   {
267     userMute = 1;
268   }
269
270   doMuting();
271
272   return userMute;
273 }
274
275 int Audio::systemMuteOn()
276 {
277   systemMute = 1;
278   return doMuting();
279 }
280
281 int Audio::systemMuteOff()
282 {
283   systemMute = 0;
284   return doMuting();
285 }
286
287 int Audio::doMuting()
288 {
289   Log::getInstance()->log("Audio", Log::DEBUG, "doMuting: user=%i sys=%i", userMute, systemMute);
290
291
292   if (userMute || systemMute)
293   {
294     return mute();
295   }
296   else
297   {
298     return unMute();
299   }
300 }