sound.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #ifndef ID_SND_BACKENDS
  21. #define ID_SND_BACKENDS
  22. class idAudioHardwareOSS : public idAudioHardware {
  23. // if you can't write MIXBUFFER_SAMPLES all at once to the audio device, split in MIXBUFFER_CHUNKS
  24. static const int MIXBUFFER_CHUNKS = 4;
  25. int m_audio_fd;
  26. int m_sample_format;
  27. unsigned int m_channels;
  28. unsigned int m_speed;
  29. void *m_buffer;
  30. int m_buffer_size;
  31. // counting the loops through the dma buffer
  32. int m_loops;
  33. // how many chunks we have left to write in cases where we need to split
  34. int m_writeChunks;
  35. // how many chunks we can write to the audio device without blocking
  36. int m_freeWriteChunks;
  37. public:
  38. idAudioHardwareOSS() {
  39. m_audio_fd = 0;
  40. m_sample_format = 0;
  41. m_channels = 0;
  42. m_speed = 0;
  43. m_buffer = NULL;
  44. m_buffer_size = 0;
  45. m_loops = 0;
  46. m_writeChunks = 0;
  47. m_freeWriteChunks = 0;
  48. }
  49. virtual ~idAudioHardwareOSS();
  50. bool Initialize( void );
  51. // Linux driver doesn't support memory map API
  52. bool Lock( void **pDSLockedBuffer, ulong *dwDSLockedBufferSize ) { return false; }
  53. bool Unlock( void *pDSLockedBuffer, dword dwDSLockedBufferSize ) { return false; }
  54. bool GetCurrentPosition( ulong *pdwCurrentWriteCursor ) { return false; }
  55. bool Flush();
  56. void Write( bool flushing );
  57. int GetNumberOfSpeakers() { return m_channels; }
  58. int GetMixBufferSize();
  59. short* GetMixBuffer();
  60. private:
  61. void Release( bool bSilent = false );
  62. void InitFailed();
  63. void ExtractOSSVersion( int version, idStr &str ) const;
  64. };
  65. #ifndef NO_ALSA
  66. // libasound2-dev
  67. // the new/old API may be a problem if we are going to dynamically load the asound lib?
  68. #define ALSA_PCM_NEW_HW_PARAMS_API
  69. #define ALSA_PCM_NEW_SW_PARAMS_API
  70. #include <alsa/asoundlib.h>
  71. #define id_snd_pcm_hw_params_alloca(ptr) do { assert(ptr); *ptr = (snd_pcm_hw_params_t *) alloca(id_snd_pcm_hw_params_sizeof()); memset(*ptr, 0, id_snd_pcm_hw_params_sizeof()); } while (0)
  72. typedef const char * ( *pfn_snd_asoundlib_version )( void );
  73. typedef snd_pcm_sframes_t ( *pfn_snd_pcm_avail_update )( snd_pcm_t *pcm );
  74. typedef int ( *pfn_snd_pcm_close )( snd_pcm_t *pcm );
  75. typedef const char * ( *pfn_snd_strerror )( int errnum );
  76. typedef int ( *pfn_snd_pcm_hw_params )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params );
  77. typedef int ( *pfn_snd_pcm_hw_params_any )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params );
  78. typedef int ( *pfn_snd_pcm_hw_params_get_buffer_size )( const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val );
  79. typedef int ( *pfn_snd_pcm_hw_params_set_access )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access );
  80. typedef int ( *pfn_snd_pcm_hw_params_set_buffer_size_min )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val );
  81. typedef int ( *pfn_snd_pcm_hw_params_set_channels )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val );
  82. typedef int ( *pfn_snd_pcm_hw_params_set_format )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t format );
  83. typedef int ( *pfn_snd_pcm_hw_params_set_rate )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir );
  84. typedef size_t ( *pfn_snd_pcm_hw_params_sizeof )( void );
  85. typedef int ( *pfn_snd_pcm_open )( snd_pcm_t **pcmp, const char *name, snd_pcm_stream_t stream, int mode );
  86. typedef int ( *pfn_snd_pcm_prepare )( snd_pcm_t *pcm );
  87. typedef snd_pcm_state_t ( *pfn_snd_pcm_state )( snd_pcm_t *pcm );
  88. typedef snd_pcm_sframes_t ( *pfn_snd_pcm_writei )( snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size );
  89. #define ALSA_DLSYM(SYM) id_##SYM = ( pfn_##SYM )dlvsym( m_handle, #SYM, "ALSA_0.9" ); if ( !id_##SYM ) { common->Printf( "dlsym "#SYM" failed: %s\n", dlerror() ); Release(); return false; }
  90. class idAudioHardwareALSA : public idAudioHardware {
  91. private:
  92. // if you can't write MIXBUFFER_SAMPLES all at once to the audio device, split in MIXBUFFER_CHUNKS
  93. static const int MIXBUFFER_CHUNKS = 4;
  94. snd_pcm_t *m_pcm_handle;
  95. unsigned int m_channels;
  96. void *m_buffer;
  97. int m_buffer_size;
  98. // how many frames remaining to be written to the device
  99. int m_remainingFrames;
  100. void *m_handle;
  101. public:
  102. idAudioHardwareALSA() {
  103. m_pcm_handle = NULL;
  104. m_channels = 0;
  105. m_buffer = NULL;
  106. m_buffer_size = 0;
  107. m_remainingFrames = 0;
  108. m_handle = NULL;
  109. }
  110. virtual ~idAudioHardwareALSA();
  111. // dlopen the lib ( check minimum version )
  112. bool DLOpen();
  113. bool Initialize( void );
  114. // Linux driver doesn't support memory map API
  115. bool Lock( void **pDSLockedBuffer, ulong *dwDSLockedBufferSize ) { return false; }
  116. bool Unlock( void *pDSLockedBuffer, dword dwDSLockedBufferSize ) { return false; }
  117. bool GetCurrentPosition( ulong *pdwCurrentWriteCursor ) { return false; }
  118. bool Flush();
  119. void Write( bool flushing );
  120. int GetNumberOfSpeakers( void ) { return m_channels; }
  121. int GetMixBufferSize( void );
  122. short* GetMixBuffer( void );
  123. private:
  124. void Release();
  125. void InitFailed();
  126. void PlayTestPattern();
  127. // may be NULL, outdated alsa versions are missing it and we just ignore
  128. pfn_snd_asoundlib_version id_snd_asoundlib_version;
  129. pfn_snd_pcm_avail_update id_snd_pcm_avail_update;
  130. pfn_snd_pcm_close id_snd_pcm_close;
  131. pfn_snd_strerror id_snd_strerror;
  132. pfn_snd_pcm_hw_params id_snd_pcm_hw_params;
  133. pfn_snd_pcm_hw_params_any id_snd_pcm_hw_params_any;
  134. pfn_snd_pcm_hw_params_get_buffer_size id_snd_pcm_hw_params_get_buffer_size;
  135. pfn_snd_pcm_hw_params_set_access id_snd_pcm_hw_params_set_access;
  136. pfn_snd_pcm_hw_params_set_buffer_size_min id_snd_pcm_hw_params_set_buffer_size_min;
  137. pfn_snd_pcm_hw_params_set_channels id_snd_pcm_hw_params_set_channels;
  138. pfn_snd_pcm_hw_params_set_format id_snd_pcm_hw_params_set_format;
  139. pfn_snd_pcm_hw_params_set_rate id_snd_pcm_hw_params_set_rate;
  140. pfn_snd_pcm_hw_params_sizeof id_snd_pcm_hw_params_sizeof;
  141. pfn_snd_pcm_open id_snd_pcm_open;
  142. pfn_snd_pcm_prepare id_snd_pcm_prepare;
  143. pfn_snd_pcm_state id_snd_pcm_state;
  144. pfn_snd_pcm_writei id_snd_pcm_writei;
  145. };
  146. #endif // NO_ALSA
  147. #endif