Code cleaning in ss_outdev.c.
[ahxm.git] / out_common.c
blob87ed41c4ff015d1e518e8514db3d102bd355245e
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 /* frame size */
39 static int frame_size;
41 /*******************
42 Code
43 ********************/
46 usual channel mapping:
48 mono: all
49 stereo: left | right
50 3 channel: left | right | center
51 quad: front left | front right | rear left | rear right
52 4 channel: left | center | right | surround
53 6 channel: left center | left | center | right center | right | surround
57 static void fput16(short int i, FILE * f)
59 fputc(i & 0x00ff, f);
60 fputc((i & 0xff00) >> 8, f);
64 static void fput32(int i, FILE * f)
66 fputc(i & 0x000000ff, f);
67 fputc((i & 0x0000ff00) >> 8, f);
68 fputc((i & 0x00ff0000) >> 16, f);
69 fputc((i & 0xff000000) >> 24, f);
73 static int _out_write_file(short int * buffer, int size)
75 int n;
77 for(n=0;n < size;n++)
78 fput16(buffer[n], f);
80 return(0);
84 /* raw files */
86 static int _out_open_raw(char * file)
88 if((f=fopen(file, "w"))==NULL)
89 return(-100);
91 return(0);
95 static int _out_close_raw(void)
97 fclose(f);
98 return(0);
102 /* pipes */
104 static int _out_open_pipe(char * file)
106 /* as there is no default pipe output,
107 file can arrive here as NULL; fail */
108 if(file == NULL)
109 return(-101);
111 if((f=popen(file, "w"))==NULL)
112 return(-100);
114 return(0);
118 static int _out_close_pipe(void)
120 pclose(f);
121 return(0);
125 /* wav files */
127 static int _out_open_wav(char * file)
129 if((f=fopen(file, "w"))==NULL)
130 return(-100);
132 frame_size=ss_nchannels * 2;
134 /* write wav header */
136 fwrite("RIFF",1,4,f);
137 /* first checkpoint (offset: 4) */
138 fput32(36, f);
139 fwrite("WAVE",1,4,f);
140 fwrite("fmt ",1,4,f);
141 fput32(16,f); /* chunk size */
142 fput16(1, f); /* 1: uncompressed PCM */
143 fput16(ss_nchannels, f); /* # of channels */
144 fput32(ss_frequency, f); /* sample rate */
145 fput32(ss_frequency * frame_size, f); /* bytes per second */
146 fput16(frame_size, f); /* 'block align' */
147 fput16(16, f); /* 16 bits per sample */
148 fwrite("data",1,4,f);
149 /* second checkpoint (offset: 40) */
150 fput32(0, f);
152 return(0);
156 static int _out_close_wav(void)
158 /* rewind file and write total size */
159 fseek(f, 4, SEEK_SET);
160 fput32((ss_output_frames * frame_size) + 36, f);
162 fseek(f, 40, SEEK_SET);
163 fput32(ss_output_frames * frame_size, f);
165 fclose(f);
167 return(0);
171 /* drivers */
173 struct _output_driver _out_driver_wav=
174 { "wav", "output.wav", _out_open_wav, _out_write_file, _out_close_wav };
176 struct _output_driver _out_driver_raw=
177 { "raw", "output.raw", _out_open_raw, _out_write_file, _out_close_raw };
179 struct _output_driver _out_driver_pipe=
180 { "pipe", NULL, _out_open_pipe, _out_write_file, _out_close_pipe };