Statistics
| Branch: | Revision:

ffmpeg / tests / audiogen.c @ 57060f89

History | View | Annotate | Download (4.55 KB)

1
/*
2
 * Generates a synthetic stereo sound
3
 * NOTE: no floats are used to guaranty a bit exact output.
4
 */
5
#include <stdlib.h>
6
#include <stdio.h>
7

    
8
#define NB_CHANNELS 2
9
#define FE 44100
10

    
11
static unsigned int myrnd(unsigned int *seed_ptr, int n)
12
{
13
    unsigned int seed, val;
14

    
15
    seed = *seed_ptr;
16
    seed = (seed * 314159) + 1;
17
    if (n == 256) {
18
        val = seed >> 24;
19
    } else {
20
        val = seed % n;
21
    }
22
    *seed_ptr = seed;
23
    return val;
24
}
25

    
26
#define FRAC_BITS 16
27
#define FRAC_ONE (1 << FRAC_BITS)
28

    
29
#define COS_TABLE_BITS 7
30

    
31
/* integer cosinus */
32
static const unsigned short cos_table[(1 << COS_TABLE_BITS) + 2] = {
33
 0x8000, 0x7ffe, 0x7ff6, 0x7fea, 0x7fd9, 0x7fc2, 0x7fa7, 0x7f87,
34
 0x7f62, 0x7f38, 0x7f0a, 0x7ed6, 0x7e9d, 0x7e60, 0x7e1e, 0x7dd6,
35
 0x7d8a, 0x7d3a, 0x7ce4, 0x7c89, 0x7c2a, 0x7bc6, 0x7b5d, 0x7aef,
36
 0x7a7d, 0x7a06, 0x798a, 0x790a, 0x7885, 0x77fb, 0x776c, 0x76d9,
37
 0x7642, 0x75a6, 0x7505, 0x7460, 0x73b6, 0x7308, 0x7255, 0x719e,
38
 0x70e3, 0x7023, 0x6f5f, 0x6e97, 0x6dca, 0x6cf9, 0x6c24, 0x6b4b,
39
 0x6a6e, 0x698c, 0x68a7, 0x67bd, 0x66d0, 0x65de, 0x64e9, 0x63ef,
40
 0x62f2, 0x61f1, 0x60ec, 0x5fe4, 0x5ed7, 0x5dc8, 0x5cb4, 0x5b9d,
41
 0x5a82, 0x5964, 0x5843, 0x571e, 0x55f6, 0x54ca, 0x539b, 0x5269,
42
 0x5134, 0x4ffb, 0x4ec0, 0x4d81, 0x4c40, 0x4afb, 0x49b4, 0x486a,
43
 0x471d, 0x45cd, 0x447b, 0x4326, 0x41ce, 0x4074, 0x3f17, 0x3db8,
44
 0x3c57, 0x3af3, 0x398d, 0x3825, 0x36ba, 0x354e, 0x33df, 0x326e,
45
 0x30fc, 0x2f87, 0x2e11, 0x2c99, 0x2b1f, 0x29a4, 0x2827, 0x26a8,
46
 0x2528, 0x23a7, 0x2224, 0x209f, 0x1f1a, 0x1d93, 0x1c0c, 0x1a83,
47
 0x18f9, 0x176e, 0x15e2, 0x1455, 0x12c8, 0x113a, 0x0fab, 0x0e1c,
48
 0x0c8c, 0x0afb, 0x096b, 0x07d9, 0x0648, 0x04b6, 0x0324, 0x0192,
49
 0x0000, 0x0000,
50
};
51

    
52
#define CSHIFT (FRAC_BITS - COS_TABLE_BITS - 2)
53

    
54
static int int_cos(int a)
55
{
56
    int neg, v, f;
57
    const unsigned short *p;
58

    
59
    a = a & (FRAC_ONE - 1); /* modulo 2 * pi */
60
    if (a >= (FRAC_ONE / 2))
61
        a = FRAC_ONE - a;
62
    neg = 0;
63
    if (a > (FRAC_ONE / 4)) {
64
        neg = -1;
65
        a = (FRAC_ONE / 2) - a;
66
    }
67
    p = cos_table + (a >> CSHIFT);
68
    /* linear interpolation */
69
    f = a & ((1 << CSHIFT) - 1);
70
    v = p[0] + (((p[1] - p[0]) * f + (1 << (CSHIFT - 1))) >> CSHIFT);
71
    v = (v ^ neg) - neg;
72
    v = v << (FRAC_BITS - 15);
73
    return v;
74
}
75

    
76
FILE *outfile;
77

    
78
void put_sample(int v)
79
{
80
    fputc(v & 0xff, outfile);
81
    fputc((v >> 8) & 0xff, outfile);
82
}
83

    
84
int main(int argc, char **argv)
85
{
86
    int i, a, v, j, f, amp, ampa;
87
    unsigned int seed = 1;
88
    int tabf1[NB_CHANNELS], tabf2[NB_CHANNELS];
89
    int taba[NB_CHANNELS];
90

    
91
    if (argc != 2) {
92
        printf("usage: %s file\n"
93
               "generate a test raw 16 bit stereo audio stream\n", argv[0]);
94
        exit(1);
95
    }
96

    
97
    outfile = fopen(argv[1], "wb");
98
    if (!outfile) {
99
        perror(argv[1]);
100
        return 1;
101
    }
102

    
103
    /* 1 second of single freq sinus at 1000 Hz */
104
    a = 0;
105
    for(i=0;i<1 * FE;i++) {
106
        v = (int_cos(a) * 10000) >> FRAC_BITS;
107
        for(j=0;j<NB_CHANNELS;j++)
108
            put_sample(v);
109
        a += (1000 * FRAC_ONE) / FE;
110
    }
111

    
112
    /* 1 second of varing frequency between 100 and 10000 Hz */
113
    a = 0;
114
    for(i=0;i<1 * FE;i++) {
115
        v = (int_cos(a) * 10000) >> FRAC_BITS;
116
        for(j=0;j<NB_CHANNELS;j++)
117
            put_sample(v);
118
        f = 100 + (((10000 - 100) * i) / FE);
119
        a += (f * FRAC_ONE) / FE;
120
    }
121

    
122
    /* 0.5 second of low amplitude white noise */
123
    for(i=0;i<FE / 2;i++) {
124
        v = myrnd(&seed, 20000) - 10000;
125
        for(j=0;j<NB_CHANNELS;j++)
126
            put_sample(v);
127
    }
128

    
129
    /* 0.5 second of high amplitude white noise */
130
    for(i=0;i<FE / 2;i++) {
131
        v = myrnd(&seed, 65535) - 32768;
132
        for(j=0;j<NB_CHANNELS;j++)
133
            put_sample(v);
134
    }
135

    
136
    /* stereo : 2 unrelated ramps */
137
    for(j=0;j<NB_CHANNELS;j++) {
138
        taba[j] = 0;
139
        tabf1[j] = 100 + myrnd(&seed, 5000);
140
        tabf2[j] = 100 + myrnd(&seed, 5000);
141
    }
142
    for(i=0;i<1 * FE;i++) {
143
        for(j=0;j<NB_CHANNELS;j++) {
144
            v = (int_cos(taba[j]) * 10000) >> FRAC_BITS;
145
            put_sample(v);
146
            f = tabf1[j] + (((tabf2[j] - tabf1[j]) * i) / FE);
147
            taba[j] += (f * FRAC_ONE) / FE;
148
        }
149
    }
150

    
151
    /* stereo 500 Hz with varying volume */
152
    a = 0;
153
    ampa = 0;
154
    for(i=0;i<2 * FE;i++) {
155
        for(j=0;j<NB_CHANNELS;j++) {
156
            amp = ((FRAC_ONE + int_cos(ampa)) * 5000) >> FRAC_BITS;
157
            if (j & 1)
158
                amp = 10000 - amp;
159
            v = (int_cos(a) * amp) >> FRAC_BITS;
160
            put_sample(v);
161
            a += (500 * FRAC_ONE) / FE;
162
            ampa += (2 * FRAC_ONE) / FE;
163
        }
164
    }
165

    
166
    fclose(outfile);
167
    return 0;
168
}