sound_alsa.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #include "../../sound/snd_local.h"
  22. #include "../posix/posix_public.h"
  23. #include "sound.h"
  24. #include <dlfcn.h>
  25. static idCVar s_alsa_pcm( "s_alsa_pcm", "default", CVAR_SYSTEM | CVAR_ARCHIVE, "which alsa pcm device to use. default, hwplug, hw.. see alsa docs" );
  26. static idCVar s_alsa_lib( "s_alsa_lib", "libasound.so.2", CVAR_SYSTEM | CVAR_ARCHIVE, "alsa client sound library" );
  27. /*
  28. ===============
  29. idAudioHardwareALSA::DLOpen
  30. ===============
  31. */
  32. bool idAudioHardwareALSA::DLOpen( void ) {
  33. const char *version;
  34. if ( m_handle ) {
  35. return true;
  36. }
  37. common->Printf( "dlopen(%s)\n", s_alsa_lib.GetString() );
  38. if ( !( m_handle = dlopen( s_alsa_lib.GetString(), RTLD_NOW | RTLD_GLOBAL ) ) ) {
  39. common->Printf( "dlopen(%s) failed: %s\n", s_alsa_lib.GetString(), dlerror() );
  40. return false;
  41. }
  42. // print the version if available
  43. id_snd_asoundlib_version = ( pfn_snd_asoundlib_version )dlsym( m_handle, "snd_asoundlib_version" );
  44. if ( !id_snd_asoundlib_version ) {
  45. common->Printf( "dlsym(\"snd_asoundlib_version\") failed: %s\n", dlerror() );
  46. common->Warning( "please consider upgrading alsa to a more recent version." );
  47. } else {
  48. version = id_snd_asoundlib_version();
  49. common->Printf( "asoundlib version: %s\n", version );
  50. }
  51. // dlsym the symbols
  52. ALSA_DLSYM(snd_pcm_avail_update);
  53. ALSA_DLSYM(snd_pcm_close);
  54. ALSA_DLSYM(snd_pcm_hw_params);
  55. ALSA_DLSYM(snd_pcm_hw_params_any);
  56. ALSA_DLSYM(snd_pcm_hw_params_get_buffer_size);
  57. ALSA_DLSYM(snd_pcm_hw_params_set_access);
  58. ALSA_DLSYM(snd_pcm_hw_params_set_buffer_size_min);
  59. ALSA_DLSYM(snd_pcm_hw_params_set_channels);
  60. ALSA_DLSYM(snd_pcm_hw_params_set_format);
  61. ALSA_DLSYM(snd_pcm_hw_params_set_rate);
  62. ALSA_DLSYM(snd_pcm_hw_params_sizeof);
  63. ALSA_DLSYM(snd_pcm_open);
  64. ALSA_DLSYM(snd_pcm_prepare);
  65. ALSA_DLSYM(snd_pcm_state);
  66. ALSA_DLSYM(snd_pcm_writei);
  67. ALSA_DLSYM(snd_strerror);
  68. return true;
  69. }
  70. /*
  71. ===============
  72. idAudioHardwareALSA::Release
  73. ===============
  74. */
  75. void idAudioHardwareALSA::Release() {
  76. if ( m_pcm_handle ) {
  77. common->Printf( "close pcm\n" );
  78. id_snd_pcm_close( m_pcm_handle );
  79. m_pcm_handle = NULL;
  80. }
  81. if ( m_buffer ) {
  82. free( m_buffer );
  83. m_buffer = NULL;
  84. }
  85. if ( m_handle ) {
  86. common->Printf( "dlclose\n" );
  87. dlclose( m_handle );
  88. m_handle = NULL;
  89. }
  90. }
  91. /*
  92. =================
  93. idAudioHardwareALSA::InitFailed
  94. =================
  95. */
  96. void idAudioHardwareALSA::InitFailed() {
  97. Release();
  98. cvarSystem->SetCVarBool( "s_noSound", true );
  99. common->Warning( "sound subsystem disabled\n" );
  100. common->Printf( "--------------------------------------\n" );
  101. }
  102. /*
  103. =====================
  104. idAudioHardwareALSA::Initialize
  105. =====================
  106. */
  107. bool idAudioHardwareALSA::Initialize( void ) {
  108. int err;
  109. common->Printf( "------ Alsa Sound Initialization -----\n" );
  110. if ( !DLOpen() ) {
  111. InitFailed();
  112. return false;
  113. }
  114. if ( ( err = id_snd_pcm_open( &m_pcm_handle, s_alsa_pcm.GetString(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK ) ) < 0 ) {
  115. common->Printf( "snd_pcm_open SND_PCM_STREAM_PLAYBACK '%s' failed: %s\n", s_alsa_pcm.GetString(), id_snd_strerror( err ) );
  116. InitFailed();
  117. return false;
  118. }
  119. common->Printf( "opened Alsa PCM device %s for playback\n", s_alsa_pcm.GetString() );
  120. // set hardware parameters ----------------------------------------------------------------------
  121. // init hwparams with the full configuration space
  122. snd_pcm_hw_params_t *hwparams;
  123. // this one is a define
  124. id_snd_pcm_hw_params_alloca( &hwparams );
  125. if ( ( err = id_snd_pcm_hw_params_any( m_pcm_handle, hwparams ) ) < 0 ) {
  126. common->Printf( "cannot configure the PCM device: %s\n", id_snd_strerror( err ) );
  127. InitFailed();
  128. return false;
  129. }
  130. if ( ( err = id_snd_pcm_hw_params_set_access( m_pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 ) {
  131. common->Printf( "SND_PCM_ACCESS_RW_INTERLEAVED failed: %s\n", id_snd_strerror( err ) );
  132. InitFailed();
  133. return false;
  134. }
  135. if ( ( err = id_snd_pcm_hw_params_set_format( m_pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE ) ) < 0 ) {
  136. common->Printf( "SND_PCM_FORMAT_S16_LE failed: %s\n", id_snd_strerror( err ) );
  137. InitFailed();
  138. return false;
  139. }
  140. // channels
  141. // sanity over number of speakers
  142. if ( idSoundSystemLocal::s_numberOfSpeakers.GetInteger() != 6 && idSoundSystemLocal::s_numberOfSpeakers.GetInteger() != 2 ) {
  143. common->Warning( "invalid value for s_numberOfSpeakers. Use either 2 or 6" );
  144. idSoundSystemLocal::s_numberOfSpeakers.SetInteger( 2 );
  145. }
  146. m_channels = idSoundSystemLocal::s_numberOfSpeakers.GetInteger();
  147. if ( ( err = id_snd_pcm_hw_params_set_channels( m_pcm_handle, hwparams, m_channels ) ) < 0 ) {
  148. common->Printf( "error setting %d channels: %s\n", m_channels, id_snd_strerror( err ) );
  149. if ( idSoundSystemLocal::s_numberOfSpeakers.GetInteger() != 2 ) {
  150. // fallback to stereo if that works
  151. m_channels = 2;
  152. if ( ( err = id_snd_pcm_hw_params_set_channels( m_pcm_handle, hwparams, m_channels ) ) < 0 ) {
  153. common->Printf( "fallback to stereo failed: %s\n", id_snd_strerror( err ) );
  154. InitFailed();
  155. return false;
  156. } else {
  157. common->Printf( "fallback to stereo\n" );
  158. idSoundSystemLocal::s_numberOfSpeakers.SetInteger( 2 );
  159. }
  160. } else {
  161. InitFailed();
  162. return false;
  163. }
  164. }
  165. // set sample rate (frequency)
  166. if ( ( err = id_snd_pcm_hw_params_set_rate( m_pcm_handle, hwparams, PRIMARYFREQ, 0 ) ) < 0 ) {
  167. common->Printf( "failed to set 44.1KHz rate: %s - try ( +set s_alsa_pcm plughw:0 ? )\n", id_snd_strerror( err ) );
  168. InitFailed();
  169. return false;
  170. }
  171. // have enough space in the input buffer for our MIXBUFFER_SAMPLE feedings and async ticks
  172. snd_pcm_uframes_t frames;
  173. frames = MIXBUFFER_SAMPLES + MIXBUFFER_SAMPLES / 3;
  174. if ( ( err = id_snd_pcm_hw_params_set_buffer_size_min( m_pcm_handle, hwparams, &frames ) ) < 0 ) {
  175. common->Printf( "buffer size select failed: %s\n", id_snd_strerror( err ) );
  176. InitFailed();
  177. return false;
  178. }
  179. // apply parameters
  180. if ( ( err = id_snd_pcm_hw_params( m_pcm_handle, hwparams ) ) < 0 ) {
  181. common->Printf( "snd_pcm_hw_params failed: %s\n", id_snd_strerror( err ) );
  182. InitFailed();
  183. return false;
  184. }
  185. // check the buffer size
  186. if ( ( err = id_snd_pcm_hw_params_get_buffer_size( hwparams, &frames ) ) < 0 ) {
  187. common->Printf( "snd_pcm_hw_params_get_buffer_size failed: %s\n", id_snd_strerror( err ) );
  188. } else {
  189. common->Printf( "device buffer size: %lu frames ( %lu bytes )\n", ( long unsigned int )frames, frames * m_channels * 2 );
  190. }
  191. // TODO: can use swparams to setup the device so it doesn't underrun but rather loops over
  192. // snd_pcm_sw_params_set_stop_threshold
  193. // To get alsa to just loop on underruns. set the swparam stop_threshold to equal buffer size. The sound buffer will just loop and never throw an xrun.
  194. // allocate the final mix buffer
  195. m_buffer_size = MIXBUFFER_SAMPLES * m_channels * 2;
  196. m_buffer = malloc( m_buffer_size );
  197. common->Printf( "allocated a mix buffer of %d bytes\n", m_buffer_size );
  198. #ifdef _DEBUG
  199. // verbose the state
  200. snd_pcm_state_t curstate = id_snd_pcm_state( m_pcm_handle );
  201. assert( curstate == SND_PCM_STATE_PREPARED );
  202. #endif
  203. common->Printf( "--------------------------------------\n" );
  204. return true;
  205. }
  206. /*
  207. ===============
  208. idAudioHardwareALSA::~idAudioHardwareALSA
  209. ===============
  210. */
  211. idAudioHardwareALSA::~idAudioHardwareALSA() {
  212. common->Printf( "----------- Alsa Shutdown ------------\n" );
  213. Release();
  214. common->Printf( "--------------------------------------\n" );
  215. }
  216. /*
  217. =================
  218. idAudioHardwareALSA::GetMixBufferSize
  219. =================
  220. */
  221. int idAudioHardwareALSA::GetMixBufferSize() {
  222. return m_buffer_size;
  223. }
  224. /*
  225. =================
  226. idAudioHardwareALSA::GetMixBuffer
  227. =================
  228. */
  229. short* idAudioHardwareALSA::GetMixBuffer() {
  230. return (short *)m_buffer;
  231. }
  232. /*
  233. ===============
  234. idAudioHardwareALSA::Flush
  235. ===============
  236. */
  237. bool idAudioHardwareALSA::Flush( void ) {
  238. int ret;
  239. snd_pcm_state_t state;
  240. state = id_snd_pcm_state( m_pcm_handle );
  241. if ( state != SND_PCM_STATE_RUNNING && state != SND_PCM_STATE_PREPARED ) {
  242. if ( ( ret = id_snd_pcm_prepare( m_pcm_handle ) ) < 0 ) {
  243. Sys_Printf( "failed to recover from SND_PCM_STATE_XRUN: %s\n", id_snd_strerror( ret ) );
  244. cvarSystem->SetCVarBool( "s_noSound", true );
  245. return false;
  246. }
  247. Sys_Printf( "preparing audio device for output\n" );
  248. }
  249. Write( true );
  250. }
  251. /*
  252. ===============
  253. idAudioHardwareALSA::Write
  254. rely on m_freeWriteChunks which has been set in Flush() before engine did the mixing for this MIXBUFFER_SAMPLE
  255. ===============
  256. */
  257. void idAudioHardwareALSA::Write( bool flushing ) {
  258. if ( !flushing && m_remainingFrames ) {
  259. // if we write after a new mixing loop, we should have m_writeChunk == 0
  260. // otherwise that last remaining chunk that was never flushed out to the audio device has just been overwritten
  261. Sys_Printf( "idAudioHardwareALSA::Write: %d frames overflowed and dropped\n", m_remainingFrames );
  262. }
  263. if ( !flushing ) {
  264. // if running after the mix loop, then we have a full buffer to write out
  265. m_remainingFrames = MIXBUFFER_SAMPLES;
  266. }
  267. if ( m_remainingFrames == 0 ) {
  268. return;
  269. }
  270. // write the max frames you can in one shot - we need to write it all out in Flush() calls before the next Write() happens
  271. int pos = (int)m_buffer + ( MIXBUFFER_SAMPLES - m_remainingFrames ) * m_channels * 2;
  272. snd_pcm_sframes_t frames = id_snd_pcm_writei( m_pcm_handle, (void*)pos, m_remainingFrames );
  273. if ( frames < 0 ) {
  274. if ( frames != -EAGAIN ) {
  275. Sys_Printf( "snd_pcm_writei %d frames failed: %s\n", m_remainingFrames, id_snd_strerror( frames ) );
  276. }
  277. return;
  278. }
  279. m_remainingFrames -= frames;
  280. }