1 /*
2  * Schism Tracker - a cross-platform Impulse Tracker clone
3  * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
4  * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
5  * copyright (c) 2009 Storlek & Mrs. Brisby
6  * copyright (c) 2010-2012 Storlek
7  * URL: http://schismtracker.org/
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 "headers.h"
25 
26 #include "it.h"
27 #include "song.h"
28 #include "page.h"
29 #include "osdefs.h"
30 #include "sdlmain.h"
31 
32 #include "disko.h"
33 
34 #define VOLUME_SCALE    31
35 
36 /* this page will be the first against the wall when the revolution comes */
37 
38 /* --------------------------------------------------------------------- */
39 /* statics */
40 
41 static struct widget widgets_preferences[48];
42 
43 static const char *interpolation_modes[] = {
44 	"Non-Interpolated", "Linear",
45 	"Cubic Spline", "8-Tap FIR Filter", NULL
46 };
47 
48 static const int interp_group[] = {
49 	2,3,4,5,-1,
50 };
51 static int ramp_group[] = { /* not const because it is modified */
52 	-1,-1,-1,
53 };
54 
55 /* --------------------------------------------------------------------- */
56 
preferences_draw_const(void)57 static void preferences_draw_const(void)
58 {
59 	char buf[80];
60 	int i;
61 
62 	draw_text("Master Volume Left", 2, 14, 0, 2);
63 	draw_text("Master Volume Right", 2, 15, 0, 2);
64 	draw_box(21, 13, 27, 16, BOX_THIN | BOX_INNER | BOX_INSET);
65 
66 	sprintf(buf, "Mixing Mode, Playback Frequency: %dHz", audio_settings.sample_rate);
67 	draw_text(buf, 2, 18, 0, 2);
68 
69 	for (i = 0; interpolation_modes[i]; i++);
70 
71 	draw_text("Output Equalizer", 2, 21+i*3, 0, 2);
72 	draw_text(     "Low Frequency Band", 7, 23+i*3, 0, 2);
73 	draw_text( "Med Low Frequency Band", 3, 24+i*3, 0, 2);
74 	draw_text("Med High Frequency Band", 2, 25+i*3, 0, 2);
75 	draw_text(    "High Frequency Band", 6, 26+i*3, 0, 2);
76 
77 	draw_text("Ramp volume at start of sample",2,29+i*3,0,2);
78 
79 	draw_box(25, 22+i*3, 47, 27+i*3, BOX_THIN | BOX_INNER | BOX_INSET);
80 	draw_box(52, 22+i*3, 74, 27+i*3, BOX_THIN | BOX_INNER | BOX_INSET);
81 
82 #define CORNER_BOTTOM "http://schismtracker.org/"
83 	draw_text(CORNER_BOTTOM, 78 - strlen(CORNER_BOTTOM), 48, 1, 2);
84 }
85 
86 /* --------------------------------------------------------------------- */
87 
preferences_set_page(void)88 static void preferences_set_page(void)
89 {
90 	int i, j;
91 	int lim = volume_get_max();
92 	volume_read(&i, &j);
93 	widgets_preferences[0].d.thumbbar.value = i * VOLUME_SCALE / lim;
94 	widgets_preferences[1].d.thumbbar.value = j * VOLUME_SCALE / lim;
95 
96 	for (i = j = 0; interpolation_modes[i]; i++) {
97 		if (i == audio_settings.interpolation_mode) {
98 			widgets_preferences[i + 2].d.togglebutton.state=1;
99 			j = 1;
100 		} else {
101 			widgets_preferences[i + 2].d.togglebutton.state=0;
102 		}
103 	}
104 	if (!j) {
105 		audio_settings.interpolation_mode = 0;
106 		widgets_preferences[2].d.togglebutton.state=1;
107 	}
108 
109 	for (j = 0; j < 4; j++) {
110 		widgets_preferences[i+2+(j*2)].d.thumbbar.value
111 						= audio_settings.eq_freq[j];
112 		widgets_preferences[i+3+(j*2)].d.thumbbar.value
113 						= audio_settings.eq_gain[j];
114 	}
115 
116 	widgets_preferences[i+10].d.togglebutton.state
117 				= audio_settings.no_ramping?0:1;
118 	widgets_preferences[i+11].d.togglebutton.state
119 				= audio_settings.no_ramping?1:0;
120 }
121 
122 /* --------------------------------------------------------------------- */
123 
change_volume(void)124 static void change_volume(void)
125 {
126 	int lim = volume_get_max();
127 	volume_write(
128 		widgets_preferences[0].d.thumbbar.value * lim / VOLUME_SCALE,
129 		widgets_preferences[1].d.thumbbar.value * lim / VOLUME_SCALE);
130 }
131 
132 #define SAVED_AT_EXIT "Audio configuration will be saved at exit"
133 
change_eq(void)134 static void change_eq(void)
135 {
136 	int i,j;
137 	for (i = 0; interpolation_modes[i]; i++);
138 	for (j = 0; j < 4; j++) {
139 		audio_settings.eq_freq[j] = widgets_preferences[i+2+(j*2)].d.thumbbar.value;
140 		audio_settings.eq_gain[j] = widgets_preferences[i+3+(j*2)].d.thumbbar.value;
141 	}
142 	song_init_eq(1, current_song->mix_frequency);
143 }
144 
145 
change_mixer(void)146 static void change_mixer(void)
147 {
148 	int i;
149 	for (i = 0; interpolation_modes[i]; i++) {
150 		if (widgets_preferences[2+i].d.togglebutton.state) {
151 			audio_settings.interpolation_mode = i;
152 		}
153 	}
154 	audio_settings.no_ramping = widgets_preferences[i+11].d.togglebutton.state;
155 
156 	song_init_modplug();
157 	status_text_flash(SAVED_AT_EXIT);
158 }
159 
160 /* --------------------------------------------------------------------- */
save_config_now(UNUSED void * ign)161 static void save_config_now(UNUSED void *ign)
162 {
163 	/* TODO */
164 	cfg_midipage_save(); /* what is this doing here? */
165 	cfg_atexit_save();
166 	status_text_flash("Configuration saved");
167 }
168 
preferences_load_page(struct page * page)169 void preferences_load_page(struct page *page)
170 {
171 	char buf[64];
172 	char *ptr;
173 	int i, j;
174         int interp_modes;
175 
176         for (interp_modes = 0; interpolation_modes[interp_modes]; interp_modes++) {
177                 /* nothing */
178         }
179 
180 	page->title = "Preferences (Shift-F5)";
181 	page->draw_const = preferences_draw_const;
182 	page->set_page = preferences_set_page;
183         page->total_widgets = 13 + interp_modes;
184 	page->widgets = widgets_preferences;
185 	page->help_index = HELP_GLOBAL;
186 
187 	create_thumbbar(widgets_preferences + 0, 22, 14, 5, 0, 1, 1, change_volume, 0, VOLUME_SCALE);
188 	create_thumbbar(widgets_preferences + 1, 22, 15, 5, 0, 2, 2, change_volume, 0, VOLUME_SCALE);
189 
190 	for (i = 0; interpolation_modes[i]; i++) {
191 		sprintf(buf, "%d Bit, %s", audio_settings.bits, interpolation_modes[i]);
192 		ptr = str_dup(buf);
193 		create_togglebutton(widgets_preferences+i+2,
194 					6, 20 + (i * 3), 26,
195                                         i+1, i+3, i+2, interp_modes+11, i+3,
196 					change_mixer,
197 					ptr,
198 					2,
199 					interp_group);
200 	}
201 
202 	for (j = 0; j < 4; j++) {
203                 int n = i+(j*2);
204 		if (j == 0) n = i+1;
205 		create_thumbbar(widgets_preferences+i+2+(j*2),
206 						26, 23+(i*3)+j,
207 						21,
208 						n, i+(j*2)+4, i+(j*2)+3,
209 						change_eq,
210 						0, 127);
211 		n = i+(j*2)+5;
212 		if (j == 3) n--;
213 		create_thumbbar(widgets_preferences+i+3+(j*2),
214 						53, 23+(i*3)+j,
215 						21,
216 						i+(j*2)+1, n, i+(j*2)+4,
217 						change_eq,
218 						0, 127);
219 	}
220 	/* default EQ setting */
221 	widgets_preferences[i+2].d.thumbbar.value = 0;
222 	widgets_preferences[i+4].d.thumbbar.value = 16;
223 	widgets_preferences[i+6].d.thumbbar.value = 96;
224 	widgets_preferences[i+8].d.thumbbar.value = 127;
225 
226 	ramp_group[0] = i+10;
227 	ramp_group[1] = i+11;
228 	create_togglebutton(widgets_preferences+i+10,
229 			33,29+i*3,9,
230 			i+9,i+12,i+10,i+11,i+11,
231 			change_mixer,
232 			"Enabled",2,
233 			ramp_group);
234 
235 	create_togglebutton(widgets_preferences+i+11,
236 			46,29+i*3,9,
237 			i+9,i+12,i+10,i+13,i+13,
238 			change_mixer,
239 			"Disabled",1,
240 			ramp_group);
241 
242 	create_button(widgets_preferences+i+12,
243 			2, 44, 27,
244 			i+10, i+12, i+12, i+13, i+13,
245 			(void *) save_config_now,
246 			"Save Output Configuration", 2);
247 }
248 
249