<commit_before>// Copyright (C) 2017 <NAME>
// This file is part of the Ouzel engine.

#include "core/CompileConfig.h"

#if OUZEL_SUPPORTS_COREAUDIO

#if OUZEL_PLATFORM_IOS || OUZEL_PLATFORM_TVOS
#import <AVFoundation/AVFoundation.h>
#include <AudioToolbox/AudioToolbox.h>
#endif

#include "AudioCA.h"
#include "utils/Log.h"

#if OUZEL_PLATFORM_MACOS

static OSStatus deviceListChanged(AudioObjectID, UInt32, const AudioObjectPropertyAddress*, void*)
{
    return 0;
}

static OSStatus deviceUnplugged(AudioObjectID, UInt32, const AudioObjectPropertyAddress*, void*)
{
    return noErr;
}
#endif

static OSStatus outputCallback(void* inRefCon,
                               AudioUnitRenderActionFlags*,
                               const AudioTimeStamp*,
                               UInt32, UInt32,
                               AudioBufferList* ioData)
{
    ouzel::audio::AudioCA* audioCA = static_cast<ouzel::audio::AudioCA*>(inRefCon);

    audioCA->outputCallback(ioData);

    return noErr;
}

namespace ouzel
{
    namespace audio
    {
        AudioCA::AudioCA():
            Audio(Audio::Driver::COREAUDIO)
        {
        }

        AudioCA::~AudioCA()
        {
            if (audioUnit)
            {
                AudioOutputUnitStop(audioUnit);

                AURenderCallbackStruct callback;
                callback.inputProc = nullptr;
                callback.inputProcRefCon = nullptr;

                const AudioUnitElement bus = 0;

                AudioUnitSetProperty(audioUnit,
                                     kAudioUnitProperty_SetRenderCallback,
                                     kAudioUnitScope_Input, bus, &callback, sizeof(callback));

                AudioComponentInstanceDispose(audioUnit);
            }

#if OUZEL_PLATFORM_MACOS
            static const AudioObjectPropertyAddress deviceListAddress = {
                kAudioHardwarePropertyDevices,
                kAudioObjectPropertyScopeGlobal,
                kAudioObjectPropertyElementMaster
            };

            AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &deviceListAddress, deviceListChanged, this);

            if (deviceId)
            {
                static const AudioObjectPropertyAddress aliveAddress = {
                    kAudioDevicePropertyDeviceIsAlive,
                    kAudioDevicePropertyScopeOutput,
                    kAudioObjectPropertyElementMaster
                };

                AudioObjectRemovePropertyListener(deviceId, &aliveAddress, deviceUnplugged, this);
            }
#endif
        }

        bool AudioCA::init()
        {
            if (!Audio::init())
            {
                return false;
            }

            OSStatus result;

#if OUZEL_PLATFORM_MACOS
            static const AudioObjectPropertyAddress deviceListAddress = {
                kAudioHardwarePropertyDevices,
                kAudioObjectPropertyScopeGlobal,
                kAudioObjectPropertyElementMaster
            };

            result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &deviceListAddress, deviceListChanged, this);
            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to add CoreAudio property listener, error: " << result;
                return false;
            }

            static const AudioObjectPropertyAddress defaultDeviceAddress = {
                kAudioHardwarePropertyDefaultOutputDevice,
                kAudioObjectPropertyScopeGlobal,
                kAudioObjectPropertyElementMaster
            };

            UInt32 size = sizeof(AudioDeviceID);
            result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultDeviceAddress,
                                                0, nullptr, &size, &deviceId);

            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to get CoreAudio output device, error: " << result;
                return false;
            }

            static const AudioObjectPropertyAddress aliveAddress = {
                kAudioDevicePropertyDeviceIsAlive,
                kAudioDevicePropertyScopeOutput,
                kAudioObjectPropertyElementMaster
            };

            UInt32 alive = 0;
            size = sizeof(alive);
            result = AudioObjectGetPropertyData(deviceId, &aliveAddress, 0, nullptr, &size, &alive);

            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to get CoreAudio device status, error: " << result;
                return false;
            }

            if (!alive)
            {
                Log(Log::Level::ERR) << "Requested CoreAudio device is not alive, error: " << result;
                return false;
            }

            static const AudioObjectPropertyAddress hogModeAddress = {
                kAudioDevicePropertyHogMode,
                kAudioDevicePropertyScopeOutput,
                kAudioObjectPropertyElementMaster
            };

            pid_t pid = 0;
            size = sizeof(pid);
            result = AudioObjectGetPropertyData(deviceId, &hogModeAddress, 0, nullptr, &size, &pid);

            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to check if CoreAudio device is in hog mode, error: " << result;
                return false;
            }

            if (pid != -1)
            {
                Log(Log::Level::ERR) << "Requested CoreAudio device is being hogged";
                return false;
            }

            static const AudioObjectPropertyAddress nameAddress = {
                kAudioObjectPropertyName,
                kAudioDevicePropertyScopeOutput,
                kAudioObjectPropertyElementMaster
            };

            CFStringRef tempStringRef = nullptr;
            size = sizeof(CFStringRef);

            result = AudioObjectGetPropertyData(deviceId, &nameAddress,
                                                0, nullptr, &size, &tempStringRef);

            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to get CoreAudio device name, error: " << result;
                return false;
            }

            if (tempStringRef)
            {
                char temp[256];
                CFStringGetCString(tempStringRef, temp, sizeof(temp), kCFStringEncodingUTF8);

                Log(Log::Level::ERR) << "Using " << temp << " for audio";

                CFRelease(tempStringRef);
            }

