doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sound.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 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.
23 
24 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.
25 
26 ===========================================================================
27 */
28 #ifndef ID_SND_BACKENDS
29 #define ID_SND_BACKENDS
30 
32  // if you can't write MIXBUFFER_SAMPLES all at once to the audio device, split in MIXBUFFER_CHUNKS
33  static const int MIXBUFFER_CHUNKS = 4;
34 
37  unsigned int m_channels;
38  unsigned int m_speed;
39  void *m_buffer;
41 
42  // counting the loops through the dma buffer
43  int m_loops;
44 
45  // how many chunks we have left to write in cases where we need to split
47  // how many chunks we can write to the audio device without blocking
49 
50 public:
52  m_audio_fd = 0;
53  m_sample_format = 0;
54  m_channels = 0;
55  m_speed = 0;
56  m_buffer = NULL;
57  m_buffer_size = 0;
58  m_loops = 0;
59  m_writeChunks = 0;
61  }
62  virtual ~idAudioHardwareOSS();
63 
64  bool Initialize( void );
65 
66  // Linux driver doesn't support memory map API
67  bool Lock( void **pDSLockedBuffer, ulong *dwDSLockedBufferSize ) { return false; }
68  bool Unlock( void *pDSLockedBuffer, dword dwDSLockedBufferSize ) { return false; }
69  bool GetCurrentPosition( ulong *pdwCurrentWriteCursor ) { return false; }
70 
71  bool Flush();
72  void Write( bool flushing );
73 
74  int GetNumberOfSpeakers() { return m_channels; }
75  int GetMixBufferSize();
76  short* GetMixBuffer();
77 
78 private:
79  void Release( bool bSilent = false );
80  void InitFailed();
81  void ExtractOSSVersion( int version, idStr &str ) const;
82 };
83 
84 #ifndef NO_ALSA
85 
86 // libasound2-dev
87 // the new/old API may be a problem if we are going to dynamically load the asound lib?
88 #define ALSA_PCM_NEW_HW_PARAMS_API
89 #define ALSA_PCM_NEW_SW_PARAMS_API
90 #include <alsa/asoundlib.h>
91 
92 #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)
93 
94 typedef const char * ( *pfn_snd_asoundlib_version )( void );
95 typedef snd_pcm_sframes_t ( *pfn_snd_pcm_avail_update )( snd_pcm_t *pcm );
96 typedef int ( *pfn_snd_pcm_close )( snd_pcm_t *pcm );
97 typedef const char * ( *pfn_snd_strerror )( int errnum );
98 typedef int ( *pfn_snd_pcm_hw_params )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params );
99 typedef int ( *pfn_snd_pcm_hw_params_any )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params );
100 typedef int ( *pfn_snd_pcm_hw_params_get_buffer_size )( const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val );
101 typedef int ( *pfn_snd_pcm_hw_params_set_access )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access );
102 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 );
103 typedef int ( *pfn_snd_pcm_hw_params_set_channels )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val );
104 typedef int ( *pfn_snd_pcm_hw_params_set_format )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t format );
105 typedef int ( *pfn_snd_pcm_hw_params_set_rate )( snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir );
106 typedef size_t ( *pfn_snd_pcm_hw_params_sizeof )( void );
107 typedef int ( *pfn_snd_pcm_open )( snd_pcm_t **pcmp, const char *name, snd_pcm_stream_t stream, int mode );
108 typedef int ( *pfn_snd_pcm_prepare )( snd_pcm_t *pcm );
109 typedef snd_pcm_state_t ( *pfn_snd_pcm_state )( snd_pcm_t *pcm );
110 typedef snd_pcm_sframes_t ( *pfn_snd_pcm_writei )( snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size );
111 
112 #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; }
113 
115 private:
116  // if you can't write MIXBUFFER_SAMPLES all at once to the audio device, split in MIXBUFFER_CHUNKS
117  static const int MIXBUFFER_CHUNKS = 4;
118 
119  snd_pcm_t *m_pcm_handle;
120  unsigned int m_channels;
121  void *m_buffer;
123 
124  // how many frames remaining to be written to the device
126 
127  void *m_handle;
128 
129 public:
131  m_pcm_handle = NULL;
132  m_channels = 0;
133  m_buffer = NULL;
134  m_buffer_size = 0;
135  m_remainingFrames = 0;
136  m_handle = NULL;
137  }
138  virtual ~idAudioHardwareALSA();
139 
140  // dlopen the lib ( check minimum version )
141  bool DLOpen();
142 
143  bool Initialize( void );
144 
145 
146  // Linux driver doesn't support memory map API
147  bool Lock( void **pDSLockedBuffer, ulong *dwDSLockedBufferSize ) { return false; }
148  bool Unlock( void *pDSLockedBuffer, dword dwDSLockedBufferSize ) { return false; }
149  bool GetCurrentPosition( ulong *pdwCurrentWriteCursor ) { return false; }
150 
151  bool Flush();
152  void Write( bool flushing );
153 
154  int GetNumberOfSpeakers( void ) { return m_channels; }
155  int GetMixBufferSize( void );
156  short* GetMixBuffer( void );
157 
158 private:
159  void Release();
160  void InitFailed();
161  void PlayTestPattern();
162 
163  // may be NULL, outdated alsa versions are missing it and we just ignore
165 
182 
183 };
184 
185 #endif // NO_ALSA
186 
187 #endif
snd_pcm_sframes_t(* pfn_snd_pcm_avail_update)(snd_pcm_t *pcm)
Definition: sound.h:95
pfn_snd_pcm_hw_params_set_buffer_size_min id_snd_pcm_hw_params_set_buffer_size_min
Definition: sound.h:173
unsigned int dword
Definition: Lib.h:77
bool Unlock(void *pDSLockedBuffer, dword dwDSLockedBufferSize)
Definition: sound.h:68
int m_sample_format
Definition: sound.h:36
GLenum GLsizei GLenum format
Definition: glext.h:2846
void InitFailed()
Definition: sound.cpp:120
virtual ~idAudioHardwareALSA()
Definition: sound_alsa.cpp:243
int(* pfn_snd_pcm_hw_params_set_channels)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
Definition: sound.h:103
snd_pcm_t * m_pcm_handle
Definition: sound.h:119
const char *(* pfn_snd_asoundlib_version)(void)
Definition: sound.h:94
bool Lock(void **pDSLockedBuffer, ulong *dwDSLockedBufferSize)
Definition: sound.h:147
void * m_handle
Definition: sound.h:127
case const int
Definition: Callbacks.cpp:52
void * m_buffer
Definition: sound.h:39
pfn_snd_pcm_hw_params id_snd_pcm_hw_params
Definition: sound.h:169
static const int MIXBUFFER_CHUNKS
Definition: sound.h:33
bool Lock(void **pDSLockedBuffer, ulong *dwDSLockedBufferSize)
Definition: sound.h:67
int(* pfn_snd_pcm_hw_params_set_access)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access)
Definition: sound.h:101
pfn_snd_pcm_hw_params_set_rate id_snd_pcm_hw_params_set_rate
Definition: sound.h:176
pfn_snd_pcm_avail_update id_snd_pcm_avail_update
Definition: sound.h:166
int m_buffer_size
Definition: sound.h:40
bool GetCurrentPosition(ulong *pdwCurrentWriteCursor)
Definition: sound.h:149
bool Initialize(void)
Definition: sound.cpp:149
int GetNumberOfSpeakers()
Definition: sound.h:74
short * GetMixBuffer()
Definition: sound.cpp:351
void ExtractOSSVersion(int version, idStr &str) const
Definition: sound.cpp:132
virtual ~idAudioHardwareOSS()
Definition: sound.cpp:85
int(* pfn_snd_pcm_hw_params)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
Definition: sound.h:98
pfn_snd_pcm_hw_params_sizeof id_snd_pcm_hw_params_sizeof
Definition: sound.h:177
size_t(* pfn_snd_pcm_hw_params_sizeof)(void)
Definition: sound.h:106
bool GetCurrentPosition(ulong *pdwCurrentWriteCursor)
Definition: sound.h:69
pfn_snd_pcm_hw_params_any id_snd_pcm_hw_params_any
Definition: sound.h:170
short * GetMixBuffer(void)
Definition: sound_alsa.cpp:263
pfn_snd_pcm_hw_params_set_access id_snd_pcm_hw_params_set_access
Definition: sound.h:172
struct version_s version
int GetMixBufferSize(void)
Definition: sound_alsa.cpp:254
unsigned int m_channels
Definition: sound.h:37
int(* pfn_snd_pcm_open)(snd_pcm_t **pcmp, const char *name, snd_pcm_stream_t stream, int mode)
Definition: sound.h:107
bool Initialize(void)
Definition: sound_alsa.cpp:122
int(* pfn_snd_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
Definition: sound.h:100
#define NULL
Definition: Lib.h:88
GLuint buffer
Definition: glext.h:3108
pfn_snd_strerror id_snd_strerror
Definition: sound.h:168
GLint mode
Definition: glext.h:4165
int GetMixBufferSize()
Definition: sound.cpp:341
int(* pfn_snd_pcm_prepare)(snd_pcm_t *pcm)
Definition: sound.h:108
pfn_snd_pcm_writei id_snd_pcm_writei
Definition: sound.h:181
const char *(* pfn_snd_strerror)(int errnum)
Definition: sound.h:97
pfn_snd_asoundlib_version id_snd_asoundlib_version
Definition: sound.h:164
int(* pfn_snd_pcm_hw_params_set_rate)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
Definition: sound.h:105
pfn_snd_pcm_hw_params_set_format id_snd_pcm_hw_params_set_format
Definition: sound.h:175
int(* pfn_snd_pcm_close)(snd_pcm_t *pcm)
Definition: sound.h:96
int(* pfn_snd_pcm_hw_params_any)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
Definition: sound.h:99
int m_freeWriteChunks
Definition: sound.h:48
void Write(bool flushing)
Definition: sound_alsa.cpp:293
bool Unlock(void *pDSLockedBuffer, dword dwDSLockedBufferSize)
Definition: sound.h:148
void Write(bool flushing)
Definition: sound.cpp:361
void Release(bool bSilent=false)
Definition: sound.cpp:94
idAudioHardwareOSS()
Definition: sound.h:51
pfn_snd_pcm_open id_snd_pcm_open
Definition: sound.h:178
pfn_snd_pcm_hw_params_set_channels id_snd_pcm_hw_params_set_channels
Definition: sound.h:174
snd_pcm_state_t(* pfn_snd_pcm_state)(snd_pcm_t *pcm)
Definition: sound.h:109
const GLcharARB * name
Definition: glext.h:3629
GLsizeiptr size
Definition: glext.h:3112
pfn_snd_pcm_hw_params_get_buffer_size id_snd_pcm_hw_params_get_buffer_size
Definition: sound.h:171
pfn_snd_pcm_prepare id_snd_pcm_prepare
Definition: sound.h:179
int GetNumberOfSpeakers(void)
Definition: sound.h:154
Definition: Str.h:116
pfn_snd_pcm_state id_snd_pcm_state
Definition: sound.h:180
unsigned int m_speed
Definition: sound.h:38
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
unsigned long ulong
Definition: Lib.h:79
int m_remainingFrames
Definition: sound.h:125
int(* pfn_snd_pcm_hw_params_set_format)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t format)
Definition: sound.h:104
static const int MIXBUFFER_CHUNKS
Definition: sound.h:117
pfn_snd_pcm_close id_snd_pcm_close
Definition: sound.h:167
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)
Definition: sound.h:102
unsigned int m_channels
Definition: sound.h:120
int m_writeChunks
Definition: sound.h:46
GLenum const GLfloat * params
Definition: glext.h:2847
void * m_buffer
Definition: sound.h:121
snd_pcm_sframes_t(* pfn_snd_pcm_writei)(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
Definition: sound.h:110