| 1 | #include "portaudiocpp/HostApi.hxx" |
| 2 | |
| 3 | #include "portaudiocpp/System.hxx" |
| 4 | #include "portaudiocpp/Device.hxx" |
| 5 | #include "portaudiocpp/SystemDeviceIterator.hxx" |
| 6 | #include "portaudiocpp/Exception.hxx" |
| 7 | |
| 8 | namespace portaudio |
| 9 | { |
| 10 | |
| 11 | // ----------------------------------------------------------------------------------- |
| 12 | |
| 13 | HostApi::HostApi(PaHostApiIndex index) : devices_(NULL) |
| 14 | { |
| 15 | try |
| 16 | { |
| 17 | info_ = Pa_GetHostApiInfo(index); |
| 18 | |
| 19 | // Create and populate devices array: |
| 20 | int numDevices = deviceCount(); |
| 21 | |
| 22 | devices_ = new Device*[numDevices]; |
| 23 | |
| 24 | for (int i = 0; i < numDevices; ++i) |
| 25 | { |
| 26 | PaDeviceIndex deviceIndex = Pa_HostApiDeviceIndexToDeviceIndex(index, i); |
| 27 | |
| 28 | if (deviceIndex < 0) |
| 29 | { |
| 30 | throw PaException(deviceIndex); |
| 31 | } |
| 32 | |
| 33 | devices_[i] = &System::instance().deviceByIndex(deviceIndex); |
| 34 | } |
| 35 | } |
| 36 | catch (const std::exception &e) |
| 37 | { |
| 38 | // Delete any (partially) constructed objects (deconstructor isn't called): |
| 39 | delete[] devices_; // devices_ is either NULL or valid |
| 40 | |
| 41 | // Re-throw exception: |
| 42 | throw e; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | HostApi::~HostApi() |
| 47 | { |
| 48 | // Destroy devices array: |
| 49 | delete[] devices_; |
| 50 | } |
| 51 | |
| 52 | // ----------------------------------------------------------------------------------- |
| 53 | |
| 54 | PaHostApiTypeId HostApi::typeId() const |
| 55 | { |
| 56 | return info_->type; |
| 57 | } |
| 58 | |
| 59 | PaHostApiIndex HostApi::index() const |
| 60 | { |
| 61 | PaHostApiIndex index = Pa_HostApiTypeIdToHostApiIndex(typeId()); |
| 62 | |
| 63 | if (index < 0) |
| 64 | throw PaException(index); |
| 65 | |
| 66 | return index; |
| 67 | } |
| 68 | |
| 69 | const char *HostApi::name() const |
| 70 | { |
| 71 | return info_->name; |
| 72 | } |
| 73 | |
| 74 | int HostApi::deviceCount() const |
| 75 | { |
| 76 | return info_->deviceCount; |
| 77 | } |
| 78 | |
| 79 | // ----------------------------------------------------------------------------------- |
| 80 | |
| 81 | HostApi::DeviceIterator HostApi::devicesBegin() |
| 82 | { |
| 83 | DeviceIterator tmp; |
| 84 | tmp.ptr_ = &devices_[0]; // begin (first element) |
| 85 | return tmp; |
| 86 | } |
| 87 | |
| 88 | HostApi::DeviceIterator HostApi::devicesEnd() |
| 89 | { |
| 90 | DeviceIterator tmp; |
| 91 | tmp.ptr_ = &devices_[deviceCount()]; // end (one past last element) |
| 92 | return tmp; |
| 93 | } |
| 94 | |
| 95 | // ----------------------------------------------------------------------------------- |
| 96 | |
| 97 | Device &HostApi::defaultInputDevice() const |
| 98 | { |
| 99 | return System::instance().deviceByIndex(info_->defaultInputDevice); |
| 100 | } |
| 101 | |
| 102 | Device &HostApi::defaultOutputDevice() const |
| 103 | { |
| 104 | return System::instance().deviceByIndex(info_->defaultOutputDevice); |
| 105 | } |
| 106 | |
| 107 | // ----------------------------------------------------------------------------------- |
| 108 | |
| 109 | bool HostApi::operator==(const HostApi &rhs) const |
| 110 | { |
| 111 | return (typeId() == rhs.typeId()); |
| 112 | } |
| 113 | |
| 114 | bool HostApi::operator!=(const HostApi &rhs) const |
| 115 | { |
| 116 | return !(*this == rhs); |
| 117 | } |
| 118 | |
| 119 | // ----------------------------------------------------------------------------------- |
| 120 | |
| 121 | } // namespace portaudio |