]> git.vomp.tv Git - vompclient-marten.git/blob - audio.cc
Changed the volume control resolution
[vompclient-marten.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   if ((volume < 0) || (volume > 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   volume = 20 - volume;
202
203   unsigned long vol = (volume << 24) | (volume << 16);
204
205   Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
206   Log::getInstance()->log("Audio", Log::DEBUG, "%i", volume);
207
208   if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
209   return 1;
210 }
211
212 int Audio::test()
213 {
214 //  ULLONG stc = 0;
215 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
216
217   aud_sync_parms_t a;
218   a.parm1 = 0;
219   a.parm2 = 0;
220
221   int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
222
223
224   printf("Audio sync disable = %i\n", b);
225
226   return 1;
227
228
229 }
230
231 int Audio::volumeUp()
232 {
233   if (!initted) return 0;
234   if (volume == 20) return volume;
235   volume++;
236   setVolume(volume);
237   return volume;
238 }
239
240 int Audio::volumeDown()
241 {
242   if (!initted) return 0;
243   if (volume == 0) return 0;
244   volume--;
245   setVolume(volume);
246   return volume;
247 }
248
249 int Audio::getVolume()
250 {
251   if (!initted) return 0;
252   return volume;
253 }
254
255 int Audio::toggleUserMute()
256 {
257   if (!initted) return 0;
258
259   if (userMute)
260   {
261     userMute = 0;
262   }
263   else
264   {
265     userMute = 1;
266   }
267
268   doMuting();
269
270   return userMute;
271 }
272
273 int Audio::systemMuteOn()
274 {
275   systemMute = 1;
276   return doMuting();
277 }
278
279 int Audio::systemMuteOff()
280 {
281   systemMute = 0;
282   return doMuting();
283 }
284
285 int Audio::doMuting()
286 {
287   Log::getInstance()->log("Audio", Log::DEBUG, "doMuting: user=%i sys=%i", userMute, systemMute);
288
289
290   if (userMute || systemMute)
291   {
292     return mute();
293   }
294   else
295   {
296     return unMute();
297   }
298 }