1 /*
2 * audio_out_al.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 #ifdef LIBAO_AL
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <dmedia/audio.h>
31 #include <inttypes.h>
32
33 #include "a52.h"
34 #include "audio_out.h"
35 #include "audio_out_internal.h"
36
37 typedef struct al_instance_s {
38 ao_instance_t ao;
39 ALport port;
40 int sample_rate;
41 int set_params;
42 int flags;
43 } al_instance_t;
44
al_setup(ao_instance_t * _instance,int sample_rate,int * flags,sample_t * level,sample_t * bias)45 static int al_setup (ao_instance_t * _instance, int sample_rate, int * flags,
46 sample_t * level, sample_t * bias)
47 {
48 al_instance_t * instance = (al_instance_t *) _instance;
49
50 if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
51 return 1;
52 instance->sample_rate = sample_rate;
53
54 *flags = instance->flags;
55 *level = 1;
56 *bias = 384;
57
58 return 0;
59 }
60
al_play(ao_instance_t * _instance,int flags,sample_t * _samples)61 static int al_play (ao_instance_t * _instance, int flags, sample_t * _samples)
62 {
63 al_instance_t * instance = (al_instance_t *) _instance;
64 int16_t int16_samples[256*6];
65 int chans = -1;
66
67 #ifdef LIBA52_DOUBLE
68 float samples[256 * 6];
69 int i;
70
71 for (i = 0; i < 256 * 6; i++)
72 samples[i] = _samples[i];
73 #else
74 float * samples = _samples;
75 #endif
76
77 chans = channels_multi (flags);
78 flags &= A52_CHANNEL_MASK | A52_LFE;
79
80 if (instance->set_params) {
81 ALconfig config;
82 ALpv params[2];
83
84 config = alNewConfig ();
85 if (!config) {
86 fprintf (stderr, "alNewConfig failed\n");
87 return 1;
88 }
89 if (alSetChannels (config, chans)) {
90 fprintf (stderr, "alSetChannels failed\n");
91 return 1;
92 }
93 if (alSetConfig (instance->port, config)) {
94 fprintf (stderr, "alSetConfig failed\n");
95 return 1;
96 }
97 alFreeConfig (config);
98
99 params[0].param = AL_MASTER_CLOCK;
100 params[0].value.i = AL_CRYSTAL_MCLK_TYPE;
101 params[1].param = AL_RATE;
102 params[1].value.ll = alIntToFixed (instance->sample_rate);
103 if (alSetParams (alGetResource (instance->port), params, 2) < 0) {
104 fprintf (stderr, "alSetParams failed\n");
105 return 1;
106 }
107
108 instance->flags = flags;
109 instance->set_params = 0;
110 } else if ((flags == A52_DOLBY) && (instance->flags == A52_STEREO)) {
111 fprintf (stderr, "Switching from stereo to dolby surround\n");
112 instance->flags = A52_DOLBY;
113 } else if ((flags == A52_STEREO) && (instance->flags == A52_DOLBY)) {
114 fprintf (stderr, "Switching from dolby surround to stereo\n");
115 instance->flags = A52_STEREO;
116 } else if (flags != instance->flags)
117 return 1;
118
119 float2s16_multi (samples, int16_samples, flags);
120 alWriteFrames (instance->port, int16_samples, 256);
121
122 return 0;
123 }
124
al_close(ao_instance_t * _instance)125 static void al_close (ao_instance_t * _instance)
126 {
127 al_instance_t * instance = (al_instance_t *) _instance;
128
129 alClosePort (instance->port);
130 }
131
al_open(int flags)132 static ao_instance_t * al_open (int flags)
133 {
134 al_instance_t * instance;
135 int format;
136
137 instance = malloc (sizeof (al_instance_t));
138 if (instance == NULL)
139 return NULL;
140
141 instance->ao.setup = al_setup;
142 instance->ao.play = al_play;
143 instance->ao.close = al_close;
144
145 instance->sample_rate = 0;
146 instance->set_params = 1;
147 instance->flags = flags;
148
149 instance->port = alOpenPort ("a52dec", "w", 0);
150 if (instance->port < 0) {
151 fprintf (stderr, "alOpenPort failed\n");
152 free (instance);
153 return NULL;
154 }
155
156 return (ao_instance_t *) instance;
157 }
158
ao_al_open(void)159 ao_instance_t * ao_al_open (void)
160 {
161 return al_open (A52_STEREO);
162 }
163
ao_aldolby_open(void)164 ao_instance_t * ao_aldolby_open (void)
165 {
166 return al_open (A52_DOLBY);
167 }
168
ao_al4_open(void)169 ao_instance_t * ao_al4_open (void)
170 {
171 return al_open (A52_2F2R);
172 }
173
ao_al6_open(void)174 ao_instance_t * ao_al6_open (void)
175 {
176 return al_open (A52_3F2R | A52_LFE);
177 }
178
179 #endif
180