Player: Save a bit of space by only using 7 bytes/char in the glyph table.
[Rockbox.git] / apps / enc_config.c
blob2fc9fb74e7c65eee2dde46cc4743de8d66bce2f9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Michael Sevakis
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <sprintf.h>
21 #include <string.h>
22 #include "config.h"
23 #include "atoi.h"
24 #include "lang.h"
25 #include "misc.h"
26 #include "talk.h"
27 #include "general.h"
28 #include "codecs.h"
29 #include "menu.h"
30 #include "statusbar.h"
31 #include "settings.h"
32 #include "audio.h"
33 #include "pcm_record.h"
34 #include "enc_config.h"
36 #define MENU_ITEM_FN(fn) \
37 ((bool (*)(void))fn)
39 #define ENC_MENU_ITEM_FN(fn) \
40 ((bool (*)(struct encoder_config *))fn)
42 #define CALL_FN_(fn, ...) \
43 if (fn) fn(__VA_ARGS__)
45 static bool enc_run_menu(int m, const struct menu_item items[],
46 struct encoder_config *cfg, bool global);
47 static bool enc_no_config_menu(struct encoder_config *cfg, bool global);
48 static void enc_rec_settings_changed(struct encoder_config *cfg);
50 /** Function definitions for each codec - add these to enc_data
51 list following the definitions **/
53 /** aiff_enc.codec **/
55 /** mp3_enc.codec **/
56 /* mp3_enc: return encoder capabilities */
57 static void mp3_enc_get_caps(const struct encoder_config *cfg,
58 struct encoder_caps *caps,
59 bool for_config)
61 int i;
62 unsigned long bitr;
64 if (!for_config)
66 /* Overall encoder capabilities */
67 caps->samplerate_caps = MPEG1_SAMPR_CAPS | MPEG2_SAMPR_CAPS;
68 caps->channel_caps = CHN_CAP_ALL;
69 return;
72 /* Restrict caps based on config */
73 i = round_value_to_list32(cfg->mp3_enc.bitrate, mp3_enc_bitr,
74 MP3_ENC_NUM_BITR, false);
75 bitr = mp3_enc_bitr[i];
77 /* sample rate caps */
79 /* check if MPEG1 sample rates are available */
80 if ((bitr >= 32 && bitr <= 128) || bitr >= 160)
81 caps->samplerate_caps |= MPEG1_SAMPR_CAPS;
83 /* check if MPEG2 sample rates and mono are available */
84 if (bitr <= 160)
86 caps->samplerate_caps |= MPEG2_SAMPR_CAPS;
87 caps->channel_caps |= CHN_CAP_MONO;
90 /* check if stereo is available */
91 if (bitr >= 32)
92 caps->channel_caps |= CHN_CAP_STEREO;
93 } /* mp3_enc_get_caps */
95 /* mp3_enc: return the default configuration */
96 static void mp3_enc_default_config(struct encoder_config *cfg)
98 cfg->mp3_enc.bitrate = 128; /* default that works for all types */
99 } /* mp3_enc_default_config */
101 static void mp3_enc_convert_config(struct encoder_config *cfg,
102 bool global)
104 if (global)
106 global_settings.mp3_enc_config.bitrate =
107 round_value_to_list32(cfg->mp3_enc.bitrate, mp3_enc_bitr,
108 MP3_ENC_NUM_BITR, false);
110 else
112 if ((unsigned)global_settings.mp3_enc_config.bitrate > MP3_ENC_NUM_BITR)
113 global_settings.mp3_enc_config.bitrate = MP3_ENC_BITRATE_CFG_DEFAULT;
114 cfg->mp3_enc.bitrate = mp3_enc_bitr[global_settings.mp3_enc_config.bitrate];
116 } /* mp3_enc_convert_config */
118 /* mp3_enc: show the bitrate setting options */
119 static bool mp3_enc_bitrate(struct encoder_config *cfg)
121 static const struct opt_items items[] =
123 /* Available in MPEG Version: */
124 #ifdef HAVE_MPEG2_SAMPR
125 #if 0
126 /* this sounds awful no matter what */
127 { "8 kBit/s", TALK_ID(8, UNIT_KBIT) }, /* 2 */
128 #endif
129 /* mono only */
130 { "16 kBit/s", TALK_ID(16, UNIT_KBIT) }, /* 2 */
131 { "24 kBit/s", TALK_ID(24, UNIT_KBIT) }, /* 2 */
132 #endif
133 /* stereo/mono */
134 { "32 kBit/s", TALK_ID(32, UNIT_KBIT) }, /* 1,2 */
135 { "40 kBit/s", TALK_ID(40, UNIT_KBIT) }, /* 1,2 */
136 { "48 kBit/s", TALK_ID(48, UNIT_KBIT) }, /* 1,2 */
137 { "56 kBit/s", TALK_ID(56, UNIT_KBIT) }, /* 1,2 */
138 { "64 kBit/s", TALK_ID(64, UNIT_KBIT) }, /* 1,2 */
139 { "80 kBit/s", TALK_ID(80, UNIT_KBIT) }, /* 1,2 */
140 { "96 kBit/s", TALK_ID(96, UNIT_KBIT) }, /* 1,2 */
141 { "112 kBit/s", TALK_ID(112, UNIT_KBIT) }, /* 1,2 */
142 { "128 kBit/s", TALK_ID(128, UNIT_KBIT) }, /* 1,2 */
143 #if 0
144 /* oddball MPEG2-only rate stuck in the middle */
145 { "144 kBit/s", TALK_ID(144, UNIT_KBIT) }, /* 2 */
146 #endif
147 { "160 kBit/s", TALK_ID(160, UNIT_KBIT) }, /* 1,2 */
148 /* stereo only */
149 { "192 kBit/s", TALK_ID(192, UNIT_KBIT) }, /* 1 */
150 { "224 kBit/s", TALK_ID(224, UNIT_KBIT) }, /* 1 */
151 { "256 kBit/s", TALK_ID(256, UNIT_KBIT) }, /* 1 */
152 { "320 kBit/s", TALK_ID(320, UNIT_KBIT) }, /* 1 */
155 unsigned long rate_list[ARRAYLEN(items)];
157 /* This is rather constant based upon the build but better than
158 storing and maintaining yet another list of numbers */
159 int n_rates = make_list_from_caps32(
160 MPEG1_BITR_CAPS | MPEG2_BITR_CAPS, mp3_enc_bitr,
161 MPEG1_BITR_CAPS
162 #ifdef HAVE_MPEG2_SAMPR
163 | (MPEG2_BITR_CAPS & ~(MP3_BITR_CAP_144 | MP3_BITR_CAP_8))
164 #endif
165 , rate_list);
167 int index = round_value_to_list32(cfg->mp3_enc.bitrate, rate_list,
168 n_rates, false);
169 bool res = set_option(str(LANG_BITRATE), &index, INT,
170 items, n_rates, NULL);
171 index = round_value_to_list32(rate_list[index], mp3_enc_bitr,
172 MP3_ENC_NUM_BITR, false);
173 cfg->mp3_enc.bitrate = mp3_enc_bitr[index];
175 return res;
176 } /* mp3_enc_bitrate */
178 /* mp3_enc: show the configuration menu */
179 static bool mp3_enc_menu(struct encoder_config *cfg, bool global)
181 static const struct menu_item items[] =
183 { ID2P(LANG_BITRATE), MENU_ITEM_FN(mp3_enc_bitrate) }
186 bool result;
187 int m = menu_init(items, ARRAYLEN(items), NULL, NULL, NULL, NULL);
188 result = enc_run_menu(m, items, cfg, global);
189 menu_exit(m);
190 return result;
191 } /* mp3_enc_menu */
193 /** wav_enc.codec **/
194 /* wav_enc: show the configuration menu */
195 #if 0
196 static bool wav_enc_menu(struct encoder_config *cfg, bool global);
197 #endif
199 /** wavpack_enc.codec **/
200 /* wavpack_enc: show the configuration menu */
201 #if 0
202 static bool wavpack_enc_menu(struct encoder_config *cfg, bool global);
203 #endif
205 /** config function pointers and/or data for each codec **/
206 static const struct encoder_data
208 void (*get_caps)(const struct encoder_config *cfg,
209 struct encoder_caps *caps, bool for_config);
210 void (*default_cfg)(struct encoder_config *cfg);
211 void (*convert_cfg)(struct encoder_config *cfg , bool global);
212 bool (*menu)(struct encoder_config *cfg , bool global);
213 } enc_data[REC_NUM_FORMATS] =
215 /* aiff_enc.codec */
216 [REC_FORMAT_AIFF] = {
217 NULL,
218 NULL,
219 NULL,
220 enc_no_config_menu,
222 /* mp3_enc.codec */
223 [REC_FORMAT_MPA_L3] = {
224 mp3_enc_get_caps,
225 mp3_enc_default_config,
226 mp3_enc_convert_config,
227 mp3_enc_menu,
229 /* wav_enc.codec */
230 [REC_FORMAT_PCM_WAV] = {
231 NULL,
232 NULL,
233 NULL,
234 enc_no_config_menu,
236 /* wavpack_enc.codec */
237 [REC_FORMAT_WAVPACK] = {
238 NULL,
239 NULL,
240 NULL,
241 enc_no_config_menu,
245 static inline bool rec_format_ok(int rec_format)
247 return (unsigned)rec_format < REC_NUM_FORMATS;
250 static bool enc_run_menu(int m, const struct menu_item items[],
251 struct encoder_config *cfg, bool global)
253 while (1)
255 int selected = menu_show(m);
257 switch (selected)
259 case MENU_SELECTED_EXIT:
260 return false;
262 case MENU_ATTACHED_USB:
263 return true;
265 default:
266 if (items[selected].function == NULL)
267 break;
269 /* If the setting being configured is global, it must be placed
270 in global_settings before updating the status bar for the
271 change to show upon exiting the item. */
272 if (global)
273 global_to_encoder_config(cfg);
275 if (ENC_MENU_ITEM_FN(items[selected].function)(cfg))
276 return true;
278 if (global)
280 enc_rec_settings_changed(cfg);
281 encoder_config_to_global(cfg);
284 gui_syncstatusbar_draw(&statusbars, true);
287 } /* enc_run_menu */
289 /* menu created when encoder has no configuration options */
290 static bool enc_no_config_menu(struct encoder_config *cfg, bool global)
292 static const struct menu_item items[] =
294 { ID2P(LANG_NO_SETTINGS), NULL }
296 int m;
297 bool result;
299 m = menu_init(items, ARRAYLEN(items), NULL, NULL, NULL, NULL);
300 result = enc_run_menu(m, items, NULL, false);
301 menu_exit(m);
303 return result;
304 (void)cfg;
305 (void)global;
306 } /* enc_no_config_menu */
308 /* update settings dependent upon encoder settings */
309 static void enc_rec_settings_changed(struct encoder_config *cfg)
311 struct encoder_config enc_config;
312 struct encoder_caps caps;
313 long table[MAX(CHN_NUM_MODES, REC_NUM_FREQ)];
314 int n;
316 if (cfg == NULL)
318 cfg = &enc_config;
319 cfg->rec_format = global_settings.rec_format;
320 global_to_encoder_config(cfg);
323 /* have to sync other settings when encoder settings change */
324 if (!enc_get_caps(cfg, &caps, true))
325 return;
327 /* rec_channels */
328 n = make_list_from_caps32(CHN_CAP_ALL, NULL,
329 caps.channel_caps, table);
331 /* no zero check needed: encoder must support at least one
332 sample rate that recording supports or it shouldn't be in
333 available in the recording options */
334 n = round_value_to_list32(global_settings.rec_channels,
335 table, n, true);
336 global_settings.rec_channels = table[n];
338 /* rec_frequency */
339 n = make_list_from_caps32(REC_SAMPR_CAPS, rec_freq_sampr,
340 caps.samplerate_caps, table);
342 n = round_value_to_list32(
343 rec_freq_sampr[global_settings.rec_frequency],
344 table, n, false);
346 global_settings.rec_frequency = round_value_to_list32(
347 table[n], rec_freq_sampr, REC_NUM_FREQ, false);
348 } /* enc_rec_settings_changed */
350 /** public stuff **/
351 void global_to_encoder_config(struct encoder_config *cfg)
353 const struct encoder_data *data = &enc_data[cfg->rec_format];
354 CALL_FN_(data->convert_cfg, cfg, false);
355 } /* global_to_encoder_config */
357 void encoder_config_to_global(const struct encoder_config *cfg)
359 const struct encoder_data *data = &enc_data[cfg->rec_format];
360 CALL_FN_(data->convert_cfg, (struct encoder_config *)cfg, true);
361 } /* encoder_config_to_global */
363 bool enc_get_caps(const struct encoder_config *cfg,
364 struct encoder_caps *caps,
365 bool for_config)
367 /* get_caps expects caps to be zeroed first */
368 memset(caps, 0, sizeof (*caps));
370 if (!rec_format_ok(cfg->rec_format))
371 return false;
373 if (enc_data[cfg->rec_format].get_caps)
375 enc_data[cfg->rec_format].get_caps(cfg, caps, for_config);
377 else
379 /* If no function provided...defaults to all */
380 caps->samplerate_caps = SAMPR_CAP_ALL;
381 caps->channel_caps = CHN_CAP_ALL;
384 return true;
385 } /* enc_get_caps */
387 /* Initializes the config struct with default values */
388 bool enc_init_config(struct encoder_config *cfg)
390 if (!rec_format_ok(cfg->rec_format))
391 return false;
392 CALL_FN_(enc_data[cfg->rec_format].default_cfg, cfg);
393 return true;
394 } /* enc_init_config */
396 /** Encoder Menus **/
397 #if 0
398 bool enc_config_menu(struct encoder_config *cfg)
400 if (!rec_format_ok(cfg->rec_format))
401 return false;
402 return enc_data[cfg->rec_format].menu(cfg, false);
403 } /* enc_config_menu */
404 #endif
406 /** Global Settings **/
408 /* Reset all codecs to defaults */
409 void enc_global_settings_reset(void)
411 struct encoder_config cfg;
412 cfg.rec_format = 0;
416 global_to_encoder_config(&cfg);
417 enc_init_config(&cfg);
418 encoder_config_to_global(&cfg);
419 if (cfg.rec_format == global_settings.rec_format)
420 enc_rec_settings_changed(&cfg);
422 while (++cfg.rec_format < REC_NUM_FORMATS);
423 } /* enc_global_settings_reset */
425 /* Apply new settings */
426 void enc_global_settings_apply(void)
428 struct encoder_config cfg;
429 if (!rec_format_ok(global_settings.rec_format))
430 global_settings.rec_format = REC_FORMAT_DEFAULT;
432 cfg.rec_format = global_settings.rec_format;
433 global_to_encoder_config(&cfg);
434 enc_rec_settings_changed(&cfg);
435 encoder_config_to_global(&cfg);
436 } /* enc_global_settings_apply */
438 /* Show an encoder's config menu based on the global_settings.
439 Modified settings are placed in global_settings.enc_config. */
440 bool enc_global_config_menu(void)
442 struct encoder_config cfg;
444 if (!rec_format_ok(global_settings.rec_format))
445 global_settings.rec_format = REC_FORMAT_DEFAULT;
447 cfg.rec_format = global_settings.rec_format;
449 return enc_data[cfg.rec_format].menu(&cfg, true);
450 } /* enc_global_config_menu */