File: convert_util.c

package info (click to toggle)
rat 4.2.22-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 3,717
  • sloc: ansic: 36,542; tcl: 2,740; sh: 2,675; makefile: 295
file content (124 lines) | stat: -rw-r--r-- 3,086 bytes parent folder | download | duplicates (5)
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
/*
 * FILE:    converter.c
 * PROGRAM: RAT
 * AUTHOR:  O.Hodson <O.Hodson@cs.ucl.ac.uk>
 *
 * Copyright (c) 1998-2001 University College London
 * All rights reserved.
 */
 
#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] = 
	"$Id: convert_util.c,v 1.11 2001/01/08 20:30:00 ucaccsp Exp $";
#endif /* HIDE_SOURCE_STRINGS */

#include "config_unix.h"
#include "config_win32.h"
#include "audio_types.h"
#include "converter_types.h"
#include "convert_util.h"
#include "debug.h"

/* Mono-Stereo Conversion ***************************************************/ 
/* Note src_len is length block in number of samples                        */
/* i.e nChannels * nSamplingIntervals                                       */

void
converter_change_channels (sample *src, 
                           int src_len, 
                           int src_channels, 
                           sample *dst, 
                           int dst_len, 
                           int dst_channels)
{
        int di, si;
        int t;

        assert(src_channels == 1 || src_channels == 2);
        assert(dst_channels == 1 || dst_channels == 2);
        assert(dst_channels != src_channels);
        assert(src_len/src_channels == dst_len/dst_channels);

        if (src_len == 0) {
                return;
        }

        /* Differing directions of conversions means we can do in place        
         * conversion if necessary.
         */

        switch(src_channels) {
        case 1:
                di = dst_len - 1;
                si = src_len - 1;
                do {
                        dst[di--] = src[si];
                        dst[di--] = src[si--];
                } while (si >= 0);
                assert(di == si);
                break;
        case 2:
                si = 0;
                di = 0;
                do {
                        t  = src[si++];
                        t += src[si++];
                        t /= 2;
                        dst[di++] = t;
                } while (si != src_len);
                assert(di == dst_len);
                break;
        }
        UNUSED(dst_channels);
}

int
gcd (int a, int b)
{
        if (b) return gcd(b, a%b);
        return a;
}

int
conversion_steps(int f1, int f2) 
{
        int minf, maxf, r;

        minf = min(f1, f2);
        maxf = max(f1, f2);
        r = maxf / minf;

        if (f1 == f2) {
                return 0;
        } else if (r * minf == maxf) {
                return 1;
        } else {
                return 2;
        }
}

int
converter_format_valid(const converter_fmt_t *cfmt)
{
        if (cfmt->src_freq % 8000 &&
            cfmt->src_freq % 11025) {
                return FALSE;
        }

        if (cfmt->src_channels != 1 &&
            cfmt->src_channels != 2) {
                return FALSE;
        }

        if (cfmt->dst_freq % 8000 &&
            cfmt->dst_freq % 11025) {
                return FALSE;
        }

        if (cfmt->dst_channels != 1 &&
            cfmt->dst_channels != 2) {
                return FALSE;
        }

        return TRUE;
}