]> git.vomp.tv Git - vompclient.git/blob - audio.cc
Another stab at radio support, fix for chasing playback
[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   aud_sync_parms_t a;
214   a.parm1 = 0;
215   a.parm2 = 0;
216
217   int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
218
219
220   printf("Audio sync disable = %i\n", b);
221
222   return 1;
223
224
225 }
226
227 int Audio::volumeUp()
228 {
229   if (!initted) return 0;
230   if (volume == 20) return volume;
231   volume++;
232   setVolume(volume);
233   return volume;
234 }
235
236 int Audio::volumeDown()
237 {
238   if (!initted) return 0;
239   if (volume == 0) return 0;
240   volume--;
241   setVolume(volume);
242   return volume;
243 }
244
245 int Audio::getVolume()
246 {
247   if (!initted) return 0;
248   return volume;
249 }
250
251 int Audio::toggleUserMute()
252 {
253   if (!initted) return 0;
254
255   if (userMute)
256   {
257     userMute = 0;
258   }
259   else
260   {
261     userMute = 1;
262   }
263
264   doMuting();
265
266   return userMute;
267 }
268
269 int Audio::systemMuteOn()
270 {
271   systemMute = 1;
272   return doMuting();
273 }
274
275 int Audio::systemMuteOff()
276 {
277   systemMute = 0;
278   return doMuting();
279 }
280
281 int Audio::doMuting()
282 {
283   Log::getInstance()->log("Audio", Log::DEBUG, "doMuting: user=%i sys=%i", userMute, systemMute);
284
285
286   if (userMute || systemMute)
287   {
288     return mute();
289   }
290   else
291   {
292     return unMute();
293   }
294 }