summaryrefslogtreecommitdiffstats
path: root/apps/enc_config.c
blob: a971343cab5ff0757cc7ce292564585ce4861a41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 Michael Sevakis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include "action.h"
#include "lang.h"
#include "misc.h"
#include "talk.h"
#include "general.h"
#include "codecs.h"
#include "menu.h"
#include "settings.h"
#include "audio.h"
#include "pcm_record.h"
#include "enc_config.h"
#include "splash.h"


#define CALL_FN_(fn, ...) \
    if (fn) fn(__VA_ARGS__)

static int enc_menuitem_callback(int action,
                             const struct menu_item_ex *this_item,
                             struct gui_synclist *this_list);
static int enc_menuitem_enteritem(int action,
                             const struct menu_item_ex *this_item,
                             struct gui_synclist *this_list);
static void enc_rec_settings_changed(struct encoder_config *cfg);
/* this is used by all encoder menu items,
   MUST be initialised before the call to do_menu() */
static struct menucallback_data {
    struct encoder_config *cfg;
    bool global;
} menu_callback_data;

/** Function definitions for each codec - add these to enc_data
    list following the definitions **/

/** aiff_enc.codec **/

/** mp3_enc.codec **/
/* mp3_enc: return encoder capabilities */
static void mp3_enc_get_caps(const struct encoder_config *cfg,
                             struct encoder_caps *caps,
                             bool for_config)
{
    int i;
    unsigned long bitr;

    if (!for_config)
    {
        /* Overall encoder capabilities */
        caps->samplerate_caps = MPEG1_SAMPR_CAPS | MPEG2_SAMPR_CAPS;
        caps->channel_caps    = CHN_CAP_ALL;
        return;
    }

    /* Restrict caps based on config */
    i = round_value_to_list32(cfg->mp3_enc.bitrate, mp3_enc_bitr,
                              MP3_ENC_NUM_BITR, false);
    bitr = mp3_enc_bitr[i];

    /* sample rate caps */

    /* check if MPEG1 sample rates are available */
    if ((bitr >= 32 && bitr <= 128) || bitr >= 160)
        caps->samplerate_caps |= MPEG1_SAMPR_CAPS;

    /* check if MPEG2 sample rates and mono are available */
    if (bitr <= 160)
    {
        caps->samplerate_caps |= MPEG2_SAMPR_CAPS;
        caps->channel_caps |= CHN_CAP_MONO;
    }

    /* check if stereo is available */
    if (bitr >= 32)
        caps->channel_caps |= CHN_CAP_STEREO;
} /* mp3_enc_get_caps */

/* mp3_enc: return the default configuration */
static void mp3_enc_default_config(struct encoder_config *cfg)
{
    cfg->mp3_enc.bitrate = 128; /* default that works for all types */
} /* mp3_enc_default_config */

static void mp3_enc_convert_config(struct encoder_config *cfg,
                                   bool global)
{
    if (global)
    {
        global_settings.mp3_enc_config.bitrate =
            round_value_to_list32(cfg->mp3_enc.bitrate, mp3_enc_bitr,
                                  MP3_ENC_NUM_BITR, false);
    }
    else
    {
        if ((unsigned)global_settings.mp3_enc_config.bitrate > MP3_ENC_NUM_BITR)
            global_settings.mp3_enc_config.bitrate = MP3_ENC_BITRATE_CFG_DEFAULT;
        cfg->mp3_enc.bitrate = mp3_enc_bitr[global_settings.mp3_enc_config.bitrate];
    }
} /* mp3_enc_convert_config */

