61
|
1 /* XMMS - Cross-platform multimedia player
|
|
2 * Copyright (C) 1998-2004 Peter Alm, Mikael Alm, Olle Hallnas,
|
|
3 * Thomas Nilsson and 4Front Technologies
|
|
4 * Copyright (C) 1999-2004 Haavard Kvaalen
|
|
5 *
|
|
6 * File name suffix option added by Heikki Orsila 2003
|
|
7 * <heikki.orsila@iki.fi> (no copyrights claimed)
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
22 */
|
|
23
|
|
24 #include "config.h"
|
|
25
|
|
26 #include <gtk/gtk.h>
|
|
27 #include <glib/gi18n.h>
|
|
28 #include <stdio.h>
|
|
29 #include <string.h>
|
|
30
|
|
31 #include "audacious/plugin.h"
|
|
32 #include "libaudacious/beepctrl.h"
|
|
33 #include "libaudacious/dirbrowser.h"
|
|
34 #include "libaudacious/configfile.h"
|
|
35 #include "libaudacious/util.h"
|
|
36
|
|
37 struct wavhead
|
|
38 {
|
|
39 guint32 main_chunk;
|
|
40 guint32 length;
|
|
41 guint32 chunk_type;
|
|
42 guint32 sub_chunk;
|
|
43 guint32 sc_len;
|
|
44 guint16 format;
|
|
45 guint16 modus;
|
|
46 guint32 sample_fq;
|
|
47 guint32 byte_p_sec;
|
|
48 guint16 byte_p_spl;
|
|
49 guint16 bit_p_spl;
|
|
50 guint32 data_chunk;
|
|
51 guint32 data_length;
|
|
52 };
|
|
53
|
|
54 static GtkWidget *configure_win = NULL, *configure_vbox;
|
|
55 static GtkWidget *path_hbox, *path_label, *path_entry, *path_browse, *path_dirbrowser = NULL;
|
|
56 static GtkWidget *configure_separator;
|
|
57 static GtkWidget *configure_bbox, *configure_ok, *configure_cancel;
|
|
58
|
|
59 static GtkWidget *use_suffix_toggle = NULL;
|
|
60 static gboolean use_suffix = FALSE;
|
|
61
|
|
62 static gchar *file_path = NULL;
|
|
63 static FILE *output_file = NULL;
|
|
64 static struct wavhead header;
|
|
65 static guint64 written = 0;
|
|
66 static AFormat afmt;
|
|
67 gint ctrlsocket_get_session_id(void); /* FIXME */
|
|
68
|
|
69 static void disk_init(void);
|
|
70 static gint disk_open(AFormat fmt, gint rate, gint nch);
|
|
71 static void disk_write(void *ptr, gint length);
|
|
72 static void disk_close(void);
|
|
73 static void disk_flush(gint time);
|
|
74 static void disk_pause(short p);
|
|
75 static gint disk_free(void);
|
|
76 static gint disk_playing(void);
|
|
77 static gint disk_get_written_time(void);
|
|
78 static gint disk_get_output_time(void);
|
|
79 static void disk_configure(void);
|
|
80
|
|
81 OutputPlugin disk_op =
|
|
82 {
|
|
83 NULL,
|
|
84 NULL,
|
|
85 NULL, /* Description */
|
|
86 disk_init,
|
|
87 NULL, /* about */
|
|
88 disk_configure, /* configure */
|
|
89 NULL, /* get_volume */
|
|
90 NULL, /* set_volume */
|
|
91 disk_open,
|
|
92 disk_write,
|
|
93 disk_close,
|
|
94 disk_flush,
|
|
95 disk_pause,
|
|
96 disk_free,
|
|
97 disk_playing,
|
|
98 disk_get_output_time,
|
|
99 disk_get_written_time,
|
|
100 };
|
|
101
|
|
102 OutputPlugin *get_oplugin_info(void)
|
|
103 {
|
|
104 disk_op.description = g_strdup_printf(_("Disk Writer Plugin %s"), VERSION);
|
|
105 return &disk_op;
|
|
106 }
|
|
107
|
|
108 static void disk_init(void)
|
|
109 {
|
|
110 ConfigFile *cfgfile;
|
|
111
|
|
112 cfgfile = xmms_cfg_open_default_file();
|
|
113 if (cfgfile)
|
|
114 {
|
|
115 xmms_cfg_read_string(cfgfile, "disk_writer", "file_path", &file_path);
|
|
116 xmms_cfg_read_boolean(cfgfile, "disk_writer", "use_suffix", &use_suffix);
|
|
117 xmms_cfg_free(cfgfile);
|
|
118 }
|
|
119
|
|
120 if (!file_path)
|
|
121 file_path = g_strdup(g_get_home_dir());
|
|
122 }
|
|
123
|
|
124 static gint disk_open(AFormat fmt, gint rate, gint nch)
|
|
125 {
|
|
126 gchar *filename, *title, *temp;
|
|
127 gint pos;
|
|
128
|
|
129 written = 0;
|
|
130 afmt = fmt;
|
|
131
|
|
132 if (xmms_check_realtime_priority())
|
|
133 {
|
|
134 xmms_show_message(_("Error"),
|
|
135 _("You cannot use the Disk Writer plugin\n"
|
|
136 "when you're running in realtime mode."),
|
|
137 _("OK"), FALSE, NULL, NULL);
|
|
138 return 0;
|
|
139 }
|
|
140
|
|
141 pos = xmms_remote_get_playlist_pos(ctrlsocket_get_session_id());
|
|
142 title = xmms_remote_get_playlist_file(ctrlsocket_get_session_id(), pos);
|
|
143 if (!use_suffix) {
|
|
144 if (title != NULL && (temp = strrchr(title, '.')) != NULL) {
|
|
145 *temp = '\0';
|
|
146 }
|
|
147 }
|
|
148 if (title == NULL || strlen(g_basename(title)) == 0)
|
|
149 {
|
|
150 g_free(title);
|
|
151 /* No filename, lets try title instead */
|
|
152 title = xmms_remote_get_playlist_title(ctrlsocket_get_session_id(), pos);
|
|
153 while (title != NULL && (temp = strchr(title, '/')) != NULL)
|
|
154 *temp = '-';
|
|
155
|
|
156 if (title == NULL || strlen(g_basename(title)) == 0)
|
|
157 {
|
|
158 g_free(title);
|
|
159 /* No title either. Just set it to something. */
|
|
160 title = g_strdup_printf("xmms-%d", pos);
|
|
161 }
|
|
162 }
|
|
163 filename = g_strdup_printf("%s/%s.wav", file_path, g_basename(title));
|
|
164 g_free(title);
|
|
165
|
|
166 output_file = fopen(filename, "wb");
|
|
167 g_free(filename);
|
|
168
|
|
169 if (!output_file)
|
|
170 return 0;
|
|
171
|
|
172 memcpy(&header.main_chunk, "RIFF", 4);
|
|
173 header.length = GUINT32_TO_LE(0);
|
|
174 memcpy(&header.chunk_type, "WAVE", 4);
|
|
175 memcpy(&header.sub_chunk, "fmt ", 4);
|
|
176 header.sc_len = GUINT32_TO_LE(16);
|
|
177 header.format = GUINT16_TO_LE(1);
|
|
178 header.modus = GUINT16_TO_LE(nch);
|
|
179 header.sample_fq = GUINT32_TO_LE(rate);
|
|
180 if (fmt == FMT_U8 || fmt == FMT_S8)
|
|
181 header.bit_p_spl = GUINT16_TO_LE(8);
|
|
182 else
|
|
183 header.bit_p_spl = GUINT16_TO_LE(16);
|
|
184 header.byte_p_sec = GUINT32_TO_LE(rate * header.modus * (GUINT16_FROM_LE(header.bit_p_spl) / 8));
|
|
185 header.byte_p_spl = GUINT16_TO_LE((GUINT16_FROM_LE(header.bit_p_spl) / (8 / nch)));
|
|
186 memcpy(&header.data_chunk, "data", 4);
|
|
187 header.data_length = GUINT32_TO_LE(0);
|
|
188 fwrite(&header, sizeof (struct wavhead), 1, output_file);
|
|
189
|
|
190 return 1;
|
|
191 }
|
|
192
|
|
193 static void convert_buffer(gpointer buffer, gint length)
|
|
194 {
|
|
195 gint i;
|
|
196
|
|
197 if (afmt == FMT_S8)
|
|
198 {
|
|
199 guint8 *ptr1 = buffer;
|
|
200 gint8 *ptr2 = buffer;
|
|
201
|
|
202 for (i = 0; i < length; i++)
|
|
203 *(ptr1++) = *(ptr2++) ^ 128;
|
|
204 }
|
|
205 if (afmt == FMT_S16_BE)
|
|
206 {
|
|
207 gint16 *ptr = buffer;
|
|
208
|
|
209 for (i = 0; i < length >> 1; i++, ptr++)
|
|
210 *ptr = GUINT16_SWAP_LE_BE(*ptr);
|
|
211 }
|
|
212 if (afmt == FMT_S16_NE)
|
|
213 {
|
|
214 gint16 *ptr = buffer;
|
|
215
|
|
216 for (i = 0; i < length >> 1; i++, ptr++)
|
|
217 *ptr = GINT16_TO_LE(*ptr);
|
|
218 }
|
|
219 if (afmt == FMT_U16_BE)
|
|
220 {
|
|
221 gint16 *ptr1 = buffer;
|
|
222 guint16 *ptr2 = buffer;
|
|
223
|
|
224 for (i = 0; i < length >> 1; i++, ptr2++)
|
|
225 *(ptr1++) = GINT16_TO_LE(GUINT16_FROM_BE(*ptr2) ^ 32768);
|
|
226 }
|
|
227 if (afmt == FMT_U16_LE)
|
|
228 {
|
|
229 gint16 *ptr1 = buffer;
|
|
230 guint16 *ptr2 = buffer;
|
|
231
|
|
232 for (i = 0; i < length >> 1; i++, ptr2++)
|
|
233 *(ptr1++) = GINT16_TO_LE(GUINT16_FROM_LE(*ptr2) ^ 32768);
|
|
234 }
|
|
235 if (afmt == FMT_U16_NE)
|
|
236 {
|
|
237 gint16 *ptr1 = buffer;
|
|
238 guint16 *ptr2 = buffer;
|
|
239
|
|
240 for (i = 0; i < length >> 1; i++, ptr2++)
|
|
241 *(ptr1++) = GINT16_TO_LE((*ptr2) ^ 32768);
|
|
242 }
|
|
243 }
|
|
244
|
|
245 static void disk_write(void *ptr, gint length)
|
|
246 {
|
|
247 if (afmt == FMT_S8 || afmt == FMT_S16_BE ||
|
|
248 afmt == FMT_U16_LE || afmt == FMT_U16_BE || afmt == FMT_U16_NE)
|
|
249 convert_buffer(ptr, length);
|
|
250 #ifdef WORDS_BIGENDIAN
|
|
251 if (afmt == FMT_S16_NE)
|
|
252 convert_buffer(ptr, length);
|
|
253 #endif
|
|
254 written += fwrite(ptr, 1, length, output_file);
|
|
255 }
|
|
256
|
|
257 static void disk_close(void)
|
|
258 {
|
|
259 if (output_file)
|
|
260 {
|
|
261 header.length = GUINT32_TO_LE(written + sizeof (struct wavhead) - 8);
|
|
262
|
|
263 header.data_length = GUINT32_TO_LE(written);
|
|
264 fseek(output_file, 0, SEEK_SET);
|
|
265 fwrite(&header, sizeof (struct wavhead), 1, output_file);
|
|
266
|
|
267 fclose(output_file);
|
|
268 written = 0;
|
|
269 }
|
|
270 output_file = NULL;
|
|
271 }
|
|
272
|
|
273 static void disk_flush(gint time)
|
|
274 {
|
|
275 }
|
|
276
|
|
277 static void disk_pause(short p)
|
|
278 {
|
|
279 }
|
|
280
|
|
281 static gint disk_free(void)
|
|
282 {
|
|
283 return 1000000;
|
|
284 }
|
|
285
|
|
286 static gint disk_playing(void)
|
|
287 {
|
|
288 return 0;
|
|
289 }
|
|
290
|
|
291 static gint disk_get_written_time(void)
|
|
292 {
|
|
293 if(header.byte_p_sec != 0)
|
|
294 return (gint) ((written * 1000) / header.byte_p_sec);
|
|
295 return 0;
|
|
296 }
|
|
297
|
|
298 static gint disk_get_output_time(void)
|
|
299 {
|
|
300 return disk_get_written_time();
|
|
301 }
|
|
302
|
|
303 static void path_dirbrowser_cb(gchar * dir)
|
|
304 {
|
|
305 gtk_entry_set_text(GTK_ENTRY(path_entry), dir);
|
|
306 }
|
|
307
|
|
308 static void path_browse_cb(GtkWidget * w, gpointer data)
|
|
309 {
|
|
310 if (!path_dirbrowser)
|
|
311 {
|
|
312 path_dirbrowser = xmms_create_dir_browser(_("Select the directory where you want to store the output files:"), file_path, GTK_SELECTION_SINGLE, path_dirbrowser_cb);
|
|
313 gtk_signal_connect(GTK_OBJECT(path_dirbrowser), "destroy", GTK_SIGNAL_FUNC(gtk_widget_destroyed), &path_dirbrowser);
|
|
314 gtk_window_set_transient_for(GTK_WINDOW(path_dirbrowser), GTK_WINDOW(configure_win));
|
|
315 gtk_widget_show(path_dirbrowser);
|
|
316 }
|
|
317 }
|
|
318
|
|
319 static void configure_ok_cb(gpointer data)
|
|
320 {
|
|
321 ConfigFile *cfgfile;
|
|
322
|
|
323 if (file_path)
|
|
324 g_free(file_path);
|
|
325 file_path = g_strdup(gtk_entry_get_text(GTK_ENTRY(path_entry)));
|
|
326
|
|
327 use_suffix =
|
|
328 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(use_suffix_toggle));
|
|
329
|
|
330 cfgfile = xmms_cfg_open_default_file();
|
|
331 if (!cfgfile)
|
|
332 cfgfile = xmms_cfg_new();
|
|
333
|
|
334 xmms_cfg_write_string(cfgfile, "disk_writer", "file_path", file_path);
|
|
335 xmms_cfg_write_boolean(cfgfile, "disk_writer", "use_suffix", use_suffix);
|
|
336 xmms_cfg_write_default_file(cfgfile);
|
|
337 xmms_cfg_free(cfgfile);
|
|
338
|
|
339 gtk_widget_destroy(configure_win);
|
|
340 if (path_dirbrowser)
|
|
341 gtk_widget_destroy(path_dirbrowser);
|
|
342 }
|
|
343
|
|
344 static void configure_destroy(void)
|
|
345 {
|
|
346 if (path_dirbrowser)
|
|
347 gtk_widget_destroy(path_dirbrowser);
|
|
348 }
|
|
349
|
|
350 static void disk_configure(void)
|
|
351 {
|
|
352 GtkTooltips *use_suffix_tooltips;
|
|
353
|
|
354 if (!configure_win)
|
|
355 {
|
|
356 configure_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
357
|
|
358 gtk_signal_connect(GTK_OBJECT(configure_win), "destroy", GTK_SIGNAL_FUNC(configure_destroy), NULL);
|
|
359 gtk_signal_connect(GTK_OBJECT(configure_win), "destroy", GTK_SIGNAL_FUNC(gtk_widget_destroyed), &configure_win);
|
|
360 gtk_window_set_title(GTK_WINDOW(configure_win), _("Disk Writer Configuration"));
|
|
361 gtk_window_set_position(GTK_WINDOW(configure_win), GTK_WIN_POS_MOUSE);
|
|
362
|
|
363 gtk_container_set_border_width(GTK_CONTAINER(configure_win), 10);
|
|
364
|
|
365 configure_vbox = gtk_vbox_new(FALSE, 10);
|
|
366 gtk_container_add(GTK_CONTAINER(configure_win), configure_vbox);
|
|
367
|
|
368 path_hbox = gtk_hbox_new(FALSE, 5);
|
|
369 gtk_box_pack_start(GTK_BOX(configure_vbox), path_hbox, FALSE, FALSE, 0);
|
|
370
|
|
371 path_label = gtk_label_new(_("Path:"));
|
|
372 gtk_box_pack_start(GTK_BOX(path_hbox), path_label, FALSE, FALSE, 0);
|
|
373 gtk_widget_show(path_label);
|
|
374
|
|
375 path_entry = gtk_entry_new();
|
|
376 if (file_path)
|
|
377 gtk_entry_set_text(GTK_ENTRY(path_entry), file_path);
|
|
378 gtk_widget_set_usize(path_entry, 200, -1);
|
|
379 gtk_box_pack_start(GTK_BOX(path_hbox), path_entry, TRUE, TRUE, 0);
|
|
380 gtk_widget_show(path_entry);
|
|
381
|
|
382 path_browse = gtk_button_new_with_label(_("Browse"));
|
|
383 gtk_signal_connect(GTK_OBJECT(path_browse), "clicked", GTK_SIGNAL_FUNC(path_browse_cb), NULL);
|
|
384 gtk_box_pack_start(GTK_BOX(path_hbox), path_browse, FALSE, FALSE, 0);
|
|
385 gtk_widget_show(path_browse);
|
|
386
|
|
387 gtk_widget_show(path_hbox);
|
|
388
|
|
389 use_suffix_toggle = gtk_check_button_new_with_label(_("Don't strip file name extension"));
|
|
390 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(use_suffix_toggle), use_suffix);
|
|
391 gtk_box_pack_start(GTK_BOX(configure_vbox), use_suffix_toggle, FALSE, FALSE, 0);
|
|
392 use_suffix_tooltips = gtk_tooltips_new();
|
|
393 gtk_tooltips_set_tip(use_suffix_tooltips, use_suffix_toggle, "If enabled, the extension from the original filename will not be stripped before adding the .wav extension to the end.", NULL);
|
|
394 gtk_tooltips_enable(use_suffix_tooltips);
|
|
395 gtk_widget_show(use_suffix_toggle);
|
|
396
|
|
397 configure_separator = gtk_hseparator_new();
|
|
398 gtk_box_pack_start(GTK_BOX(configure_vbox), configure_separator, FALSE, FALSE, 0);
|
|
399 gtk_widget_show(configure_separator);
|
|
400
|
|
401 configure_bbox = gtk_hbutton_box_new();
|
|
402 gtk_button_box_set_layout(GTK_BUTTON_BOX(configure_bbox), GTK_BUTTONBOX_END);
|
|
403 gtk_button_box_set_spacing(GTK_BUTTON_BOX(configure_bbox), 5);
|
|
404 gtk_box_pack_start(GTK_BOX(configure_vbox), configure_bbox, FALSE, FALSE, 0);
|
|
405
|
|
406 configure_ok = gtk_button_new_with_label(_("OK"));
|
|
407 gtk_signal_connect(GTK_OBJECT(configure_ok), "clicked", GTK_SIGNAL_FUNC(configure_ok_cb), NULL);
|
|
408 GTK_WIDGET_SET_FLAGS(configure_ok, GTK_CAN_DEFAULT);
|
|
409 gtk_box_pack_start(GTK_BOX(configure_bbox), configure_ok, TRUE, TRUE, 0);
|
|
410 gtk_widget_show(configure_ok);
|
|
411 gtk_widget_grab_default(configure_ok);
|
|
412
|
|
413 configure_cancel = gtk_button_new_with_label(_("Cancel"));
|
|
414 gtk_signal_connect_object(GTK_OBJECT(configure_cancel), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(configure_win));
|
|
415 GTK_WIDGET_SET_FLAGS(configure_cancel, GTK_CAN_DEFAULT);
|
|
416 gtk_box_pack_start(GTK_BOX(configure_bbox), configure_cancel, TRUE, TRUE, 0);
|
|
417 gtk_widget_show(configure_cancel);
|
|
418 gtk_widget_show(configure_bbox);
|
|
419 gtk_widget_show(configure_vbox);
|
|
420 gtk_widget_show(configure_win);
|
|
421 }
|
|
422 }
|