~ubuntu-branches/ubuntu/vivid/libav/vivid

« back to all changes in this revision

Viewing changes to tests/audiogen.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2013-10-22 23:24:08 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: package-import@ubuntu.com-20131022232408-b8tvvn4pyzri9mi3
Tags: 6:9.10-1ubuntu1
* Build all -extra flavors from this source package, as libav got demoted
  from main to universe, cf LP: #1243235
* Simplify debian/rules to follow exactly the code that debian executes
* New upstream (LP: #1180288) fixes lots of security issues (LP: #1242802)
* Merge from unstable, remaining changes:
  - build-depend on libtiff5-dev rather than libtiff4-dev,
    avoids FTBFS caused by imlib
  - follow the regular debian codepaths

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 */
23
23
 
24
24
#include <stdlib.h>
 
25
#include <stdint.h>
25
26
#include <stdio.h>
 
27
#include <string.h>
26
28
 
27
29
#define MAX_CHANNELS 8
28
30
 
93
95
 
94
96
FILE *outfile;
95
97
 
96
 
static void put_sample(int v)
 
98
static void put16(int16_t v)
97
99
{
98
 
    fputc(v & 0xff, outfile);
 
100
    fputc( v       & 0xff, outfile);
99
101
    fputc((v >> 8) & 0xff, outfile);
100
102
}
101
103
 
 
104
static void put32(uint32_t v)
 
105
{
 
106
    fputc( v        & 0xff, outfile);
 
107
    fputc((v >>  8) & 0xff, outfile);
 
108
    fputc((v >> 16) & 0xff, outfile);
 
109
    fputc((v >> 24) & 0xff, outfile);
 
110
}
 
111
 
 
112
#define HEADER_SIZE      46
 
113
#define FMT_SIZE         18
 
114
#define SAMPLE_SIZE       2
 
115
#define WFORMAT_PCM  0x0001
 
116
 
 
117
static void put_wav_header(int sample_rate, int channels, int nb_samples)
 
118
{
 
119
    int block_align = SAMPLE_SIZE * channels;
 
120
    int data_size   = block_align * nb_samples;
 
121
 
 
122
    fputs("RIFF", outfile);
 
123
    put32(HEADER_SIZE + data_size);
 
124
    fputs("WAVEfmt ", outfile);
 
125
    put32(FMT_SIZE);
 
126
    put16(WFORMAT_PCM);
 
127
    put16(channels);
 
128
    put32(sample_rate);
 
129
    put32(block_align * sample_rate);
 
130
    put16(block_align);
 
131
    put16(SAMPLE_SIZE * 8);
 
132
    put16(0);
 
133
    fputs("data", outfile);
 
134
    put32(data_size);
 
135
}
 
136
 
102
137
int main(int argc, char **argv)
103
138
{
104
139
    int i, a, v, j, f, amp, ampa;
107
142
    int taba[MAX_CHANNELS];
108
143
    int sample_rate = 44100;
109
144
    int nb_channels = 2;
 
145
    char *ext;
110
146
 
111
 
    if (argc < 2 || argc > 4) {
112
 
        printf("usage: %s file [<sample rate> [<channels>]]\n"
 
147
    if (argc < 2 || argc > 5) {
 
148
        printf("usage: %s file [<sample rate> [<channels>] [<random seed>]]\n"
113
149
               "generate a test raw 16 bit audio stream\n"
 
150
               "If the file extension is .wav a WAVE header will be added.\n"
114
151
               "default: 44100 Hz stereo\n", argv[0]);
115
152
        exit(1);
116
153
    }
131
168
        }
132
169
    }
133
170
 
 
171
    if (argc > 4)
 
172
        seed = atoi(argv[4]);
 
173
 
134
174
    outfile = fopen(argv[1], "wb");
135
175
    if (!outfile) {
136
176
        perror(argv[1]);
137
177
        return 1;
138
178
    }
139
179
 
 
180
    if ((ext = strrchr(argv[1], '.')) != NULL && !strcmp(ext, ".wav"))
 
181
        put_wav_header(sample_rate, nb_channels, 6 * sample_rate);
 
182
 
140
183
    /* 1 second of single freq sinus at 1000 Hz */
141
184
    a = 0;
142
185
    for (i = 0; i < 1 * sample_rate; i++) {
143
186
        v = (int_cos(a) * 10000) >> FRAC_BITS;
144
187
        for (j = 0; j < nb_channels; j++)
145
 
            put_sample(v);
 
188
            put16(v);
146
189
        a += (1000 * FRAC_ONE) / sample_rate;
147
190
    }
148
191
 
149
 
    /* 1 second of varing frequency between 100 and 10000 Hz */
 
192
    /* 1 second of varying frequency between 100 and 10000 Hz */
150
193
    a = 0;
151
194
    for (i = 0; i < 1 * sample_rate; i++) {
152
195
        v = (int_cos(a) * 10000) >> FRAC_BITS;
153
196
        for (j = 0; j < nb_channels; j++)
154
 
            put_sample(v);
 
197
            put16(v);
155
198
        f  = 100 + (((10000 - 100) * i) / sample_rate);
156
199
        a += (f * FRAC_ONE) / sample_rate;
157
200
    }
160
203
    for (i = 0; i < sample_rate / 2; i++) {
161
204
        v = myrnd(&seed, 20000) - 10000;
162
205
        for (j = 0; j < nb_channels; j++)
163
 
            put_sample(v);
 
206
            put16(v);
164
207
    }
165
208
 
166
209
    /* 0.5 second of high amplitude white noise */
167
210
    for (i = 0; i < sample_rate / 2; i++) {
168
211
        v = myrnd(&seed, 65535) - 32768;
169
212
        for (j = 0; j < nb_channels; j++)
170
 
            put_sample(v);
 
213
            put16(v);
171
214
    }
172
215
 
173
216
    /* 1 second of unrelated ramps for each channel */
179
222
    for (i = 0; i < 1 * sample_rate; i++) {
180
223
        for (j = 0; j < nb_channels; j++) {
181
224
            v = (int_cos(taba[j]) * 10000) >> FRAC_BITS;
182
 
            put_sample(v);
 
225
            put16(v);
183
226
            f        = tabf1[j] + (((tabf2[j] - tabf1[j]) * i) / sample_rate);
184
227
            taba[j] += (f * FRAC_ONE) / sample_rate;
185
228
        }
194
237
            if (j & 1)
195
238
                amp = 10000 - amp;
196
239
            v = (int_cos(a) * amp) >> FRAC_BITS;
197
 
            put_sample(v);
 
240
            put16(v);
198
241
            a    += (500 * FRAC_ONE) / sample_rate;
199
242
            ampa += (2 * FRAC_ONE) / sample_rate;
200
243
        }