summaryrefslogtreecommitdiff
path: root/plugins/gtkui/converter.c
blob: 5a7795c3ed1a54038fddf96d7fdab7616eb07453 (plain)
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
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>

    This program 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 2
    of the License, or (at your option) any later version.
    
    This program 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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "converter.h"
#include "support.h"
#include "interface.h"
#include "gtkui.h"

static GtkWidget *converter;

void
converter_show (void) {
    if (!converter) {
        converter = create_converterdlg ();
        gtk_entry_set_text (GTK_ENTRY (lookup_widget (converter, "output_folder")), deadbeef->conf_get_str ("converter.output_folder", ""));

        GtkComboBox *combo;
        // fill encoder presets
        combo = GTK_COMBO_BOX (lookup_widget (converter, "encoder"));
        //GtkTreeModel *mdl = gtk_combo_box_get_model (combobox);
        //gtk_combo_box_append_text (combo, _("Preset Name"));
        gtk_combo_box_set_active (combo, deadbeef->conf_get_int ("converter.encoder_preset", 0));

        // fill dsp presets
        combo = GTK_COMBO_BOX (lookup_widget (converter, "dsp_preset"));
        gtk_combo_box_set_active (combo, deadbeef->conf_get_int ("converter.dsp_preset", 0));
        
        // fill channel maps
        combo = GTK_COMBO_BOX (lookup_widget (converter, "channelmap"));
        gtk_combo_box_set_active (combo, deadbeef->conf_get_int ("converter.channelmap_preset", 0));

        // select output format
        combo = GTK_COMBO_BOX (lookup_widget (converter, "output_format"));
        gtk_combo_box_set_active (combo, deadbeef->conf_get_int ("converter.output_format", 0));

    }
    gtk_widget_show (converter);
}

void
on_converter_output_browse_clicked     (GtkButton       *button,
                                        gpointer         user_data)
{
    GtkWidget *dlg = gtk_file_chooser_dialog_new (_("Select folder..."), GTK_WINDOW (converter), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);

    gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dlg), FALSE);
    // restore folder
    gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dlg), deadbeef->conf_get_str ("filechooser.lastdir", ""));
    int response = gtk_dialog_run (GTK_DIALOG (dlg));
    // store folder
    gchar *folder = gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (dlg));
    if (folder) {
        deadbeef->conf_set_str ("filechooser.lastdir", folder);
        g_free (folder);
        deadbeef->sendmessage (M_CONFIGCHANGED, 0, 0, 0);
    }
    if (response == GTK_RESPONSE_OK) {
        folder = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlg));
        gtk_widget_destroy (dlg);
        if (folder) {
            GtkWidget *entry = lookup_widget (converter, "output_folder");
            gtk_entry_set_text (GTK_ENTRY (entry), folder);
            g_free (folder);
        }
    }
    else {
        gtk_widget_destroy (dlg);
    }
}


void
on_converter_cancel_clicked            (GtkButton       *button,
                                        gpointer         user_data)
{
    gtk_widget_destroy (converter);
    converter = NULL;
}

DB_decoder_t *
plug_get_decoder_for_id (const char *id) {
    DB_decoder_t **plugins = deadbeef->plug_get_decoder_list ();
    for (int c = 0; plugins[c]; c++) {
        if (!strcmp (id, plugins[c]->plugin.id)) {
            return plugins[c];
        }
    }
    return NULL;
}


