Set x bit aka fix red...
[kugel-rb.git] / apps / enc_config.c
blob8f3237d2e6c98bad526dfa307a4ebb2b9ddc95a9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Michael Sevakis
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <sprintf.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "config.h"
26 #include "action.h"
27 #include "lang.h"
28 #include "misc.h"
29 #include "talk.h"
30 #include "general.h"
31 #include "codecs.h"
32 #include "menu.h"
33 #include "statusbar.h"
34 #include "settings.h"
35 #include "audio.h"
36 #include "pcm_record.h"
37 #include "enc_config.h"
38 #include "splash.h"
41 #define CALL_FN_(fn, ...) \
42 if (fn) fn(__VA_ARGS__)
44 static int enc_menuitem_callback(int action,
45 const struct menu_item_ex *this_item);
46 static int enc_menuitem_enteritem(int action,
47 const struct menu_item_ex *this_item);
48 static void enc_rec_settings_changed(struct encoder_config *cfg);
49 /* this is used by all encoder menu items,
50 MUST be initialised before the call to do_menu() */
51 static struct menucallback_data {
52 struct encoder_config *cfg;
53 bool global;
54 } menu_callback_data;
56 /** Function definitions for each codec - add these to enc_data
57 list following the definitions **/
59 /** aiff_enc.codec **/
61 /** mp3_enc.codec **/
62 /* mp3_enc: return encoder capabilities */
63 static void mp3_enc_get_caps(const struct encoder_config *cfg,
64 struct encoder_caps *caps,
65 bool for_config)
67 int i;
68 unsigned long bitr;
70 if (!for_config)
72 /* Overall encoder capabilities */
73 caps->samplerate_caps = MPEG1_SAMPR_CAPS | MPEG2_SAMPR_CAPS;
74 caps->channel_caps = CHN_CAP_ALL;
75 return;
78 /* Restrict caps based on config */
79 i = round_value_to_list32(cfg->mp3_enc.bitrate, mp3_enc_bitr,
80 MP3_ENC_NUM_BITR, false);
81 bitr = mp3_enc_bitr[i];
83 /* sample rate caps */
85 /* check if MPEG1 sample rates are available */
86 if ((bitr >= 32 && bitr <= 128) || bitr >= 160)
87 caps->samplerate_caps |= MPEG1_SAMPR_CAPS;
89 /* check if MPEG2 sample rates and mono are available */
90 if (bitr <= 160)
92 caps->samplerate_caps |= MPEG2_SAMPR_CAPS;
93 caps->channel_caps |= CHN_CAP_MONO;
96 /* check if stereo is available */
97 if (bitr >= 32)
98 caps->channel_caps |= CHN_CAP_STEREO;
99 } /* mp3_enc_get_caps */
101 /* mp3_enc: return the default configuration */
102 static void mp3_enc_default_config(struct encoder_config *cfg)
104 cfg->mp3_enc.bitrate = 128; /* default that works for all types */
105 } /* mp3_enc_default_config */
107 static void mp3_enc_convert_config(struct encoder_config *cfg,
108 bool global)
110 if (global)
112 global_settings.mp3_enc_config.bitrate =
113 round_value_to_list32(cfg->mp3_enc.bitrate, mp3_enc_bitr,
114 MP3_ENC_NUM_BITR, false);
116 else
118 if ((unsigned)global_settings.mp3_enc_config.bitrate > MP3_ENC_NUM_BITR)
119 global_settings.mp3_enc_config.bitrate = MP3_ENC_BITRATE_CFG_DEFAULT;
120 cfg->mp3_enc.bitrate = mp3_enc_bitr[global_settings.mp3_enc_config.bitrate];
122 } /* mp3_enc_convert_config */
124 /* mp3_enc: show the bitrate setting options */
125 static bool mp3_enc_bitrate(struct menucallback_data *data)
127 struct encoder_config *cfg = data->cfg;
128 static const struct opt_items items[] =
130 /* Available in MPEG Version: */
131 #ifdef HAVE_MPEG2_SAMPR
132 #if 0
133 /* this sounds awful no matter what */
134 { "8 kBit/s", TALK_ID(8, UNIT_KBIT) }, /* 2 */
135 #endif
136 /* mono only */
137 { "16 kBit/s", TALK_ID(16, UNIT_KBIT) }, /* 2 */
138 { "24 kBit/s", TALK_ID(24, UNIT_KBIT) }, /* 2 */
139 #endif /* HAVE_MPEG2_SAMPR */
140 /* stereo/mono */
141 { "32 kBit/s", TALK_ID(32, UNIT_KBIT) }, /* 1,2 */
142 { "40 kBit/s", TALK_ID(40, UNIT_KBIT) }, /* 1,2 */
143 { "48 kBit/s", TALK_ID(48, UNIT_KBIT) }, /* 1,2 */
144 { "56 kBit/s", TALK_ID(56, UNIT_KBIT) }, /* 1,2 */
145 { "64 kBit/s", TALK_ID(64, UNIT_KBIT) }, /* 1,2 */
146 { "80 kBit/s", TALK_ID(80, UNIT_KBIT) }, /* 1,2 */
147 { "96 kBit/s", TALK_ID(96, UNIT_KBIT) }, /* 1,2 */
148 { "112 kBit/s", TALK_ID(112, UNIT_KBIT) }, /* 1,2 */
149 { "128 kBit/s", TALK_ID(128, UNIT_KBIT) }, /* 1,2 */
150 /* Leave out 144 when there is both MPEG 1 and 2 */
151 #if defined(HAVE_MPEG2_SAMPR) && !defined (HAVE_MPEG1_SAMPR)
152 /* oddball MPEG2-only rate stuck in the middle */
153 { "144 kBit/s", TALK_ID(144, UNIT_KBIT) }, /* 2 */
154 #endif
155 { "160 kBit/s", TALK_ID(160, UNIT_KBIT) }, /* 1,2 */
156 #ifdef HAVE_MPEG1_SAMPR
157 /* stereo only */
158 { "192 kBit/s", TALK_ID(192, UNIT_KBIT) }, /* 1 */
159 { "224 kBit/s", TALK_ID(224, UNIT_KBIT) }, /* 1 */
160 { "256 kBit/s", TALK_ID(256, UNIT_KBIT) }, /* 1 */
161 { "320 kBit/s", TALK_ID(320, UNIT_KBIT) }, /* 1 */
162 #endif
165 unsigned long rate_list[ARRAYLEN(items)];
167 /* This is rather constant based upon the build but better than
168 storing and maintaining yet another list of numbers */
169 int n_rates = make_list_from_caps32(
170 MPEG1_BITR_CAPS | MPEG2_BITR_CAPS, mp3_enc_bitr,
172 #ifdef HAVE_MPEG1_SAMPR
173 | MPEG1_BITR_CAPS
174 #endif
175 #ifdef HAVE_MPEG2_SAMPR
176 #ifdef HAVE_MPEG1_SAMPR
177 | (MPEG2_BITR_CAPS & ~(MP3_BITR_CAP_144 | MP3_BITR_CAP_8))
178 #else
179 | (MPEG2_BITR_CAPS & ~(MP3_BITR_CAP_8))
180 #endif
181 #endif /* HAVE_MPEG2_SAMPR */
182 , rate_list);
184 int index = round_value_to_list32(cfg->mp3_enc.bitrate, rate_list,
185 n_rates, false);
186 bool res = set_option(str(LANG_BITRATE), &index, INT,
187 items, n_rates, NULL);
188 index = round_value_to_list32(rate_list[index], mp3_enc_bitr,
189 MP3_ENC_NUM_BITR, false);
190 cfg->mp3_enc.bitrate = mp3_enc_bitr[index];
192 return res;
193 } /* mp3_enc_bitrate */
195 /* mp3_enc configuration menu */
196 MENUITEM_FUNCTION(mp3_bitrate, MENU_FUNC_USEPARAM, ID2P(LANG_BITRATE),
197 mp3_enc_bitrate,
198 &menu_callback_data, enc_menuitem_callback, Icon_NOICON);
199 MAKE_MENU( mp3_enc_menu, ID2P(LANG_ENCODER_SETTINGS),
200 enc_menuitem_enteritem, Icon_NOICON,
201 &mp3_bitrate);
204 /** wav_enc.codec **/
205 /* wav_enc: show the configuration menu */
206 #if 0
207 MAKE_MENU( wav_enc_menu, ID2P(LANG_ENCODER_SETTINGS),
208 enc_menuitem_enteritem, Icon_NOICON,
210 #endif
212 /** wavpack_enc.codec **/
213 /* wavpack_enc: show the configuration menu */
214 #if 0
215 MAKE_MENU( wavpack_enc_menu, ID2P(LANG_ENCODER_SETTINGS),
216 enc_menuitem_enteritem, Icon_NOICON,
218 #endif
220 /** config function pointers and/or data for each codec **/
221 static const struct encoder_data
223 void (*get_caps)(const struct encoder_config *cfg,
224 struct encoder_caps *caps, bool for_config);
225 void (*default_cfg)(struct encoder_config *cfg);
226 void (*convert_cfg)(struct encoder_config *cfg , bool global);
227 const struct menu_item_ex *menu;
228 } enc_data[REC_NUM_FORMATS] =
230 /* aiff_enc.codec */
231 [REC_FORMAT_AIFF] = {
232 NULL,
233 NULL,
234 NULL,
235 NULL,
237 /* mp3_enc.codec */
238 [REC_FORMAT_MPA_L3] = {
239 mp3_enc_get_caps,
240 mp3_enc_default_config,
241 mp3_enc_convert_config,
242 &mp3_enc_menu,
244 /* wav_enc.codec */
245 [REC_FORMAT_PCM_WAV] = {
246 NULL,
247 NULL,
248 NULL,
249 NULL,
251 /* wavpack_enc.codec */
252 [REC_FORMAT_WAVPACK] = {
253 NULL,
254 NULL,
255 NULL,
256 NULL,
260 static inline bool rec_format_ok(int rec_format)
262 return (unsigned)rec_format < REC_NUM_FORMATS;
264 /* This is called before entering the menu with the encoder settings
265 Its needed to make sure the settings can take effect. */
266 static int enc_menuitem_enteritem(int action,
267 const struct menu_item_ex *this_item)
269 (void)this_item;
270 /* this struct must be init'ed before calling do_menu() so this is safe */
271 struct menucallback_data *data = &menu_callback_data;
272 if (action == ACTION_STD_OK) /* entering the item */
274 if (data->global)
275 global_to_encoder_config(data->cfg);
277 return action;
279 /* this is called when a encoder setting is exited
280 It is used to update the status bar and save the setting */
281 static int enc_menuitem_callback(int action,
282 const struct menu_item_ex *this_item)
284 struct menucallback_data *data =
285 (struct menucallback_data*)this_item->function->param;
287 if (action == ACTION_EXIT_MENUITEM)
289 /* If the setting being configured is global, it must be placed
290 in global_settings before updating the status bar for the
291 change to show upon exiting the item. */
292 if (data->global)
294 enc_rec_settings_changed(data->cfg);
295 encoder_config_to_global(data->cfg);
299 return action;
302 /* update settings dependent upon encoder settings */
303 static void enc_rec_settings_changed(struct encoder_config *cfg)
305 struct encoder_config enc_config;
306 struct encoder_caps caps;
307 long table[MAX(CHN_NUM_MODES, REC_NUM_FREQ)];
308 int n;
310 if (cfg == NULL)
312 cfg = &enc_config;
313 cfg->rec_format = global_settings.rec_format;
314 global_to_encoder_config(cfg);
317 /* have to sync other settings when encoder settings change */
318 if (!enc_get_caps(cfg, &caps, true))
319 return;
321 /* rec_channels */
322 n = make_list_from_caps32(CHN_CAP_ALL, NULL,
323 caps.channel_caps, table);
325 /* no zero check needed: encoder must support at least one
326 sample rate that recording supports or it shouldn't be in
327 available in the recording options */
328 n = round_value_to_list32(global_settings.rec_channels,
329 table, n, true);
330 global_settings.rec_channels = table[n];
332 /* rec_frequency */
333 n = make_list_from_caps32(REC_SAMPR_CAPS, rec_freq_sampr,
334 caps.samplerate_caps, table);
336 n = round_value_to_list32(
337 rec_freq_sampr[global_settings.rec_frequency],
338 table, n, false);
340 global_settings.rec_frequency = round_value_to_list32(
341 table[n], rec_freq_sampr, REC_NUM_FREQ, false);
342 } /* enc_rec_settings_changed */
344 /** public stuff **/
345 void global_to_encoder_config(struct encoder_config *cfg)
347 const struct encoder_data *data = &enc_data[cfg->rec_format];
348 CALL_FN_(data->convert_cfg, cfg, false);
349 } /* global_to_encoder_config */
351 void encoder_config_to_global(const struct encoder_config *cfg)
353 const struct encoder_data *data = &enc_data[cfg->rec_format];
354 CALL_FN_(data->convert_cfg, (struct encoder_config *)cfg, true);
355 } /* encoder_config_to_global */
357 bool enc_get_caps(const struct encoder_config *cfg,
358 struct encoder_caps *caps,
359 bool for_config)
361 /* get_caps expects caps to be zeroed first */
362 memset(caps, 0, sizeof (*caps));
364 if (!rec_format_ok(cfg->rec_format))
365 return false;
367 if (enc_data[cfg->rec_format].get_caps)
369 enc_data[cfg->rec_format].get_caps(cfg, caps, for_config);
371 else
373 /* If no function provided...defaults to all */
374 caps->samplerate_caps = SAMPR_CAP_ALL;
375 caps->channel_caps = CHN_CAP_ALL;
378 return true;
379 } /* enc_get_caps */
381 /* Initializes the config struct with default values */
382 bool enc_init_config(struct encoder_config *cfg)
384 if (!rec_format_ok(cfg->rec_format))
385 return false;
386 CALL_FN_(enc_data[cfg->rec_format].default_cfg, cfg);
387 return true;
388 } /* enc_init_config */
390 /** Encoder Menus **/
391 #if 0
392 bool enc_config_menu(struct encoder_config *cfg)
394 if (!rec_format_ok(cfg->rec_format))
395 return false;
396 if (enc_data[cfg->rec_format].menu)
398 menu_callback_data.cfg = &cfg;
399 menu_callback_data.global = false;
400 return do_menu(enc_data[cfg->rec_format].menu, NULL, NULL, false)
401 == MENU_ATTACHED_USB;
403 else
405 splash(HZ, str(LANG_NO_SETTINGS));
406 return false;
408 } /* enc_config_menu */
409 #endif
411 /** Global Settings **/
413 /* Reset all codecs to defaults */
414 void enc_global_settings_reset(void)
416 struct encoder_config cfg;
417 cfg.rec_format = 0;
421 global_to_encoder_config(&cfg);
422 enc_init_config(&cfg);
423 encoder_config_to_global(&cfg);
424 if (cfg.rec_format == global_settings.rec_format)
425 enc_rec_settings_changed(&cfg);
427 while (++cfg.rec_format < REC_NUM_FORMATS);
428 } /* enc_global_settings_reset */
430 /* Apply new settings */
431 void enc_global_settings_apply(void)
433 struct encoder_config cfg;
434 if (!rec_format_ok(global_settings.rec_format))
435 global_settings.rec_format = REC_FORMAT_DEFAULT;
437 cfg.rec_format = global_settings.rec_format;
438 global_to_encoder_config(&cfg);
439 enc_rec_settings_changed(&cfg);
440 encoder_config_to_global(&cfg);
441 } /* enc_global_settings_apply */
443 /* Show an encoder's config menu based on the global_settings.
444 Modified settings are placed in global_settings.enc_config. */
445 bool enc_global_config_menu(void)
447 struct encoder_config cfg;
449 if (!rec_format_ok(global_settings.rec_format))
450 global_settings.rec_format = REC_FORMAT_DEFAULT;
452 cfg.rec_format = global_settings.rec_format;
454 if (enc_data[cfg.rec_format].menu)
456 menu_callback_data.cfg = &cfg;
457 menu_callback_data.global = true;
458 return do_menu(enc_data[cfg.rec_format].menu, NULL, NULL, false)
459 == MENU_ATTACHED_USB;
461 else
463 splash(HZ, str(LANG_NO_SETTINGS));
464 return false;
466 } /* enc_global_config_menu */