/* mp3_enc: show the bitrate setting options */
static bool mp3_enc_bitrate(struct menucallback_data *data)
{
    struct encoder_config *cfg = data->cfg;
    static const struct opt_items items[] =
    {
                            /* Available in MPEG Version: */
#ifdef HAVE_MPEG2_SAMPR
#if 0
        /* this sounds awful no matter what */
        { "8 kBit/s",   TALK_ID(8,   UNIT_KBIT) }, /*   2 */
#endif
        /* mono only */
        { "16 kBit/s",  TALK_ID(16,  UNIT_KBIT) }, /*   2 */
        { "24 kBit/s",  TALK_ID(24,  UNIT_KBIT) }, /*   2 */
#endif /* HAVE_MPEG2_SAMPR */
        /* stereo/mono */
        { "32 kBit/s",  TALK_ID(32,  UNIT_KBIT) }, /* 1,2 */
        { "40 kBit/s",  TALK_ID(40,  UNIT_KBIT) }, /* 1,2 */
        { "48 kBit/s",  TALK_ID(48,  UNIT_KBIT) }, /* 1,2 */
        { "56 kBit/s",  TALK_ID(56,  UNIT_KBIT) }, /* 1,2 */
        { "64 kBit/s",  TALK_ID(64,  UNIT_KBIT) }, /* 1,2 */
        { "80 kBit/s",  TALK_ID(80,  UNIT_KBIT) }, /* 1,2 */
        { "96 kBit/s",  TALK_ID(96,  UNIT_KBIT) }, /* 1,2 */
        { "112 kBit/s", TALK_ID(112, UNIT_KBIT) }, /* 1,2 */
        { "128 kBit/s", TALK_ID(128, UNIT_KBIT) }, /* 1,2 */
        /* Leave out 144 when there is both MPEG 1 and 2  */
#if defined(HAVE_MPEG2_SAMPR) && !defined (HAVE_MPEG1_SAMPR)
        /* oddball MPEG2-only rate stuck in the middle */
        { "144 kBit/s", TALK_ID(144, UNIT_KBIT) }, /*   2 */
#endif
        { "160 kBit/s", TALK_ID(160, UNIT_KBIT) }, /* 1,2 */
#ifdef HAVE_MPEG1_SAMPR
        /* stereo only */
        { "192 kBit/s", TALK_ID(192, UNIT_KBIT) }, /* 1   */
        { "224 kBit/s", TALK_ID(224, UNIT_KBIT) }, /* 1   */
        { "256 kBit/s", TALK_ID(256, UNIT_KBIT) }, /* 1   */
        { "320 kBit/s", TALK_ID(320, UNIT_KBIT) }, /* 1   */
#endif
    };

    unsigned long rate_list[ARRAYLEN(items)];

    /* This is rather constant based upon the build but better than
       storing and maintaining yet another list of numbers */
    int n_rates = make_list_from_caps32(
            MPEG1_BITR_CAPS | MPEG2_BITR_CAPS, mp3_enc_bitr,
            0
#ifdef HAVE_MPEG1_SAMPR
            | MPEG1_BITR_CAPS
#endif
#ifdef HAVE_MPEG2_SAMPR
#ifdef HAVE_MPEG1_SAMPR
            | (MPEG2_BITR_CAPS & ~(MP3_BITR_CAP_144 | MP3_BITR_CAP_8))
#else
            | (MPEG2_BITR_CAPS & ~(MP3_BITR_CAP_8))
#endif
#endif /* HAVE_MPEG2_SAMPR */
            , rate_list);

    int index = round_value_to_list32(cfg->mp3_enc.bitrate, rate_list,
                                      n_rates, false);
    bool res = set_option(str(LANG_BITRATE), &index, INT,
                          items, n_rates, NULL);
    index = round_value_to_list32(rate_list[index], mp3_enc_bitr,
                                  MP3_ENC_NUM_BITR, false);
    cfg->mp3_enc.bitrate = mp3_enc_bitr[index];

    return res;
} /* mp3_enc_bitrate */

/* mp3_enc configuration menu */
MENUITEM_FUNCTION(mp3_bitrate, MENU_FUNC_USEPARAM, ID2P(LANG_BITRATE),
                   mp3_enc_bitrate,
                   &menu_callback_data, enc_menuitem_callback, Icon_NOICON);
MAKE_MENU( mp3_enc_menu, ID2P(LANG_ENCODER_SETTINGS),
           enc_menuitem_enteritem, Icon_NOICON,
           &mp3_bitrate);


/** wav_enc.codec **/
/* wav_enc: show the configuration menu */
#if 0
MAKE_MENU( wav_enc_menu, ID2P(LANG_ENCODER_SETTINGS),
           enc_menuitem_enteritem, Icon_NOICON,
           );
