A new script, gigdump2ahs.pl, to convert the output from gigdump to
[ahxm.git] / ss_output.c
bloba9fdefa99f2a73cf3aa331149a9325b4c19ef457
1 /*
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
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <math.h>
34 #include "annhell.h"
36 /*******************
37 Data
38 ********************/
40 /* master output volume */
41 float ss_master_volume=1.0;
43 /* output accounting */
44 int ss_output_frames=0;
45 int ss_output_clipped=0;
46 float ss_output_lowest=0;
47 float ss_output_highest=0;
49 /* optimal output volume */
50 float ss_optimal_volume;
52 /* driver data */
53 static void * outdev;
56 usual channel mapping:
58 mono: all
59 stereo: left | right
60 3 channel: left | right | center
61 quad: front left | front right | rear left | rear right
62 4 channel: left | center | right | surround
63 6 channel: left center | left | center | right center | right | surround
66 /*******************
67 Code
68 ********************/
70 void * ss_outdev_open(char * drvname, char * file, int freq, int n_channels);
71 void ss_outdev_write(void * drv, short int * frame);
72 void ss_outdev_close(void * drv);
74 /**
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. "raw" or "wav"), @filename contains the (optional) name
81 * of the output file or device (i.e. a filename for "raw" or
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 * The ss_nchannels global variable can be changed on output if
90 * the driver doesn't support so much channels.
92 * Returns a negative number in case of error, or zero otherwise.
94 int ss_output_open(char * drvname, char * filename)
96 if(strcmp(drvname, "default") == 0)
97 drvname=NULL;
99 /* reset accounting */
100 ss_output_frames=ss_output_clipped=0;
101 ss_output_lowest=ss_output_highest=0.0;
103 return((outdev=ss_outdev_open(drvname,
104 filename, ss_frequency, ss_nchannels)) == NULL);
108 void ss_output_init_frame(float samples[])
110 int n;
112 for(n=0;n < SS_MAX_CHANNELS;n++)
113 samples[n] = 0.0;
118 * ss_output_write - Outputs a frame of samples.
119 * @samples: the frame of samples
121 * Outputs a frame of samples. The output samples are
122 * intermixed if needed, have the master volume applied and trimmed
123 * before being sent to the output driver itself.
125 * If the maximum number of channels the output driver supports
126 * is lower than the global ss_nchannels, they are sequentially intermixed
127 * (for example, when outputting 4 channel data to a stereo device,
128 * 0 and 2 channels will go to left and 1 and 3 to right).
130 * Returns a negative value in case of error, or 0 otherwise.
132 int ss_output_write(float samples[])
134 int n, clip, ret=0;
135 float s;
136 short int is[SS_MAX_CHANNELS];
138 /* final corrections */
139 for(n=clip=0;n < ss_nchannels;n++)
141 s=samples[n];
143 /* store the lowest and highest
144 amplitude seen before master volume */
145 if(s < ss_output_lowest)
146 ss_output_lowest=s;
148 if(s > ss_output_highest)
149 ss_output_highest=s;
151 /* apply master volume */
152 s *= ss_master_volume;
154 /* clip samples to signed 16 bit boundaries */
155 if(s < -32768)
157 s=-32768;
158 clip++;
161 if(s > 32767)
163 s=32767;
164 clip++;
167 /* store in buffer */
168 is[n]=(short int) s;
171 /* update accounting */
172 ss_output_frames++;
174 if(clip)
175 ss_output_clipped++;
177 /* finally write */
178 ss_outdev_write(outdev, is);
180 return(ret);
185 * ss_output_close - Closes the output device.
187 * Closes the output driver.
189 void ss_output_close(void)
191 ss_outdev_close(outdev);
193 /* calculate optimal master volume for
194 zero saturation */
195 ss_output_lowest=fabs(ss_output_lowest);
197 ss_optimal_volume = ss_output_lowest > ss_output_highest ?
198 32768.0 / ss_output_lowest :
199 32767.0 / ss_output_highest;