Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
478 andreas 1
/*
2
 * Copyright (C) 2024 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "taudioconvert.h"
20
#include "terror.h"
21
 
22
TAudioConvert::TAudioConvert()
23
{
24
    DECL_TRACER("TAudioConvert::TAudioConvert()");
25
}
26
 
27
TAudioConvert::~TAudioConvert()
28
{
29
    DECL_TRACER("TAudioConvert::~TAudioConvert()");
30
}
31
 
32
int16_t TAudioConvert::uLawDecodeDigital(int8_t number)
33
{
34
    const uint16_t MULAW_BIAS = 33;
35
    uint8_t sign = 0, position = 0;
36
    int16_t decoded = 0;
37
    number = ~number;
38
 
39
    if (number & 0x80)
40
    {
41
        number &= ~(1 << 7);
42
        sign = -1;
43
    }
44
 
45
    position = ((number & 0xF0) >> 4) + 5;
46
    decoded = ((1 << position) | ((number & 0x0F) << (position - 4)) | (1 << (position - 5))) - MULAW_BIAS;
47
    return (sign == 0) ? (decoded) : (-(decoded));
48
}
49
 
50
int8_t TAudioConvert::uLawEncodeDigital(int16_t number)
51
{
52
    const uint16_t MULAW_MAX = 0x1FFF;
53
    const uint16_t MULAW_BIAS = 33;
54
    uint16_t mask = 0x1000;
55
    uint8_t sign = 0;
56
    uint8_t position = 12;
57
    uint8_t lsb = 0;
58
 
59
    if (number < 0)
60
    {
61
        number = -number;
62
        sign = 0x80;
63
    }
64
 
65
    number += MULAW_BIAS;
66
 
67
    if (number > MULAW_MAX)
68
        number = MULAW_MAX;
69
 
70
    for (; ((number & mask) != mask && position >= 5); mask >>= 1, position--)
71
        ;
72
 
73
    lsb = (number >> (position - 4)) & 0x0f;
74
    return (~(sign | ((position - 5) << 4) | lsb));
75
}
76
 
77
int8_t TAudioConvert::linearToMuLaw(int16_t sample)
78
{
79
    //We get the sign
80
    int sign = (sample >> 8) & 0x0080;
81
 
82
    if (sign != 0)
83
        sample = -sample;
84
 
85
    if (sample > cClip)
86
        sample = cClip;
87
 
88
    sample += cBias;
89
 
90
    int exponent = static_cast<int>(MuLawCompressTable[(sample >> 7) & 0xFF]);
91
    int mantissa = (sample >> (exponent + 3)) & 0x0F;
92
    int compressedByte = ~(sign | (exponent << 4) | mantissa);
93
 
94
    return static_cast<int8_t>(compressedByte);
95
}
96
 
97
int8_t TAudioConvert::linearToALaw(int16_t sample)
98
 
99
{
100
    int16_t sign, exponent, mantissa;
101
    uint8_t compressedByte;
102
 
103
    sign = ((~sample) >> 8) & 0x80;
104
 
105
    if (!sign)
106
        sample = (short)-sample;
107
 
108
    if (sample > cClip)
109
        sample = cClip;
110
 
111
    if (sample >= 256)
112
    {
113
        exponent = static_cast<int16_t>(ALawCompressTable[(sample >> 8) & 0x7F]);
114
        mantissa = (sample >> (exponent + 3) ) & 0x0F;
115
        compressedByte = ((exponent << 4) | mantissa);
116
    }
117
    else
118
        compressedByte = static_cast<uint8_t>(sample >> 4);
119
 
120
    compressedByte ^= (sign ^ 0x55);
121
    return compressedByte;
122
}
123
 
124
int16_t TAudioConvert::muLawToLinear(uint8_t ulawbyte)
125
{
126
    return MuLawDecompressTable[ulawbyte];
127
}
128
 
129
int16_t TAudioConvert::aLawToLinear(uint8_t alawbyte)
130
{
131
    return ALawDecompressTable[alawbyte];
132
}