#endif

/** wavpack_enc.codec **/
/* wavpack_enc: show the configuration menu */
#if 0
MAKE_MENU( wavpack_enc_menu, ID2P(LANG_ENCODER_SETTINGS),
           enc_menuitem_enteritem, Icon_NOICON,
           );
#endif

/** config function pointers and/or data for each codec **/
static const struct encoder_data
{
    void   (*get_caps)(const struct encoder_config *cfg,
                       struct encoder_caps *caps, bool for_config);
    void   (*default_cfg)(struct encoder_config *cfg);
    void   (*convert_cfg)(struct encoder_config *cfg , bool global);
    const struct menu_item_ex *menu;
} enc_data[REC_NUM_FORMATS] =
{
    /* aiff_enc.codec */
    [REC_FORMAT_AIFF] = {
        NULL,
        NULL,
        NULL,
        NULL,
    },
    /* mp3_enc.codec */
    [REC_FORMAT_MPA_L3] = {
        mp3_enc_get_caps,
        mp3_enc_default_config,
        mp3_enc_convert_config,
        &mp3_enc_menu,
    },
    /* wav_enc.codec */
    [REC_FORMAT_PCM_WAV] = {
        NULL,
        NULL,
        NULL,
        NULL,
    },
    /* wavpack_enc.codec */
    [REC_FORMAT_WAVPACK] = {
        NULL,
        NULL,
        NULL,
        NULL,
    },
};

static inline bool rec_format_ok(int rec_format)
{
    return (unsigned)rec_format < REC_NUM_FORMATS;
}
/* This is called before entering the menu with the encoder settings
   Its needed to make sure the settings can take effect. */
static int enc_menuitem_enteritem(int action,
                                  const struct menu_item_ex *this_item,
                                  struct gui_synclist *this_list)
{
    (void)this_item;
    (void)this_list;
    /* this struct must be init'ed before calling do_menu() so this is safe */
    struct menucallback_data *data = &menu_callback_data;
    if (action == ACTION_STD_OK) /* entering the item */
    {
        if (data->global)
            global_to_encoder_config(data->cfg);
    }
    return action;
}
/* this is called when a encoder setting is exited
   It is used to update the status bar and save the setting */
static int enc_menuitem_callback(int action,
                                 const struct menu_item_ex *this_item,
                                 struct gui_synclist *this_list)
{
    (void)this_list;
    struct menucallback_data *data = 
            (struct menucallback_data*)this_item->function->param;
    
    if (action == ACTION_EXIT_MENUITEM)
    {
        /* If the setting being configured is global, it must be placed
               in global_settings before updating the status bar for the
               change to show upon exiting the item. */
        if (data->global)
        {
            enc_rec_settings_changed(data->cfg);
            encoder_config_to_global(data->cfg);
        }

    }
    return action;
}

/* update settings dependent upon encoder settings */
static void enc_rec_settings_changed(struct encoder_config *cfg)
{
    struct encoder_config enc_config;
    struct encoder_caps caps;
    long table[MAX((int)CHN_NUM_MODES, (int)REC_NUM_FREQ)];
    int n;

    if (cfg == NULL)
    {
        cfg = &enc_config;
        cfg->rec_format = global_settings.rec_format;
        global_to_encoder_config(cfg);
    }

    /* have to sync other settings when encoder settings change */
    if (!enc_get_caps(cfg, &caps, true))
        return;

    /* rec_channels */
    n = make_list_from_caps32(CHN_CAP_ALL, NULL,
                              caps.channel_caps, table);

    /* no zero check needed: encoder must support at least one
       sample rate that recording supports or it shouldn't be in
       available in the recording options */
    n = round_value_to_list32(global_settings.rec_channels,
                              table, n, true);
    global_settings.rec_channels = table[n];

    /* rec_frequency */
    n = make_list_from_caps32(REC_SAMPR_CAPS, rec_freq_sampr,
                              caps.samplerate_caps, table);

    n = round_value_to_list32(
                rec_freq_sampr[global_settings.rec_frequency],
                table, n, false);

    global_settings.rec_frequency = round_value_to_list32(
            table[n], rec_freq_sampr, REC_NUM_FREQ, false);
} /* enc_rec_settings_changed */

