Code cleaning to ss_gen.c.
[ahxm.git] / out_common.c
blobe9ffc543773a6381def97e55764d97ed095593ce
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 Angel Ortega <angel@triptico.com>
6 out_common.c - Driver for output not depending on external libraries
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"
27 #include <stdio.h>
28 #include "ss_output.h"
30 #include "ss_core.h"
32 /*******************
33 Data
34 ********************/
36 static FILE * f;
38 /* file positions where total number of samples is stored */
39 static long _p1, _p2;
41 /* frame size */
42 static int frame_size;
44 /*******************
45 Code
46 ********************/
49 usual channel mapping:
51 mono: all
52 stereo: left | right
53 3 channel: left | right | center
54 quad: front left | front right | rear left | rear right
55 4 channel: left | center | right | surround
56 6 channel: left center | left | center | right center | right | surround
60 static void fput16(short int i, FILE * f)
62 fputc(i & 0x00ff, f);
63 fputc((i & 0xff00) >> 8, f);
67 static void fput32(int i, FILE * f)
69 fputc(i & 0x000000ff, f);
70 fputc((i & 0x0000ff00) >> 8, f);
71 fputc((i & 0x00ff0000) >> 16, f);
72 fputc((i & 0xff000000) >> 24, f);
76 static int _out_write_file(short int * buffer, int size)
78 int n;
80 for(n=0;n < size;n++)
81 fput16(buffer[n], f);
83 return(0);
87 /* raw files */
89 static int _out_open_raw(char * file)
91 if((f=fopen(file, "w"))==NULL)
92 return(-100);
94 return(0);
98 static int _out_close_raw(void)
100 fclose(f);
101 return(0);
105 /* pipes */
107 static int _out_open_pipe(char * file)
109 /* as there is no default pipe output,
110 file can arrive here as NULL; fail */
111 if(file == NULL)
112 return(-101);
114 if((f=popen(file, "w"))==NULL)
115 return(-100);
117 return(0);
121 static int _out_close_pipe(void)
123 pclose(f);
124 return(0);
128 /* wav files */
130 static int _out_open_wav(char * file)
132 if((f=fopen(file, "w"))==NULL)
133 return(-100);
135 frame_size=ss_nchannels * 2;
137 /* write wav header */
139 fwrite("RIFF",1,4,f);
140 _p1=ftell(f);
141 fput32(36, f);
142 fwrite("WAVE",1,4,f);
143 fwrite("fmt ",1,4,f);
144 fput32(16,f); /* chunk size */
145 fput16(1, f); /* 1: uncompressed PCM */
146 fput16(ss_nchannels, f); /* # of channels */
147 fput32(ss_frequency, f); /* sample rate */
148 fput32(ss_frequency * frame_size, f); /* bytes per second */
149 fput16(frame_size, f); /* 'block align' */
150 fput16(16, f); /* 16 bits per sample */
151 fwrite("data",1,4,f);
152 _p2=ftell(f);
153 fput32(0, f);
155 return(0);
159 static int _out_close_wav(void)
161 /* rewind file and write total size */
162 fseek(f, _p1, SEEK_SET);
163 fput32((ss_output_frames * frame_size) + 36, f);
165 fseek(f, _p2, SEEK_SET);
166 fput32(ss_output_frames * frame_size, f);
168 fclose(f);
170 return(0);
174 /* drivers */
176 struct _output_driver _out_driver_wav=
177 { "wav", "output.wav", _out_open_wav, _out_write_file, _out_close_wav };
179 struct _output_driver _out_driver_raw=
180 { "raw", "output.raw", _out_open_raw, _out_write_file, _out_close_raw };
182 struct _output_driver _out_driver_pipe=
183 { "pipe", NULL, _out_open_pipe, _out_write_file, _out_close_pipe };