#else
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:Nil];
#endif

            AudioComponentDescription desc;
            desc.componentType = kAudioUnitType_Output;

#if OUZEL_PLATFORM_MACOS
            desc.componentSubType = kAudioUnitSubType_DefaultOutput;
#elif OUZEL_PLATFORM_IOS || OUZEL_PLATFORM_TVOS
            desc.componentSubType = kAudioUnitSubType_RemoteIO;
#endif

            desc.componentManufacturer = kAudioUnitManufacturer_Apple;
            desc.componentFlags = 0;
            desc.componentFlagsMask = 0;

            audioComponent = AudioComponentFindNext(nullptr, &desc);

            if (!audioComponent)
            {
                Log(Log::Level::ERR) << "Failed to find requested CoreAudio component";
                return false;
            }
<commit_msg>Change the log level of audio device name log<commit_after>// Copyright (C) 2017 <NAME>
// This file is part of the Ouzel engine.

#include "core/CompileConfig.h"

#if OUZEL_SUPPORTS_COREAUDIO

#if OUZEL_PLATFORM_IOS || OUZEL_PLATFORM_TVOS
#import <AVFoundation/AVFoundation.h>
#include <AudioToolbox/AudioToolbox.h>
#endif

#include "AudioCA.h"
#include "utils/Log.h"

#if OUZEL_PLATFORM_MACOS

static OSStatus deviceListChanged(AudioObjectID, UInt32, const AudioObjectPropertyAddress*, void*)
{
    return 0;
}

static OSStatus deviceUnplugged(AudioObjectID, UInt32, const AudioObjectPropertyAddress*, void*)
{
    return noErr;
}
#endif

static OSStatus outputCallback(void* inRefCon,
                               AudioUnitRenderActionFlags*,
                               const AudioTimeStamp*,
                               UInt32, UInt32,
                               AudioBufferList* ioData)
{
    ouzel::audio::AudioCA* audioCA = static_cast<ouzel::audio::AudioCA*>(inRefCon);

    audioCA->outputCallback(ioData);

    return noErr;
}

namespace ouzel
{
    namespace audio
    {
        AudioCA::AudioCA():
            Audio(Audio::Driver::COREAUDIO)
        {
        }

        AudioCA::~AudioCA()
        {
            if (audioUnit)
            {
                AudioOutputUnitStop(audioUnit);

                AURenderCallbackStruct callback;
                callback.inputProc = nullptr;
                callback.inputProcRefCon = nullptr;

                const AudioUnitElement bus = 0;

                AudioUnitSetProperty(audioUnit,
                                     kAudioUnitProperty_SetRenderCallback,
                                     kAudioUnitScope_Input, bus, &callback, sizeof(callback));

                AudioComponentInstanceDispose(audioUnit);
            }

#if OUZEL_PLATFORM_MACOS
            static const AudioObjectPropertyAddress deviceListAddress = {
                kAudioHardwarePropertyDevices,
                kAudioObjectPropertyScopeGlobal,
                kAudioObjectPropertyElementMaster
            };

            AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &deviceListAddress, deviceListChanged, this);

