Debian Codegraph @

src/snd-sdl.c

raw symbols SCIP
1 /**
2 * \file snd-sdl.c
3 * \brief SDL sound support
4 *
5 * Copyright (c) 2004 Brendon Oliver <[email protected]>
6 * Copyright (c) 2007 Andi Sidwell <[email protected]>
7 * Copyright (c) 2016 Graeme Russ <[email protected]>
8 * A large chunk of this file was taken and modified from main-ros.
9 *
10 * This work is free software; you can redistribute it and/or modify it
11 * under the terms of either:
12 *
13 * a) the GNU General Public License as published by the Free Software
14 * Foundation, version 2, or
15 *
16 * b) the "Angband licence":
17 * This software may be copied and distributed for educational, research,
18 * and not for profit purposes provided that this copyright and statement
19 * are included in all such copies. Other copyrights may also apply.
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 /* SOUND_SDL */
30
31 #ifdef SOUND_SDL2
32 # include <SDL.h>
33 # include <SDL_mixer.h>
34 #endif /* SOUND_SDL2 */
35
36 /**
37 * Struct representing all data about an event sample
38 */
39 typedef struct
40 {
41 union {
42 Mix_Chunk *chunk; /* Sample in WAVE format */
43 Mix_Music *music; /* Sample in MP3 format */
44 } sample_data;
45
46 int sample_type;
47 } sdl_sample;
48
49 /* Supported file types */
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 * Initialise SDL and open the mixer.
62 */
63 static bool open_audio_sdl(void)
64 {
65 /* Initialize variables */
66 int audio_rate = 22050;
67 Uint16 audio_format = AUDIO_S16;
68 int audio_channels = 2;
69
70 /* Initialize the SDL library */
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 /* Try to open the audio */
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 /* Success */
84 return true;
85 }
86
87 /**
88 * Load a sound from file.
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 * Load a sound and return a pointer to the associated SDL Sound data
119 * structure back to the core sound module.
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 /* Try and load the sample file */
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 * Play the sound stored in the provided SDL Sound data structure.
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 * Free resources referenced in the provided SDL Sound data structure.
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 * Shut down the SDL sound module and free resources.
204 */
205 static bool close_audio_sdl(void)
206 {
207 /*
208 * Close the audio.
209 *
210 * NOTE: All samples will have been free'd by the sound subsystem
211 * calling unload_sound_sdl() for every sample that was loaded.
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 * Init the SDL sound module.
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 /* Success */
237 return (0);
238 }