summaryrefslogtreecommitdiff
path: root/3rdparty/portaudio/bindings/cpp/source/portaudiocpp/HostApi.cxx
blob: 6a09670c0027a8f352ac36f305f2cacd0f1103f8 (plain)
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
#include "portaudiocpp/HostApi.hxx"

#include "portaudiocpp/System.hxx"
#include "portaudiocpp/Device.hxx"
#include "portaudiocpp/SystemDeviceIterator.hxx"
#include "portaudiocpp/Exception.hxx"

namespace portaudio
{

	// -----------------------------------------------------------------------------------

	HostApi::HostApi(PaHostApiIndex index) : devices_(NULL)
	{
		try
		{
			info_ = Pa_GetHostApiInfo(index);

			// Create and populate devices array:
			int numDevices = deviceCount();

			devices_ = new Device*[numDevices];

			for (int i = 0; i < numDevices; ++i)
			{
				PaDeviceIndex deviceIndex = Pa_HostApiDeviceIndexToDeviceIndex(index, i);

				if (deviceIndex < 0)
				{
					throw PaException(deviceIndex);
				}

				devices_[i] = &System::instance().deviceByIndex(deviceIndex);
			}
		}
		catch (const std::exception &e)
		{
			// Delete any (partially) constructed objects (deconstructor isn't called):
			delete[] devices_; // devices_ is either NULL or valid

			// Re-throw exception:
			throw e;
		}
	}

	HostApi::~HostApi()
	{
		// Destroy devices array:
		delete[] devices_;
	}

	// -----------------------------------------------------------------------------------

	PaHostApiTypeId HostApi::typeId() const
	{
		return info_->type;
	}

	PaHostApiIndex HostApi::index() const
	{
		PaHostApiIndex index = Pa_HostApiTypeIdToHostApiIndex(typeId());

		if (index < 0)
			throw PaException(index);

		return index;
	}

	const char *HostApi::name() const
	{
		return info_->name;
	}

	int HostApi::deviceCount() const
	{
		return info_->deviceCount;
	}

	// -----------------------------------------------------------------------------------

	HostApi::DeviceIterator HostApi::devicesBegin()
	{
		DeviceIterator tmp;
		tmp.ptr_ = &devices_[0]; // begin (first element)
		return tmp;
	}

	HostApi::DeviceIterator HostApi::devicesEnd()
	{
		DeviceIterator tmp;
		tmp.ptr_ = &devices_[deviceCount()]; // end (one past last element)
		return tmp;
	}

	// -----------------------------------------------------------------------------------

	Device &HostApi::defaultInputDevice() const
	{
		return System::instance().deviceByIndex(info_->defaultInputDevice);
	}

	Device &HostApi::defaultOutputDevice() const
	{
		return System::instance().deviceByIndex(info_->defaultOutputDevice);
	}

	// -----------------------------------------------------------------------------------

	bool HostApi::operator==(const HostApi &rhs) const
	{
		return (typeId() == rhs.typeId());
	}

	bool HostApi::operator!=(const HostApi &rhs) const
	{
		return !(*this == rhs);
	}

	// -----------------------------------------------------------------------------------

} // namespace portaudio