summaryrefslogtreecommitdiff
path: root/lib/rbcodec/dsp/channel_mode.c
blob: 5259b46b111b5225468d28460428804c63a8c260 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 Thom Johansen
 * Copyright (C) 2012 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 "config.h"
#include "system.h"
#include "sound.h"
#include "fixedpoint.h"
#include "fracmul.h"
#include "dsp_proc_entry.h"
#include "channel_mode.h"

#if 0
/* SOUND_CHAN_STEREO mode is a noop so has no function - just outline one for
 * completeness. */
void channel_mode_proc_stereo(struct dsp_proc_entry *this,
                              struct dsp_buffer **buf_p);
#endif
void channel_mode_proc_mono(struct dsp_proc_entry *this,
                            struct dsp_buffer **buf_p);
void channel_mode_proc_mono_left(struct dsp_proc_entry *this,
                                 struct dsp_buffer **buf_p);
void channel_mode_proc_mono_right(struct dsp_proc_entry *this,
                                  struct dsp_buffer **buf_p);
void channel_mode_proc_custom(struct dsp_proc_entry *this,
                              struct dsp_buffer **buf_p);
void channel_mode_proc_karaoke(struct dsp_proc_entry *this,
                               struct dsp_buffer **buf_p);

static struct channel_mode_data
{
    long  sw_gain;   /* 00h: for mode: custom */
    long  sw_cross;  /* 04h: for mode: custom */
    struct dsp_config *dsp;
    int   mode;
    const dsp_proc_fn_type fns[SOUND_CHAN_NUM_MODES];
} channel_mode_data =
{
    .sw_gain = 0,
    .sw_cross = 0,
    .mode = SOUND_CHAN_STEREO,
    .fns =
    {
        [SOUND_CHAN_STEREO]     = NULL,
        [SOUND_CHAN_MONO]       = channel_mode_proc_mono,
        [SOUND_CHAN_CUSTOM]     = channel_mode_proc_custom,
        [SOUND_CHAN_MONO_LEFT]  = channel_mode_proc_mono_left,
        [SOUND_CHAN_MONO_RIGHT] = channel_mode_proc_mono_right,
        [SOUND_CHAN_KARAOKE]    = channel_mode_proc_karaoke,
    },
};

static dsp_proc_fn_type get_process_fn(void)
{
    return channel_mode_data.fns[channel_mode_data.mode];
}

#if 0
/* SOUND_CHAN_STEREO mode is a noop so has no function - just outline one for
 * completeness. */
void channel_mode_proc_stereo(struct dsp_proc_entry *this,
                              struct dsp_buffer **buf_p)
{
    /* The channels are each just themselves */
    (void)this; (void)buf_p;
}
#endif

#if !defined(CPU_COLDFIRE) && !defined(CPU_ARM)
/* Unoptimized routines */
void channel_mode_proc_mono(struct dsp_proc_entry *this,
                            struct dsp_buffer **buf_p)
{
    struct dsp_buffer *buf = *buf_p;
    int32_t *sl = buf->p32[0];
    int32_t *sr = buf->p32[1];
    int count = buf->remcount;

    do
    {
        int32_t lr = *sl / 2 + *sr / 2;
        *sl++ = lr;
        *sr++ = lr;
    }
    while (--count > 0);

    (void)this;
}

void channel_mode_proc_custom(struct dsp_proc_entry *this,
                              struct dsp_buffer **buf_p)
{
    struct channel_mode_data *data = (void *)this->data;
    struct dsp_buffer *buf = *buf_p;

    int32_t *sl = buf->p32[0];
    int32_t *sr = buf->p32[1];
    int count = buf->remcount;

    const int32_t gain  = data->sw_gain;
    const int32_t cross = data->sw_cross;

    do
    {
        int32_t l = *sl;
        int32_t r = *sr;
        *sl++ = FRACMUL(l, gain) + FRACMUL(r, cross);
        *sr++ = FRACMUL(r, gain) + FRACMUL(l, cross);
    }
    while (--count > 0);
}

