1 /* sox.c
2 * Copyright (C) 2004, 2005 Sylvain Cresto <scresto@gmail.com>
3 *
4 * This file is part of graveman!
5 *
6 * graveman! is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or
9 * (at your option) any later version.
10 *
11 * graveman! is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with program; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * URL: http://www.nongnu.org/graveman/
22 *
23 */
24
25 #include "graveman.h"
26
27 #define SOX_ERROR "sox:"
28 #define SOX_FILE_NOT_FOUND "No such file or directory"
29 #define SOX_FAILED_READING "Failed reading"
30
31 #define SOX_MP3 "mp3"
32 #define SOX_OGG "vorbis"
33
34 /* communication avec sox */
35
36 /* verification que sox ai bien le support ogg et mp3 */
check_sox()37 gboolean check_sox()
38 {
39 gchar *Lsox = conf_get_string("sox");
40 gchar *Lcommandline, *Lerr = NULL;
41 gboolean Lstatus;
42 gint Lexit = 0;
43
44 GsupportMp3 = FALSE;
45 GsupportOgg = FALSE;
46
47 if (!Lsox || !*Lsox) return FALSE;
48
49 Lcommandline = g_strdup_printf("%s -help", conf_get_string("sox"));
50 Lstatus = g_spawn_command_line_sync(Lcommandline, NULL, &Lerr, &Lexit, NULL);
51 g_free(Lcommandline);
52
53 GsupportMp3 = (Lstatus == TRUE && Lerr && ((strstr(Lerr, SOX_MP3))));
54 GsupportOgg = (Lstatus == TRUE && Lerr && ((strstr(Lerr, SOX_OGG))));
55
56 g_free(Lerr);
57
58 return Lstatus;
59 }
60
61
62 /* transformation fichier son vers piste audio */
63
64 /* pour le moment on ne fais pas grand chose dans le callback ...
65 * plus tard il y aura une meilleur gestion des erreurs ! */
sox_callback(GIOChannel * Astd,GIOCondition Acond,gpointer Adata)66 gboolean sox_callback(GIOChannel *Astd, GIOCondition Acond, gpointer Adata)
67 {
68 GIOStatus Lstatus;
69 Tgrave *Lg = (Tgrave *)Adata;
70 gchar *Lbuffer = NULL;
71 gchar *s;
72 gint *Lcont = (gint *) sc_grave_get_data(Lg, "cont"); /* on traite encore des donnees ? */
73 GError **Lerreur = (GError **) sc_grave_get_data(Lg, "gerror"); /* pointeur erreur */
74
75
76 /* fin du callback lorsque l'on recoi un signal comme quoi le pipe est ferme */
77 if (Acond == G_IO_HUP || Acond == G_IO_ERR) {
78 *Lcont = 1;
79 return FALSE;
80 }
81
82 Lstatus = g_io_channel_read_line(Astd, &Lbuffer, NULL, NULL, NULL);
83 _DEB("%s\n", Lbuffer);
84 if (Lbuffer) {
85 if (( (strstr(Lbuffer, SOX_FILE_NOT_FOUND)) || (strstr(Lbuffer, SOX_FAILED_READING)) ) &&
86 ((s=strstr(Lbuffer, SOX_ERROR)))) {
87 g_set_error(Lerreur, GRAVEMAN_ERROR, _ERR_SOX, s+strlen(SOX_ERROR));
88 g_free(Lbuffer);
89 return FALSE;
90 }
91 }
92
93 g_free(Lbuffer);
94
95 return TRUE;
96 }
97
SoundToCdr(Tgrave * Ag,gchar * AFichier,gchar * AVers,GError ** Aerror)98 gboolean SoundToCdr(Tgrave *Ag, gchar *AFichier, gchar *AVers, GError **Aerror)
99 {
100 gchar *Lcommandline;
101 gchar **Lcmd;
102 gint *Lpid = (gint *) sc_grave_get_data(Ag, "pid");
103 gboolean *Labort = (gboolean *)sc_grave_get_data(Ag, "gabort");
104 gint *Lcont = (gint *) sc_grave_get_data(Ag, "cont");
105 gint g_out, g_err, Lnbrarg;
106 gchar *Lfic;
107 gchar *Lvers;
108 GIOChannel *Lcom, *Lcomerr;
109 GIOStatus Lstatus;
110 gboolean Lbolstatus;
111 guint Lcomevent, Lcomerrevent;
112
113 Lfic = sc_strescape(AFichier);
114 Lvers = sc_strescape(AVers);
115 Lcommandline = g_strdup_printf("%s %s \"%s\" \"%s\"", conf_get_string("sox"), conf_get_string("soxpara"), Lfic, Lvers);
116 g_free(Lvers); g_free(Lfic);
117 _DEB("execution [%s]\n", Lcommandline);
118
119 Lstatus = g_shell_parse_argv(Lcommandline, &Lnbrarg, &Lcmd, Aerror);
120 g_free(Lcommandline);
121 if (Lstatus == FALSE) {
122 return FALSE;
123 }
124
125 Lbolstatus = g_spawn_async_with_pipes(NULL, Lcmd, NULL, /* env argument */
126 (GSpawnFlags ) (G_SPAWN_DO_NOT_REAP_CHILD),
127 NULL, NULL, Lpid, NULL, &g_out, &g_err, Aerror);
128 g_strfreev(Lcmd);
129
130 if (Lbolstatus == FALSE) {
131 g_warning("ERROR EXECUTION !\n");
132 return FALSE;
133 }
134 Lcom = g_io_channel_unix_new( g_out );
135 g_io_channel_set_encoding (Lcom, NULL, NULL);
136 g_io_channel_set_flags( Lcom, G_IO_FLAG_NONBLOCK, NULL );
137 Lcomevent = g_io_add_watch (Lcom, (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI),
138 sox_callback,
139 Ag);
140
141 Lcomerr = g_io_channel_unix_new( g_err );
142 g_io_channel_set_encoding (Lcomerr, NULL, NULL);
143 g_io_channel_set_flags( Lcomerr, G_IO_FLAG_NONBLOCK, NULL );
144 Lcomerrevent = g_io_add_watch (Lcomerr, (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI),
145 sox_callback,
146 Ag);
147
148 while (*Lcont > 1 && *Labort == FALSE) {
149 gtk_main_iteration();
150 }
151 exit_prog(*Lpid, *Labort, Aerror, NULL);
152
153 g_source_remove(Lcomerrevent);
154 g_source_remove(Lcomevent);
155
156 g_io_channel_shutdown(Lcomerr, FALSE, NULL);
157 g_io_channel_unref(Lcomerr);
158 g_io_channel_shutdown(Lcom, FALSE, NULL);
159 g_io_channel_unref(Lcom);
160 g_spawn_close_pid(*Lpid);
161 *Lpid = 0;
162
163 /* l'utilisateur a annule l'operation */
164 if (*Labort==TRUE || *Aerror) return FALSE;
165
166 return TRUE;
167 }
168
169 /*
170 * vim:et:ts=8:sts=2:sw=2
171 */
172