File: control.cc

package info (click to toggle)
alsaplayer 0.99.82-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,892 kB
  • sloc: ansic: 40,741; cpp: 14,400; makefile: 983; sh: 796; lex: 751; asm: 45; python: 29; sed: 16
file content (330 lines) | stat: -rw-r--r-- 8,280 bytes parent folder | download | duplicates (2)
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
/*  control.cc
 *  Copyright (C) 2006 Austin Bingham <austin.bingham@gmail.com>
 *
 *  This file is part of the python bindings for AlsaPlayer.
 *
 *  The python bindings for AlsaPlayer is free software;
 *  you can redistribute it and/or modify it under the terms of the
 *  GNU General Public License as published by the Free Software
 *  Foundation; either version 3 of the License, or (at your option)
 *  any later version.
 *
 *  The python bindings for AlsaPlayer is distributed in the hope that
 *  it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/ 


#include <alsaplayer/control.h>
#include <boost/bind.hpp>
#include <boost/python.hpp>
#include <sstream>
#include <string>

using namespace boost::python;

namespace
{

  // This is how most of the functions communicate that they
  // have experienced an error. Instead of returning an
  // error code, this is thrown.
  struct APException
  {
    APException(int e,
		std::string msg = std::string()) : 
      err(e),
      message(msg)
    {}

    int err;
    std::string message;
  };
  
  void translator(APException const& e) {
    std::ostringstream oss;

    oss << "ALSAPlayerException: ";
    if (!e.message.empty()) 
      oss << e.message << ", ";
    oss << "id=" << e.err;

    PyErr_SetString(PyExc_UserWarning, oss.str().c_str());
  }
  
  /*
    The rest of these functions serve to put the alsaplayer interface into
    a more pythonic format. For instance, get-functions return values
    rather than modify paramters, they throw exceptions on error, they return
    bools rather than ints where appropriate, and so forth. Otherwise,
    these are basically a thin layer of the alsaplayer C API
  */

  int find_session(const std::string& name)
  {
    int rval;
    int err = ap_find_session(const_cast<char*>(name.c_str()), &rval);
    if (err == 0) throw APException(err);
    return rval;
  }

  bool session_running(int sid)
  {
    return ap_session_running(sid);
  }
  
  template <class T>
  T get(int(*f)(int, T*), int sid)
  {
    T rval;
    int err = f(sid, &rval);
    if (0 == err) throw APException(err);
    return rval;
  }

  template <int MaxSize>
  std::string get_string(int(*f)(int, char*), int sid)
  {
    char result[MaxSize + 1];
    int err = f(sid, result);
    if (0 == err) throw APException(err);
    return std::string(result);
  }

  // TODO: Consider using Boost.PP to simplify this repetition
  int get_playlist_length(int sid) 
  { return get<int>(&ap_get_playlist_length, sid); }

  float get_speed(int sid) 
  { return get<float>(&ap_get_speed, sid); }

  float get_volume(int sid) 
  { return get<float>(&ap_get_volume, sid); }

  float get_pan(int sid) 
  { return get<float>(&ap_get_pan, sid); }

  bool is_looping(int sid) 
  { return get<int>(&ap_is_looping, sid); }

  bool is_playlist_looping(int sid) 
  { return get<int>(&ap_is_playlist_looping, sid); }

  int get_tracks(int sid)
  { return get<int>(&ap_get_tracks, sid); }

  std::string get_session_name(int sid) 
  { return get_string<AP_SESSION_MAX>(ap_get_session_name, sid); }

  std::string get_title(int sid)
  { return get_string<AP_TITLE_MAX>(ap_get_title, sid); }

  std::string get_artist(int sid)
  { return get_string<AP_ARTIST_MAX>(ap_get_artist, sid); }

  std::string get_album(int sid)
  { return get_string<AP_ALBUM_MAX>(ap_get_album, sid); }

  std::string get_genre(int sid)
  { return get_string<AP_GENRE_MAX>(ap_get_genre, sid); }

  std::string get_year(int sid)
  { return get_string<AP_YEAR_MAX>(ap_get_year, sid); }

  std::string get_track_number(int sid)
  { return get_string<AP_TRACK_NUMBER_MAX>(ap_get_track_number, sid); }

  std::string get_comment(int sid)
  { return get_string<AP_COMMENT_MAX>(ap_get_comment, sid); }

  std::string get_file_path(int sid)
  { return get_string<AP_FILE_PATH_MAX>(ap_get_file_path, sid); }

  int get_position(int sid)
  { return get<int>(ap_get_position, sid); }

  int get_length(int sid)
  { return get<int>(ap_get_length, sid); }

  int get_frame(int sid)
  { return get<int>(ap_get_frame, sid); }

  int get_frames(int sid)
  { return get<int>(ap_get_frames, sid); }

  std::string get_stream_type(int sid)
  { return get_string<AP_STREAM_TYPE_MAX>(ap_get_stream_type, sid); }

  std::string get_status(int sid)
  { return get_string<AP_STATUS_MAX>(ap_get_status, sid); }

  bool is_playing(int sid)
  { return get<int>(ap_is_playing, sid); }

  void sort(int sid, const std::string& sz)
  { 
    int err = ap_sort(sid, const_cast<char*>(sz.c_str()));
    if (0 == err) throw APException(err);
  }

  int get_playlist_position(int sid)
  { return get<int>(ap_get_playlist_position, sid); }

  std::string get_file_path_for_track(int sid, int pos)
  {
    char path[AP_FILE_PATH_MAX + 1];
    int err = ap_get_file_path_for_track(sid, path, pos);
    if (0 == err) throw APException(err);
    return std::string(path);
  }

  void insert(int sid, const std::string& sz, int pos)
  {
    int err = ap_insert(sid, const_cast<char*>(sz.c_str()), pos);
    if (0 == err) throw APException(err);
  }

  list get_playlist(int sid)
  {
    int length;
    char** playlist;

    int err = ap_get_playlist(sid, &length, &playlist);
    if (0 == err) throw APException(err);

    list rval;
    for (int idx = 0; idx < length; ++idx)
      {
	rval.append(str(std::string(playlist[idx])));
	free(playlist[idx]);
      }
    free(playlist);
    return rval;
  }
  
}

