FAudioDevice* FAudioDeviceManager::CreateAudioDevice(uint32& HandleOut, bool bCreateNewDevice) { // If we don't have an audio device module, then we can't create new audio devices. if (AudioDeviceModule == nullptr) { HandleOut = AUDIO_DEVICE_HANDLE_INVALID; return nullptr; } // If we are running without the editor, we only need one audio device. if (!GIsEditor) { if (NumActiveAudioDevices == 1) { FAudioDevice* MainAudioDevice = GEngine->GetMainAudioDevice(); if (MainAudioDevice) { HandleOut = MainAudioDevice->DeviceHandle; return MainAudioDevice; } return nullptr; } } FAudioDevice* NewAudioDevice = nullptr; if (NumActiveAudioDevices < AUDIO_DEVICE_DEFAULT_ALLOWED_DEVICE_COUNT || (bCreateNewDevice && NumActiveAudioDevices < AUDIO_DEVICE_MAX_DEVICE_COUNT)) { // Create the new audio device and make sure it succeeded NewAudioDevice = AudioDeviceModule->CreateAudioDevice(); if (NewAudioDevice == nullptr) { HandleOut = AUDIO_DEVICE_HANDLE_INVALID; return nullptr; } // Now generation a new audio device handle for the device and store the // ptr to the new device in the array of audio devices. uint32 AudioDeviceIndex(AUDIO_DEVICE_HANDLE_INVALID); // First check to see if we should start recycling audio device indices, if not // then we add a new entry to the Generation array and generate a new index if (FreeIndicesSize > AUDIO_DEVICE_MINIMUM_FREE_AUDIO_DEVICE_INDICES) { FreeIndices.Dequeue(AudioDeviceIndex); --FreeIndicesSize; check(int32(AudioDeviceIndex) < Devices.Num()); check(Devices[AudioDeviceIndex] == nullptr); Devices[AudioDeviceIndex] = NewAudioDevice; } else { // Add a zeroth generation entry in the Generation array, get a brand new // index and append the created device to the end of the Devices array Generations.Add(0); AudioDeviceIndex = Generations.Num() - 1; check(AudioDeviceIndex < (1 << AUDIO_DEVICE_HANDLE_INDEX_BITS)); Devices.Add(NewAudioDevice); } HandleOut = CreateHandle(AudioDeviceIndex, Generations[AudioDeviceIndex]); // Store the handle on the audio device itself NewAudioDevice->DeviceHandle = HandleOut; } else { ++NumWorldsUsingMainAudioDevice; FAudioDevice* MainAudioDevice = GEngine->GetMainAudioDevice(); if (MainAudioDevice) { HandleOut = MainAudioDevice->DeviceHandle; NewAudioDevice = MainAudioDevice; } } ++NumActiveAudioDevices; return NewAudioDevice; }
bool FAudioDeviceManager::CreateAudioDevice(bool bCreateNewDevice, FCreateAudioDeviceResults& OutResults) { OutResults = FCreateAudioDeviceResults(); // If we don't have an audio device module, then we can't create new audio devices. if (AudioDeviceModule == nullptr) { return false; } // If we are running without the editor, we only need one audio device. if (!GIsEditor) { if (NumActiveAudioDevices == 1) { FAudioDevice* MainAudioDevice = GEngine->GetMainAudioDevice(); if (MainAudioDevice) { OutResults.Handle = MainAudioDevice->DeviceHandle; OutResults.AudioDevice = MainAudioDevice; return true; } return false; } } if (NumActiveAudioDevices < AUDIO_DEVICE_DEFAULT_ALLOWED_DEVICE_COUNT || (bCreateNewDevice && NumActiveAudioDevices < AUDIO_DEVICE_MAX_DEVICE_COUNT)) { // Create the new audio device and make sure it succeeded OutResults.AudioDevice = AudioDeviceModule->CreateAudioDevice(); if (OutResults.AudioDevice == nullptr) { return false; } // Now generation a new audio device handle for the device and store the // ptr to the new device in the array of audio devices. uint32 AudioDeviceIndex(INDEX_NONE); // First check to see if we should start recycling audio device indices, if not // then we add a new entry to the Generation array and generate a new index if (FreeIndicesSize > AUDIO_DEVICE_MINIMUM_FREE_AUDIO_DEVICE_INDICES) { FreeIndices.Dequeue(AudioDeviceIndex); --FreeIndicesSize; check(int32(AudioDeviceIndex) < Devices.Num()); check(Devices[AudioDeviceIndex] == nullptr); Devices[AudioDeviceIndex] = OutResults.AudioDevice; } else { // Add a zeroth generation entry in the Generation array, get a brand new // index and append the created device to the end of the Devices array Generations.Add(0); AudioDeviceIndex = Generations.Num() - 1; check(AudioDeviceIndex < (1 << AUDIO_DEVICE_HANDLE_INDEX_BITS)); Devices.Add(OutResults.AudioDevice); } OutResults.bNewDevice = true; OutResults.Handle = CreateHandle(AudioDeviceIndex, Generations[AudioDeviceIndex]); // Store the handle on the audio device itself OutResults.AudioDevice->DeviceHandle = OutResults.Handle; } else { ++NumWorldsUsingMainAudioDevice; FAudioDevice* MainAudioDevice = GEngine->GetMainAudioDevice(); if (MainAudioDevice) { OutResults.Handle = MainAudioDevice->DeviceHandle; OutResults.AudioDevice = MainAudioDevice; } } ++NumActiveAudioDevices; const UAudioSettings* AudioSettings = GetDefault<UAudioSettings>(); if (!OutResults.AudioDevice->Init(AudioSettings->GetQualityLevelSettings(GEngine->GetGameUserSettings()->GetAudioQualityLevel()).MaxChannels)) { ShutdownAudioDevice(OutResults.Handle); OutResults = FCreateAudioDeviceResults(); } return (OutResults.AudioDevice != nullptr); }