            if (deviceId)
            {
                static const AudioObjectPropertyAddress aliveAddress = {
                    kAudioDevicePropertyDeviceIsAlive,
                    kAudioDevicePropertyScopeOutput,
                    kAudioObjectPropertyElementMaster
                };

                AudioObjectRemovePropertyListener(deviceId, &aliveAddress, deviceUnplugged, this);
            }
#endif
        }

        bool AudioCA::init()
        {
            if (!Audio::init())
            {
                return false;
            }

            OSStatus result;

#if OUZEL_PLATFORM_MACOS
            static const AudioObjectPropertyAddress deviceListAddress = {
                kAudioHardwarePropertyDevices,
                kAudioObjectPropertyScopeGlobal,
                kAudioObjectPropertyElementMaster
            };

            result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &deviceListAddress, deviceListChanged, this);
            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to add CoreAudio property listener, error: " << result;
                return false;
            }

            static const AudioObjectPropertyAddress defaultDeviceAddress = {
                kAudioHardwarePropertyDefaultOutputDevice,
                kAudioObjectPropertyScopeGlobal,
                kAudioObjectPropertyElementMaster
            };

            UInt32 size = sizeof(AudioDeviceID);
            result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultDeviceAddress,
                                                0, nullptr, &size, &deviceId);

            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to get CoreAudio output device, error: " << result;
                return false;
            }

            static const AudioObjectPropertyAddress aliveAddress = {
                kAudioDevicePropertyDeviceIsAlive,
                kAudioDevicePropertyScopeOutput,
                kAudioObjectPropertyElementMaster
            };

            UInt32 alive = 0;
            size = sizeof(alive);
            result = AudioObjectGetPropertyData(deviceId, &aliveAddress, 0, nullptr, &size, &alive);

            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to get CoreAudio device status, error: " << result;
                return false;
            }

            if (!alive)
            {
                Log(Log::Level::ERR) << "Requested CoreAudio device is not alive, error: " << result;
                return false;
            }

            static const AudioObjectPropertyAddress hogModeAddress = {
                kAudioDevicePropertyHogMode,
                kAudioDevicePropertyScopeOutput,
                kAudioObjectPropertyElementMaster
            };

            pid_t pid = 0;
            size = sizeof(pid);
            result = AudioObjectGetPropertyData(deviceId, &hogModeAddress, 0, nullptr, &size, &pid);

            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to check if CoreAudio device is in hog mode, error: " << result;
                return false;
            }

            if (pid != -1)
            {
                Log(Log::Level::ERR) << "Requested CoreAudio device is being hogged";
                return false;
            }

            static const AudioObjectPropertyAddress nameAddress = {
                kAudioObjectPropertyName,
                kAudioDevicePropertyScopeOutput,
                kAudioObjectPropertyElementMaster
            };

            CFStringRef tempStringRef = nullptr;
            size = sizeof(CFStringRef);

            result = AudioObjectGetPropertyData(deviceId, &nameAddress,
                                                0, nullptr, &size, &tempStringRef);

            if (result != noErr)
            {
                Log(Log::Level::ERR) << "Failed to get CoreAudio device name, error: " << result;
                return false;
            }

            if (tempStringRef)
            {
                char temp[256];
                CFStringGetCString(tempStringRef, temp, sizeof(temp), kCFStringEncodingUTF8);

                Log(Log::Level::INFO) << "Using " << temp << " for audio";

                CFRelease(tempStringRef);
            }

#else
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:Nil];
#endif

            AudioComponentDescription desc;
            desc.componentType = kAudioUnitType_Output;

#if OUZEL_PLATFORM_MACOS
            desc.componentSubType = kAudioUnitSubType_DefaultOutput;
#elif OUZEL_PLATFORM_IOS || OUZEL_PLATFORM_TVOS
            desc.componentSubType = kAudioUnitSubType_RemoteIO;
#endif

            desc.componentManufacturer = kAudioUnitManufacturer_Apple;
            desc.componentFlags = 0;
            desc.componentFlagsMask = 0;

            audioComponent = AudioComponentFindNext(nullptr, &desc);

            if (!audioComponent)
            {
                Log(Log::Level::ERR) << "Failed to find requested CoreAudio component";
                return false;
            }

{'fraction_non_alphanumeric': 0.07460349874072561, 'fraction_numerical': 0.004220270914165135, 'mean_word_length': 1.7663340237243457, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3375924', 'n_tokens_mistral': 3755, 'n_tokens_neox': 3518, 'n_words': 748}