/* This defines the actual alsaplayer module */
BOOST_PYTHON_MODULE(alsaplayer)
{

  register_exception_translator< ::APException >(::translator);
    
  def("find_session",            
      ::find_session);
  def("session_running",
      ::session_running);
  def("version",
      ap_version);
  def("play",
      ap_play);
  def("stop",
      ap_stop);
  def("pause",                   
      ap_pause);
  def("unpause",
      ap_unpause);
  def("next",
      ap_next);
  def("prev",
      ap_prev);
  def("ping",
      ap_ping);
  def("quit",
      ap_quit);
  def("clear_playlist",
      ap_clear_playlist);
  def("add_path",
      ap_add_path);
  def("add_and_play",
      ap_add_and_play);
  def("add_playlist",
      ap_add_playlist);
  def("shuffle_playlist",
      ap_shuffle_playlist);
  def("save_playlist",
      ap_save_playlist);
  def("get_playlist_length",
      ::get_playlist_length);
  def("set_speed",
      ap_set_speed);
  def("get_speed",
      ::get_speed);
  def("set_volume",
      ap_set_volume);
  def("get_volume",
      ::get_volume);
  def("set_pan",
      ap_set_pan);
  def("get_pan",
      ::get_pan);
  def("set_looping",
      ap_set_looping);
  def("is_looping",
      ::is_looping);
  def("set_playlist_looping",
      ap_set_playlist_looping);
  def("is_playlist_looping",
      ::is_playlist_looping);
  def("get_tracks",
      ::get_tracks);
  def("get_session_name",
      ::get_session_name);
  def("get_title",
      ::get_title);
  def("get_artist",
      ::get_artist);
  def("get_album",
      ::get_album);
  def("get_genre", 
      ::get_genre);
  def("get_year",
      ::get_year);
  def("get_track_number",
      ::get_track_number);
  def("get_comment",
      ::get_comment);
  def("get_file_path", 
      ::get_file_path);
  def("set_position",
      ap_set_position);
  def("get_position",
      ::get_position);
  def("set_position_relative",
      ap_set_position_relative);
  def("get_length",
      ::get_length);
  def("set_frame",
      ap_set_frame);
  def("get_frame",
      ::get_frame);
  def("get_frames",
      ::get_frames);
  def("get_stream_type",
      ::get_stream_type);
  def("get_status",
      ::get_status);
  def("is_playing",
      ::is_playing);
  def("sort",
      ::sort);
  def("jump_to",
      ap_jump_to);
  def("get_playlist_position",
      ::get_playlist_position);
  def("get_file_path_for_track",
      ::get_file_path_for_track);
  def("insert",
      ::insert);
  def("remove",
      ap_remove);
  def("set_current",
      ap_set_current);
  def("get_playlist",
      ::get_playlist);
}