fluid_adriver.c
上传用户:tjmskj2
上传日期:2020-08-17
资源大小:577k
文件大小:13k
源码类别:

midi

开发平台:

C/C++

  1. /* FluidSynth - A Software Synthesizer
  2.  *
  3.  * Copyright (C) 2003  Peter Hanappe and others.
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public License
  7.  * as published by the Free Software Foundation; either version 2 of
  8.  * the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18.  * 02111-1307, USA
  19.  */
  20. #include "fluid_adriver.h"
  21. #include "fluid_settings.h"
  22. /*
  23.  * fluid_adriver_definition_t
  24.  */
  25. typedef struct _fluid_audriver_definition_t
  26. {
  27.   char* name;
  28.   fluid_audio_driver_t* (*new)(fluid_settings_t* settings, fluid_synth_t* synth);
  29.   fluid_audio_driver_t* (*new2)(fluid_settings_t* settings,
  30. fluid_audio_func_t func,
  31. void* data);
  32.   int (*free)(fluid_audio_driver_t* driver);
  33.   void (*settings)(fluid_settings_t* settings);
  34. } fluid_audriver_definition_t;
  35. #if PULSE_SUPPORT
  36. fluid_audio_driver_t* new_fluid_pulse_audio_driver(fluid_settings_t* settings,
  37.    fluid_synth_t* synth);
  38. fluid_audio_driver_t* new_fluid_pulse_audio_driver2(fluid_settings_t* settings,
  39.     fluid_audio_func_t func, void* data);
  40. int delete_fluid_pulse_audio_driver(fluid_audio_driver_t* p);
  41. void fluid_pulse_audio_driver_settings(fluid_settings_t* settings);
  42. #endif
  43. #if ALSA_SUPPORT
  44. fluid_audio_driver_t* new_fluid_alsa_audio_driver(fluid_settings_t* settings,
  45.   fluid_synth_t* synth);
  46. fluid_audio_driver_t* new_fluid_alsa_audio_driver2(fluid_settings_t* settings,
  47.  fluid_audio_func_t func, void* data);
  48. int delete_fluid_alsa_audio_driver(fluid_audio_driver_t* p);
  49. void fluid_alsa_audio_driver_settings(fluid_settings_t* settings);
  50. #endif
  51. #if OSS_SUPPORT
  52. fluid_audio_driver_t* new_fluid_oss_audio_driver(fluid_settings_t* settings,
  53.  fluid_synth_t* synth);
  54. fluid_audio_driver_t* new_fluid_oss_audio_driver2(fluid_settings_t* settings,
  55. fluid_audio_func_t func, void* data);
  56. int delete_fluid_oss_audio_driver(fluid_audio_driver_t* p);
  57. void fluid_oss_audio_driver_settings(fluid_settings_t* settings);
  58. #endif
  59. #if COREAUDIO_SUPPORT
  60. fluid_audio_driver_t* new_fluid_core_audio_driver(fluid_settings_t* settings,
  61.   fluid_synth_t* synth);
  62. fluid_audio_driver_t* new_fluid_core_audio_driver2(fluid_settings_t* settings,
  63.       fluid_audio_func_t func,
  64.       void* data);
  65. int delete_fluid_core_audio_driver(fluid_audio_driver_t* p);
  66. void fluid_core_audio_driver_settings(fluid_settings_t* settings);
  67. #endif
  68. #if DSOUND_SUPPORT
  69. fluid_audio_driver_t* new_fluid_dsound_audio_driver(fluid_settings_t* settings,
  70.   fluid_synth_t* synth);
  71. int delete_fluid_dsound_audio_driver(fluid_audio_driver_t* p);
  72. void fluid_dsound_audio_driver_settings(fluid_settings_t* settings);
  73. #endif
  74. #if PORTAUDIO_SUPPORT
  75. void fluid_portaudio_driver_settings (fluid_settings_t *settings);
  76. fluid_audio_driver_t* new_fluid_portaudio_driver(fluid_settings_t* settings,
  77.  fluid_synth_t* synth);
  78. int delete_fluid_portaudio_driver(fluid_audio_driver_t* p);
  79. #endif
  80. #if JACK_SUPPORT
  81. fluid_audio_driver_t* new_fluid_jack_audio_driver(fluid_settings_t* settings, fluid_synth_t* synth);
  82. fluid_audio_driver_t* new_fluid_jack_audio_driver2(fluid_settings_t* settings,
  83.  fluid_audio_func_t func, void* data);
  84. int delete_fluid_jack_audio_driver(fluid_audio_driver_t* p);
  85. void fluid_jack_audio_driver_settings(fluid_settings_t* settings);
  86. #endif
  87. #if SNDMAN_SUPPORT
  88. fluid_audio_driver_t* new_fluid_sndmgr_audio_driver(fluid_settings_t* settings,
  89.   fluid_synth_t* synth);
  90. fluid_audio_driver_t* new_fluid_sndmgr_audio_driver2(fluid_settings_t* settings,
  91.    fluid_audio_func_t func,
  92.    void* data);
  93. int delete_fluid_sndmgr_audio_driver(fluid_audio_driver_t* p);
  94. #endif
  95. #if DART_SUPPORT
  96. fluid_audio_driver_t* new_fluid_dart_audio_driver(fluid_settings_t* settings,
  97.                           fluid_synth_t* synth);
  98. int delete_fluid_dart_audio_driver(fluid_audio_driver_t* p);
  99. void fluid_dart_audio_driver_settings(fluid_settings_t* settings);
  100. #endif
  101. #if AUFILE_SUPPORT
  102. fluid_audio_driver_t* new_fluid_file_audio_driver(fluid_settings_t* settings,
  103.   fluid_synth_t* synth);
  104. int delete_fluid_file_audio_driver(fluid_audio_driver_t* p);
  105. #endif
  106. /* Available audio drivers, listed in order of preference */
  107. fluid_audriver_definition_t fluid_audio_drivers[] = {
  108. #if JACK_SUPPORT
  109.   { "jack",
  110.     new_fluid_jack_audio_driver,
  111.     new_fluid_jack_audio_driver2,
  112.     delete_fluid_jack_audio_driver,
  113.     fluid_jack_audio_driver_settings },
  114. #endif
  115. #if ALSA_SUPPORT
  116.   { "alsa",
  117.     new_fluid_alsa_audio_driver,
  118.     new_fluid_alsa_audio_driver2,
  119.     delete_fluid_alsa_audio_driver,
  120.     fluid_alsa_audio_driver_settings },
  121. #endif
  122. #if OSS_SUPPORT
  123.   { "oss",
  124.     new_fluid_oss_audio_driver,
  125.     new_fluid_oss_audio_driver2,
  126.     delete_fluid_oss_audio_driver,
  127.     fluid_oss_audio_driver_settings },
  128. #endif
  129. #if PULSE_SUPPORT
  130.   { "pulseaudio",
  131.     new_fluid_pulse_audio_driver,
  132.     new_fluid_pulse_audio_driver2,
  133.     delete_fluid_pulse_audio_driver,
  134.     fluid_pulse_audio_driver_settings },
  135. #endif
  136. #if COREAUDIO_SUPPORT
  137.   { "coreaudio",
  138.     new_fluid_core_audio_driver,
  139.     new_fluid_core_audio_driver2,
  140.     delete_fluid_core_audio_driver,
  141.     fluid_core_audio_driver_settings },
  142. #endif
  143. #if DSOUND_SUPPORT
  144.   { "dsound",
  145.     new_fluid_dsound_audio_driver,
  146.     NULL,
  147.     delete_fluid_dsound_audio_driver,
  148.     fluid_dsound_audio_driver_settings },
  149. #endif
  150. #if PORTAUDIO_SUPPORT
  151.   { "portaudio",
  152.     new_fluid_portaudio_driver,
  153.     NULL,
  154.     delete_fluid_portaudio_driver,
  155.     fluid_portaudio_driver_settings },
  156. #endif
  157. #if SNDMAN_SUPPORT
  158.   { "sndman",
  159.     new_fluid_sndmgr_audio_driver,
  160.     new_fluid_sndmgr_audio_driver2,
  161.     delete_fluid_sndmgr_audio_driver,
  162.     NULL },
  163. #endif
  164. #if DART_SUPPORT
  165.   { "dart",
  166.     new_fluid_dart_audio_driver,
  167.     NULL,
  168.     delete_fluid_dart_audio_driver,
  169.     fluid_dart_audio_driver_settings },
  170. #endif
  171. #if AUFILE_SUPPORT
  172.   { "file",
  173.     new_fluid_file_audio_driver,
  174.     NULL,
  175.     delete_fluid_file_audio_driver,
  176.     NULL },
  177. #endif
  178.   { NULL, NULL, NULL, NULL, NULL }
  179. };
  180. void fluid_audio_driver_settings(fluid_settings_t* settings)
  181. {
  182.   int i;
  183.   fluid_settings_register_str(settings, "audio.sample-format", "16bits", 0, NULL, NULL);
  184.   fluid_settings_add_option(settings, "audio.sample-format", "16bits");
  185.   fluid_settings_add_option(settings, "audio.sample-format", "float");
  186.   fluid_settings_register_int(settings, "audio.output-channels", 2, 2, 32, 0, NULL, NULL);
  187.   fluid_settings_register_int(settings, "audio.input-channels", 0, 0, 2, 0, NULL, NULL);
  188. #if defined(WIN32)
  189.   fluid_settings_register_int(settings, "audio.period-size", 512, 64, 8192, 0, NULL, NULL);
  190.   fluid_settings_register_int(settings, "audio.periods", 8, 2, 64, 0, NULL, NULL);
  191. #elif defined(MACOS9)
  192.   fluid_settings_register_int(settings, "audio.period-size", 64, 64, 8192, 0, NULL, NULL);
  193.   fluid_settings_register_int(settings, "audio.periods", 8, 2, 64, 0, NULL, NULL);
  194. #else
  195.   fluid_settings_register_int(settings, "audio.period-size", 64, 64, 8192, 0, NULL, NULL);
  196.   fluid_settings_register_int(settings, "audio.periods", 16, 2, 64, 0, NULL, NULL);
  197. #endif
  198.   fluid_settings_register_int (settings, "audio.realtime-prio",
  199.                                FLUID_DEFAULT_AUDIO_RT_PRIO, 0, 99, 0, NULL, NULL);
  200.   /* Set the default driver */
  201. #if JACK_SUPPORT
  202.   fluid_settings_register_str(settings, "audio.driver", "jack", 0, NULL, NULL);
  203. #elif ALSA_SUPPORT
  204.   fluid_settings_register_str(settings, "audio.driver", "alsa", 0, NULL, NULL);
  205. #elif PULSE_SUPPORT
  206.   fluid_settings_register_str(settings, "audio.driver", "pulseaudio", 0, NULL, NULL);
  207. #elif OSS_SUPPORT
  208.   fluid_settings_register_str(settings, "audio.driver", "oss", 0, NULL, NULL);
  209. #elif COREAUDIO_SUPPORT
  210.   fluid_settings_register_str(settings, "audio.driver", "coreaudio", 0, NULL, NULL);
  211. #elif DSOUND_SUPPORT
  212.   fluid_settings_register_str(settings, "audio.driver", "dsound", 0, NULL, NULL);
  213. #elif SNDMAN_SUPPORT
  214.   fluid_settings_register_str(settings, "audio.driver", "sndman", 0, NULL, NULL);
  215. #elif PORTAUDIO_SUPPORT
  216.   fluid_settings_register_str(settings, "audio.driver", "portaudio", 0, NULL, NULL);
  217. #elif DART_SUPPORT
  218.   fluid_settings_register_str(settings, "audio.driver", "dart", 0, NULL, NULL);
  219. #elif AUFILE_SUPPORT
  220.   fluid_settings_register_str(settings, "audio.driver", "file", 0, NULL, NULL);
  221. #else
  222.   fluid_settings_register_str(settings, "audio.driver", "", 0, NULL, NULL);
  223. #endif
  224.   /* Add all drivers to the list of options */
  225. #if PULSE_SUPPORT
  226.   fluid_settings_add_option(settings, "audio.driver", "pulseaudio");
  227. #endif
  228. #if ALSA_SUPPORT
  229.   fluid_settings_add_option(settings, "audio.driver", "alsa");
  230. #endif
  231. #if OSS_SUPPORT
  232.   fluid_settings_add_option(settings, "audio.driver", "oss");
  233. #endif
  234. #if COREAUDIO_SUPPORT
  235.   fluid_settings_add_option(settings, "audio.driver", "coreaudio");
  236. #endif
  237. #if DSOUND_SUPPORT
  238.   fluid_settings_add_option(settings, "audio.driver", "dsound");
  239. #endif
  240. #if SNDMAN_SUPPORT
  241.   fluid_settings_add_option(settings, "audio.driver", "sndman");
  242. #endif
  243. #if PORTAUDIO_SUPPORT
  244.   fluid_settings_add_option(settings, "audio.driver", "portaudio");
  245. #endif
  246. #if JACK_SUPPORT
  247.   fluid_settings_add_option(settings, "audio.driver", "jack");
  248. #endif
  249. #if DART_SUPPORT
  250.   fluid_settings_add_option(settings, "audio.driver", "dart");
  251. #endif
  252. #if AUFILE_SUPPORT
  253.   fluid_settings_add_option(settings, "audio.driver", "file");
  254. #endif
  255.   for (i = 0; fluid_audio_drivers[i].name != NULL; i++) {
  256.     if (fluid_audio_drivers[i].settings != NULL) {
  257.       fluid_audio_drivers[i].settings(settings);
  258.     }
  259.   }
  260. }
  261. /**
  262.  * Create a new audio driver.
  263.  * @param settings Configuration settings used to select and create the audio
  264.  *   driver.
  265.  * @param synth Synthesizer instance for which the audio driver is created for.
  266.  * @return The new audio driver instance.
  267.  *
  268.  * Creates a new audio driver for a given 'synth' instance with a defined set
  269.  * of configuration 'settings'.
  270.  */
  271. fluid_audio_driver_t*
  272. new_fluid_audio_driver(fluid_settings_t* settings, fluid_synth_t* synth)
  273. {
  274.   int i;
  275.   fluid_audio_driver_t* driver = NULL;
  276.   char* name;
  277.   char *allnames;
  278.   for (i = 0; fluid_audio_drivers[i].name != NULL; i++) {
  279.     if (fluid_settings_str_equal(settings, "audio.driver", fluid_audio_drivers[i].name)) {
  280.       FLUID_LOG(FLUID_DBG, "Using '%s' audio driver", fluid_audio_drivers[i].name);
  281.       driver = (*fluid_audio_drivers[i].new)(settings, synth);
  282.       if (driver) {
  283. driver->name = fluid_audio_drivers[i].name;
  284.       }
  285.       return driver;
  286.     }
  287.   }
  288.   allnames = fluid_settings_option_concat (settings, "audio.driver", NULL);
  289.   fluid_settings_dupstr (settings, "audio.driver", &name);       /* ++ alloc name */
  290.   FLUID_LOG(FLUID_ERR, "Couldn't find the requested audio driver %s. Valid drivers are: %s.",
  291.             name ? name : "NULL", allnames ? allnames : "ERROR");
  292.   if (name) FLUID_FREE (name);
  293.   if (allnames) FLUID_FREE (allnames);
  294.   return NULL;
  295. }
  296. /**
  297.  * Create a new audio driver.
  298.  * @param settings Configuration settings used to select and create the audio
  299.  *   driver.
  300.  * @param func Function called to fill audio buffers for audio playback
  301.  * @param data User defined data pointer to pass to 'func'
  302.  * @return The new audio driver instance.
  303.  *
  304.  * Like new_fluid_audio_driver() but allows for custom audio processing before
  305.  * audio is sent to audio driver.  It is the responsibility of the callback
  306.  * 'func' to render the audio into the buffers.
  307.  *
  308.  * NOTE: Not as efficient as new_fluid_audio_driver().
  309.  */
  310. fluid_audio_driver_t*
  311. new_fluid_audio_driver2(fluid_settings_t* settings, fluid_audio_func_t func, void* data)
  312. {
  313.   int i;
  314.   fluid_audio_driver_t* driver = NULL;
  315.   char* name;
  316.   for (i = 0; fluid_audio_drivers[i].name != NULL; i++) {
  317.     if (fluid_settings_str_equal(settings, "audio.driver", fluid_audio_drivers[i].name) &&
  318. (fluid_audio_drivers[i].new2 != NULL)) {
  319.       FLUID_LOG(FLUID_DBG, "Using '%s' audio driver", fluid_audio_drivers[i].name);
  320.       driver = (*fluid_audio_drivers[i].new2)(settings, func, data);
  321.       if (driver) {
  322. driver->name = fluid_audio_drivers[i].name;
  323.       }
  324.       return driver;
  325.     }
  326.   }
  327.   fluid_settings_dupstr(settings, "audio.driver", &name);       /* ++ alloc name */
  328.   FLUID_LOG(FLUID_ERR, "Couldn't find the requested audio driver: %s",
  329.             name ? name : "NULL");
  330.   if (name) FLUID_FREE (name);
  331.   return NULL;
  332. }
  333. /**
  334.  * Deletes an audio driver instance.
  335.  * @param driver Audio driver instance to delete
  336.  *
  337.  * Shuts down an audio driver and deletes its instance.
  338.  */
  339. void
  340. delete_fluid_audio_driver(fluid_audio_driver_t* driver)
  341. {
  342.   int i;
  343.   for (i = 0; fluid_audio_drivers[i].name != NULL; i++) {
  344.     if (fluid_audio_drivers[i].name == driver->name) {
  345.       fluid_audio_drivers[i].free(driver);
  346.       return;
  347.     }
  348.   }
  349. }