<commit_before>//This is undocumented  COM magic.  Including this here should be sufficient for most of the library, but maybe not.
#include <initguid.h>
#include "wasapi.hpp"
#include <audio_io/private/audio_outputs.hpp>
#include <audio_io/private/single_threaded_apartment.hpp>
#include <windows.h>
#include <audioclient.h>
#include <mmdeviceapi.h>
#include <functiondiscoverykeys_devpkey.h>

namespace audio_io {
namespace implementation {

WasapiOutputDeviceFactory::WasapiOutputDeviceFactory() {
	APARTMENTCALL(CoCreateInstance, CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&enumerator);
	//todo: errors.
	rescan();
}

WasapiOutputDeviceFactory::~WasapiOutputDeviceFactory() {
	if(enumerator) enumerator->Release();
}

std::vector<std::string> WasapiOutputDeviceFactory::getOutputNames() {
	return names;
}

std::vector<int> WasapiOutputDeviceFactory::getOutputMaxChannels() {
	return max_channels;
}

std::shared_ptr<OutputDevice> WasapiOutputDeviceFactory::createDevice(std::function<void(float*, int)> callback, int index, unsigned int channels, unsigned int sr, unsigned int blockSize, unsigned int mixAhead) {
	HRESULT res;
	IMMDevice* dev;
	if(index == -1) res = APARTMENTCALL(enumerator->GetDefaultAudioEndpoint, eRender, eMultimedia, &dev);
	else res = APARTMENTCALL(enumerator->GetDevice, ids_to_id_strings[index].c_str(), &dev);
	//Proper error handling!
	if(res != S_OK) return nullptr;
	return std::make_shared<WasapiOutputDevice>(callback, dev, blockSize, channels, sr, mixAhead);
}

unsigned int WasapiOutputDeviceFactory::getOutputCount() {
	return names.size();
}

std::string WasapiOutputDeviceFactory::getName() {
	return "Wasapi";
}

void WasapiOutputDeviceFactory::rescan() {
	names.clear();<commit_msg>Register wasapi devices with their factory.<commit_after>//This is undocumented  COM magic.  Including this here should be sufficient for most of the library, but maybe not.
#include <initguid.h>
#include "wasapi.hpp"
#include <audio_io/private/audio_outputs.hpp>
#include <audio_io/private/single_threaded_apartment.hpp>
#include <windows.h>
#include <audioclient.h>
#include <mmdeviceapi.h>
#include <functiondiscoverykeys_devpkey.h>

namespace audio_io {
namespace implementation {

WasapiOutputDeviceFactory::WasapiOutputDeviceFactory() {
	APARTMENTCALL(CoCreateInstance, CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&enumerator);
	//todo: errors.
	rescan();
}

WasapiOutputDeviceFactory::~WasapiOutputDeviceFactory() {
	if(enumerator) enumerator->Release();
}

std::vector<std::string> WasapiOutputDeviceFactory::getOutputNames() {
	return names;
}

std::vector<int> WasapiOutputDeviceFactory::getOutputMaxChannels() {
	return max_channels;
}

std::shared_ptr<OutputDevice> WasapiOutputDeviceFactory::createDevice(std::function<void(float*, int)> callback, int index, unsigned int channels, unsigned int sr, unsigned int blockSize, unsigned int mixAhead) {
	HRESULT res;
	IMMDevice* dev;
	if(index == -1) res = APARTMENTCALL(enumerator->GetDefaultAudioEndpoint, eRender, eMultimedia, &dev);
	else res = APARTMENTCALL(enumerator->GetDevice, ids_to_id_strings[index].c_str(), &dev);
	//Proper error handling!
	if(res != S_OK) return nullptr;
	auto ret = std::make_shared<WasapiOutputDevice>(callback, dev, blockSize, channels, sr, mixAhead);
	created_devices.push_back(ret);
	return ret;
}

unsigned int WasapiOutputDeviceFactory::getOutputCount() {
	return names.size();
}

std::string WasapiOutputDeviceFactory::getName() {
	return "Wasapi";
}

void WasapiOutputDeviceFactory::rescan() {
	names.clear();
{'fraction_non_alphanumeric': 0.12118691070438159, 'fraction_numerical': 0.0005546311702717693, 'mean_word_length': 7.906172839506173, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, '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': '5993265', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1159, 'n_words': 248}