1 /* wavbreaker - A tool to split a wave file up into multiple waves.
2  * Copyright (C) 2002-2004 Timothy Robinson
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 #include "autosplit.h"
20 
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 
25 #include "sample.h"
26 #include "wavbreaker.h"
27 
28 #include "gettext.h"
29 
30 static long
parse_time_string(const char * autosplit_time)31 parse_time_string(const char *autosplit_time)
32 {
33     long min, sec, subsec;
34     if (sscanf(autosplit_time, "%ld:%ld.%ld", &min, &sec, &subsec) == 3) {
35         return (min * 60 + sec) * CD_BLOCKS_PER_SEC + subsec;
36     } else if (sscanf(autosplit_time, "%ld:%ld", &min, &sec) == 2) {
37         return (min * 60 + sec) * CD_BLOCKS_PER_SEC;
38     } else if (sscanf(autosplit_time, "%ld.%ld", &sec, &subsec) == 2) {
39         return sec * CD_BLOCKS_PER_SEC + subsec;
40     }
41 
42     return atoi(autosplit_time) * 60 * CD_BLOCKS_PER_SEC;
43 }
44 
45 static void
on_split_button_clicked(GtkWidget * widget,gpointer user_data)46 on_split_button_clicked(GtkWidget *widget, gpointer user_data)
47 {
48     GtkEntry *entry = GTK_ENTRY(user_data);
49     GtkPopover *popover = GTK_POPOVER(g_object_get_data(G_OBJECT(entry), "popover"));
50 
51     gtk_popover_popdown(popover);
52 
53     long time = parse_time_string(gtk_entry_get_text(entry));
54     if (time > 0) {
55         wavbreaker_autosplit(time);
56 
57         long subsec = time % CD_BLOCKS_PER_SEC;
58         time -= subsec;
59         time /= CD_BLOCKS_PER_SEC;
60 
61         long sec = time % 60;
62         time -= sec;
63         time /= 60;
64 
65         gchar *tmp = g_strdup_printf("%02ld:%02ld.%02ld", time, sec, subsec);
66         gtk_entry_set_text(entry, tmp);
67         g_free(tmp);
68     }
69 }
70 
71 GtkWidget *
autosplit_create(GtkPopover * popover)72 autosplit_create(GtkPopover *popover)
73 {
74     GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
75 
76     GtkWidget *message_label = gtk_label_new(_("Interval (MM:SS.FF, MM:SS, SS.FF or MM):"));
77     gtk_box_pack_start(GTK_BOX(hbox), message_label, FALSE, FALSE, 0);
78 
79     GtkWidget *autosplit_time_entry = gtk_entry_new();
80     g_object_set_data(G_OBJECT(autosplit_time_entry), "popover", popover);
81 
82     g_signal_connect(autosplit_time_entry, "activate", G_CALLBACK(on_split_button_clicked), autosplit_time_entry);
83     gtk_entry_set_text(GTK_ENTRY(autosplit_time_entry), "5:00.00");
84     gtk_entry_set_width_chars(GTK_ENTRY(autosplit_time_entry), 10);
85     gtk_box_pack_start(GTK_BOX(hbox), autosplit_time_entry, FALSE, FALSE, 0);
86 
87     GtkWidget *ok_button = gtk_button_new_with_label(_("Split"));
88     g_signal_connect(G_OBJECT(ok_button), "clicked", G_CALLBACK(on_split_button_clicked), autosplit_time_entry);
89     gtk_box_pack_start(GTK_BOX(hbox), ok_button, FALSE, FALSE, 0);
90 
91     gtk_widget_show_all(hbox);
92 
93     return hbox;
94 }
95