| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "angband.h" |
| 22 |
#include "init.h" |
| 23 |
#include "snd-sdl.h" |
| 24 |
#include "sound.h" |
| 25 |
|
| 26 |
#ifdef SOUND_SDL |
| 27 |
# include <SDL/SDL.h> |
| 28 |
# include <SDL/SDL_mixer.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#ifdef SOUND_SDL2 |
| 32 |
# include <SDL.h> |
| 33 |
# include <SDL_mixer.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
typedef struct |
| 40 |
{ |
| 41 |
union { |
| 42 |
Mix_Chunk *chunk; |
| 43 |
Mix_Music *music; |
| 44 |
} sample_data; |
| 45 |
|
| 46 |
int sample_type; |
| 47 |
} sdl_sample; |
| 48 |
|
| 49 |
|
| 50 |
enum { |
| 51 |
SDL_NULL = 0, |
| 52 |
SDL_CHUNK, |
| 53 |
SDL_MUSIC |
| 54 |
}; |
| 55 |
|
| 56 |
static const struct sound_file_type supported_sound_files[] = { {".mp3", SDL_MUSIC}, |
| 57 |
{".ogg", SDL_CHUNK}, |
| 58 |
{"", SDL_NULL} }; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static bool open_audio_sdl(void) |
| 64 |
{ |
| 65 |
|
| 66 |
int audio_rate = 22050; |
| 67 |
Uint16 audio_format = AUDIO_S16; |
| 68 |
int audio_channels = 2; |
| 69 |
|
| 70 |
|
| 71 |
if (SDL_Init(SDL_INIT_AUDIO) < 0) { |
| 72 |
plog_fmt("SDL: Couldn't initialize SDL: %s", SDL_GetError()); |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) { |
| 78 |
plog_fmt("SDL: Couldn't open mixer: %s", SDL_GetError()); |
| 79 |
SDL_QuitSubSystem(SDL_INIT_AUDIO); |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static bool load_sample_sdl(const char *filename, int ft, sdl_sample *sample) |
| 91 |
{ |
| 92 |
switch (ft) { |
| 93 |
case SDL_CHUNK: |
| 94 |
sample->sample_data.chunk = Mix_LoadWAV(filename); |
| 95 |
|
| 96 |
if (sample->sample_data.chunk) |
| 97 |
return true; |
| 98 |
|
| 99 |
break; |
| 100 |
|
| 101 |
case SDL_MUSIC: |
| 102 |
sample->sample_data.music = Mix_LoadMUS(filename); |
| 103 |
|
| 104 |
if (sample->sample_data.music) |
| 105 |
return true; |
| 106 |
|
| 107 |
break; |
| 108 |
|
| 109 |
default: |
| 110 |
plog("SDL: Oops - Unsupported file type"); |
| 111 |
break; |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static bool load_sound_sdl(const char *filename, int ft, struct sound_data *data) |
| 122 |
{ |
| 123 |
sdl_sample *sample = (sdl_sample *)(data->plat_data); |
| 124 |
|
| 125 |
if (!sample) |
| 126 |
sample = mem_zalloc(sizeof(*sample)); |
| 127 |
|
| 128 |
|
| 129 |
if (load_sample_sdl(filename, ft, sample)) { |
| 130 |
data->status = SOUND_ST_LOADED; |
| 131 |
sample->sample_type = ft; |
| 132 |
} else { |
| 133 |
mem_free(sample); |
| 134 |
sample = NULL; |
| 135 |
} |
| 136 |
|
| 137 |
data->plat_data = (void *)sample; |
| 138 |
|
| 139 |
return (NULL != sample); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
static bool play_sound_sdl(struct sound_data *data) |
| 146 |
{ |
| 147 |
sdl_sample *sample = (sdl_sample *)(data->plat_data); |
| 148 |
|
| 149 |
if (sample) { |
| 150 |
switch (sample->sample_type) { |
| 151 |
case SDL_CHUNK: |
| 152 |
if (sample->sample_data.chunk) |
| 153 |
return (0 == Mix_PlayChannel(-1, sample->sample_data.chunk, 0)); |
| 154 |
break; |
| 155 |
|
| 156 |
case SDL_MUSIC: |
| 157 |
if (sample->sample_data.music) |
| 158 |
return (0 == Mix_PlayMusic(sample->sample_data.music, 1)); |
| 159 |
break; |
| 160 |
|
| 161 |
default: |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
static bool unload_sound_sdl(struct sound_data *data) |
| 173 |
{ |
| 174 |
sdl_sample *sample = (sdl_sample *)(data->plat_data); |
| 175 |
|
| 176 |
if (sample) { |
| 177 |
switch (sample->sample_type) { |
| 178 |
case SDL_CHUNK: |
| 179 |
if (sample->sample_data.chunk) |
| 180 |
Mix_FreeChunk(sample->sample_data.chunk); |
| 181 |
|
| 182 |
break; |
| 183 |
|
| 184 |
case SDL_MUSIC: |
| 185 |
if (sample->sample_data.music) |
| 186 |
Mix_FreeMusic(sample->sample_data.music); |
| 187 |
|
| 188 |
break; |
| 189 |
|
| 190 |
default: |
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
mem_free(sample); |
| 195 |
data->plat_data = NULL; |
| 196 |
data->status = SOUND_ST_UNKNOWN; |
| 197 |
} |
| 198 |
|
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static bool close_audio_sdl(void) |
| 206 |
{ |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
Mix_CloseAudio(); |
| 214 |
SDL_QuitSubSystem(SDL_INIT_AUDIO); |
| 215 |
|
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
static const struct sound_file_type *supported_files_sdl(void) |
| 220 |
{ |
| 221 |
return supported_sound_files; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
errr init_sound_sdl(struct sound_hooks *hooks, int argc, char **argv) |
| 228 |
{ |
| 229 |
hooks->open_audio_hook = open_audio_sdl; |
| 230 |
hooks->supported_files_hook = supported_files_sdl; |
| 231 |
hooks->close_audio_hook = close_audio_sdl; |
| 232 |
hooks->load_sound_hook = load_sound_sdl; |
| 233 |
hooks->unload_sound_hook = unload_sound_sdl; |
| 234 |
hooks->play_sound_hook = play_sound_sdl; |
| 235 |
|
| 236 |
|
| 237 |
return (0); |
| 238 |
} |