initial commit
[goodguy/history.git] / cinelerra-5.0 / libzmpeg3 / a52dec-0.7.3 / libao / audio_out_wav.c
1 /*
2  * audio_out_wav.c
3  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  * This file is part of a52dec, a free ATSC A-52 stream decoder.
7  * See http://liba52.sourceforge.net/ for updates.
8  *
9  * a52dec is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * a52dec is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <inttypes.h>
30
31 #include "a52.h"
32 #include "audio_out.h"
33 #include "audio_out_internal.h"
34
35 typedef struct wav_instance_s {
36     ao_instance_t ao;
37     int sample_rate;
38     int set_params;
39     int flags;
40     int size;
41 } wav_instance_t;
42
43 static uint8_t wav_header[] = {
44     'R', 'I', 'F', 'F', 0xfc, 0xff, 0xff, 0xff, 'W', 'A', 'V', 'E',
45     'f', 'm', 't', ' ', 16, 0, 0, 0,
46     1, 0, 2, 0, -1, -1, -1, -1, -1, -1, -1, -1, 4, 0, 16, 0,
47     'd', 'a', 't', 'a', 0xd8, 0xff, 0xff, 0xff
48 };
49
50 int wav_setup (ao_instance_t * _instance, int sample_rate, int * flags,
51                    sample_t * level, sample_t * bias)
52 {
53     wav_instance_t * instance = (wav_instance_t *) _instance;
54
55     if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
56         return 1;
57     instance->sample_rate = sample_rate;
58
59     *flags = instance->flags;
60     *level = 1;
61     *bias = 384;
62
63     return 0;
64 }
65
66 static void store (uint8_t * buf, int value)
67 {
68     buf[0] = value;
69     buf[1] = value >> 8;
70     buf[2] = value >> 16;
71     buf[3] = value >> 24;
72 }
73
74 int wav_play (ao_instance_t * _instance, int flags, sample_t * _samples)
75 {
76     wav_instance_t * instance = (wav_instance_t *) _instance;
77     int16_t int16_samples[256*2];
78
79 #ifdef LIBA52_DOUBLE
80     float samples[256 * 2];
81     int i;
82
83     for (i = 0; i < 256 * 2; i++)
84         samples[i] = _samples[i];
85 #else
86     float * samples = _samples;
87 #endif
88
89     if (instance->set_params) {
90         instance->set_params = 0;
91         store (wav_header + 24, instance->sample_rate);
92         store (wav_header + 28, instance->sample_rate * 4);
93         fwrite (wav_header, sizeof (wav_header), 1, stdout);
94     }
95
96     float2s16_2 (samples, int16_samples);
97     s16_LE (int16_samples, 2);
98     fwrite (int16_samples, 256 * sizeof (int16_t) * 2, 1, stdout);
99
100     instance->size += 256 * sizeof (int16_t) * 2;
101
102     return 0;
103 }
104
105 void wav_close (ao_instance_t * _instance)
106 {
107     wav_instance_t * instance = (wav_instance_t *) _instance;
108
109     if (fseek (stdout, 0, SEEK_SET) < 0)
110         return;
111
112     store (wav_header + 4, instance->size + 36);
113     store (wav_header + 40, instance->size);
114     fwrite (wav_header, sizeof (wav_header), 1, stdout);
115 }
116
117 ao_instance_t * wav_open (int flags)
118 {
119     wav_instance_t * instance;
120
121     instance = malloc (sizeof (wav_instance_t));
122     if (instance == NULL)
123         return NULL;
124
125     instance->ao.setup = wav_setup;
126     instance->ao.play = wav_play;
127     instance->ao.close = wav_close;
128
129     instance->sample_rate = 0;
130     instance->set_params = 1;
131     instance->flags = flags;
132     instance->size = 0;
133
134     return (ao_instance_t *) instance;
135 }
136
137 ao_instance_t * ao_wav_open (void)
138 {
139     return wav_open (A52_STEREO);
140 }
141
142 ao_instance_t * ao_wavdolby_open (void)
143 {
144     return wav_open (A52_DOLBY);
145 }