3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 Angel Ortega <angel@triptico.com>
6 ss_output.c - Softsynth 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 ss_master_volume
=0.5;
43 /* output accounting */
44 int ss_output_clipped
=0;
45 float ss_output_lowest
=0;
46 float ss_output_highest
=0;
48 /* optimal output volume */
49 float ss_optimal_volume
;
55 usual channel mapping:
59 3 channel: left | right | center
60 quad: front left | front right | rear left | rear right
61 4 channel: left | center | right | surround
62 6 channel: left center | left | center | right center | right | surround
69 void * ss_outdev_open(char * drvname
, char * file
, int freq
, int n_channels
);
70 void ss_outdev_write(void * drv
, short int * frame
);
71 void ss_outdev_close(void * drv
);
75 * ss_output_open - Opens an output device.
76 * @name: name of the driver
77 * @filename: name of the file or device
79 * Opens an output device. @name contains the name of the driver
80 * (i.e. "oss" or "wav"), @filename contains the (optional) name
81 * of the output file or device (i.e. a filename
82 * "wav" or "/dev/dsp" for a direct audio device like "oss").
83 * @Name can be the special pseudo-driver "default"
84 * to select the most appropriate (usually a platform-specific
85 * direct output device, or "wav" if no one exists).
86 * @filename can also be NULL; in that case, a driver dependent,
87 * default value is used.
89 * Returns zero if the output device was correctly open, or
92 int ss_output_open(char * drvname
, char * filename
)
94 if(strcmp(drvname
, "default") == 0)
97 /* reset accounting */
99 ss_output_lowest
=ss_output_highest
=0.0;
101 return((outdev
=ss_outdev_open(drvname
,
102 filename
, ss_frequency
, ss_nchannels
)) == NULL
);
107 * ss_output_init_frame - Inits a frame
110 * Inits a frame, setting all samples to zero.
112 void ss_output_init_frame(float frame
[])
116 for(n
=0;n
< SS_MAX_CHANNELS
;n
++)
122 * ss_output_write - Outputs a frame of samples.
123 * @frame: the frame of samples
125 * Outputs a frame of samples. The output samples are
126 * intermixed if needed, have the master volume applied and trimmed
127 * before being sent to the output driver itself.
129 * If the maximum number of channels the output driver supports
130 * is lower than the global ss_nchannels, they are sequentially intermixed
131 * (for example, when outputting 4 channel data to a stereo device,
132 * 0 and 2 channels will go to left and 1 and 3 to right).
134 * Returns a negative value in case of error, or 0 otherwise.
136 int ss_output_write(float frame
[])
140 short int is
[SS_MAX_CHANNELS
];
142 /* final corrections */
143 for(n
=clip
=0;n
< ss_nchannels
;n
++)
147 /* store the lowest and highest
148 amplitude seen before master volume */
149 if(s
< ss_output_lowest
)
152 if(s
> ss_output_highest
)
155 /* apply master volume */
156 s
*= ss_master_volume
;
158 /* clip samples to signed 16 bit boundaries */
171 /* store in buffer */
179 ss_outdev_write(outdev
, is
);
186 * ss_output_close - Closes the output device.
188 * Closes the output driver.
190 void ss_output_close(void)
192 ss_outdev_close(outdev
);
194 /* calculate optimal master volume for
196 ss_output_lowest
=fabs(ss_output_lowest
);
198 ss_optimal_volume
= ss_output_lowest
> ss_output_highest
?
199 32768.0 / ss_output_lowest
:
200 32767.0 / ss_output_highest
;