<commit_before>// Copyright (c) 2015, <NAME>.
// Distributed under the BSD 2-Clause License (see LICENSE.txt for details)

#include "CoreAudio.h"

#include <math.h>
#include <stdlib.h>

#include "../../Logger.h"
#include "Functiondiscoverykeys_devpkey.h"
#include "VolumeTransformation.h"

// {EC9CB649-7E84-4B42-B367-7FC39BE17806}
static const GUID G3RVXCoreAudioEvent = { 0xec9cb649, 0x7e84, 0x4b42,
    { 0xb3, 0x67, 0x7f, 0xc3, 0x9b, 0xe1, 0x78, 0x6 } };
 
HRESULT CoreAudio::Init() {
    HRESULT hr;

    hr = CoCreateInstance(
        __uuidof(MMDeviceEnumerator),
        nullptr,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&_devEnumerator));

    if (SUCCEEDED(hr)) {
        hr = _devEnumerator->RegisterEndpointNotificationCallback(this);

        if (SUCCEEDED(hr)) {
            hr = AttachDevice();
        }
    }

    return hr;
}

HRESULT CoreAudio::Init(std::wstring deviceId) {
    _devId = deviceId;
    return Init();
}

void CoreAudio::Dispose() {
    DetachDevice();
    _devEnumerator->UnregisterEndpointNotificationCallback(this);
}

HRESULT CoreAudio::AttachDevice() {
    HRESULT hr;

    if (_devId.empty()) {
        /* Use default device */
        hr = _devEnumerator->GetDefaultAudioEndpoint(eRender,
            eMultimedia, &_device);
        if (SUCCEEDED(hr)) {
            LPWSTR id = nullptr;
            hr = _device->GetId(&id);
            if (SUCCEEDED(hr) && id) {
                _devId = std::wstring(id);
                CoTaskMemFree(id);
            }
        }
    } else {
        hr = _devEnumerator->GetDevice(_devId.c_str(), &_device);
    }

    if (SUCCEEDED(hr)) {
        hr = _device->Activate(__uuidof(_volumeControl),
            CLSCTX_INPROC_SERVER, nullptr, (void **) &_volumeControl);

        CLOG(L"Attached to audio device: [%s]", DeviceName().c_str());

        if (SUCCEEDED(hr)) {
            hr = _volumeControl->RegisterControlChangeNotify(this);
            _registeredNotifications = SUCCEEDED(hr);
        }
    } else {
        CLOG(L"Failed to find audio device!");
        _volumeControl = nullptr;
    }

    return hr;
}

void CoreAudio::DetachDevice() {
    if (_volumeControl != nullptr) {

        if (_registeredNotifications) {
            _volumeControl->UnregisterControlChangeNotify(this);
            _registeredNotifications = false;
        }<commit_msg>Failure to find a device should null out _device member<commit_after>// Copyright (c) 2015, <NAME>.
// Distributed under the BSD 2-Clause License (see LICENSE.txt for details)

#include "CoreAudio.h"

#include <math.h>
#include <stdlib.h>

#include "../../Logger.h"
#include "Functiondiscoverykeys_devpkey.h"
#include "VolumeTransformation.h"

// {EC9CB649-7E84-4B42-B367-7FC39BE17806}
static const GUID G3RVXCoreAudioEvent = { 0xec9cb649, 0x7e84, 0x4b42,
    { 0xb3, 0x67, 0x7f, 0xc3, 0x9b, 0xe1, 0x78, 0x6 } };
 
HRESULT CoreAudio::Init() {
    HRESULT hr;

    hr = CoCreateInstance(
        __uuidof(MMDeviceEnumerator),
        nullptr,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&_devEnumerator));

    if (SUCCEEDED(hr)) {
        hr = _devEnumerator->RegisterEndpointNotificationCallback(this);

        if (SUCCEEDED(hr)) {
            hr = AttachDevice();
        }
    }

    return hr;
}

HRESULT CoreAudio::Init(std::wstring deviceId) {
    _devId = deviceId;
    return Init();
}

void CoreAudio::Dispose() {
    DetachDevice();
    _devEnumerator->UnregisterEndpointNotificationCallback(this);
}

HRESULT CoreAudio::AttachDevice() {
    HRESULT hr;

    if (_devId.empty()) {
        /* Use default device */
        hr = _devEnumerator->GetDefaultAudioEndpoint(eRender,
            eMultimedia, &_device);
        if (SUCCEEDED(hr)) {
            LPWSTR id = nullptr;
            hr = _device->GetId(&id);
            if (SUCCEEDED(hr) && id) {
                _devId = std::wstring(id);
                CoTaskMemFree(id);
            }
        }
    } else {
        hr = _devEnumerator->GetDevice(_devId.c_str(), &_device);
    }

    if (SUCCEEDED(hr)) {
        hr = _device->Activate(__uuidof(_volumeControl),
            CLSCTX_INPROC_SERVER, nullptr, (void **) &_volumeControl);

        CLOG(L"Attached to audio device: [%s]", DeviceName().c_str());

        if (SUCCEEDED(hr)) {
            hr = _volumeControl->RegisterControlChangeNotify(this);
            _registeredNotifications = SUCCEEDED(hr);
        }
    } else {
        CLOG(L"Failed to find audio device!");
        _device = nullptr;
        _volumeControl = nullptr;
    }

    return hr;
}

void CoreAudio::DetachDevice() {
    if (_volumeControl != nullptr) {

        if (_registeredNotifications) {
            _volumeControl->UnregisterControlChangeNotify(this);
            _registeredNotifications = false;
        }
{'fraction_non_alphanumeric': 0.12486905510161324, 'fraction_numerical': 0.024303373140582445, 'mean_word_length': 2.5760299625468166, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2475622', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1582, 'n_words': 299}