Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / libzmpeg3 / a52dec-0.7.3 / libao / audio_out_solaris.c
1 /*
2  * audio_out_solaris.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_SOLARIS
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/ioctl.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/audioio.h>
34 #include <inttypes.h>
35
36 #include "a52.h"
37 #include "audio_out.h"
38 #include "audio_out_internal.h"
39
40 typedef struct solaris_instance_s {
41     ao_instance_t ao;
42     int fd;
43     int sample_rate;
44     int set_params;
45     int flags;
46 } solaris_instance_t;
47
48 int solaris_setup (ao_instance_t * _instance, int sample_rate, int * flags,
49                    sample_t * level, sample_t * bias)
50 {
51     solaris_instance_t * instance = (solaris_instance_t *) _instance;
52
53     if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
54         return 1;
55     instance->sample_rate = sample_rate;
56
57     *flags = instance->flags;
58     *level = 1;
59     *bias = 384;
60
61     return 0;
62 }
63
64 int solaris_play (ao_instance_t * _instance, int flags, sample_t * _samples)
65 {
66     solaris_instance_t * instance = (solaris_instance_t *) _instance;
67     int16_t int16_samples[256*2];
68
69 #ifdef LIBA52_DOUBLE
70     float samples[256 * 2];
71     int i;
72
73     for (i = 0; i < 256 * 2; i++)
74         samples[i] = _samples[i];
75 #else
76     float * samples = _samples;
77 #endif
78
79     if (instance->set_params) {
80         audio_info_t info;
81
82         /* Setup our parameters */
83         AUDIO_INITINFO (&info);
84         info.play.sample_rate = instance->sample_rate;
85         info.play.precision = 16;
86         info.play.channels = 2;
87         info.play.encoding = AUDIO_ENCODING_LINEAR;
88         
89         /* Write our configuration */
90         /* An implicit GETINFO is also performed. */
91         if (ioctl (instance->fd, AUDIO_SETINFO, &info) < 0) {
92             perror ("Writing audio config block");
93             return 1;
94         }
95
96         if ((info.play.sample_rate != instance->sample_rate) ||
97             (info.play.precision != 16) || (info.play.channels != 2)) {
98             fprintf (stderr, "Wanted %dHz %d bits %d channels\n",
99                      instance->sample_rate, 16, 2);
100             fprintf (stderr, "Got    %dHz %d bits %d channels\n",
101                      info.play.sample_rate, info.play.precision,
102                      info.play.channels);
103             return 1;
104         }
105
106         instance->flags = flags;
107         instance->set_params = 0;
108     } else if ((flags == A52_DOLBY) && (instance->flags == A52_STEREO)) {
109         fprintf (stderr, "Switching from stereo to dolby surround\n");
110         instance->flags = A52_DOLBY;
111     } else if ((flags == A52_STEREO) && (instance->flags == A52_DOLBY)) {
112         fprintf (stderr, "Switching from dolby surround to stereo\n");
113         instance->flags = A52_STEREO;
114     } else if (flags != instance->flags)
115         return 1;
116
117     float2s16_2 (samples, int16_samples);
118     write (instance->fd, int16_samples, 256 * sizeof (int16_t) * 2);
119
120     return 0;
121 }
122
123 void solaris_close (ao_instance_t * _instance)
124 {
125     solaris_instance_t * instance = (solaris_instance_t *) _instance;
126
127     close (instance->fd);
128 }
129
130 ao_instance_t * solaris_open (int flags)
131 {
132     solaris_instance_t * instance;
133
134     instance = malloc (sizeof (solaris_instance_t));
135     if (instance == NULL)
136         return NULL;
137
138     instance->ao.setup = solaris_setup;
139     instance->ao.play = solaris_play;
140     instance->ao.close = solaris_close;
141
142     instance->sample_rate = 0;
143     instance->set_params = 1;
144     instance->flags = flags;
145
146     instance->fd = open ("/dev/audio", O_WRONLY);
147     if (instance->fd < 0) {
148         fprintf (stderr, "Can not open /dev/audio\n");
149         free (instance);
150         return NULL;
151     }
152
153     return (ao_instance_t *) instance;
154 }
155
156 ao_instance_t * ao_solaris_open (void)
157 {
158     return solaris_open (A52_STEREO);
159 }
160
161 ao_instance_t * ao_solarisdolby_open (void)
162 {
163     return solaris_open (A52_DOLBY);
164 }
165
166 #endif