summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/dsp/dsp_sample_io.c
blob: 8a0d5da7ccbe5da26fc22341170b3dd7ca7caae1 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Miika Pekkarinen
 * 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 "rbcodecconfig.h"
#include "platform.h"
#include "dsp_core.h"
#include "dsp_sample_io.h"
#include "dsp_proc_entry.h"
#include "fixedpoint.h"

/* increment the format version counter */
static void format_change_set(struct sample_io_data *this)
{
    if (this->format_dirty)
        return;

    this->format.version = (uint8_t)(this->format.version + 1) ?: 1;
    this->format_dirty = 1;
}

void dsp_sample_io_init(struct sample_io_data *this, unsigned int dsp_id)
{
    this->output_sampr = DSP_OUT_DEFAULT_HZ;
    dsp_sample_input_init(this, dsp_id);
    dsp_sample_output_init(this);
}

bool dsp_sample_io_configure(struct sample_io_data *this,
                             unsigned int setting,
                             intptr_t *value_p)
{
    intptr_t value = *value_p;

    switch (setting)
    {
    case DSP_RESET:
        /* Reset all sample descriptions to default */
        format_change_set(this);
        this->format.num_channels = 2;
        this->format.frac_bits = WORD_FRACBITS;
        this->format.output_scale = WORD_FRACBITS + 1 - NATIVE_DEPTH;
        this->format.frequency = this->output_sampr;
        this->format.codec_frequency = this->output_sampr;
        this->sample_depth = NATIVE_DEPTH;
        this->stereo_mode = STEREO_NONINTERLEAVED;
        break;

    case DSP_SET_FREQUENCY:
        format_change_set(this);
        value = value > 0 ? (unsigned int)value : this->output_sampr;
        this->format.frequency = value;
        this->format.codec_frequency = value;
        break;

    case DSP_SET_SAMPLE_DEPTH:
        format_change_set(this);
        this->format.frac_bits =
            value <= NATIVE_DEPTH ? WORD_FRACBITS : value;
        this->format.output_scale =
            this->format.frac_bits + 1 - NATIVE_DEPTH;
        this->sample_depth = value;
        break;

    case DSP_SET_STEREO_MODE:
        format_change_set(this);
        this->format.num_channels = value == STEREO_MONO ? 1 : 2;
        this->stereo_mode = value;
        break;

    case DSP_FLUSH:
        dsp_sample_input_flush(this);
        dsp_sample_output_flush(this);
        break;

    case DSP_SET_PITCH:
        format_change_set(this);
        value = value > 0 ? value : (1 << 16);
        this->format.frequency =
            fp_mul(value, this->format.codec_frequency, 16);
        break;

    case DSP_SET_OUT_FREQUENCY:
        value = value > 0 ? value : DSP_OUT_DEFAULT_HZ;
        value = MIN(DSP_OUT_MAX_HZ, MAX(DSP_OUT_MIN_HZ, value));
        *value_p = value;

        if ((unsigned int)value == this->output_sampr)
            return true; /* No change; don't broadcast */

        this->output_sampr = value;
        break;

    case DSP_GET_OUT_FREQUENCY:
        *value_p = this->output_sampr;
        return true; /* Only I/O handles it */
    }

    return false;
}