1 /*
2 	snd_disk.c
3 
4 	write sound to a disk file
5 
6 	Copyright (C) 1999,2000  contributors of the QuakeForge project
7 	Please see the file "AUTHORS" for a list of contributors
8 
9 	This program is free software; you can redistribute it and/or
10 	modify it under the terms of the GNU General Public License
11 	as published by the Free Software Foundation; either version 2
12 	of the License, or (at your option) any later version.
13 
14 	This program is distributed in the hope that it will be useful,
15 	but WITHOUT ANY WARRANTY; without even the implied warranty of
16 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 	See the GNU General Public License for more details.
19 
20 	You should have received a copy of the GNU General Public License
21 	along with this program; if not, write to:
22 
23 		Free Software Foundation, Inc.
24 		59 Temple Place - Suite 330
25 		Boston, MA  02111-1307, USA
26 
27 */
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #endif
38 #ifdef HAVE_STRINGS_H
39 # include <strings.h>
40 #endif
41 
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <sys/types.h>
46 
47 #include "QF/cvar.h"
48 #include "QF/qargs.h"
49 #include "QF/sys.h"
50 
51 #include "snd_internal.h"
52 
53 static int  snd_inited;
54 static QFile      *snd_file;
55 static int snd_blocked = 0;
56 static volatile dma_t sn;
57 
58 static plugin_t				plugin_info;
59 static plugin_data_t		plugin_info_data;
60 static plugin_funcs_t		plugin_info_funcs;
61 static general_data_t		plugin_info_general_data;
62 static general_funcs_t		plugin_info_general_funcs;
63 static snd_output_data_t	plugin_info_snd_output_data;
64 static snd_output_funcs_t	plugin_info_snd_output_funcs;
65 
66 
67 static volatile dma_t *
SNDDMA_Init(void)68 SNDDMA_Init (void)
69 {
70 	memset ((dma_t *) &sn, 0, sizeof (sn));
71 	sn.channels = 2;
72 	sn.submission_chunk = 1;			// don't mix less than this #
73 	sn.framepos = 0;
74 	sn.samplebits = 16;
75 	sn.frames = 16384;
76 	sn.speed = 44100;
77 	sn.buffer = malloc (sn.frames * sn.channels * sn.samplebits / 8);
78 	if (!sn.buffer) {
79 		Sys_Printf ("SNDDMA_Init: memory allocation failure\n");
80 		return 0;
81 	}
82 
83 	Sys_Printf ("%5d channels\n", sn.channels - 1);
84 	Sys_Printf ("%5d samples\n", sn.frames);
85 	Sys_Printf ("%5d samplepos\n", sn.framepos);
86 	Sys_Printf ("%5d samplebits\n", sn.samplebits);
87 	Sys_Printf ("%5d submission_chunk\n", sn.submission_chunk);
88 	Sys_Printf ("%5d speed\n", sn.speed);
89 	Sys_Printf ("0x%lx dma buffer\n", (long) sn.buffer);
90 
91 	if (!(snd_file = Qopen ("qf.raw", "wb")))
92 		return 0;
93 
94 	snd_inited = 1;
95 	return &sn;
96 }
97 
98 static void
SNDDMA_Init_Cvars(void)99 SNDDMA_Init_Cvars (void)
100 {
101 }
102 
103 static int
SNDDMA_GetDMAPos(void)104 SNDDMA_GetDMAPos (void)
105 {
106 	sn.framepos = 0;
107 	return sn.framepos;
108 }
109 
110 static void
SNDDMA_Shutdown(void)111 SNDDMA_Shutdown (void)
112 {
113 	if (snd_inited) {
114 		Qclose (snd_file);
115 		snd_file = 0;
116 		free (sn.buffer);
117 		snd_inited = 0;
118 	}
119 }
120 
121 /*
122 	SNDDMA_Submit
123 
124 	Send sound to device if buffer isn't really the dma buffer
125 */
126 static void
SNDDMA_Submit(void)127 SNDDMA_Submit (void)
128 {
129 	int		count = ((*plugin_info_snd_output_data.paintedtime -
130 					  *plugin_info_snd_output_data.soundtime) *
131 					 sn.samplebits / 8);
132 
133 	if (snd_blocked)
134 		return;
135 
136 	Qwrite (snd_file, sn.buffer, count);
137 }
138 
139 static void
SNDDMA_BlockSound(void)140 SNDDMA_BlockSound (void)
141 {
142 	++snd_blocked;
143 }
144 
145 static void
SNDDMA_UnblockSound(void)146 SNDDMA_UnblockSound (void)
147 {
148 	if (!snd_blocked)
149 		return;
150 	--snd_blocked;
151 }
152 
PLUGIN_INFO(snd_output,disk)153 PLUGIN_INFO(snd_output, disk)
154 {
155 	plugin_info.type = qfp_snd_output;
156 	plugin_info.api_version = QFPLUGIN_VERSION;
157 	plugin_info.plugin_version = "0.1";
158 	plugin_info.description = "disk output";
159 	plugin_info.copyright = "Copyright (C) 1996-1997 id Software, Inc.\n"
160 		"Copyright (C) 1999,2000,2001  contributors of the QuakeForge "
161 		"project\n"
162 		"Please see the file \"AUTHORS\" for a list of contributors";
163 	plugin_info.functions = &plugin_info_funcs;
164 	plugin_info.data = &plugin_info_data;
165 
166 	plugin_info_data.general = &plugin_info_general_data;
167 	plugin_info_data.input = NULL;
168 	plugin_info_data.snd_output = &plugin_info_snd_output_data;
169 
170 	plugin_info_funcs.general = &plugin_info_general_funcs;
171 	plugin_info_funcs.input = NULL;
172 	plugin_info_funcs.snd_output = &plugin_info_snd_output_funcs;
173 
174 	plugin_info_general_funcs.p_Init = SNDDMA_Init_Cvars;
175 	plugin_info_general_funcs.p_Shutdown = NULL;
176 	plugin_info_snd_output_funcs.pS_O_Init = SNDDMA_Init;
177 	plugin_info_snd_output_funcs.pS_O_Shutdown = SNDDMA_Shutdown;
178 	plugin_info_snd_output_funcs.pS_O_GetDMAPos = SNDDMA_GetDMAPos;
179 	plugin_info_snd_output_funcs.pS_O_Submit = SNDDMA_Submit;
180 	plugin_info_snd_output_funcs.pS_O_BlockSound = SNDDMA_BlockSound;
181 	plugin_info_snd_output_funcs.pS_O_UnblockSound = SNDDMA_UnblockSound;
182 
183 	return &plugin_info;
184 }
185