3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003 Angel Ortega <angel@triptico.com>
6 output.c - Output Interface
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
40 /* master output volume */
41 float _master_volume
=1.0;
43 /* output accounting */
45 int _output_clipped
=0;
46 float _output_lowest
=0;
47 float _output_highest
=0;
49 /* optimal output volume */
50 float _optimal_volume
;
53 /* the output drivers */
55 extern struct _output_driver _out_driver_oss
;
56 extern struct _output_driver _out_driver_sgi
;
57 extern struct _output_driver _out_driver_wav
;
58 extern struct _output_driver _out_driver_raw
;
59 extern struct _output_driver _out_driver_pipe
;
60 extern struct _output_driver _out_driver_esd
;
62 static struct _output_driver _out_driver_eof
=
63 { NULL
, NULL
, NULL
, NULL
, NULL
};
65 static struct _output_driver
* _drivers
[] = {
76 static struct _output_driver
* drv
=NULL
;
84 * output_open - Opens an output device.
85 * @name: name of the driver
86 * @filename: name of the file or device
88 * Opens an output device. @name contains the name of the driver
89 * (i.e. "raw" or "wav"), @filename contains the (optional) name
90 * of the output file or device (i.e. a filename for "raw" or
91 * "wav" or "/dev/dsp" for a direct audio device like "oss").
92 * @Name can be the special pseudo-driver "default"
93 * to select the most appropriate (usually a platform-specific
94 * direct output device, or "wav" if no supported one exists).
95 * @filename can also be NULL; in that case, a driver dependent,
96 * default value is used.
98 * Returns the number of channels accepted by the output
99 * driver, than can be less than the global _n_channels (in that
100 * case, output will be intermixed).
102 int output_open(char * name
, char * filename
)
109 /* if driver name is 'default', open the first usable driver
110 (supposedly the best for the current platform) */
111 if(strcmp(name
, "default") == 0)
115 if(_drivers
[n
]->name
== NULL
)
118 if(_drivers
[n
]->open
!= NULL
)
129 if(_drivers
[n
]->name
== NULL
)
132 if(strcmp(name
, _drivers
[n
]->name
) == 0)
143 /* if filename is NULL, take the default one */
145 filename
=drv
->default_file
;
147 /* the output driver must acknowledge the requested
148 number of channels or return the maximum supported */
149 _n_channels
= drv
->open(filename
);
151 /* unassign driver if opening fails */
155 /* reset accounting */
156 _output_frames
=_output_clipped
=0;
157 _output_lowest
=_output_highest
=0.0;
163 void output_init_frame(float samples
[])
167 for(n
=0;n
< CHANNELS
;n
++)
173 * output_write - Outputs a frame of samples.
174 * @samples: the frame of samples
176 * Outputs a frame of samples. The output samples are
177 * intermixed if needed, have the master volume applied and trimmed
178 * before being sent to the output driver itself.
180 * If the maximum number of channels the output driver supports
181 * is lower than the global _n_channels, they are sequentially intermixed
182 * (for example, when outputting 4 channel data to a stereo device,
183 * 0 and 2 channels will go to left and 1 and 3 to right).
185 * Returns a negative value in case of error, or 0 otherwise.
187 int output_write(float samples
[])
192 /* clean samples first */
193 for(n
=0;n
< _n_channels
;n
++)
196 /* move samples, 'down-channeling' if needed */
197 for(n
=0;n
< CHANNELS
;n
++)
198 s
[n
% _n_channels
] += samples
[n
];
200 /* final corrections */
201 for(n
=clip
=0;n
< _n_channels
;n
++)
203 /* store the lowest and highest
204 amplitude seen before master volume */
205 if(s
[n
] < _output_lowest
)
208 if(s
[n
] > _output_highest
)
209 _output_highest
=s
[n
];
211 /* apply master volume */
212 s
[n
] *= _master_volume
;
214 /* clip samples to signed 16 bit boundaries */
228 /* update accounting */
234 return(drv
->write(s
));
239 * output_close - Closes the output device.
241 * Closes the output driver.
243 int output_close(void)
250 /* calculate optimal master volume for
252 _output_lowest
=fabs(_output_lowest
);
254 _optimal_volume
= _output_lowest
> _output_highest
?
255 32768.0 / _output_lowest
:
256 32767.0 / _output_highest
;