summaryrefslogtreecommitdiff
path: root/dxr3audio.c
blob: fa61708ddb01ad7192131f3704ea0640de0222f0 (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
/*
 * dxr3audio.c
 *
 * Copyright (C) 2009 Christian Gmeiner
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <string.h>     // for memeset
#include <math.h>       // for pow
#include <vdr/device.h> // for MAXVOLUME

#include "dxr3audio.h"

const static int AUDIO_STEREO = 0;
const static int AUDIO_MONO_LEFT = 1;
const static int AUDIO_MONO_RIGHT = 2;

iAudio::iAudio() : open(false), enabled(false), vol(0), audioChannel(0), digitalAudio(false)
{
    memset(&curContext, 0, sizeof(SampleContext));
}

void iAudio::changeVolume(int16_t* pcmbuf, size_t size)
{
    if (vol == 0) {
	    memset(pcmbuf, 0, size);
    } else if (vol < MAXVOLUME) {

	    int factor = (int)(pow(10.0, -2.5 + 2.5*vol/256.0)*0x7FFF);
        size = size / 2;

        while (size > 0) {
            register int32_t tmp = (int32_t)(*pcmbuf) * factor;
            *pcmbuf = (int16_t) (tmp>>15);
            pcmbuf++;
            size--;
        }
    }

    // respect audio channel setting
    if (audioChannel == AUDIO_STEREO)
        return;

    if (audioChannel == AUDIO_MONO_RIGHT) {

        for (unsigned int i = 0; i < size / sizeof(short); i++) {
            if (!(i & 0x1))
                pcmbuf[i] = pcmbuf[i+1];
        }
    } else if (audioChannel == AUDIO_MONO_LEFT) {

        for (unsigned int i = 0; i < size / sizeof(short); i++) {
            if ((i & 0x1))
                pcmbuf[i] = pcmbuf[i-1];
        }
    }
}