1 /*
2  * Copyright (C) 2002 2003 2004 2005 2010 2012, Magnus Hjorth
3  *
4  * This file is part of mhWaveEdit.
5  *
6  * mhWaveEdit 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 of the License, or
9  * (at your option) any later version.
10  *
11  * mhWaveEdit 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mhWaveEdit; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 
22 #include <config.h>
23 
24 #include "main.h"
25 #include "formatselector.h"
26 #include "inifile.h"
27 #include "gettext.h"
28 
29 #define DEFAULT_CHANS 2
30 #define DEFAULT_RATE 44100
31 
format_selector_class_init(GtkObjectClass * klass)32 static void format_selector_class_init(GtkObjectClass *klass)
33 {
34 }
35 
samplesize_changed(Combo * obj,gpointer user_data)36 static void samplesize_changed(Combo *obj, gpointer user_data)
37 {
38      int i;
39      FormatSelector *fs = FORMAT_SELECTOR(user_data);
40      i = combo_selected_index(obj);
41      gtk_widget_set_sensitive(GTK_WIDGET(fs->sign_combo),(i<4));
42      gtk_widget_set_sensitive(GTK_WIDGET(fs->packing_combo),(i==2));
43      if (i == 0)
44 	  combo_set_selection(fs->sign_combo,0);
45      else if (i < 4)
46 	  combo_set_selection(fs->sign_combo,1);
47      else
48 	  combo_set_selection(fs->endian_combo,
49 			      dataformat_sample_t.bigendian?1:0);
50 }
51 
format_selector_init(GtkWidget * widget)52 static void format_selector_init(GtkWidget *widget)
53 {
54      GtkWidget *a=widget,*b;
55      FormatSelector *fs = FORMAT_SELECTOR(widget);
56      GList *l;
57 
58      gtk_table_set_row_spacings(GTK_TABLE(widget),3);
59      gtk_table_set_col_spacings(GTK_TABLE(widget),4);
60      attach_label(_("Sample type: "),widget,1,0);
61      b = combo_new();
62      l = g_list_append(NULL,_("8 bit PCM"));
63      l = g_list_append(l, _("16 bit PCM"));
64      l = g_list_append(l, _("24 bit PCM"));
65      l = g_list_append(l, _("32 bit PCM"));
66      l = g_list_append(l, _("Floating-point, single"));
67      l = g_list_append(l, _("Floating-point, double"));
68      combo_set_items(COMBO(b),l,0);
69      g_list_free(l);
70      fs->samplesize_combo = COMBO(b);
71      gtk_signal_connect(GTK_OBJECT(b),"selection_changed",
72 			GTK_SIGNAL_FUNC(samplesize_changed),fs);
73      gtk_table_attach(GTK_TABLE(a),b,1,2,1,2,GTK_FILL,0,0,0);
74      attach_label(_("Signedness: "),a,2,0);
75      b = combo_new();
76      l = g_list_append(NULL, _("Unsigned"));
77      l = g_list_append(l, _("Signed"));
78      combo_set_items(COMBO(b),l,0);
79      g_list_free(l);
80      fs->sign_combo = COMBO(b);
81      gtk_table_attach(GTK_TABLE(a),b,1,2,2,3,GTK_FILL,0,0,0);
82      attach_label(_("Endianness: "),a,3,0);
83      b = combo_new();
84      l = g_list_append(NULL,_("Little endian"));
85      l = g_list_append(l,_("Big endian"));
86      combo_set_items(COMBO(b),l,0);
87      g_list_free(l);
88      fs->endian_combo = COMBO(b);
89      gtk_table_attach(GTK_TABLE(a),b,1,2,3,4,GTK_FILL,0,0,0);
90      attach_label(_("Alignment:"),a,4,0);
91      b = combo_new();
92      l = g_list_append(NULL,_("Packed"));
93      l = g_list_append(l, _("Top bytes"));
94      l = g_list_append(l, _("Bottom bytes"));
95      combo_set_items(COMBO(b),l,0);
96      g_list_free(l);
97      fs->packing_combo = COMBO(b);
98      gtk_widget_set_sensitive(b, FALSE);
99      gtk_table_attach(GTK_TABLE(a),b,1,2,4,5,GTK_FILL,0,0,0);
100 
101      fs->channel_combo = NULL;
102      fs->rate_box = NULL;
103 
104      gtk_widget_show_all(widget);
105      gtk_widget_hide(widget);
106 }
107 
format_selector_get_type(void)108 GtkType format_selector_get_type(void)
109 {
110      static GtkType id = 0;
111      if (!id) {
112 	  GtkTypeInfo info = {
113 	       "FormatSelector",
114 	       sizeof(FormatSelector),
115 	       sizeof(FormatSelectorClass),
116 	       (GtkClassInitFunc) format_selector_class_init,
117 	       (GtkObjectInitFunc) format_selector_init
118 	  };
119 	  id = gtk_type_unique(gtk_table_get_type(),&info);
120      }
121      return id;
122 }
123 
124 /* Show channel and sample rate items */
format_selector_show_full(FormatSelector * fs)125 static void format_selector_show_full(FormatSelector *fs)
126 {
127      GList *l=NULL;
128      GtkWidget *a=GTK_WIDGET(fs),*b,*c;
129      guint i;
130      attach_label(_("Channels: "),a,0,0);
131      b = combo_new();
132      for (i=1; i<9; i++) l=g_list_append(l,g_strdup(channel_format_name(i)));
133      combo_set_items(COMBO(b),l,DEFAULT_CHANS-1);
134      g_list_foreach(l,(GFunc)g_free,NULL);
135      g_list_free(l);
136      gtk_table_attach(GTK_TABLE(fs),b,1,2,0,1,GTK_FILL,0,0,0);
137      fs->channel_combo = COMBO(b);
138      gtk_widget_show(b);
139      attach_label(_("Sample rate: "),a,5,0);
140      b = gtk_alignment_new(0.0,0.5,0.0,1.0);
141      gtk_table_attach(GTK_TABLE(fs),b,1,2,5,6,GTK_FILL,0,0,0);
142      gtk_widget_show(b);
143      c = intbox_new(DEFAULT_RATE);
144      fs->rate_box = INTBOX(c);
145      gtk_container_add(GTK_CONTAINER(b),c);
146      gtk_widget_show(c);
147 }
148 
format_selector_new(gboolean show_full)149 GtkWidget *format_selector_new(gboolean show_full)
150 {
151      GtkWidget *fs = gtk_type_new(format_selector_get_type());
152      if (show_full) format_selector_show_full(FORMAT_SELECTOR(fs));
153      return fs;
154 }
155 
format_selector_set(FormatSelector * fs,Dataformat * fmt)156 void format_selector_set(FormatSelector *fs, Dataformat *fmt)
157 {
158      if (fmt->type == DATAFORMAT_PCM) {
159 	  if (fmt->samplesize == 4 && fmt->packing != 0) {
160 	       combo_set_selection(fs->samplesize_combo, 2);
161 	       combo_set_selection(fs->packing_combo, fmt->packing);
162 	  } else
163 	       combo_set_selection(fs->samplesize_combo, fmt->samplesize-1);
164 	  combo_set_selection(fs->sign_combo, fmt->sign?1:0);
165      } else {
166 	  if (fmt->samplesize == sizeof(float))
167 	       combo_set_selection(fs->samplesize_combo, 4);
168 	  else
169 	       combo_set_selection(fs->samplesize_combo, 5);
170      }
171      combo_set_selection(fs->endian_combo, fmt->bigendian?1:0);
172      if (fs->channel_combo != NULL)
173 	  combo_set_selection(fs->channel_combo, fmt->channels-1);
174      if (fs->rate_box != NULL)
175 	  intbox_set(fs->rate_box, fmt->samplerate);
176 }
177 
format_selector_get(FormatSelector * fs,Dataformat * result)178 void format_selector_get(FormatSelector *fs, Dataformat *result)
179 {
180      int i;
181      i = combo_selected_index(fs->samplesize_combo);
182      if (i<4) {
183 	  result->type = DATAFORMAT_PCM;
184 	  if (i == 2) {
185 	       result->packing = combo_selected_index(fs->packing_combo);
186 	       result->samplesize = (result->packing != 0)?4:3;
187 	  } else {
188 	       result->samplesize = i+1;
189 	       result->packing = 0;
190 	  }
191      } else {
192 	  result->type = DATAFORMAT_FLOAT;
193 	  result->samplesize = (i>4)?sizeof(double):sizeof(float);
194      }
195      result->sign = combo_selected_index(fs->sign_combo);
196      result->bigendian = combo_selected_index(fs->endian_combo);
197      if (fs->channel_combo != NULL)
198 	  result->channels = combo_selected_index(fs->channel_combo)+1;
199      else
200 	  result->channels = DEFAULT_CHANS;
201      if (fs->rate_box != NULL) {
202 	  intbox_check(fs->rate_box);
203 	  result->samplerate = fs->rate_box->val;
204      } else
205 	  result->samplerate = DEFAULT_RATE;
206      result->samplebytes = result->samplesize * result->channels;
207 }
208 
format_selector_set_from_inifile(FormatSelector * fs,gchar * ini_prefix)209 void format_selector_set_from_inifile(FormatSelector *fs, gchar *ini_prefix)
210 {
211      Dataformat fmt;
212      if (dataformat_get_from_inifile(ini_prefix,fs->channel_combo!=NULL,&fmt))
213 	  format_selector_set(fs,&fmt);
214 }
215 
format_selector_save_to_inifile(FormatSelector * fs,gchar * ini_prefix)216 void format_selector_save_to_inifile(FormatSelector *fs, gchar *ini_prefix)
217 {
218      Dataformat f;
219      format_selector_get(fs,&f);
220      dataformat_save_to_inifile(ini_prefix,&f,fs->channel_combo!=NULL);
221 }
222 
format_selector_check(FormatSelector * fs)223 gboolean format_selector_check(FormatSelector *fs)
224 {
225      if (fs->rate_box != NULL)
226 	  return intbox_check_limit(fs->rate_box,1000,500000,_("sample rate"));
227      else
228 	  return FALSE;
229 }
230