audio_out_al.c 4.42 KB
Newer Older
Gildas Bazin's avatar
Gildas Bazin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * audio_out_al.c
 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
 * This file is part of a52dec, a free ATSC A-52 stream decoder.
 * See http://liba52.sourceforge.net/ for updates.
 *
 * a52dec is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * a52dec is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
20 21 22
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Gildas Bazin's avatar
Gildas Bazin committed
23 24 25 26 27 28 29 30 31 32 33
 */

#include "config.h"

#ifdef LIBAO_AL

#include <stdio.h>
#include <stdlib.h>
#include <dmedia/audio.h>
#include <inttypes.h>

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
34
#include "dca.h"
Gildas Bazin's avatar
Gildas Bazin committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include "audio_out.h"
#include "audio_out_internal.h"

typedef struct al_instance_s {
    ao_instance_t ao;
    ALport port;
    int sample_rate;
    int set_params;
    int flags;
} al_instance_t;

static int al_setup (ao_instance_t * _instance, int sample_rate, int * flags,
		     level_t * level, sample_t * bias)
{
    al_instance_t * instance = (al_instance_t *) _instance;

    if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
	return 1;
    instance->sample_rate = sample_rate;

    *flags = instance->flags;
    *level = CONVERT_LEVEL;
    *bias = CONVERT_BIAS;

    return 0;
}

static int al_play (ao_instance_t * _instance, int flags, sample_t * _samples)
{
    al_instance_t * instance = (al_instance_t *) _instance;
    int16_t int16_samples[256*6];
    int chans = -1;

68
#ifdef LIBDCA_DOUBLE
Gildas Bazin's avatar
Gildas Bazin committed
69 70 71 72 73 74 75 76 77 78
    convert_t samples[256 * 6];
    int i;

    for (i = 0; i < 256 * 6; i++)
	samples[i] = _samples[i];
#else
    convert_t * samples = _samples;
#endif

    chans = channels_multi (flags);
79
    flags &= DCA_CHANNEL_MASK | DCA_LFE;
Gildas Bazin's avatar
Gildas Bazin committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    if (instance->set_params) {
	ALconfig config;
	ALpv params[2];

	config = alNewConfig ();
	if (!config) {
	    fprintf (stderr, "alNewConfig failed\n");
	    return 1;
	}
	if (alSetChannels (config, chans)) {
	    fprintf (stderr, "alSetChannels failed\n");
	    return 1;
	}
	if (alSetConfig (instance->port, config)) {
	    fprintf (stderr, "alSetConfig failed\n");
	    return 1;
	}
	alFreeConfig (config);

	params[0].param = AL_MASTER_CLOCK;
	params[0].value.i = AL_CRYSTAL_MCLK_TYPE;
	params[1].param = AL_RATE;
	params[1].value.ll = alIntToFixed (instance->sample_rate);
	if (alSetParams (alGetResource (instance->port), params, 2) < 0) {
	    fprintf (stderr, "alSetParams failed\n");
	    return 1;
	}

	instance->flags = flags;
	instance->set_params = 0;
111
    } else if ((flags == DCA_DOLBY) && (instance->flags == DCA_STEREO)) {
Gildas Bazin's avatar
Gildas Bazin committed
112
	fprintf (stderr, "Switching from stereo to dolby surround\n");
113 114
	instance->flags = DCA_DOLBY;
    } else if ((flags == DCA_STEREO) && (instance->flags == DCA_DOLBY)) {
Gildas Bazin's avatar
Gildas Bazin committed
115
	fprintf (stderr, "Switching from dolby surround to stereo\n");
116
	instance->flags = DCA_STEREO;
Gildas Bazin's avatar
Gildas Bazin committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    } else if (flags != instance->flags)
	return 1;

    convert2s16_multi (samples, int16_samples, flags);
    alWriteFrames (instance->port, int16_samples, 256);

    return 0;
}

static void al_close (ao_instance_t * _instance)
{
    al_instance_t * instance = (al_instance_t *) _instance;

    alClosePort (instance->port);
}

static ao_instance_t * al_open (int flags)
{
    al_instance_t * instance;
    int format;

    instance = malloc (sizeof (al_instance_t));
    if (instance == NULL)
	return NULL;

    instance->ao.setup = al_setup;
    instance->ao.play = al_play;
    instance->ao.close = al_close;

    instance->sample_rate = 0;
    instance->set_params = 1;
    instance->flags = flags;

    instance->port = alOpenPort ("a52dec", "w", 0);
    if (instance->port < 0) {
	fprintf (stderr, "alOpenPort failed\n");
	free (instance);
	return NULL;
    }

    return (ao_instance_t *) instance;
}

ao_instance_t * ao_al_open (void)
{
162
    return al_open (DCA_STEREO);
Gildas Bazin's avatar
Gildas Bazin committed
163 164 165 166
}

ao_instance_t * ao_aldolby_open (void)
{
167
    return al_open (DCA_DOLBY);
Gildas Bazin's avatar
Gildas Bazin committed
168 169 170 171
}

ao_instance_t * ao_al4_open (void)
{
172
    return al_open (DCA_2F2R);
Gildas Bazin's avatar
Gildas Bazin committed
173 174 175 176
}

ao_instance_t * ao_al6_open (void)
{
177
    return al_open (DCA_3F2R | DCA_LFE);
Gildas Bazin's avatar
Gildas Bazin committed
178 179 180
}

#endif