/** public stuff **/
void global_to_encoder_config(struct encoder_config *cfg)
{
    const struct encoder_data *data = &enc_data[cfg->rec_format];
    CALL_FN_(data->convert_cfg, cfg, false);
} /* global_to_encoder_config */

void encoder_config_to_global(const struct encoder_config *cfg)
{
    const struct encoder_data *data = &enc_data[cfg->rec_format];
    CALL_FN_(data->convert_cfg, (struct encoder_config *)cfg, true);
} /* encoder_config_to_global */

bool enc_get_caps(const struct encoder_config *cfg,
                  struct encoder_caps *caps,
                  bool for_config)
{
    /* get_caps expects caps to be zeroed first */
    memset(caps, 0, sizeof (*caps));

    if (!rec_format_ok(cfg->rec_format))
        return false;

    if (enc_data[cfg->rec_format].get_caps)
    {
        enc_data[cfg->rec_format].get_caps(cfg, caps, for_config);
    }
    else
    {
        /* If no function provided...defaults to all */
        caps->samplerate_caps = SAMPR_CAP_ALL;
        caps->channel_caps    = CHN_CAP_ALL;
    }

    return true;
} /* enc_get_caps */

/* Initializes the config struct with default values */
bool enc_init_config(struct encoder_config *cfg)
{
    if (!rec_format_ok(cfg->rec_format))
        return false;
    CALL_FN_(enc_data[cfg->rec_format].default_cfg, cfg);
    return true;
} /* enc_init_config */

/** Encoder Menus **/
#if 0
bool enc_config_menu(struct encoder_config *cfg)
{
    if (!rec_format_ok(cfg->rec_format))
        return false;
    if (enc_data[cfg->rec_format].menu)
    {
        menu_callback_data.cfg = &cfg;
        menu_callback_data.global = false;
        return do_menu(enc_data[cfg->rec_format].menu, NULL, NULL, false)
                == MENU_ATTACHED_USB;
    }
    else
    {
        splash(HZ, ID2P(LANG_NO_SETTINGS));
        return false;
    }
} /* enc_config_menu */
#endif

/** Global Settings **/

/* Reset all codecs to defaults */
void enc_global_settings_reset(void)
{
    struct encoder_config cfg;
    cfg.rec_format = 0;

    do
    {
        global_to_encoder_config(&cfg);
        enc_init_config(&cfg);
        encoder_config_to_global(&cfg);
        if (cfg.rec_format == global_settings.rec_format)
            enc_rec_settings_changed(&cfg);
    }
    while (++cfg.rec_format < REC_NUM_FORMATS);
} /* enc_global_settings_reset */

/* Apply new settings */
void enc_global_settings_apply(void)
{
    struct encoder_config cfg;
    if (!rec_format_ok(global_settings.rec_format))
        global_settings.rec_format = REC_FORMAT_DEFAULT;

    cfg.rec_format = global_settings.rec_format;
    global_to_encoder_config(&cfg);
    enc_rec_settings_changed(&cfg);
    encoder_config_to_global(&cfg);
} /* enc_global_settings_apply */

/* Show an encoder's config menu based on the global_settings.
   Modified settings are placed in global_settings.enc_config. */
int enc_global_config_menu(void)
{
    struct encoder_config cfg;

    if (!rec_format_ok(global_settings.rec_format))
        global_settings.rec_format = REC_FORMAT_DEFAULT;

    cfg.rec_format = global_settings.rec_format;

    if (enc_data[cfg.rec_format].menu)
    {
        menu_callback_data.cfg = &cfg;
        menu_callback_data.global = true;
        int retmenu = do_menu(enc_data[cfg.rec_format].menu, NULL, NULL, false);
        return (retmenu == MENU_ATTACHED_USB) ? 1 : 0;
    }
    else
    {
        splash(HZ, ID2P(LANG_NO_SETTINGS));
        return 0;
    }
} /* enc_global_config_menu */