]> git.vomp.tv Git - vompclient.git/blob - audiomvp.cc
Update for windows
[vompclient.git] / audiomvp.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 "audiomvp.h"
22
23 AudioMVP::AudioMVP()
24 {
25   if (instance) return;
26   initted = 0;
27   fdAudio = 0;
28   streamType = 0;
29   volume = 20;
30   muted = 0;
31 }
32
33 AudioMVP::~AudioMVP()
34 {
35 }
36
37 int AudioMVP::init(UCHAR tstreamType)
38 {
39   if (initted) return 0;
40   initted = 1;
41
42 //  if ((fdAudio = open("/dev/adec_mpg", O_RDWR | O_NONBLOCK)) < 0) return 0;
43   if ((fdAudio = open("/dev/adec_mpg", O_WRONLY)) < 0) return 0;
44
45   streamType = tstreamType;
46
47   if (!initAllParams())
48   {
49     shutdown();
50     return 0;
51   }
52
53   unMute();
54
55   // Set the volume variable to what the hardware is set at now
56   int hwvol = -1;
57   int hwvolFail = ioctl(fdAudio, AV_GET_AUD_VOLUME, &hwvol);
58   if (!hwvolFail)
59   {
60     volume = 20 - ((hwvol >> 16) & 0xFF);
61     if ((volume < 0) || (volume > 20)) volume = 20;
62   }
63
64   return 1;
65 }
66
67 int AudioMVP::initAllParams()
68 {
69   return (setStreamType(streamType) && setChannel() && setSource());
70 }
71
72 int AudioMVP::shutdown()
73 {
74   if (!initted) return 0;
75   initted = 0;
76   close(fdAudio);
77   return 1;
78 }
79
80 int AudioMVP::setStreamType(UCHAR type)
81 {
82   if (!initted) return 0;
83
84   if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
85   return 1;
86 }
87
88 int AudioMVP::setChannel()
89 {
90   if (!initted) return 0;
91
92   if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
93   return 1;
94 }
95
96 int AudioMVP::setSource()
97 {
98   if (!initted) return 0;
99
100   if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
101   return 1;
102 }
103
104 int AudioMVP::sync()
105 {
106   if (!initted) return 0;
107
108   if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
109   return 1;
110 }
111
112 int AudioMVP::play()
113 {
114   if (!initted) return 0;
115
116   if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
117   return 1;
118 }
119
120 int AudioMVP::stop()
121 {
122   if (!initted) return 0;
123
124   if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
125   return 1;
126 }
127
128 int AudioMVP::mute()
129 {
130   if (!initted) return 0;
131
132   if (ioctl(fdAudio, AV_SET_AUD_MUTE, 1) != 0) return 0;
133   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
134
135   muted = 1;
136   return 1;
137 }
138
139 int AudioMVP::unMute()
140 {
141   if (!initted) return 0;
142
143   if (ioctl(fdAudio, AV_SET_AUD_MUTE, 0) != 0) return 0;
144   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
145
146   muted = 0;
147   return 1;
148 }
149
150 int AudioMVP::pause()
151 {
152   if (!initted) return 0;
153
154   if (ioctl(fdAudio, AV_SET_AUD_PAUSE, 1) != 0) return 0;
155   return 1;
156 }
157
158 int AudioMVP::unPause()
159 {
160   if (!initted) return 0;
161
162   if (ioctl(fdAudio, AV_SET_AUD_UNPAUSE, 1) != 0) return 0;
163   return 1;
164 }
165
166 int AudioMVP::reset()
167 {
168   if (!initted) return 0;
169 //test();
170   if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
171   if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
172
173   doMuting();
174   return 1;
175 }
176
177 int AudioMVP::setVolume(int tvolume)
178 {
179   // parameter: 0 for silence, 20 for full
180   if ((tvolume < 0) || (tvolume > 20)) return 0;
181
182 // volume = 2 * (20 - volume);
183 // Right, that one was rubbish... 0-10 were almost
184 // inaudible, 11-20 did what should have been done
185 // over the whole 0-20 range
186
187   tvolume = 20 - tvolume;
188
189   audio_volume Avolume;
190   Avolume.frontleft = tvolume + Aoffset.frontleft;
191   Avolume.frontright = tvolume + Aoffset.frontright;
192   Avolume.rearleft = tvolume + Aoffset.rearleft;
193   Avolume.rearright = tvolume + Aoffset.rearright;
194   Avolume.center = tvolume + Aoffset.center;
195   Avolume.lfe = tvolume + Aoffset.lfe;
196
197   if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &Avolume) != 0) return 0;
198
199 //  unsigned long vol = (tvolume << 24) | (tvolume << 16);
200 //
201 //  Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
202 //  Log::getInstance()->log("Audio", Log::DEBUG, "%i", tvolume);
203
204 //  if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
205   return 1;
206 }
207
208 #ifdef DEV
209 int AudioMVP::test()
210 {
211 //  ULLONG stc = 0;
212 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
213
214   aud_sync_parms_t a;
215   a.parm1 = 0;
216   a.parm2 = 0;
217
218   int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
219
220
221   /*OK*/ printf("Audio sync disable = %i\n", b);
222
223   return 1;
224
225
226 }
227 #endif
228
229 void AudioMVP::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
230 {
231   packet = mplist.front();
232 }
233
234 UINT AudioMVP::DeliverMediaSample(const UCHAR* buffer, UINT* samplepos)
235 {
236   int written = ::write(fdAudio, buffer + packet.pos_buffer + *samplepos,
237                                  packet.length - *samplepos);
238   if (written == (int)(packet.length - *samplepos)) {*samplepos = 0; return 1;}
239   if (written > 0) *samplepos += written;
240   return 0;
241 }
242
243 void AudioMVP::ResetTimeOffsets()
244 {
245 }