aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/openmpt/sounddevice/SoundDevice.cpp
blob: e0448b1c0c07fc49610694ae05bc59d0f8878127 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: Olivier Lapicque */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */


#include "openmpt/all/BuildSettings.hpp"

#include "SoundDevice.hpp"

#include "mpt/base/alloc.hpp"
#include "mpt/binary/hex.hpp"
#include "mpt/format/join.hpp"
#include "mpt/format/message_macros.hpp"
#include "mpt/format/simple.hpp"
#include "mpt/parse/split.hpp"
#include "mpt/string/types.hpp"
#include "mpt/string/utility.hpp"
#include "mpt/string_transcode/transcode.hpp"
#include "openmpt/base/Types.hpp"

#include <map>
#include <string>
#include <vector>


OPENMPT_NAMESPACE_BEGIN


namespace SoundDevice
{


SoundDevice::Type ParseType(const SoundDevice::Identifier &identifier)
{
	std::vector<mpt::ustring> tmp = mpt::split(identifier, MPT_USTRING("_"));
	if(tmp.size() == 0)
	{
		return SoundDevice::Type();
	}
	return tmp[0];
}


mpt::ustring Info::GetDisplayName() const
{
	mpt::ustring result = apiName + MPT_USTRING(" - ") + mpt::trim(name);
	switch(flags.usability)
	{
		case SoundDevice::Info::Usability::Experimental:
			result += MPT_USTRING(" [experimental]");
			break;
		case SoundDevice::Info::Usability::Deprecated:
			result += MPT_USTRING(" [deprecated]");
			break;
		case SoundDevice::Info::Usability::Broken:
			result += MPT_USTRING(" [broken]");
			break;
		case SoundDevice::Info::Usability::NotAvailable:
			result += MPT_USTRING(" [alien]");
			break;
		default:
			// nothing
			break;
	}
	if(default_ == SoundDevice::Info::Default::Named)
	{
		result += MPT_USTRING(" [default]");
	}
	if(apiPath.size() > 0)
	{
		result += MPT_USTRING(" (") + mpt::join(apiPath, MPT_USTRING("/")) + MPT_USTRING(")");
	}
	return result;
}


SoundDevice::Identifier Info::GetIdentifier() const
{
	if(!IsValid())
	{
		return mpt::ustring();
	}
	mpt::ustring result = mpt::ustring();
	result += type;
	result += MPT_USTRING("_");
	if(useNameAsIdentifier)
	{
		// UTF8-encode the name and convert the utf8 to hex.
		// This ensures that no special characters are contained in the configuration key.
		std::string utf8String = mpt::transcode<std::string>(mpt::common_encoding::utf8, name);
		mpt::ustring hexString = mpt::encode_hex(mpt::as_span(utf8String));
		result += hexString;
	} else
	{
		result += internalID;  // safe to not contain special characters
	}
	return result;
}


ChannelMapping::ChannelMapping(uint32 numHostChannels)
{
	ChannelToDeviceChannel.resize(numHostChannels);
	for(uint32 channel = 0; channel < numHostChannels; ++channel)
	{
		ChannelToDeviceChannel[channel] = channel;
	}
}


ChannelMapping::ChannelMapping(const std::vector<int32> &mapping)
{
	if(IsValid(mapping))
	{
		ChannelToDeviceChannel = mapping;
	}
}


ChannelMapping ChannelMapping::BaseChannel(uint32 channels, int32 baseChannel)
{
	SoundDevice::ChannelMapping result;
	result.ChannelToDeviceChannel.resize(channels);
	for(uint32 channel = 0; channel < channels; ++channel)
	{
		result.ChannelToDeviceChannel[channel] = channel + baseChannel;
	}
	return result;
}


bool ChannelMapping::IsValid(const std::vector<int32> &mapping)
{
	if(mapping.empty())
	{
		return true;
	}
	std::map<int32, uint32> inverseMapping;
	for(uint32 hostChannel = 0; hostChannel < mapping.size(); ++hostChannel)
	{
		int32 deviceChannel = mapping[hostChannel];
		if(deviceChannel < 0)
		{
			return false;
		}
		if(deviceChannel > MaxDeviceChannel)
		{
			return false;
		}
		inverseMapping[deviceChannel] = hostChannel;
	}
	if(inverseMapping.size() != mapping.size())
	{
		return false;
	}
	return true;
}


mpt::ustring ChannelMapping::ToUString() const
{
	return mpt::join_format<mpt::ustring, int32>(ChannelToDeviceChannel, MPT_USTRING(","));
}


ChannelMapping ChannelMapping::FromString(const mpt::ustring &str)
{
	return SoundDevice::ChannelMapping(mpt::split_parse<int32>(str, MPT_USTRING(",")));
}


}  // namespace SoundDevice


OPENMPT_NAMESPACE_END