- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathossaudiodev.c
1135 lines (976 loc) · 31.8 KB
/
ossaudiodev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ossaudiodev -- Python interface to the OSS (Open Sound System) API.
* This is the standard audio API for Linux and some
* flavours of BSD [XXX which ones?]; it is also available
* for a wide range of commercial Unices.
*
* Originally written by Peter Bosch, March 2000, as linuxaudiodev.
*
* Renamed to ossaudiodev and rearranged/revised/hacked up
* by Greg Ward <gward@python.net>, November 2002.
* Mixer interface by Nicholas FitzRoy-Dale <wzdd@lardcave.net>, Dec 2002.
*
* (c) 2000 Peter Bosch. All Rights Reserved.
* (c) 2002 Gregory P. Ward. All Rights Reserved.
* (c) 2002 Python Software Foundation. All Rights Reserved.
*
* XXX need a license statement
*
* $Id$
*/
#include"Python.h"
#include"structmember.h"
#ifdefHAVE_FCNTL_H
#include<fcntl.h>
#else
#defineO_RDONLY 00
#defineO_WRONLY 01
#endif
#include<sys/ioctl.h>
#include<sys/soundcard.h>
#if defined(linux)
#ifndefHAVE_STDINT_H
typedefunsigned longuint32_t;
#endif
#elif defined(__FreeBSD__)
# ifndefSNDCTL_DSP_CHANNELS
# defineSNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
# endif
#endif
typedefstruct {
PyObject_HEAD
char*devicename; /* name of the device file */
intfd; /* file descriptor */
intmode; /* file mode (O_RDONLY, etc.) */
inticount; /* input count */
intocount; /* output count */
uint32_tafmts; /* audio formats supported by hardware */
} oss_audio_t;
typedefstruct {
PyObject_HEAD
intfd; /* The open mixer device */
} oss_mixer_t;
staticPyTypeObjectOSSAudioType;
staticPyTypeObjectOSSMixerType;
staticPyObject*OSSAudioError;
/* ----------------------------------------------------------------------
* DSP object initialization/deallocation
*/
staticoss_audio_t*
newossobject(PyObject*arg)
{
oss_audio_t*self;
intfd, afmts, imode;
char*devicename=NULL;
char*mode=NULL;
/* Two ways to call open():
open(device, mode) (for consistency with builtin open())
open(mode) (for backwards compatibility)
because the *first* argument is optional, parsing args is
a wee bit tricky. */
if (!PyArg_ParseTuple(arg, "s|s:open", &devicename, &mode))
returnNULL;
if (mode==NULL) { /* only one arg supplied */
mode=devicename;
devicename=NULL;
}
if (strcmp(mode, "r") ==0)
imode=O_RDONLY;
elseif (strcmp(mode, "w") ==0)
imode=O_WRONLY;
elseif (strcmp(mode, "rw") ==0)
imode=O_RDWR;
else {
PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'");
returnNULL;
}
/* Open the correct device: either the 'device' argument,
or the AUDIODEV environment variable, or "/dev/dsp". */
if (devicename==NULL) { /* called with one arg */
devicename=getenv("AUDIODEV");
if (devicename==NULL) /* $AUDIODEV not set */
devicename="/dev/dsp";
}
/* Open with O_NONBLOCK to avoid hanging on devices that only allow
one open at a time. This does *not* affect later I/O; OSS
provides a special ioctl() for non-blocking read/write, which is
exposed via oss_nonblock() below. */
if ((fd=open(devicename, imode|O_NONBLOCK)) ==-1) {
PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
returnNULL;
}
/* And (try to) put it back in blocking mode so we get the
expected write() semantics. */
if (fcntl(fd, F_SETFL, 0) ==-1) {
close(fd);
PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
returnNULL;
}
if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) ==-1) {
close(fd);
PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
returnNULL;
}
/* Create and initialize the object */
if ((self=PyObject_New(oss_audio_t, &OSSAudioType)) ==NULL) {
close(fd);
returnNULL;
}
self->devicename=devicename;
self->fd=fd;
self->mode=imode;
self->icount=self->ocount=0;
self->afmts=afmts;
returnself;
}
staticvoid
oss_dealloc(oss_audio_t*self)
{
/* if already closed, don't reclose it */
if (self->fd!=-1)
close(self->fd);
PyObject_Del(self);
}
/* ----------------------------------------------------------------------
* Mixer object initialization/deallocation
*/
staticoss_mixer_t*
newossmixerobject(PyObject*arg)
{
char*devicename=NULL;
intfd;
oss_mixer_t*self;
if (!PyArg_ParseTuple(arg, "|s", &devicename)) {
returnNULL;
}
if (devicename==NULL) {
devicename=getenv("MIXERDEV");
if (devicename==NULL) /* MIXERDEV not set */
devicename="/dev/mixer";
}
if ((fd=open(devicename, O_RDWR)) ==-1) {
PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
returnNULL;
}
if ((self=PyObject_New(oss_mixer_t, &OSSMixerType)) ==NULL) {
close(fd);
returnNULL;
}
self->fd=fd;
returnself;
}
staticvoid
oss_mixer_dealloc(oss_mixer_t*self)
{
/* if already closed, don't reclose it */
if (self->fd!=-1)
close(self->fd);
PyObject_Del(self);
}
/* Methods to wrap the OSS ioctls. The calling convention is pretty
simple:
nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
etc.
*/
/* ----------------------------------------------------------------------
* Helper functions
*/
/* _do_ioctl_1() is a private helper function used for the OSS ioctls --
SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that are called from C
like this:
ioctl(fd, SNDCTL_DSP_cmd, &arg)
where arg is the value to set, and on return the driver sets arg to
the value that was actually set. Mapping this to Python is obvious:
arg = dsp.xxx(arg)
*/
staticPyObject*
_do_ioctl_1(intfd, PyObject*args, char*fname, intcmd)
{
charargfmt[33] ="i:";
intarg;
assert(strlen(fname) <= 30);
strcat(argfmt, fname);
if (!PyArg_ParseTuple(args, argfmt, &arg))
returnNULL;
if (ioctl(fd, cmd, &arg) ==-1)
returnPyErr_SetFromErrno(PyExc_IOError);
returnPyInt_FromLong(arg);
}
/* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
but return an output -- ie. we need to pass a pointer to a local C
variable so the driver can write its output there, but from Python
all we see is the return value. For example,
SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
devices, but does not use the value of the parameter passed-in in any
way.
*/
staticPyObject*
_do_ioctl_1_internal(intfd, PyObject*args, char*fname, intcmd)
{
charargfmt[32] =":";
intarg=0;
assert(strlen(fname) <= 30);
strcat(argfmt, fname);
if (!PyArg_ParseTuple(args, argfmt, &arg))
returnNULL;
if (ioctl(fd, cmd, &arg) ==-1)
returnPyErr_SetFromErrno(PyExc_IOError);
returnPyInt_FromLong(arg);
}
/* _do_ioctl_0() is a private helper for the no-argument ioctls:
SNDCTL_DSP_{SYNC,RESET,POST}. */
staticPyObject*
_do_ioctl_0(intfd, PyObject*args, char*fname, intcmd)
{
charargfmt[32] =":";
intrv;
assert(strlen(fname) <= 30);
strcat(argfmt, fname);
if (!PyArg_ParseTuple(args, argfmt))
returnNULL;
/* According to hannu@opensound.com, all three of the ioctls that
use this function can block, so release the GIL. This is
especially important for SYNC, which can block for several
seconds. */
Py_BEGIN_ALLOW_THREADS
rv=ioctl(fd, cmd, 0);
Py_END_ALLOW_THREADS
if (rv==-1)
returnPyErr_SetFromErrno(PyExc_IOError);
Py_INCREF(Py_None);
returnPy_None;
}
/* ----------------------------------------------------------------------
* Methods of DSP objects (OSSAudioType)
*/
staticPyObject*
oss_nonblock(oss_audio_t*self, PyObject*unused)
{
/* Hmmm: it doesn't appear to be possible to return to blocking
mode once we're in non-blocking mode! */
if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) ==-1)
returnPyErr_SetFromErrno(PyExc_IOError);
Py_INCREF(Py_None);
returnPy_None;
}
staticPyObject*
oss_setfmt(oss_audio_t*self, PyObject*args)
{
return_do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
}
staticPyObject*
oss_getfmts(oss_audio_t*self, PyObject*unused)
{
intmask;
if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) ==-1)
returnPyErr_SetFromErrno(PyExc_IOError);
returnPyInt_FromLong(mask);
}
staticPyObject*
oss_channels(oss_audio_t*self, PyObject*args)
{
return_do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
}
staticPyObject*
oss_speed(oss_audio_t*self, PyObject*args)
{
return_do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
}
staticPyObject*
oss_sync(oss_audio_t*self, PyObject*args)
{
return_do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
}
staticPyObject*
oss_reset(oss_audio_t*self, PyObject*args)
{
return_do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
}
staticPyObject*
oss_post(oss_audio_t*self, PyObject*args)
{
return_do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
}
/* Regular file methods: read(), write(), close(), etc. as well
as one convenience method, writeall(). */
staticPyObject*
oss_read(oss_audio_t*self, PyObject*args)
{
intsize, count;
char*cp;
PyObject*rv;
if (!PyArg_ParseTuple(args, "i:read", &size))
returnNULL;
rv=PyString_FromStringAndSize(NULL, size);
if (rv==NULL)
returnNULL;
cp=PyString_AS_STRING(rv);
Py_BEGIN_ALLOW_THREADS
count=read(self->fd, cp, size);
Py_END_ALLOW_THREADS
if (count<0) {
PyErr_SetFromErrno(PyExc_IOError);
Py_DECREF(rv);
returnNULL;
}
self->icount+=count;
_PyString_Resize(&rv, count);
returnrv;
}
staticPyObject*
oss_write(oss_audio_t*self, PyObject*args)
{
char*cp;
intrv, size;
if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
returnNULL;
}
Py_BEGIN_ALLOW_THREADS
rv=write(self->fd, cp, size);
Py_END_ALLOW_THREADS
if (rv==-1) {
returnPyErr_SetFromErrno(PyExc_IOError);
} else {
self->ocount+=rv;
}
returnPyInt_FromLong(rv);
}
staticPyObject*
oss_writeall(oss_audio_t*self, PyObject*args)
{
char*cp;
intrv, size;
fd_setwrite_set_fds;
intselect_rv;
/* NB. writeall() is only useful in non-blocking mode: according to
Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
(http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
write() in blocking mode consumes the whole buffer. In blocking
mode, the behaviour of write() and writeall() from Python is
indistinguishable. */
if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
returnNULL;
if (!_PyIsSelectable_fd(self->fd)) {
PyErr_SetString(PyExc_ValueError,
"file descriptor out of range for select");
returnNULL;
}
/* use select to wait for audio device to be available */
FD_ZERO(&write_set_fds);
FD_SET(self->fd, &write_set_fds);
while (size>0) {
Py_BEGIN_ALLOW_THREADS
select_rv=select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Py_END_ALLOW_THREADS
assert(select_rv!=0); /* no timeout, can't expire */
if (select_rv==-1)
returnPyErr_SetFromErrno(PyExc_IOError);
Py_BEGIN_ALLOW_THREADS
rv=write(self->fd, cp, size);
Py_END_ALLOW_THREADS
if (rv==-1) {
if (errno==EAGAIN) { /* buffer is full, try again */
errno=0;
continue;
} else/* it's a real error */
returnPyErr_SetFromErrno(PyExc_IOError);
} else { /* wrote rv bytes */
self->ocount+=rv;
size-=rv;
cp+=rv;
}
}
Py_INCREF(Py_None);
returnPy_None;
}
staticPyObject*
oss_close(oss_audio_t*self, PyObject*unused)
{
if (self->fd >= 0) {
Py_BEGIN_ALLOW_THREADS
close(self->fd);
Py_END_ALLOW_THREADS
self->fd=-1;
}
Py_INCREF(Py_None);
returnPy_None;
}
staticPyObject*
oss_fileno(oss_audio_t*self, PyObject*unused)
{
returnPyInt_FromLong(self->fd);
}
/* Convenience methods: these generally wrap a couple of ioctls into one
common task. */
staticPyObject*
oss_setparameters(oss_audio_t*self, PyObject*args)
{
intwanted_fmt, wanted_channels, wanted_rate, strict=0;
intfmt, channels, rate;
if (!PyArg_ParseTuple(args, "iii|i:setparameters",
&wanted_fmt, &wanted_channels, &wanted_rate,
&strict))
returnNULL;
fmt=wanted_fmt;
if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) ==-1) {
returnPyErr_SetFromErrno(PyExc_IOError);
}
if (strict&&fmt!=wanted_fmt) {
returnPyErr_Format
(OSSAudioError,
"unable to set requested format (wanted %d, got %d)",
wanted_fmt, fmt);
}
channels=wanted_channels;
if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) ==-1) {
returnPyErr_SetFromErrno(PyExc_IOError);
}
if (strict&&channels!=wanted_channels) {
returnPyErr_Format
(OSSAudioError,
"unable to set requested channels (wanted %d, got %d)",
wanted_channels, channels);
}
rate=wanted_rate;
if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) ==-1) {
returnPyErr_SetFromErrno(PyExc_IOError);
}
if (strict&&rate!=wanted_rate) {
returnPyErr_Format
(OSSAudioError,
"unable to set requested rate (wanted %d, got %d)",
wanted_rate, rate);
}
/* Construct the return value: a (fmt, channels, rate) tuple that
tells what the audio hardware was actually set to. */
returnPy_BuildValue("(iii)", fmt, channels, rate);
}
staticint
_ssize(oss_audio_t*self, int*nchannels, int*ssize)
{
intfmt;
fmt=0;
if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) <0)
return-errno;
switch (fmt) {
caseAFMT_MU_LAW:
caseAFMT_A_LAW:
caseAFMT_U8:
caseAFMT_S8:
*ssize=1; /* 8 bit formats: 1 byte */
break;
caseAFMT_S16_LE:
caseAFMT_S16_BE:
caseAFMT_U16_LE:
caseAFMT_U16_BE:
*ssize=2; /* 16 bit formats: 2 byte */
break;
caseAFMT_MPEG:
caseAFMT_IMA_ADPCM:
default:
return-EOPNOTSUPP;
}
if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) <0)
return-errno;
return0;
}
/* bufsize returns the size of the hardware audio buffer in number
of samples */
staticPyObject*
oss_bufsize(oss_audio_t*self, PyObject*unused)
{
audio_buf_infoai;
intnchannels=0, ssize=0;
if (_ssize(self, &nchannels, &ssize) <0|| !nchannels|| !ssize) {
PyErr_SetFromErrno(PyExc_IOError);
returnNULL;
}
if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) <0) {
PyErr_SetFromErrno(PyExc_IOError);
returnNULL;
}
returnPyInt_FromLong((ai.fragstotal*ai.fragsize) / (nchannels*ssize));
}
/* obufcount returns the number of samples that are available in the
hardware for playing */
staticPyObject*
oss_obufcount(oss_audio_t*self, PyObject*unused)
{
audio_buf_infoai;
intnchannels=0, ssize=0;
if (_ssize(self, &nchannels, &ssize) <0|| !nchannels|| !ssize) {
PyErr_SetFromErrno(PyExc_IOError);
returnNULL;
}
if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) <0) {
PyErr_SetFromErrno(PyExc_IOError);
returnNULL;
}
returnPyInt_FromLong((ai.fragstotal*ai.fragsize-ai.bytes) /
(ssize*nchannels));
}
/* obufcount returns the number of samples that can be played without
blocking */
staticPyObject*
oss_obuffree(oss_audio_t*self, PyObject*unused)
{
audio_buf_infoai;
intnchannels=0, ssize=0;
if (_ssize(self, &nchannels, &ssize) <0|| !nchannels|| !ssize) {
PyErr_SetFromErrno(PyExc_IOError);
returnNULL;
}
if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) <0) {
PyErr_SetFromErrno(PyExc_IOError);
returnNULL;
}
returnPyInt_FromLong(ai.bytes / (ssize*nchannels));
}
staticPyObject*
oss_getptr(oss_audio_t*self, PyObject*unused)
{
count_infoinfo;
intreq;
if (self->mode==O_RDONLY)
req=SNDCTL_DSP_GETIPTR;
else
req=SNDCTL_DSP_GETOPTR;
if (ioctl(self->fd, req, &info) ==-1) {
PyErr_SetFromErrno(PyExc_IOError);
returnNULL;
}
returnPy_BuildValue("iii", info.bytes, info.blocks, info.ptr);
}
/* ----------------------------------------------------------------------
* Methods of mixer objects (OSSMixerType)
*/
staticPyObject*
oss_mixer_close(oss_mixer_t*self, PyObject*unused)
{
if (self->fd >= 0) {
close(self->fd);
self->fd=-1;
}
Py_INCREF(Py_None);
returnPy_None;
}
staticPyObject*
oss_mixer_fileno(oss_mixer_t*self, PyObject*unused)
{
returnPyInt_FromLong(self->fd);
}
/* Simple mixer interface methods */
staticPyObject*
oss_mixer_controls(oss_mixer_t*self, PyObject*args)
{
return_do_ioctl_1_internal(self->fd, args, "controls",
SOUND_MIXER_READ_DEVMASK);
}
staticPyObject*
oss_mixer_stereocontrols(oss_mixer_t*self, PyObject*args)
{
return_do_ioctl_1_internal(self->fd, args, "stereocontrols",
SOUND_MIXER_READ_STEREODEVS);
}
staticPyObject*
oss_mixer_reccontrols(oss_mixer_t*self, PyObject*args)
{
return_do_ioctl_1_internal(self->fd, args, "reccontrols",
SOUND_MIXER_READ_RECMASK);
}
staticPyObject*
oss_mixer_get(oss_mixer_t*self, PyObject*args)
{
intchannel, volume;
/* Can't use _do_ioctl_1 because of encoded arg thingy. */
if (!PyArg_ParseTuple(args, "i:get", &channel))
returnNULL;
if (channel<0||channel>SOUND_MIXER_NRDEVICES) {
PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
returnNULL;
}
if (ioctl(self->fd, MIXER_READ(channel), &volume) ==-1)
returnPyErr_SetFromErrno(PyExc_IOError);
returnPy_BuildValue("(ii)", volume&0xff, (volume&0xff00) >> 8);
}
staticPyObject*
oss_mixer_set(oss_mixer_t*self, PyObject*args)
{
intchannel, volume, leftVol, rightVol;
/* Can't use _do_ioctl_1 because of encoded arg thingy. */
if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
returnNULL;
if (channel<0||channel>SOUND_MIXER_NRDEVICES) {
PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
returnNULL;
}
if (leftVol<0||rightVol<0||leftVol>100||rightVol>100) {
PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
returnNULL;
}
volume= (rightVol << 8) | leftVol;
if (ioctl(self->fd, MIXER_WRITE(channel), &volume) ==-1)
returnPyErr_SetFromErrno(PyExc_IOError);
returnPy_BuildValue("(ii)", volume&0xff, (volume&0xff00) >> 8);
}
staticPyObject*
oss_mixer_get_recsrc(oss_mixer_t*self, PyObject*args)
{
return_do_ioctl_1_internal(self->fd, args, "get_recsrc",
SOUND_MIXER_READ_RECSRC);
}
staticPyObject*
oss_mixer_set_recsrc(oss_mixer_t*self, PyObject*args)
{
return_do_ioctl_1(self->fd, args, "set_recsrc",
SOUND_MIXER_WRITE_RECSRC);
}
/* ----------------------------------------------------------------------
* Method tables and other bureaucracy
*/
staticPyMethodDefoss_methods[] = {
/* Regular file methods */
{ "read", (PyCFunction)oss_read, METH_VARARGS },
{ "write", (PyCFunction)oss_write, METH_VARARGS },
{ "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
{ "close", (PyCFunction)oss_close, METH_NOARGS },
{ "fileno", (PyCFunction)oss_fileno, METH_NOARGS },
/* Simple ioctl wrappers */
{ "nonblock", (PyCFunction)oss_nonblock, METH_NOARGS },
{ "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
{ "getfmts", (PyCFunction)oss_getfmts, METH_NOARGS },
{ "channels", (PyCFunction)oss_channels, METH_VARARGS },
{ "speed", (PyCFunction)oss_speed, METH_VARARGS },
{ "sync", (PyCFunction)oss_sync, METH_VARARGS },
{ "reset", (PyCFunction)oss_reset, METH_VARARGS },
{ "post", (PyCFunction)oss_post, METH_VARARGS },
/* Convenience methods -- wrap a couple of ioctls together */
{ "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
{ "bufsize", (PyCFunction)oss_bufsize, METH_NOARGS },
{ "obufcount", (PyCFunction)oss_obufcount, METH_NOARGS },
{ "obuffree", (PyCFunction)oss_obuffree, METH_NOARGS },
{ "getptr", (PyCFunction)oss_getptr, METH_NOARGS },
/* Aliases for backwards compatibility */
{ "flush", (PyCFunction)oss_sync, METH_VARARGS },
{ NULL, NULL} /* sentinel */
};
staticPyMethodDefoss_mixer_methods[] = {
/* Regular file method - OSS mixers are ioctl-only interface */
{ "close", (PyCFunction)oss_mixer_close, METH_NOARGS },
{ "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS },
/* Simple ioctl wrappers */
{ "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
{ "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
{ "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
{ "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
{ "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
{ "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
{ "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
{ NULL, NULL}
};
staticPyObject*
oss_getattr(oss_audio_t*self, char*name)
{
PyObject*rval=NULL;
if (strcmp(name, "closed") ==0) {
rval= (self->fd==-1) ? Py_True : Py_False;
Py_INCREF(rval);
}
elseif (strcmp(name, "name") ==0) {
rval=PyString_FromString(self->devicename);
}
elseif (strcmp(name, "mode") ==0) {
/* No need for a "default" in this switch: from newossobject(),
self->mode can only be one of these three values. */
switch(self->mode) {
caseO_RDONLY:
rval=PyString_FromString("r");
break;
caseO_RDWR:
rval=PyString_FromString("rw");
break;
caseO_WRONLY:
rval=PyString_FromString("w");
break;
}
}
else {
rval=Py_FindMethod(oss_methods, (PyObject*)self, name);
}
returnrval;
}
staticPyObject*
oss_mixer_getattr(oss_mixer_t*self, char*name)
{
returnPy_FindMethod(oss_mixer_methods, (PyObject*)self, name);
}
staticPyTypeObjectOSSAudioType= {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"ossaudiodev.oss_audio_device", /*tp_name*/
sizeof(oss_audio_t), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)oss_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)oss_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
};
staticPyTypeObjectOSSMixerType= {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"ossaudiodev.oss_mixer_device", /*tp_name*/
sizeof(oss_mixer_t), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)oss_mixer_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)oss_mixer_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
};
staticPyObject*
ossopen(PyObject*self, PyObject*args)
{
return (PyObject*)newossobject(args);
}
staticPyObject*
ossopenmixer(PyObject*self, PyObject*args)
{
return (PyObject*)newossmixerobject(args);
}
staticPyMethodDefossaudiodev_methods[] = {
{ "open", ossopen, METH_VARARGS },
{ "openmixer", ossopenmixer, METH_VARARGS },
{ 0, 0 },
};
#define_EXPORT_INT(mod, name) \
if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
staticchar*control_labels[] =SOUND_DEVICE_LABELS;
staticchar*control_names[] =SOUND_DEVICE_NAMES;
staticint
build_namelists (PyObject*module)
{
PyObject*labels;
PyObject*names;
PyObject*s;
intnum_controls;
inti;
num_controls=sizeof(control_labels) / sizeof(control_labels[0]);
assert(num_controls==sizeof(control_names) / sizeof(control_names[0]));
labels=PyList_New(num_controls);
names=PyList_New(num_controls);
if (labels==NULL||names==NULL)
goto error2;
for (i=0; i<num_controls; i++) {
s=PyString_FromString(control_labels[i]);
if (s==NULL)
goto error2;
PyList_SET_ITEM(labels, i, s);
s=PyString_FromString(control_names[i]);
if (s==NULL)
goto error2;
PyList_SET_ITEM(names, i, s);
}
if (PyModule_AddObject(module, "control_labels", labels) ==-1)
goto error2;
if (PyModule_AddObject(module, "control_names", names) ==-1)
goto error1;
return0;
error2:
Py_XDECREF(labels);
error1:
Py_XDECREF(names);
return-1;
}
void
initossaudiodev(void)
{
PyObject*m;
m=Py_InitModule("ossaudiodev", ossaudiodev_methods);
if (m==NULL)
return;
OSSAudioError=PyErr_NewException("ossaudiodev.OSSAudioError",
NULL, NULL);
if (OSSAudioError) {
/* Each call to PyModule_AddObject decrefs it; compensate: */
Py_INCREF(OSSAudioError);
Py_INCREF(OSSAudioError);
PyModule_AddObject(m, "error", OSSAudioError);
PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
}
/* Build 'control_labels' and 'control_names' lists and add them
to the module. */
if (build_namelists(m) ==-1) /* XXX what to do here? */
return;
/* Expose the audio format numbers -- essential! */
_EXPORT_INT(m, AFMT_QUERY);
_EXPORT_INT(m, AFMT_MU_LAW);
_EXPORT_INT(m, AFMT_A_LAW);
_EXPORT_INT(m, AFMT_IMA_ADPCM);
_EXPORT_INT(m, AFMT_U8);
_EXPORT_INT(m, AFMT_S16_LE);
_EXPORT_INT(m, AFMT_S16_BE);
_EXPORT_INT(m, AFMT_S8);
_EXPORT_INT(m, AFMT_U16_LE);
_EXPORT_INT(m, AFMT_U16_BE);
_EXPORT_INT(m, AFMT_MPEG);
#ifdefAFMT_AC3
_EXPORT_INT(m, AFMT_AC3);
#endif
#ifdefAFMT_S16_NE
_EXPORT_INT(m, AFMT_S16_NE);
#endif
#ifdefAFMT_U16_NE
_EXPORT_INT(m, AFMT_U16_NE);
#endif
#ifdefAFMT_S32_LE
_EXPORT_INT(m, AFMT_S32_LE);
#endif
#ifdefAFMT_S32_BE
_EXPORT_INT(m, AFMT_S32_BE);
#endif
#ifdefAFMT_MPEG
_EXPORT_INT(m, AFMT_MPEG);
#endif
/* Expose the sound mixer device numbers. */
_EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
_EXPORT_INT(m, SOUND_MIXER_VOLUME);
_EXPORT_INT(m, SOUND_MIXER_BASS);
_EXPORT_INT(m, SOUND_MIXER_TREBLE);