author | llornkcor <llornkcor> | 2003-04-04 03:03:38 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2003-04-04 03:03:38 (UTC) |
commit | ce6388c4f44e9e76342a338954fff598372e06f0 (patch) (unidiff) | |
tree | 895c202343fc394335e6effdb535ccb864a587d3 /noncore/multimedia/opierec/device.cpp | |
parent | 487971af0c1b70babcc39fd549dc0d8142cd4865 (diff) | |
download | opie-ce6388c4f44e9e76342a338954fff598372e06f0.zip opie-ce6388c4f44e9e76342a338954fff598372e06f0.tar.gz opie-ce6388c4f44e9e76342a338954fff598372e06f0.tar.bz2 |
bitches owe me money. initial commit. zaurus/vercel has sound drivers that doesnt actually change samplerates, so this kinda has problems, currently
Diffstat (limited to 'noncore/multimedia/opierec/device.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/opierec/device.cpp | 341 |
1 files changed, 341 insertions, 0 deletions
diff --git a/noncore/multimedia/opierec/device.cpp b/noncore/multimedia/opierec/device.cpp new file mode 100644 index 0000000..c2029b7 --- a/dev/null +++ b/noncore/multimedia/opierec/device.cpp | |||
@@ -0,0 +1,341 @@ | |||
1 | // device.cpp | ||
2 | |||
3 | #include "device.h" | ||
4 | #include "qtrec.h" | ||
5 | |||
6 | #include <qpe/qpeapplication.h> | ||
7 | #include <qpe/config.h> | ||
8 | #include <qpe/qcopenvelope_qws.h> | ||
9 | |||
10 | #include <qslider.h> | ||
11 | #include <qmessagebox.h> | ||
12 | |||
13 | #include <qfile.h> | ||
14 | #include <qtextstream.h> | ||
15 | #include <fcntl.h> | ||
16 | #include <stdio.h> | ||
17 | #include <stdlib.h> | ||
18 | #include <sys/ioctl.h> | ||
19 | #include <sys/soundcard.h> | ||
20 | #include <unistd.h> | ||
21 | #include<sys/wait.h> | ||
22 | // #include <sys/stat.h> | ||
23 | // #include <sys/time.h> | ||
24 | // #include <sys/types.h> | ||
25 | #include <unistd.h> | ||
26 | #include <errno.h> | ||
27 | |||
28 | //extern QtRec *qperec; | ||
29 | |||
30 | Device::Device( QObject * parent, const char * dsp, const char * mixr, bool record ) | ||
31 | : QObject( parent) | ||
32 | { | ||
33 | dspstr = (char *)dsp; | ||
34 | mixstr = (char *)mixr; | ||
35 | |||
36 | devForm=-1; | ||
37 | devCh=-1; | ||
38 | devRate=-1; | ||
39 | |||
40 | if( !record){ //playing | ||
41 | qDebug("setting up DSP for playing"); | ||
42 | flags = O_WRONLY; | ||
43 | } else { //recording | ||
44 | qDebug("setting up DSP for recording"); | ||
45 | flags = O_RDWR; | ||
46 | // flags = O_RDONLY; | ||
47 | selectMicInput(); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | bool Device::openDsp() { | ||
52 | if( openDevice( flags) == -1) { | ||
53 | perror("<<<<<<<<<<<<<<ioctl(\"Open device\")"); | ||
54 | return false; | ||
55 | } | ||
56 | return true; | ||
57 | } | ||
58 | |||
59 | int Device::getOutVolume( ) { | ||
60 | unsigned int volume; | ||
61 | int mixerHandle = open( mixstr, O_RDWR ); | ||
62 | if ( mixerHandle >= 0 ) { | ||
63 | if(ioctl( mixerHandle, MIXER_READ(SOUND_MIXER_VOLUME), &volume )==-1) | ||
64 | perror("<<<<<<<<<<<<<<ioctl(\"MIXER_READ\")"); | ||
65 | ::close( mixerHandle ); | ||
66 | } else | ||
67 | perror("open(\"/dev/mixer\")"); | ||
68 | printf("<<<<<<<<<<<<<<<<<<<<output volume %d\n",volume); | ||
69 | |||
70 | Config cfg("qpe"); | ||
71 | cfg.setGroup("Volume"); | ||
72 | |||
73 | return cfg.readNumEntry("VolumePercent"); | ||
74 | } | ||
75 | |||
76 | int Device::getInVolume() { | ||
77 | unsigned int volume=0; | ||
78 | int mixerHandle = ::open( mixstr, O_RDWR ); | ||
79 | if ( mixerHandle >= 0 ) { | ||
80 | if(ioctl( mixerHandle, MIXER_READ(SOUND_MIXER_MIC), &volume )==-1) | ||
81 | perror("<<<<<<<<<<<<<<<ioctl(\"MIXER_READ\")"); | ||
82 | ::close( mixerHandle ); | ||
83 | } else | ||
84 | perror("open(\"/dev/mixer\")"); | ||
85 | printf("<<<<<<<<<<<<<<input volume %d\n", volume ); | ||
86 | Config cfg("qpe"); | ||
87 | cfg.setGroup("Volume"); | ||
88 | |||
89 | return cfg.readNumEntry("Mic"); | ||
90 | } | ||
91 | |||
92 | void Device::changedOutVolume(int vol) { | ||
93 | int level = (vol << 8) + vol; | ||
94 | int fd = 0; | ||
95 | if ((fd = open("/dev/mixer", O_RDWR))>=0) { | ||
96 | if(ioctl(fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &level) == -1) | ||
97 | perror("ioctl(\"MIXER_IN_WRITE\")"); | ||
98 | |||
99 | Config cfg("qpe"); | ||
100 | cfg.setGroup("Volume"); | ||
101 | cfg.writeEntry("VolumePercent", QString::number( vol )); | ||
102 | QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << false; | ||
103 | } | ||
104 | ::close(fd); | ||
105 | } | ||
106 | |||
107 | void Device::changedInVolume(int vol ) { | ||
108 | int level = (vol << 8) + vol; | ||
109 | int fd = 0; | ||
110 | if ((fd = open("/dev/mixer", O_RDWR))>=0) { | ||
111 | if(ioctl(fd, MIXER_WRITE(SOUND_MIXER_MIC), &level) == -1) | ||
112 | perror("ioctl(\"MIXER_IN_WRITE\")"); | ||
113 | Config cfg("qpe"); | ||
114 | cfg.setGroup("Volume"); | ||
115 | cfg.writeEntry("Mic", QString::number(vol )); | ||
116 | QCopEnvelope( "QPE/System", "micChange(bool)" ) << false; | ||
117 | } | ||
118 | ::close(fd); | ||
119 | } | ||
120 | |||
121 | bool Device::selectMicInput() { | ||
122 | /* | ||
123 | int md=0; | ||
124 | int info=MIXER_WRITE(SOUND_MIXER_MIC); | ||
125 | md = ::open( "/dev/mixer", O_RDWR ); | ||
126 | if ( md == -1) | ||
127 | perror("open(\"/dev/mixer\")"); | ||
128 | else { | ||
129 | if( ioctl( md, SOUND_MIXER_WRITE_RECSRC, &info) == -1) | ||
130 | perror("ioctl(\"SOUND_MIXER_WRITE_RECSRC\")"); | ||
131 | ::close(md); | ||
132 | return false; | ||
133 | } | ||
134 | ::close(md); | ||
135 | */ | ||
136 | return true; | ||
137 | } | ||
138 | |||
139 | int Device::openDevice( int flags) { | ||
140 | /* pid_t pid; | ||
141 | int status; | ||
142 | int pipefd[2]; | ||
143 | char message[20]; | ||
144 | if (pipe(pipefd) == -1){ | ||
145 | perror ("Error creating pipe"); | ||
146 | exit(1); | ||
147 | } | ||
148 | switch (pid = fork()) { | ||
149 | case -1: | ||
150 | perror("The fork failed!"); | ||
151 | break; | ||
152 | case 0: { | ||
153 | */ | ||
154 | if (( sd = ::open( dspstr, flags)) == -1) { | ||
155 | perror("open(\"/dev/dsp\")"); | ||
156 | QString errorMsg="Could not open audio device\n /dev/dsp\n" | ||
157 | +(QString)strerror(errno); | ||
158 | qDebug(errorMsg); | ||
159 | return -1; | ||
160 | } | ||
161 | |||
162 | int mixerHandle=0; | ||
163 | /* Set the input dsp device and its input gain the weird Zaurus way */ | ||
164 | if (( mixerHandle = open("/dev/mixer1",O_RDWR))<0) { | ||
165 | perror("open(\"/dev/mixer1\")"); | ||
166 | } | ||
167 | |||
168 | if(ioctl(sd,SNDCTL_DSP_RESET,0)<0){ | ||
169 | perror("ioctl RESET"); | ||
170 | } | ||
171 | // sprintf(message, "%d", sd); | ||
172 | |||
173 | /* QFile f1("/pid"); | ||
174 | f1.open(IO_WriteOnly ); | ||
175 | f1.writeBlock(message, strlen(message)); | ||
176 | f1.close(); | ||
177 | */ | ||
178 | /* close(pipefd[0]); | ||
179 | write(pipefd[1], message, sizeof(message)); | ||
180 | close(pipefd[1]); | ||
181 | // qDebug("%d",soundDevice->sd ); | ||
182 | _exit(0); | ||
183 | } | ||
184 | default: | ||
185 | // pid greater than zero is parent getting the child's pid | ||
186 | printf("Child's pid is %d\n",pid); | ||
187 | QString s; | ||
188 | close(pipefd[1]); | ||
189 | read(pipefd[0], message, sizeof(message)); | ||
190 | s = message; | ||
191 | close(pipefd[0]); | ||
192 | |||
193 | // while(wait(NULL)!=pid) | ||
194 | // ; | ||
195 | printf("child %ld terminated normally, return status is zero\n", (long) pid); | ||
196 | */ | ||
197 | // filePara.sd=(long) pid; | ||
198 | /* QFile f2("/pid"); | ||
199 | f2.open(IO_ReadOnly); | ||
200 | QTextStream t(&f2); | ||
201 | // for(int f=0; f < t.atEnd() ;f++) { | ||
202 | s = t.readLine(); | ||
203 | // } | ||
204 | */ | ||
205 | // bool ok; | ||
206 | // sd = s.toInt(&ok, 10); | ||
207 | // qDebug("<<<<<<<<<<<<<>>>>>>>>>>>>"+s); | ||
208 | |||
209 | // f2.close(); | ||
210 | // } | ||
211 | ::close(mixerHandle ); | ||
212 | qDebug("open device %s", dspstr); | ||
213 | qDebug("success! %d",sd); | ||
214 | return sd; | ||
215 | } | ||
216 | |||
217 | bool Device::closeDevice( bool b) { | ||
218 | // if(b) {//close now | ||
219 | // if (ioctl( sd, SNDCTL_DSP_RESET, 0) == -1) { | ||
220 | // perror("ioctl(\"SNDCTL_DSP_RESET\")"); | ||
221 | // } | ||
222 | // } else { //let play | ||
223 | // if (ioctl( sd, SNDCTL_DSP_SYNC, 0) == -1) { | ||
224 | // perror("ioctl(\"SNDCTL_DSP_SYNC\")"); | ||
225 | // } | ||
226 | // } | ||
227 | |||
228 | ::close( sd); //close sound device | ||
229 | // sdfd=0; | ||
230 | // sd=0; | ||
231 | qDebug("closed dsp"); | ||
232 | return true; | ||
233 | } | ||
234 | |||
235 | bool Device::setDeviceFormat( int form) { | ||
236 | qDebug("set device res %d %d", form, sd); | ||
237 | if (ioctl( sd, SNDCTL_DSP_SETFMT, &form)==-1) { //set format | ||
238 | perror("ioctl(\"SNDCTL_DSP_SETFMT\")"); | ||
239 | return false; | ||
240 | } | ||
241 | devRes=form; | ||
242 | return true; | ||
243 | } | ||
244 | |||
245 | bool Device::setDeviceChannels( int ch) { | ||
246 | qDebug("set channels %d %d", ch, sd); | ||
247 | if (ioctl( sd, SNDCTL_DSP_CHANNELS, &ch)==-1) { | ||
248 | perror("ioctl(\"SNDCTL_DSP_CHANNELS\")"); | ||
249 | return false; | ||
250 | } | ||
251 | devCh=ch; | ||
252 | return true; | ||
253 | } | ||
254 | |||
255 | bool Device::setDeviceRate( int rate) { | ||
256 | qDebug("set rate %d %d", rate, sd); | ||
257 | if (ioctl( sd, SNDCTL_DSP_SPEED, &rate) == -1) { | ||
258 | perror("ioctl(\"SNDCTL_DSP_SPEED\")"); | ||
259 | return false; | ||
260 | } | ||
261 | |||
262 | devRate=rate; | ||
263 | |||
264 | return true; | ||
265 | } | ||
266 | |||
267 | int Device::getRes() { | ||
268 | return devRes; | ||
269 | } | ||
270 | |||
271 | int Device::getFormat() { | ||
272 | return devForm; | ||
273 | } | ||
274 | |||
275 | int Device::getRate() { | ||
276 | return devRate; | ||
277 | } | ||
278 | |||
279 | int Device::getChannels() { | ||
280 | return devCh; | ||
281 | } | ||
282 | |||
283 | int Device::getDeviceFormat() { | ||
284 | return 0; | ||
285 | } | ||
286 | |||
287 | |||
288 | int Device::getDeviceRate() { | ||
289 | int dRate=0; | ||
290 | if (ioctl( sd, SOUND_PCM_READ_RATE, &dRate) == -1) { | ||
291 | perror("ioctl(\"SNDCTL_PCM_READ_RATE\")"); | ||
292 | } | ||
293 | return dRate; | ||
294 | |||
295 | } | ||
296 | |||
297 | int Device::getDeviceBits() { | ||
298 | int dBits=0; | ||
299 | #ifndef QT_QWS_EBX // zaurus doesnt have this | ||
300 | if (ioctl( sd, SOUND_PCM_READ_BITS, &dBits) == -1) { | ||
301 | perror("ioctl(\"SNDCTL_PCM_READ_BITS\")"); | ||
302 | } | ||
303 | #endif | ||
304 | return dBits; | ||
305 | } | ||
306 | |||
307 | int Device::getDeviceChannels() { | ||
308 | int dCh=0; | ||
309 | if (ioctl( sd, SOUND_PCM_READ_CHANNELS, &dCh) == -1) { | ||
310 | perror("ioctl(\"SNDCTL_PCM_READ_CHANNELS\")"); | ||
311 | } | ||
312 | return dCh; | ||
313 | } | ||
314 | |||
315 | int Device::getDeviceFragSize() { | ||
316 | int frag_size; | ||
317 | |||
318 | if (ioctl( sd, SNDCTL_DSP_GETBLKSIZE, &frag_size) == -1) { | ||
319 | qDebug("no fragsize"); | ||
320 | } else | ||
321 | qDebug("driver says frag size is %d", frag_size); | ||
322 | return frag_size; | ||
323 | } | ||
324 | |||
325 | bool Device::setFragSize(int frag) { | ||
326 | if (ioctl(sd, SNDCTL_DSP_SETFRAGMENT, &frag)) { | ||
327 | perror("ioctl(\"SNDCTL_DSP_SETFRAGMENT\")"); | ||
328 | return false; | ||
329 | } | ||
330 | return true; | ||
331 | } | ||
332 | |||
333 | bool Device::reset() { | ||
334 | closeDevice(true); | ||
335 | openDsp(); | ||
336 | if (ioctl( sd, SNDCTL_DSP_RESET, 0) == -1) { | ||
337 | perror("ioctl(\"SNDCTL_DSP_RESET\")"); | ||
338 | return false; | ||
339 | } | ||
340 | return true; | ||
341 | } | ||