1 // ----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2008-2020 Fons Adriaensen <fons@linuxaudio.org>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // ----------------------------------------------------------------------------
19
20
21 #include <Python.h>
22 #include "jplayer.h"
23
24
25 static const char *capslabel = "Jplayer";
26
27
destroy(PyObject * P)28 extern "C" void destroy (PyObject *P)
29 {
30 delete (Jplayer *) PyCapsule_GetPointer (P, capslabel);
31 }
32
33
makecaps(PyObject * self,PyObject * args)34 extern "C" PyObject* makecaps (PyObject *self, PyObject *args)
35 {
36 Jplayer *J;
37 PyObject *P;
38 const char *client_name;
39 const char *server_name;
40 int nchan;
41
42 if (! PyArg_ParseTuple(args, "Oszi", &P, &client_name, &server_name,
43 &nchan)) return NULL;
44 J = new Jplayer (client_name, server_name, nchan);
45 return Py_BuildValue ("NN",
46 PyCapsule_New ((void *) J, capslabel, destroy),
47 PyCapsule_New (dynamic_cast<Jclient *>(J), "Jclient", 0));
48 }
49
50
set_state(PyObject * self,PyObject * args)51 extern "C" PyObject* set_state (PyObject *self, PyObject *args)
52 {
53 Jplayer *J;
54 PyObject *P;
55 int state;
56
57 if (! PyArg_ParseTuple(args, "Oi", &P, &state)) return NULL;
58 J = (Jplayer *) PyCapsule_GetPointer (P, capslabel);
59 J->set_state (state);
60 Py_RETURN_NONE;
61 }
62
63
get_posit(PyObject * self,PyObject * args)64 extern "C" PyObject* get_posit (PyObject *self, PyObject *args)
65 {
66 Jplayer *J;
67 PyObject *P;
68
69 if (! PyArg_ParseTuple(args, "O", &P)) return NULL;
70 J = (Jplayer *) PyCapsule_GetPointer (P, capslabel);
71 return Py_BuildValue ("iL", J->get_state(), J->get_posit());
72 }
73
74
set_posit(PyObject * self,PyObject * args)75 extern "C" PyObject* set_posit (PyObject *self, PyObject *args)
76 {
77 Jplayer *J;
78 PyObject *P;
79 long long posit;
80
81 if (! PyArg_ParseTuple(args, "OL", &P, &posit)) return NULL;
82 J = (Jplayer *) PyCapsule_GetPointer (P, capslabel);
83 J->set_posit (posit);
84 Py_RETURN_NONE;
85 }
86
87
set_gain(PyObject * self,PyObject * args)88 extern "C" PyObject* set_gain (PyObject *self, PyObject *args)
89 {
90 Jplayer *J;
91 PyObject *P;
92 float gain, time;
93
94 if (! PyArg_ParseTuple(args, "Off", &P, &gain, &time)) return NULL;
95 J = (Jplayer *) PyCapsule_GetPointer (P, capslabel);
96 J->set_gain (gain, time);
97 Py_RETURN_NONE;
98 }
99
100
open_file(PyObject * self,PyObject * args)101 extern "C" PyObject* open_file (PyObject *self, PyObject *args)
102 {
103 Jplayer *J;
104 PyObject *P;
105 const char *name;
106
107 if (! PyArg_ParseTuple(args, "Os", &P, &name)) return NULL;
108 J = (Jplayer *) PyCapsule_GetPointer (P, capslabel);
109 return Py_BuildValue ("i", J->open_file(name));
110 }
111
112
close_file(PyObject * self,PyObject * args)113 extern "C" PyObject* close_file (PyObject *self, PyObject *args)
114 {
115 Jplayer *J;
116 PyObject *P;
117
118 if (! PyArg_ParseTuple(args, "O", &P)) return NULL;
119 J = (Jplayer *) PyCapsule_GetPointer (P, capslabel);
120 return Py_BuildValue ("i", J->close_file());
121 }
122
123
get_file_info(PyObject * self,PyObject * args)124 extern "C" PyObject* get_file_info (PyObject *self, PyObject *args)
125 {
126 Jplayer *J;
127 PyObject *P;
128
129 if (! PyArg_ParseTuple(args, "O", &P)) return NULL;
130 J = (Jplayer *) PyCapsule_GetPointer (P, capslabel);
131 return Py_BuildValue ("(iiL)", J->file_chan(), J->file_rate(), J->file_size());
132 }
133
134
135 static PyMethodDef JackPlayerMethods[] =
136 {
137 {"makecaps", makecaps, METH_VARARGS, "Create object capsules."},
138 {"set_state", set_state, METH_VARARGS, "Set new state."},
139 {"get_posit", get_posit, METH_VARARGS, "Get current state and position."},
140 {"set_posit", set_posit, METH_VARARGS, "Locate to new postition."},
141 {"set_gain", set_gain, METH_VARARGS, "Set playback gain."},
142 {"open_file", open_file, METH_VARARGS, "Open an audio file."},
143 {"close_file", close_file, METH_VARARGS, "Close current audio file."},
144 {"get_file_info", get_file_info, METH_VARARGS, "Get (chan, rate, size)."},
145 {NULL, NULL, 0, NULL}
146 };
147
148
149
150 #if PY_VERSION_HEX >= 0x03000000
151
152 static struct PyModuleDef JackPlayerModule =
153 {
154 PyModuleDef_HEAD_INIT,
155 "jackplayer_ext",
156 NULL,
157 -1,
158 JackPlayerMethods
159 };
160
PyInit_jackplayer_ext(void)161 PyMODINIT_FUNC PyInit_jackplayer_ext(void)
162 {
163 return PyModule_Create (&JackPlayerModule);
164 }
165
166 #else
167
initjackplayer_ext(void)168 PyMODINIT_FUNC initjackplayer_ext(void)
169 {
170 (void) Py_InitModule ("jackplayer_ext", JackPlayerMethods);
171 }
172
173 #endif
174