This is a fork of Zandronum used on servers hosted by The Sentinels Playground (TSPG), Euroboros (EB), and Down Under Doomers (DUD).
修訂 | 4fd3ada9ffe08549db04a44cd99ee150a5d0b1da (tree) |
---|---|
時間 | 2009-03-11 14:10:55 |
作者 | Randy Heit <rheit@zdoo...> |
Commiter | Randy Heit |
It would help if I actually added the output_sdl code, eh?
SVN r1474 (trunk)
@@ -0,0 +1,8 @@ | ||
1 | +cmake_minimum_required( VERSION 2.4 ) | |
2 | +add_library( output_sdl MODULE output_sdl.c ) | |
3 | +include_directories( ${FMOD_INCLUDE_DIR} ${SDL_INCLUDE_DIR} ) | |
4 | + | |
5 | +FILE( WRITE ${CMAKE_CURRENT_BINARY_DIR}/link-make "if [ ! -e ${ZDOOM_OUTPUT_DIR}/liboutput_sdl.so ]; then ln -sf output_sdl/liboutput_sdl.so ${ZDOOM_OUTPUT_DIR}/liboutput_sdl.so; fi" ) | |
6 | +add_custom_command( TARGET output_sdl POST_BUILD | |
7 | + COMMAND chmod +x ${CMAKE_CURRENT_BINARY_DIR}/link-make | |
8 | + COMMAND /bin/sh -c ${CMAKE_CURRENT_BINARY_DIR}/link-make ) |
@@ -0,0 +1,215 @@ | ||
1 | +#include <stdio.h> | |
2 | +#include "fmod.h" | |
3 | +#include "fmod_output.h" | |
4 | +#include "SDL.h" | |
5 | + | |
6 | +#define CONVERTBUFFER_SIZE 4096 // in bytes | |
7 | + | |
8 | +#define D(x) | |
9 | + | |
10 | +#define FALSE 0 | |
11 | +#define TRUE 1 | |
12 | + | |
13 | +typedef int BOOL; | |
14 | + | |
15 | +struct AudioData | |
16 | +{ | |
17 | + FMOD_OUTPUT_STATE *Output; | |
18 | + BOOL ConvertU8toS8; | |
19 | + BOOL ConvertU16toS16; | |
20 | + int BytesPerSample; | |
21 | +}; | |
22 | + | |
23 | +FMOD_SOUND_FORMAT Format_SDLtoFMOD(Uint16 format) | |
24 | +{ | |
25 | + if ((format & (AUDIO_U8 | AUDIO_U16LSB)) == AUDIO_U8) | |
26 | + { | |
27 | + return FMOD_SOUND_FORMAT_PCM8; | |
28 | + } | |
29 | + return FMOD_SOUND_FORMAT_PCM16; | |
30 | +} | |
31 | + | |
32 | +Uint16 Format_FMODtoSDL(FMOD_SOUND_FORMAT format) | |
33 | +{ | |
34 | + switch (format) | |
35 | + { | |
36 | + case FMOD_SOUND_FORMAT_PCM8: return AUDIO_S8; | |
37 | + case FMOD_SOUND_FORMAT_PCM16: return AUDIO_S16SYS; | |
38 | + default: return AUDIO_S16SYS; | |
39 | + } | |
40 | +} | |
41 | + | |
42 | +static void SDLCALL AudioCallback(void *userdata, Uint8 *stream, int len) | |
43 | +{ | |
44 | + struct AudioData *data = (struct AudioData *)userdata; | |
45 | + int i; | |
46 | + | |
47 | + data->Output->readfrommixer(data->Output, stream, len / data->BytesPerSample); | |
48 | + | |
49 | + if (data->ConvertU8toS8) | |
50 | + { | |
51 | + for (i = 0; i < len; ++i) | |
52 | + { | |
53 | + stream[i] -= 0x80; | |
54 | + } | |
55 | + } | |
56 | + else if (data->ConvertU16toS16) | |
57 | + { | |
58 | + len /= 2; | |
59 | + for (i = 0; i < len; ++i) | |
60 | + { | |
61 | + ((short *)stream)[i] -= 0x8000; | |
62 | + } | |
63 | + } | |
64 | +} | |
65 | + | |
66 | +static FMOD_RESULT F_CALLBACK GetNumDrivers(FMOD_OUTPUT_STATE *output_state, int *numdrivers) | |
67 | +{ | |
68 | + if (numdrivers == NULL) | |
69 | + { | |
70 | + return FMOD_ERR_INVALID_PARAM; | |
71 | + } | |
72 | + *numdrivers = 1; | |
73 | + return FMOD_OK; | |
74 | +} | |
75 | + | |
76 | +static FMOD_RESULT F_CALLBACK GetDriverName(FMOD_OUTPUT_STATE *output_state, int id, char *name, int namelen) | |
77 | +{ | |
78 | + if (id != 0 || name == NULL) | |
79 | + { | |
80 | + return FMOD_ERR_INVALID_PARAM; | |
81 | + } | |
82 | + strncpy(name, "SDL default", namelen); | |
83 | + return FMOD_OK; | |
84 | +} | |
85 | + | |
86 | +static FMOD_RESULT F_CALLBACK GetDriverCaps(FMOD_OUTPUT_STATE *output_state, int id, FMOD_CAPS *caps) | |
87 | +{ | |
88 | + if (id != 0 || caps == NULL) | |
89 | + { | |
90 | + return FMOD_ERR_INVALID_PARAM; | |
91 | + } | |
92 | + *caps = FMOD_CAPS_OUTPUT_FORMAT_PCM8 | FMOD_CAPS_OUTPUT_FORMAT_PCM16 | FMOD_CAPS_OUTPUT_MULTICHANNEL; | |
93 | + return FMOD_OK; | |
94 | +} | |
95 | + | |
96 | +static FMOD_RESULT F_CALLBACK Init(FMOD_OUTPUT_STATE *output_state, int selecteddriver, | |
97 | + FMOD_INITFLAGS flags, int *outputrate, int outputchannels, | |
98 | + FMOD_SOUND_FORMAT *outputformat, int dspbufferlength, int dspnumbuffers, | |
99 | + void *extradriverdata) | |
100 | +{ | |
101 | + SDL_AudioSpec desired, obtained; | |
102 | + struct AudioData *data; | |
103 | + | |
104 | + if (selecteddriver != 0 || outputrate == NULL || outputformat == NULL) | |
105 | + { | |
106 | + D(printf("invalid param\n")); | |
107 | + return FMOD_ERR_INVALID_PARAM; | |
108 | + } | |
109 | + if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) | |
110 | + { | |
111 | + D(printf("init subsystem failed\n")); | |
112 | + return FMOD_ERR_OUTPUT_INIT; | |
113 | + } | |
114 | + data = malloc(sizeof(*data)); | |
115 | + if (data == NULL) | |
116 | + { | |
117 | + D(printf("nomem\n")); | |
118 | + SDL_QuitSubSystem(SDL_INIT_AUDIO); | |
119 | + return FMOD_ERR_MEMORY; | |
120 | + } | |
121 | + desired.freq = *outputrate; | |
122 | + desired.format = Format_FMODtoSDL(*outputformat); | |
123 | + desired.channels = outputchannels; | |
124 | + desired.samples = dspbufferlength; | |
125 | + desired.callback = AudioCallback; | |
126 | + desired.userdata = data; | |
127 | + if (SDL_OpenAudio(&desired, &obtained) < 0) | |
128 | + { | |
129 | + D(printf("openaudio failed\n")); | |
130 | + free(data); | |
131 | + SDL_QuitSubSystem(SDL_INIT_AUDIO); | |
132 | + return FMOD_ERR_OUTPUT_INIT; | |
133 | + } | |
134 | + if (obtained.channels != outputchannels) | |
135 | + { // Obtained channels don't match what we wanted. | |
136 | + SDL_CloseAudio(); | |
137 | + SDL_QuitSubSystem(SDL_INIT_AUDIO); | |
138 | + free(data); | |
139 | + return FMOD_ERR_OUTPUT_CREATEBUFFER; | |
140 | + } | |
141 | + data->Output = output_state; | |
142 | + data->ConvertU8toS8 = FALSE; | |
143 | + data->ConvertU16toS16 = FALSE; | |
144 | + if (obtained.format == AUDIO_U8) | |
145 | + { | |
146 | + data->ConvertU8toS8 = TRUE; | |
147 | + D(printf("convert u8 to s8\n")); | |
148 | + } | |
149 | + else if (obtained.format == AUDIO_U16SYS) | |
150 | + { | |
151 | + data->ConvertU16toS16 = TRUE; | |
152 | + D(printf("convert u16 to s16\n")); | |
153 | + } | |
154 | + output_state->plugindata = data; | |
155 | + *outputrate = obtained.freq; | |
156 | + *outputformat = Format_SDLtoFMOD(obtained.format); | |
157 | + data->BytesPerSample = *outputformat == FMOD_SOUND_FORMAT_PCM16 ? 2 : 1; | |
158 | + data->BytesPerSample *= desired.channels; | |
159 | + D(printf("init ok\n")); | |
160 | + SDL_PauseAudio(0); | |
161 | + return FMOD_OK; | |
162 | +} | |
163 | + | |
164 | +static FMOD_RESULT F_CALLBACK Close(FMOD_OUTPUT_STATE *output_state) | |
165 | +{ | |
166 | + struct AudioData *data = (struct AudioData *)output_state->plugindata; | |
167 | + | |
168 | + D(printf("Close\n")); | |
169 | + if (data != NULL) | |
170 | + { | |
171 | + SDL_CloseAudio(); | |
172 | + SDL_QuitSubSystem(SDL_INIT_AUDIO); | |
173 | + free(data); | |
174 | + } | |
175 | + return FMOD_OK; | |
176 | +} | |
177 | + | |
178 | +static FMOD_RESULT F_CALLBACK Update(FMOD_OUTPUT_STATE *update) | |
179 | +{ | |
180 | + return FMOD_OK; | |
181 | +} | |
182 | + | |
183 | +static FMOD_RESULT F_CALLBACK GetHandle(FMOD_OUTPUT_STATE *output_state, void **handle) | |
184 | +{ | |
185 | + D(printf("Gethandle\n")); | |
186 | + // SDL's audio isn't multi-instanced, so this is pretty meaningless | |
187 | + if (handle == NULL) | |
188 | + { | |
189 | + return FMOD_ERR_INVALID_PARAM; | |
190 | + } | |
191 | + *handle = output_state->plugindata; | |
192 | + return FMOD_OK; | |
193 | +} | |
194 | + | |
195 | +static FMOD_OUTPUT_DESCRIPTION Desc = | |
196 | +{ | |
197 | + "SDL Output", // name | |
198 | + 1, // version | |
199 | + 0, // polling | |
200 | + GetNumDrivers, | |
201 | + GetDriverName, | |
202 | + GetDriverCaps, | |
203 | + Init, | |
204 | + Close, | |
205 | + Update, | |
206 | + GetHandle, | |
207 | + NULL, // getposition | |
208 | + NULL, // lock | |
209 | + NULL // unlock | |
210 | +}; | |
211 | + | |
212 | +F_DECLSPEC F_DLLEXPORT FMOD_OUTPUT_DESCRIPTION * F_API FMODGetOutputDescription() | |
213 | +{ | |
214 | + return &Desc; | |
215 | +} |