void channel_mode_proc_karaoke(struct dsp_proc_entry *this,
                               struct dsp_buffer **buf_p)
{
    struct dsp_buffer *buf = *buf_p;
    int32_t *sl = buf->p32[0];
    int32_t *sr = buf->p32[1];
    int count = buf->remcount;

    do
    {
        int32_t ch = *sl / 2 - *sr / 2;
        *sl++ = ch;
        *sr++ = -ch;
    }
    while (--count > 0);

    (void)this;
}
#endif /* CPU */

void channel_mode_proc_mono_left(struct dsp_proc_entry *this,
                                 struct dsp_buffer **buf_p)
{
    /* Just copy over the other channel */
    struct dsp_buffer *buf = *buf_p;
    memcpy(buf->p32[1], buf->p32[0], buf->remcount * sizeof (int32_t));
    (void)this;
}

void channel_mode_proc_mono_right(struct dsp_proc_entry *this,
                                  struct dsp_buffer **buf_p)
{
    /* Just copy over the other channel */
    struct dsp_buffer *buf = *buf_p;
    memcpy(buf->p32[0], buf->p32[1], buf->remcount * sizeof (int32_t));
    (void)this;
}

/* This is the initial function pointer when first enabled/changed in order
 * to facilitate verification of the format compatibility at the proper time
 * This gets called for changes even if stage is inactive. */
static void channel_mode_process_new_format(struct dsp_proc_entry *this,
                                            struct dsp_buffer **buf_p)
{
    struct channel_mode_data *data = (void *)this->data;
    struct dsp_buffer *buf = *buf_p;

    DSP_PRINT_FORMAT(DSP_PROC_CHANNEL_MODE, DSP_PROC_CHANNEL_MODE,
                     buf->format);

    bool active = buf->format.num_channels >= 2;
    dsp_proc_activate(data->dsp, DSP_PROC_CHANNEL_MODE, active);

    if (!active)
    {
        /* Can't do this. Sleep until next change. */
        DEBUGF("  DSP_PROC_CHANNEL_MODE- deactivated\n");
        return;
    }

    /* Switch to the real function and call it once */
    this->process[0] = get_process_fn();
    dsp_proc_call(this, buf_p, (unsigned)buf->format.changed - 1);
}

void channel_mode_set_config(int value)
{
    if (value < 0 || value >= SOUND_CHAN_NUM_MODES)
        value = SOUND_CHAN_STEREO; /* Out of range */

    if (value == channel_mode_data.mode)
        return;

    channel_mode_data.mode = value;
    dsp_proc_enable(dsp_get_config(CODEC_IDX_AUDIO), DSP_PROC_CHANNEL_MODE,
                    value != SOUND_CHAN_STEREO);
}

void channel_mode_custom_set_width(int value)
{
    long width, straight, cross;

    width = value * 0x7fffff / 100;

    if (value <= 100)
    {
        straight = (0x7fffff + width) / 2;
        cross = straight - width;
    }
    else
    {
        /* straight = (1 + width) / (2 * width) */
        straight = fp_div(0x7fffff + width, width, 22);
        cross = straight - 0x7fffff;
    }

    channel_mode_data.sw_gain  = straight << 8;
    channel_mode_data.sw_cross = cross << 8;
}

/* DSP message hook */
static intptr_t channel_mode_configure(struct dsp_proc_entry *this,
                                       struct dsp_config *dsp,
                                       unsigned int setting,
                                       intptr_t value)
{
    switch (setting)
    {
    case DSP_PROC_INIT:
        if (value == 0)
        {
            /* New object */
            this->data = (intptr_t)&channel_mode_data;
            this->process[1] = channel_mode_process_new_format;
            ((struct channel_mode_data *)this->data)->dsp = dsp;
        }

        /* Force format change call each time */
        this->process[0] = channel_mode_process_new_format;
        dsp_proc_activate(dsp, DSP_PROC_CHANNEL_MODE, true);
        break;

    case DSP_PROC_CLOSE:
        ((struct channel_mode_data *)this->data)->dsp = NULL;
        break;
    }

    return 1;
}

/* Database entry */
DSP_PROC_DB_ENTRY(
    CHANNEL_MODE,
    channel_mode_configure);