2 /* =========================== Module Snd =========================== */
8 #define SystemSevenOrLater 1
16 extern PyObject
*ResObj_New(Handle
);
17 extern PyObject
*ResObj_OptNew(Handle
);
18 extern int ResObj_Convert(PyObject
*, Handle
*);
20 extern PyObject
*WinObj_New(WindowPtr
);
21 extern int WinObj_Convert(PyObject
*, WindowPtr
*);
23 extern PyObject
*DlgObj_New(DialogPtr
);
24 extern int DlgObj_Convert(PyObject
*, DialogPtr
*);
25 extern PyTypeObject Dialog_Type
;
26 #define DlgObj_Check(x) ((x)->ob_type == &Dialog_Type)
28 extern PyObject
*MenuObj_New(MenuHandle
);
29 extern int MenuObj_Convert(PyObject
*, MenuHandle
*);
31 extern PyObject
*CtlObj_New(ControlHandle
);
32 extern int CtlObj_Convert(PyObject
*, ControlHandle
*);
34 extern PyObject
*WinObj_WhichWindow(WindowPtr
);
38 #ifndef HAVE_UNIVERSAL_HEADERS
39 #define SndCallBackUPP ProcPtr
40 #define NewSndCallBackProc(x) ((SndCallBackProcPtr)(x))
41 #define SndListHandle Handle
44 #include <OSUtils.h> /* for Set(Current)A5 */
46 /* Create a SndCommand object (an (int, int, int) tuple) */
48 SndCmd_New(SndCommand
*pc
)
50 return Py_BuildValue("hhl", pc
->cmd
, pc
->param1
, pc
->param2
);
53 /* Convert a SndCommand argument */
55 SndCmd_Convert(PyObject
*v
, SndCommand
*pc
)
60 if (PyTuple_Check(v
)) {
61 if (PyArg_ParseTuple(v
, "h|hl", &pc
->cmd
, &pc
->param1
, &pc
->param2
))
64 return PyArg_ParseTuple(v
, "hhs#", &pc
->cmd
, &pc
->param1
, &pc
->param2
, &len
);
66 return PyArg_Parse(v
, "h", &pc
->cmd
);
69 /* Create a NumVersion object (a quintuple of integers) */
71 NumVer_New(NumVersion nv
)
73 return Py_BuildValue("iiiii",
79 (nv
.minorAndBugRev
>>4) & 0xf,
80 nv
.minorAndBugRev
& 0xf,
86 static pascal void SndCh_UserRoutine(SndChannelPtr chan
, SndCommand
*cmd
); /* Forward */
88 static PyObject
*Snd_Error
;
90 /* --------------------- Object type SndChannel --------------------- */
92 staticforward PyTypeObject SndChannel_Type
;
94 #define SndCh_Check(x) ((x)->ob_type == &SndChannel_Type)
96 typedef struct SndChannelObject
{
98 SndChannelPtr ob_itself
;
99 /* Members used to implement callbacks: */
100 PyObject
*ob_callback
;
105 static PyObject
*SndCh_New(itself
)
106 SndChannelPtr itself
;
108 SndChannelObject
*it
;
109 it
= PyObject_NEW(SndChannelObject
, &SndChannel_Type
);
110 if (it
== NULL
) return NULL
;
111 it
->ob_itself
= itself
;
112 it
->ob_callback
= NULL
;
113 it
->ob_A5
= SetCurrentA5();
114 return (PyObject
*)it
;
116 static SndCh_Convert(v
, p_itself
)
118 SndChannelPtr
*p_itself
;
122 PyErr_SetString(PyExc_TypeError
, "SndChannel required");
125 *p_itself
= ((SndChannelObject
*)v
)->ob_itself
;
129 static void SndCh_dealloc(self
)
130 SndChannelObject
*self
;
132 SndDisposeChannel(self
->ob_itself
, 1);
133 Py_XDECREF(self
->ob_callback
);
137 static PyObject
*SndCh_SndDoCommand(_self
, _args
)
138 SndChannelObject
*_self
;
141 PyObject
*_res
= NULL
;
145 if (!PyArg_ParseTuple(_args
, "O&b",
146 SndCmd_Convert
, &cmd
,
149 _err
= SndDoCommand(_self
->ob_itself
,
152 if (_err
!= noErr
) return PyMac_Error(_err
);
158 static PyObject
*SndCh_SndDoImmediate(_self
, _args
)
159 SndChannelObject
*_self
;
162 PyObject
*_res
= NULL
;
165 if (!PyArg_ParseTuple(_args
, "O&",
166 SndCmd_Convert
, &cmd
))
168 _err
= SndDoImmediate(_self
->ob_itself
,
170 if (_err
!= noErr
) return PyMac_Error(_err
);
176 static PyObject
*SndCh_SndPlay(_self
, _args
)
177 SndChannelObject
*_self
;
180 PyObject
*_res
= NULL
;
182 SndListHandle sndHdl
;
184 if (!PyArg_ParseTuple(_args
, "O&b",
185 ResObj_Convert
, &sndHdl
,
188 _err
= SndPlay(_self
->ob_itself
,
191 if (_err
!= noErr
) return PyMac_Error(_err
);
197 static PyObject
*SndCh_SndStartFilePlay(_self
, _args
)
198 SndChannelObject
*_self
;
201 PyObject
*_res
= NULL
;
207 if (!PyArg_ParseTuple(_args
, "hhlb",
213 _err
= SndStartFilePlay(_self
->ob_itself
,
221 if (_err
!= noErr
) return PyMac_Error(_err
);
227 static PyObject
*SndCh_SndPauseFilePlay(_self
, _args
)
228 SndChannelObject
*_self
;
231 PyObject
*_res
= NULL
;
233 if (!PyArg_ParseTuple(_args
, ""))
235 _err
= SndPauseFilePlay(_self
->ob_itself
);
236 if (_err
!= noErr
) return PyMac_Error(_err
);
242 static PyObject
*SndCh_SndStopFilePlay(_self
, _args
)
243 SndChannelObject
*_self
;
246 PyObject
*_res
= NULL
;
249 if (!PyArg_ParseTuple(_args
, "b",
252 _err
= SndStopFilePlay(_self
->ob_itself
,
254 if (_err
!= noErr
) return PyMac_Error(_err
);
260 static PyObject
*SndCh_SndChannelStatus(_self
, _args
)
261 SndChannelObject
*_self
;
264 PyObject
*_res
= NULL
;
267 SCStatus theStatus__out__
;
268 if (!PyArg_ParseTuple(_args
, "h",
271 _err
= SndChannelStatus(_self
->ob_itself
,
274 if (_err
!= noErr
) return PyMac_Error(_err
);
275 _res
= Py_BuildValue("s#",
276 (char *)&theStatus__out__
, (int)sizeof(SCStatus
));
277 theStatus__error__
: ;
281 static PyMethodDef SndCh_methods
[] = {
282 {"SndDoCommand", (PyCFunction
)SndCh_SndDoCommand
, 1,
283 "(SndCommand cmd, Boolean noWait) -> None"},
284 {"SndDoImmediate", (PyCFunction
)SndCh_SndDoImmediate
, 1,
285 "(SndCommand cmd) -> None"},
286 {"SndPlay", (PyCFunction
)SndCh_SndPlay
, 1,
287 "(SndListHandle sndHdl, Boolean async) -> None"},
288 {"SndStartFilePlay", (PyCFunction
)SndCh_SndStartFilePlay
, 1,
289 "(short fRefNum, short resNum, long bufferSize, Boolean async) -> None"},
290 {"SndPauseFilePlay", (PyCFunction
)SndCh_SndPauseFilePlay
, 1,
292 {"SndStopFilePlay", (PyCFunction
)SndCh_SndStopFilePlay
, 1,
293 "(Boolean quietNow) -> None"},
294 {"SndChannelStatus", (PyCFunction
)SndCh_SndChannelStatus
, 1,
295 "(short theLength) -> (SCStatus theStatus)"},
299 static PyMethodChain SndCh_chain
= { SndCh_methods
, NULL
};
301 static PyObject
*SndCh_getattr(self
, name
)
302 SndChannelObject
*self
;
305 return Py_FindMethodInChain(&SndCh_chain
, (PyObject
*)self
, name
);
308 #define SndCh_setattr NULL
310 staticforward PyTypeObject SndChannel_Type
= {
311 PyObject_HEAD_INIT(&PyType_Type
)
313 "SndChannel", /*tp_name*/
314 sizeof(SndChannelObject
), /*tp_basicsize*/
317 (destructor
) SndCh_dealloc
, /*tp_dealloc*/
319 (getattrfunc
) SndCh_getattr
, /*tp_getattr*/
320 (setattrfunc
) SndCh_setattr
, /*tp_setattr*/
323 /* ------------------- End object type SndChannel ------------------- */
326 static PyObject
*Snd_SetSoundVol(_self
, _args
)
330 PyObject
*_res
= NULL
;
332 if (!PyArg_ParseTuple(_args
, "h",
341 static PyObject
*Snd_GetSoundVol(_self
, _args
)
345 PyObject
*_res
= NULL
;
347 if (!PyArg_ParseTuple(_args
, ""))
350 _res
= Py_BuildValue("h",
355 static PyObject
*Snd_SndNewChannel(_self
, _args
)
359 PyObject
*_res
= NULL
;
361 SndChannelPtr chan
= 0;
364 PyObject
* userRoutine
;
365 if (!PyArg_ParseTuple(_args
, "hlO",
370 if (userRoutine
!= Py_None
&& !PyCallable_Check(userRoutine
))
372 PyErr_SetString(PyExc_TypeError
, "callback must be callable");
373 goto userRoutine__error__
;
375 _err
= SndNewChannel(&chan
,
378 NewSndCallBackProc(SndCh_UserRoutine
));
379 if (_err
!= noErr
) return PyMac_Error(_err
);
380 _res
= Py_BuildValue("O&",
382 if (_res
!= NULL
&& userRoutine
!= Py_None
)
384 SndChannelObject
*p
= (SndChannelObject
*)_res
;
385 p
->ob_itself
->userInfo
= (long)p
;
386 Py_INCREF(userRoutine
);
387 p
->ob_callback
= userRoutine
;
389 userRoutine__error__
: ;
393 static PyObject
*Snd_SndControl(_self
, _args
)
397 PyObject
*_res
= NULL
;
401 if (!PyArg_ParseTuple(_args
, "h",
404 _err
= SndControl(id
,
406 if (_err
!= noErr
) return PyMac_Error(_err
);
407 _res
= Py_BuildValue("O&",
412 static PyObject
*Snd_SndSoundManagerVersion(_self
, _args
)
416 PyObject
*_res
= NULL
;
418 if (!PyArg_ParseTuple(_args
, ""))
420 _rv
= SndSoundManagerVersion();
421 _res
= Py_BuildValue("O&",
426 static PyObject
*Snd_SndManagerStatus(_self
, _args
)
430 PyObject
*_res
= NULL
;
433 SMStatus theStatus__out__
;
434 if (!PyArg_ParseTuple(_args
, "h",
437 _err
= SndManagerStatus(theLength
,
439 if (_err
!= noErr
) return PyMac_Error(_err
);
440 _res
= Py_BuildValue("s#",
441 (char *)&theStatus__out__
, (int)sizeof(SMStatus
));
442 theStatus__error__
: ;
446 static PyObject
*Snd_SndGetSysBeepState(_self
, _args
)
450 PyObject
*_res
= NULL
;
452 if (!PyArg_ParseTuple(_args
, ""))
454 SndGetSysBeepState(&sysBeepState
);
455 _res
= Py_BuildValue("h",
460 static PyObject
*Snd_SndSetSysBeepState(_self
, _args
)
464 PyObject
*_res
= NULL
;
467 if (!PyArg_ParseTuple(_args
, "h",
470 _err
= SndSetSysBeepState(sysBeepState
);
471 if (_err
!= noErr
) return PyMac_Error(_err
);
477 static PyObject
*Snd_MACEVersion(_self
, _args
)
481 PyObject
*_res
= NULL
;
483 if (!PyArg_ParseTuple(_args
, ""))
486 _res
= Py_BuildValue("O&",
491 static PyObject
*Snd_Comp3to1(_self
, _args
)
495 PyObject
*_res
= NULL
;
499 int buffer__in_len__
;
500 StateBlock
*state__in__
;
501 StateBlock state__out__
;
503 unsigned long numChannels
;
504 unsigned long whichChannel
;
505 if (!PyArg_ParseTuple(_args
, "s#s#ll",
506 &buffer__in__
, &buffer__in_len__
,
507 (char **)&state__in__
, &state__in_len__
,
511 if ((buffer__out__
= malloc(buffer__in_len__
)) == NULL
)
514 goto buffer__error__
;
516 buffer__len__
= buffer__in_len__
;
517 if (state__in_len__
!= sizeof(StateBlock
))
519 PyErr_SetString(PyExc_TypeError
, "buffer length should be sizeof(StateBlock)");
522 Comp3to1(buffer__in__
, buffer__out__
, (long)buffer__len__
,
523 state__in__
, &state__out__
,
526 _res
= Py_BuildValue("s#s#",
527 buffer__out__
, (int)buffer__len__
,
528 (char *)&state__out__
, (int)sizeof(StateBlock
));
535 static PyObject
*Snd_Exp1to3(_self
, _args
)
539 PyObject
*_res
= NULL
;
543 int buffer__in_len__
;
544 StateBlock
*state__in__
;
545 StateBlock state__out__
;
547 unsigned long numChannels
;
548 unsigned long whichChannel
;
549 if (!PyArg_ParseTuple(_args
, "s#s#ll",
550 &buffer__in__
, &buffer__in_len__
,
551 (char **)&state__in__
, &state__in_len__
,
555 if ((buffer__out__
= malloc(buffer__in_len__
)) == NULL
)
558 goto buffer__error__
;
560 buffer__len__
= buffer__in_len__
;
561 if (state__in_len__
!= sizeof(StateBlock
))
563 PyErr_SetString(PyExc_TypeError
, "buffer length should be sizeof(StateBlock)");
566 Exp1to3(buffer__in__
, buffer__out__
, (long)buffer__len__
,
567 state__in__
, &state__out__
,
570 _res
= Py_BuildValue("s#s#",
571 buffer__out__
, (int)buffer__len__
,
572 (char *)&state__out__
, (int)sizeof(StateBlock
));
579 static PyObject
*Snd_Comp6to1(_self
, _args
)
583 PyObject
*_res
= NULL
;
587 int buffer__in_len__
;
588 StateBlock
*state__in__
;
589 StateBlock state__out__
;
591 unsigned long numChannels
;
592 unsigned long whichChannel
;
593 if (!PyArg_ParseTuple(_args
, "s#s#ll",
594 &buffer__in__
, &buffer__in_len__
,
595 (char **)&state__in__
, &state__in_len__
,
599 if ((buffer__out__
= malloc(buffer__in_len__
)) == NULL
)
602 goto buffer__error__
;
604 buffer__len__
= buffer__in_len__
;
605 if (state__in_len__
!= sizeof(StateBlock
))
607 PyErr_SetString(PyExc_TypeError
, "buffer length should be sizeof(StateBlock)");
610 Comp6to1(buffer__in__
, buffer__out__
, (long)buffer__len__
,
611 state__in__
, &state__out__
,
614 _res
= Py_BuildValue("s#s#",
615 buffer__out__
, (int)buffer__len__
,
616 (char *)&state__out__
, (int)sizeof(StateBlock
));
623 static PyObject
*Snd_Exp1to6(_self
, _args
)
627 PyObject
*_res
= NULL
;
631 int buffer__in_len__
;
632 StateBlock
*state__in__
;
633 StateBlock state__out__
;
635 unsigned long numChannels
;
636 unsigned long whichChannel
;
637 if (!PyArg_ParseTuple(_args
, "s#s#ll",
638 &buffer__in__
, &buffer__in_len__
,
639 (char **)&state__in__
, &state__in_len__
,
643 if ((buffer__out__
= malloc(buffer__in_len__
)) == NULL
)
646 goto buffer__error__
;
648 buffer__len__
= buffer__in_len__
;
649 if (state__in_len__
!= sizeof(StateBlock
))
651 PyErr_SetString(PyExc_TypeError
, "buffer length should be sizeof(StateBlock)");
654 Exp1to6(buffer__in__
, buffer__out__
, (long)buffer__len__
,
655 state__in__
, &state__out__
,
658 _res
= Py_BuildValue("s#s#",
659 buffer__out__
, (int)buffer__len__
,
660 (char *)&state__out__
, (int)sizeof(StateBlock
));
667 static PyObject
*Snd_GetSysBeepVolume(_self
, _args
)
671 PyObject
*_res
= NULL
;
674 if (!PyArg_ParseTuple(_args
, ""))
676 _err
= GetSysBeepVolume(&level
);
677 if (_err
!= noErr
) return PyMac_Error(_err
);
678 _res
= Py_BuildValue("l",
683 static PyObject
*Snd_SetSysBeepVolume(_self
, _args
)
687 PyObject
*_res
= NULL
;
690 if (!PyArg_ParseTuple(_args
, "l",
693 _err
= SetSysBeepVolume(level
);
694 if (_err
!= noErr
) return PyMac_Error(_err
);
700 static PyObject
*Snd_GetDefaultOutputVolume(_self
, _args
)
704 PyObject
*_res
= NULL
;
707 if (!PyArg_ParseTuple(_args
, ""))
709 _err
= GetDefaultOutputVolume(&level
);
710 if (_err
!= noErr
) return PyMac_Error(_err
);
711 _res
= Py_BuildValue("l",
716 static PyObject
*Snd_SetDefaultOutputVolume(_self
, _args
)
720 PyObject
*_res
= NULL
;
723 if (!PyArg_ParseTuple(_args
, "l",
726 _err
= SetDefaultOutputVolume(level
);
727 if (_err
!= noErr
) return PyMac_Error(_err
);
733 static PyObject
*Snd_GetSoundHeaderOffset(_self
, _args
)
737 PyObject
*_res
= NULL
;
739 SndListHandle sndHandle
;
741 if (!PyArg_ParseTuple(_args
, "O&",
742 ResObj_Convert
, &sndHandle
))
744 _err
= GetSoundHeaderOffset(sndHandle
,
746 if (_err
!= noErr
) return PyMac_Error(_err
);
747 _res
= Py_BuildValue("l",
752 static PyMethodDef Snd_methods
[] = {
753 {"SetSoundVol", (PyCFunction
)Snd_SetSoundVol
, 1,
754 "(short level) -> None"},
755 {"GetSoundVol", (PyCFunction
)Snd_GetSoundVol
, 1,
756 "() -> (short level)"},
757 {"SndNewChannel", (PyCFunction
)Snd_SndNewChannel
, 1,
758 "(short synth, long init, PyObject* userRoutine) -> (SndChannelPtr chan)"},
759 {"SndControl", (PyCFunction
)Snd_SndControl
, 1,
760 "(short id) -> (SndCommand cmd)"},
761 {"SndSoundManagerVersion", (PyCFunction
)Snd_SndSoundManagerVersion
, 1,
762 "() -> (NumVersion _rv)"},
763 {"SndManagerStatus", (PyCFunction
)Snd_SndManagerStatus
, 1,
764 "(short theLength) -> (SMStatus theStatus)"},
765 {"SndGetSysBeepState", (PyCFunction
)Snd_SndGetSysBeepState
, 1,
766 "() -> (short sysBeepState)"},
767 {"SndSetSysBeepState", (PyCFunction
)Snd_SndSetSysBeepState
, 1,
768 "(short sysBeepState) -> None"},
769 {"MACEVersion", (PyCFunction
)Snd_MACEVersion
, 1,
770 "() -> (NumVersion _rv)"},
771 {"Comp3to1", (PyCFunction
)Snd_Comp3to1
, 1,
772 "(Buffer buffer, StateBlock state, unsigned long numChannels, unsigned long whichChannel) -> (Buffer buffer, StateBlock state)"},
773 {"Exp1to3", (PyCFunction
)Snd_Exp1to3
, 1,
774 "(Buffer buffer, StateBlock state, unsigned long numChannels, unsigned long whichChannel) -> (Buffer buffer, StateBlock state)"},
775 {"Comp6to1", (PyCFunction
)Snd_Comp6to1
, 1,
776 "(Buffer buffer, StateBlock state, unsigned long numChannels, unsigned long whichChannel) -> (Buffer buffer, StateBlock state)"},
777 {"Exp1to6", (PyCFunction
)Snd_Exp1to6
, 1,
778 "(Buffer buffer, StateBlock state, unsigned long numChannels, unsigned long whichChannel) -> (Buffer buffer, StateBlock state)"},
779 {"GetSysBeepVolume", (PyCFunction
)Snd_GetSysBeepVolume
, 1,
780 "() -> (long level)"},
781 {"SetSysBeepVolume", (PyCFunction
)Snd_SetSysBeepVolume
, 1,
782 "(long level) -> None"},
783 {"GetDefaultOutputVolume", (PyCFunction
)Snd_GetDefaultOutputVolume
, 1,
784 "() -> (long level)"},
785 {"SetDefaultOutputVolume", (PyCFunction
)Snd_SetDefaultOutputVolume
, 1,
786 "(long level) -> None"},
787 {"GetSoundHeaderOffset", (PyCFunction
)Snd_GetSoundHeaderOffset
, 1,
788 "(SndListHandle sndHandle) -> (long offset)"},
794 /* Routine passed to Py_AddPendingCall -- call the Python callback */
796 SndCh_CallCallBack(arg
)
799 SndChannelObject
*p
= (SndChannelObject
*)arg
;
802 args
= Py_BuildValue("(O(hhl))",
803 p
, p
->ob_cmd
.cmd
, p
->ob_cmd
.param1
, p
->ob_cmd
.param2
);
804 res
= PyEval_CallObject(p
->ob_callback
, args
);
812 /* Routine passed to NewSndChannel -- schedule a call to SndCh_CallCallBack */
814 SndCh_UserRoutine(SndChannelPtr chan
, SndCommand
*cmd
)
816 SndChannelObject
*p
= (SndChannelObject
*)(chan
->userInfo
);
817 if (p
->ob_callback
!= NULL
) {
818 long A5
= SetA5(p
->ob_A5
);
820 Py_AddPendingCall(SndCh_CallCallBack
, (void *)p
);
835 m
= Py_InitModule("Snd", Snd_methods
);
836 d
= PyModule_GetDict(m
);
837 Snd_Error
= PyMac_GetOSErrException();
838 if (Snd_Error
== NULL
||
839 PyDict_SetItemString(d
, "Error", Snd_Error
) != 0)
840 Py_FatalError("can't initialize Snd.Error");
843 /* ========================= End module Snd ========================= */