1 /* FluidSynth - A Software Synthesizer
2  *
3  * Copyright (C) 2003  Peter Hanappe and others.
4  * Copyright (C) 2018  Carlo Bramini
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA
20  */
21 
22 #include "fluid_synth.h"
23 #include "fluid_adriver.h"
24 #include "fluid_settings.h"
25 
26 #if SDL2_SUPPORT
27 
28 #include "SDL.h"
29 
30 typedef struct
31 {
32     fluid_audio_driver_t driver;
33 
34     fluid_synth_t *synth;
35     fluid_audio_callback_t write_ptr;
36 
37     SDL_AudioDeviceID devid;
38 
39     int frame_size;
40 
41 } fluid_sdl2_audio_driver_t;
42 
43 
44 static void
SDLAudioCallback(void * data,void * stream,int len)45 SDLAudioCallback(void *data, void *stream, int len)
46 {
47     fluid_sdl2_audio_driver_t *dev = (fluid_sdl2_audio_driver_t *)data;
48 
49     len /= dev->frame_size;
50 
51     dev->write_ptr(dev->synth, len, stream, 0, 2, stream, 1, 2);
52 }
53 
fluid_sdl2_audio_driver_settings(fluid_settings_t * settings)54 void fluid_sdl2_audio_driver_settings(fluid_settings_t *settings)
55 {
56     int n, nDevs;
57 
58     fluid_settings_register_str(settings, "audio.sdl2.device", "default", 0);
59     fluid_settings_add_option(settings, "audio.sdl2.device", "default");
60 
61     if(!SDL_WasInit(SDL_INIT_AUDIO))
62     {
63 #if FLUID_VERSION_CHECK(FLUIDSYNTH_VERSION_MAJOR, FLUIDSYNTH_VERSION_MINOR, FLUIDSYNTH_VERSION_MICRO) < FLUID_VERSION_CHECK(2,2,0)
64         FLUID_LOG(FLUID_WARN, "SDL2 not initialized, SDL2 audio driver won't be usable");
65 #endif
66         return;
67     }
68 
69     nDevs = SDL_GetNumAudioDevices(0);
70 
71     for(n = 0; n < nDevs; n++)
72     {
73         const char *dev_name = SDL_GetAudioDeviceName(n, 0);
74 
75         if(dev_name != NULL)
76         {
77             FLUID_LOG(FLUID_DBG, "SDL2 driver testing audio device: %s", dev_name);
78             fluid_settings_add_option(settings, "audio.sdl2.device", dev_name);
79         }
80     }
81 }
82 
83 
84 /*
85  * new_fluid_sdl2_audio_driver
86  */
87 fluid_audio_driver_t *
new_fluid_sdl2_audio_driver(fluid_settings_t * settings,fluid_synth_t * synth)88 new_fluid_sdl2_audio_driver(fluid_settings_t *settings, fluid_synth_t *synth)
89 {
90     fluid_sdl2_audio_driver_t *dev = NULL;
91     fluid_audio_callback_t write_ptr;
92     double sample_rate;
93     int period_size, sample_size;
94     SDL_AudioSpec aspec, rspec;
95     char *device;
96     const char *dev_name;
97 
98     /* Check if SDL library has been started */
99     if(!SDL_WasInit(SDL_INIT_AUDIO))
100     {
101         FLUID_LOG(FLUID_ERR, "Failed to create SDL2 audio driver, because the audio subsystem of SDL2 is not initialized.");
102         return NULL;
103     }
104 
105     /* Retrieve the settings */
106     fluid_settings_getnum(settings, "synth.sample-rate", &sample_rate);
107     fluid_settings_getint(settings, "audio.period-size", &period_size);
108 
109     /* Lower values do not seem to give good results */
110     if(period_size < 1024)
111     {
112         period_size = 1024;
113     }
114     else
115     {
116         /* According to documentation, it MUST be a power of two */
117         if((period_size & (period_size - 1)) != 0)
118         {
119             FLUID_LOG(FLUID_ERR, "\"audio.period-size\" must be a power of 2 for SDL2");
120             return NULL;
121         }
122     }
123     /* Clear the format buffer */
124     FLUID_MEMSET(&aspec, 0, sizeof(aspec));
125 
126     /* Setup mixing frequency */
127     aspec.freq = (int)sample_rate;
128 
129     /* Check the format */
130     if(fluid_settings_str_equal(settings, "audio.sample-format", "float"))
131     {
132         FLUID_LOG(FLUID_DBG, "Selected 32 bit sample format");
133 
134         sample_size = sizeof(float);
135         write_ptr   = fluid_synth_write_float;
136 
137         aspec.format = AUDIO_F32SYS;
138     }
139     else if(fluid_settings_str_equal(settings, "audio.sample-format", "16bits"))
140     {
141         FLUID_LOG(FLUID_DBG, "Selected 16 bit sample format");
142 
143         sample_size = sizeof(short);
144         write_ptr   = fluid_synth_write_s16;
145 
146         aspec.format = AUDIO_S16SYS;
147     }
148     else
149     {
150         FLUID_LOG(FLUID_ERR, "Unhandled sample format");
151         return NULL;
152     }
153 
154     /* Compile the format buffer */
155     aspec.channels   = 2;
156     aspec.samples    = aspec.channels * ((period_size + 7) & ~7);
157     aspec.callback   = (SDL_AudioCallback)SDLAudioCallback;
158 
159     /* Set default device to use */
160     device   = NULL;
161     dev_name = NULL;
162 
163     /* get the selected device name. if none is specified, use default device. */
164     if(fluid_settings_dupstr(settings, "audio.sdl2.device", &device) == FLUID_OK
165             && device != NULL && device[0] != '\0')
166     {
167         int n, nDevs = SDL_GetNumAudioDevices(0);
168 
169         for(n = 0; n < nDevs; n++)
170         {
171             dev_name = SDL_GetAudioDeviceName(n, 0);
172 
173             if(FLUID_STRCASECMP(dev_name, device) == 0)
174             {
175                 FLUID_LOG(FLUID_DBG, "Selected audio device GUID: %s", dev_name);
176                 break;
177             }
178         }
179 
180         if(n >= nDevs)
181         {
182             FLUID_LOG(FLUID_DBG, "Audio device %s, using \"default\"", device);
183             dev_name = NULL;
184         }
185     }
186 
187     if(device != NULL)
188     {
189         FLUID_FREE(device);
190     }
191 
192     do
193     {
194         /* create and clear the driver data */
195         dev = FLUID_NEW(fluid_sdl2_audio_driver_t);
196 
197         if(dev == NULL)
198         {
199             FLUID_LOG(FLUID_ERR, "Out of memory");
200             break;
201         }
202 
203         FLUID_MEMSET(dev, 0, sizeof(fluid_sdl2_audio_driver_t));
204 
205         /* set device pointer to userdata */
206         aspec.userdata = dev;
207 
208         /* Save copy of synth */
209         dev->synth = synth;
210 
211         /* Save copy of other variables */
212         dev->write_ptr = write_ptr;
213         dev->frame_size = sample_size * aspec.channels;
214 
215         /* Open audio device */
216         dev->devid = SDL_OpenAudioDevice(dev_name, 0, &aspec, &rspec, 0);
217 
218         if(!dev->devid)
219         {
220             FLUID_LOG(FLUID_ERR, "Failed to open audio device");
221             break;
222         }
223 
224         /* Start to play */
225         SDL_PauseAudioDevice(dev->devid, 0);
226 
227         return (fluid_audio_driver_t *) dev;
228     }
229     while(0);
230 
231     delete_fluid_sdl2_audio_driver(&dev->driver);
232     return NULL;
233 }
234 
235 
delete_fluid_sdl2_audio_driver(fluid_audio_driver_t * d)236 void delete_fluid_sdl2_audio_driver(fluid_audio_driver_t *d)
237 {
238     fluid_sdl2_audio_driver_t *dev = (fluid_sdl2_audio_driver_t *) d;
239 
240     if(dev != NULL)
241     {
242         if(dev->devid)
243         {
244             /* Stop audio and close */
245             SDL_PauseAudioDevice(dev->devid, 1);
246             SDL_CloseAudioDevice(dev->devid);
247         }
248 
249         FLUID_FREE(dev);
250     }
251 }
252 
253 #endif /* SDL2_SUPPORT */
254