| 276 | }; |
| 277 | |
| 278 | static Result InitializeInternal(SoundSystem* sound, ThreadArgs* args) |
| 279 | { |
| 280 | float master_gain = args->m_MasterGain; |
| 281 | uint32_t max_sound_data = args->m_MaxSoundData; |
| 282 | uint32_t max_sources = args->m_MaxSources; |
| 283 | uint32_t max_instances = args->m_MaxInstances; |
| 284 | uint32_t max_buffers = args->m_MaxBuffers; |
| 285 | |
| 286 | Result r = sound->InitOpenal(); |
| 287 | if (r != RESULT_OK) { |
| 288 | return r; |
| 289 | } |
| 290 | |
| 291 | ALCint sample_rate; |
| 292 | alcGetIntegerv(sound->m_AlDevice, ALC_FREQUENCY, 1, &sample_rate); |
| 293 | sound->m_MixRate = (uint32_t)sample_rate; |
| 294 | sound->m_Instances.SetCapacity(max_instances); |
| 295 | sound->m_Instances.SetSize(max_instances); |
| 296 | sound->m_InstancesPool.SetCapacity(max_instances); |
| 297 | sound->m_InstancesActive.SetCapacity(max_instances); |
| 298 | for (uint32_t i = 0; i < max_instances; ++i) |
| 299 | { |
| 300 | SoundInstance* instance = &sound->m_Instances[i]; |
| 301 | memset(instance, 0, sizeof(*instance)); |
| 302 | instance->m_Index = 0xffff; |
| 303 | instance->m_SourceIndex = 0xffff; |
| 304 | instance->m_SoundDataIndex = 0xffff; |
| 305 | instance->m_Parameters[PARAMETER_SPEED] = 1.0f; |
| 306 | } |
| 307 | |
| 308 | sound->m_SoundData.SetCapacity(max_sound_data); |
| 309 | sound->m_SoundData.SetSize(max_sound_data); |
| 310 | sound->m_SoundDataPool.SetCapacity(max_sound_data); |
| 311 | for (uint32_t i = 0; i < max_sound_data; ++i) |
| 312 | { |
| 313 | sound->m_SoundData[i].m_Index = 0xffff; |
| 314 | } |
| 315 | |
| 316 | sound->m_Sources.SetCapacity(max_sources); |
| 317 | sound->m_Sources.SetSize(max_sources); |
| 318 | sound->m_SourcesPool.SetCapacity(max_sources); |
| 319 | alGenSources(max_sources, sound->m_Sources.Begin()); |
| 320 | checkForAlErrors("alGenSources"); |
| 321 | |
| 322 | /** |
| 323 | * Currently we repurpose the unused max_sound_buffers option to determine the number of |
| 324 | * buffers per source; we interpret this as total number of buffered 60FPS frames worth of |
| 325 | * audio across all sources, plus a minimum of two per source, because it works out to a |
| 326 | * reasonable amount of buffering. Ideally we should create a dedicated setting for this. |
| 327 | */ |
| 328 | uint32_t buffers_per_source = (2 + (max_buffers / max_sources)) * BUFFERS_PER_FRAME; |
| 329 | uint32_t total_buffers = buffers_per_source * max_sources; |
| 330 | uint32_t buffer_size = SAMPLES_PER_FRAME / BUFFERS_PER_FRAME * sizeof(float) * SOUND_MAX_DECODE_CHANNELS; |
| 331 | uint32_t total_buffer_size = total_buffers * buffer_size; |
| 332 | /** |
| 333 | * This is a single contiguous block to simplify buffer allocation, but as a result it |
| 334 | * must assume that every source could require the maximum amount of buffer (stereo, |
| 335 | * 32-bit samples). |
no test coverage detected