3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates.
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.
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.
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
32 #include "audio_out.h"
33 #include "audio_out_internal.h"
35 typedef struct wav_instance_s {
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
50 int wav_setup (ao_instance_t * _instance, int sample_rate, int * flags,
51 sample_t * level, sample_t * bias)
53 wav_instance_t * instance = (wav_instance_t *) _instance;
55 if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
57 instance->sample_rate = sample_rate;
59 *flags = instance->flags;
66 static void store (uint8_t * buf, int value)
74 int wav_play (ao_instance_t * _instance, int flags, sample_t * _samples)
76 wav_instance_t * instance = (wav_instance_t *) _instance;
77 int16_t int16_samples[256*2];
80 float samples[256 * 2];
83 for (i = 0; i < 256 * 2; i++)
84 samples[i] = _samples[i];
86 float * samples = _samples;
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);
96 float2s16_2 (samples, int16_samples);
97 s16_LE (int16_samples, 2);
98 fwrite (int16_samples, 256 * sizeof (int16_t) * 2, 1, stdout);
100 instance->size += 256 * sizeof (int16_t) * 2;
105 void wav_close (ao_instance_t * _instance)
107 wav_instance_t * instance = (wav_instance_t *) _instance;
109 if (fseek (stdout, 0, SEEK_SET) < 0)
112 store (wav_header + 4, instance->size + 36);
113 store (wav_header + 40, instance->size);
114 fwrite (wav_header, sizeof (wav_header), 1, stdout);
117 ao_instance_t * wav_open (int flags)
119 wav_instance_t * instance;
121 instance = malloc (sizeof (wav_instance_t));
122 if (instance == NULL)
125 instance->ao.setup = wav_setup;
126 instance->ao.play = wav_play;
127 instance->ao.close = wav_close;
129 instance->sample_rate = 0;
130 instance->set_params = 1;
131 instance->flags = flags;
134 return (ao_instance_t *) instance;
137 ao_instance_t * ao_wav_open (void)
139 return wav_open (A52_STEREO);
142 ao_instance_t * ao_wavdolby_open (void)
144 return wav_open (A52_DOLBY);