summaryrefslogtreecommitdiffstats
path: root/polyp/channelmap.c
blob: 97876528ab2c323fd052b5711c82d0479ec487a3 (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
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio 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 of the License,
  or (at your option) any later version.
 
  polypaudio 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
  General Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public License
  along with polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>

#include "channelmap.h"


struct pa_channel_map* pa_channel_map_init(struct pa_channel_map *m) {
    unsigned c;
    assert(m);

    for (c = 0; c < PA_CHANNELS_MAX; c++)
        m->map[c] = PA_CHANNEL_POSITION_INVALID;

    return m;
}

struct pa_channel_map* pa_channel_map_init_mono(struct pa_channel_map *m) {
    assert(m);

    pa_channel_map_init(m);
    m->map[0] = PA_CHANNEL_POSITION_MONO;
    return m;
}

struct pa_channel_map* pa_channel_map_init_stereo(struct pa_channel_map *m) {
    assert(m);

    pa_channel_map_init(m);
    m->map[0] = PA_CHANNEL_POSITION_LEFT;
    m->map[1] = PA_CHANNEL_POSITION_RIGHT;
    return m;
}

struct pa_channel_map* pa_channel_map_init_auto(struct pa_channel_map *m, int channels) {
    assert(m);
    assert(channels > 0);

    pa_channel_map_init(m);
    
    switch (channels) {
        case 1:
            m->map[0] = PA_CHANNEL_POSITION_MONO;
            return m;

        case 8:
            m->mpa[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
            m->mpa[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
            /* Fall through */
            
        case 6:
            m->mpa[5] = PA_CHANNEL_POSITION_LFE;
            /* Fall through */
            
        case 5:
            m->map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
            /* Fall through */
            
        case 4:
            m->map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
            m->map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
            /* Fall through */
            
        case 2:
            m->map[0] = PA_CHANNEL_MAP_FRONT_LEFT;
            m->map[1] = PA_CHANNEL_MAP_FRONT_RIGHT;
            return m;
            
        default:
            return NULL;
    }
}


const char* pa_channel_position_to_string(pa_channel_position_t pos) {
    const char *const table[] = {
        [PA_CHANNEL_POSITION_MONO] = "mono",

        [PA_CHANNEL_POSITION_FRONT_CENTER] = "front-center",
        [PA_CHANNEL_POSITION_FRONT_LEFT] = "front-left",
        [PA_CHANNEL_POSITION_FRONT_RIGHT] = "front-right",
        
        [PA_CHANNEL_POSITION_REAR_CENTER] = "rear-center",
        [PA_CHANNEL_POSITION_REAR_LEFT] = "rear-left",
        [PA_CHANNEL_POSITION_REAR_RIGHT] = "rear-right",

        [PA_CHANNEL_POSITION_LFE] = "lfe",

        [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "front-left-of-center",
        [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "front-right-of-center",
        
        [PA_CHANNEL_POSITION_SIDE_LEFT] = "side-left",
        [PA_CHANNEL_POSITION_SIDE_RIGHT] = "side-right"
    };

    if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
        return NULL;

    return table[pos];
}

int pa_channel_map_equal(struct pa_channel_map *a, struct pa_channel_map *b, int channels) {
    char c;
    
    assert(a);
    assert(b);
    assert(channels > 0);

    if (channels > PA_CHANNELS_MAX)
        channels = PA_CHANNELS_MAX;

    for (c = 0; c < channels; c++)
        if (a->map[c] != b->map[c])
            return 1;

    return 0;
}