void
on_converter_ok_clicked                (GtkButton       *button,
                                        gpointer         user_data)
{
    char *outfolder = strdup (gtk_entry_get_text (GTK_ENTRY (lookup_widget (converter, "output_folder"))));
    deadbeef->conf_set_str ("converter.output_folder", outfolder);
    char *encoder = strdup (gtk_entry_get_text (GTK_ENTRY (lookup_widget (converter, "encoder_cmd_line"))));
    deadbeef->conf_set_str ("converter.encoder", encoder);
    deadbeef->conf_save ();
    gtk_widget_destroy (converter);
    converter = NULL;

    deadbeef->pl_lock ();

    // copy list
    int nsel = deadbeef->pl_getselcount ();
    if (0 < nsel) {
        DB_playItem_t **items = malloc (sizeof (DB_playItem_t *) * nsel);
        if (items) {
            int n = 0;
            DB_playItem_t *it = deadbeef->pl_get_first (PL_MAIN);
            while (it) {
                if (deadbeef->pl_is_selected (it)) {
                    assert (n < nsel);
                    deadbeef->pl_item_ref (it);
                    items[n++] = it;
                }
                DB_playItem_t *next = deadbeef->pl_get_next (it, PL_MAIN);
                deadbeef->pl_item_unref (it);
                it = next;
            }
            deadbeef->pl_unlock ();

            // ... convert ...
            for (n = 0; n < nsel; n++) {
                it = items[n];
                DB_decoder_t *dec = NULL;
                dec = plug_get_decoder_for_id (items[n]->decoder_id);
                if (dec) {
                    DB_fileinfo_t *fileinfo = dec->open (0);
                    if (fileinfo && dec->init (fileinfo, DB_PLAYITEM (it)) != 0) {
                        dec->free (fileinfo);
                        fileinfo = NULL;
                    }
                    if (fileinfo) {
                        char *fname = strrchr (it->fname, '/');
                        if (!fname) {
                            fname = it->fname;
                        }
                        else {
                            fname++;
                        }
                        char out[1024];
                        snprintf (out, sizeof (out), "%s/%s", outfolder, fname);
                        char enc[1024];
                        snprintf (enc, sizeof (enc), encoder, out);
                        printf ("executing: %s\n", enc);
                        FILE *fp = popen (enc, "w");
                        if (!fp) {
                            fprintf (stderr, "converter: failed to open encoder: %s\n", encoder);
                        }
                        else {
                            // write wave header
                            char wavehdr[] = {
                                0x52, 0x49, 0x46, 0x46, 0x24, 0x70, 0x0d, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61
                            };
                            fwrite (wavehdr, 1, sizeof (wavehdr), fp);
                            uint32_t size = (it->endsample-it->startsample) * fileinfo->fmt.channels * fileinfo->fmt.bps / 8;
                            fwrite (&size, 1, sizeof (size), fp);

                            int bs = 8192;
                            char buffer[bs];
                            for (;;) {
                                int sz = dec->read (fileinfo, buffer, bs);
                                fwrite (buffer, 1, sz, fp);
                                if (sz != bs) {
                                    break;
                                }
                            }

                            pclose (fp);
                        }
                        dec->free (fileinfo);
                    }
                }
            }

            for (n = 0; n < nsel; n++) {
                deadbeef->pl_item_unref (items[n]);
            }
            free (items);
        }
        else {
            fprintf (stderr, "converter: error allocating memory to store %d tracks\n", nsel);
            deadbeef->pl_unlock ();
        }
    }


    free (outfolder);
    free (encoder);
}

gboolean
on_converterdlg_delete_event           (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{
    converter = NULL;
    return FALSE;
}

void
on_encoder_preset_add                     (GtkButton       *button,
                                        gpointer         user_data)
{
    GtkWidget *dlg = create_convpreset_editor ();
    gtk_dialog_run (GTK_DIALOG (dlg));
    gtk_widget_destroy (dlg);
}

void
on_encoder_preset_edit                     (GtkButton       *button,
                                        gpointer         user_data)
{
    GtkWidget *dlg = create_convpreset_editor ();
    gtk_dialog_run (GTK_DIALOG (dlg));
    gtk_widget_destroy (dlg);
}

void
on_encoder_preset_remove                     (GtkButton       *button,
                                        gpointer         user_data)
{
    GtkWidget *dlg = gtk_message_dialog_new (GTK_WINDOW (mainwin), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_YES_NO, _("Remove preset"));
    gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dlg), _("This action will delete the selected preset. Are you sure?"));
    gtk_window_set_title (GTK_WINDOW (dlg), _("Warning"));

    int response = gtk_dialog_run (GTK_DIALOG (dlg));
    gtk_widget_destroy (dlg);
    if (response != GTK_RESPONSE_YES) {
        // .. remove ..
    }
}

void
on_edit_encoder_presets_clicked        (GtkButton       *button,
                                        gpointer         user_data)
{
    GtkWidget *dlg = create_preset_list ();
    g_signal_connect ((gpointer)lookup_widget (dlg, "add"), "clicked", G_CALLBACK (on_encoder_preset_add), NULL);
    g_signal_connect ((gpointer)lookup_widget (dlg, "remove"), "clicked", G_CALLBACK (on_encoder_preset_remove), NULL);
    g_signal_connect ((gpointer)lookup_widget (dlg, "edit"), "clicked", G_CALLBACK (on_encoder_preset_edit), NULL);

    gtk_dialog_run (GTK_DIALOG (dlg));
    gtk_widget_destroy (dlg);
}


void
on_edit_dsp_presets_clicked            (GtkButton       *button,
                                        gpointer         user_data)
{
}


void
on_edit_channel_maps_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
}