| 136 | } |
| 137 | |
| 138 | void openStream() { |
| 139 | // Open new device |
| 140 | if (deviceInfo.outputChannels == 0 && deviceInfo.inputChannels == 0) { |
| 141 | throw Exception("RtAudio %s device %d has 0 inputs and 0 outputs", driver->getName().c_str(), deviceId); |
| 142 | } |
| 143 | |
| 144 | inputParameters = RtAudio::StreamParameters(); |
| 145 | inputParameters.deviceId = deviceId; |
| 146 | inputParameters.nChannels = deviceInfo.inputChannels; |
| 147 | inputParameters.firstChannel = 0; |
| 148 | |
| 149 | outputParameters = RtAudio::StreamParameters(); |
| 150 | outputParameters.deviceId = deviceId; |
| 151 | outputParameters.nChannels = deviceInfo.outputChannels; |
| 152 | outputParameters.firstChannel = 0; |
| 153 | |
| 154 | options = RtAudio::StreamOptions(); |
| 155 | // options.flags |= RTAUDIO_MINIMIZE_LATENCY; |
| 156 | options.flags |= RTAUDIO_SCHEDULE_REALTIME; |
| 157 | options.numberOfBuffers = 2; |
| 158 | options.streamName = "VCV Rack"; |
| 159 | |
| 160 | int32_t closestSampleRate = deviceInfo.preferredSampleRate; |
| 161 | if (sampleRate > 0) { |
| 162 | // Find the closest sample rate to the requested one. |
| 163 | for (int32_t sr : deviceInfo.sampleRates) { |
| 164 | if (std::fabs(sr - sampleRate) < std::fabs(closestSampleRate - sampleRate)) { |
| 165 | closestSampleRate = sr; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (blockSize <= 0) { |
| 171 | // DirectSound should use a higher default block size |
| 172 | if (driver->api == RtAudio::WINDOWS_DS) |
| 173 | blockSize = 1024; |
| 174 | else |
| 175 | blockSize = 256; |
| 176 | } |
| 177 | |
| 178 | INFO("Opening RtAudio %s device %d: %s (%d in, %d out, %d sample rate, %d block size)", driver->getName().c_str(), deviceId, deviceInfo.name.c_str(), inputParameters.nChannels, outputParameters.nChannels, closestSampleRate, blockSize); |
| 179 | if (rtAudio->openStream( |
| 180 | outputParameters.nChannels > 0 ? &outputParameters : NULL, |
| 181 | inputParameters.nChannels > 0 ? &inputParameters : NULL, |
| 182 | RTAUDIO_FLOAT32, closestSampleRate, (unsigned int*) &blockSize, |
| 183 | &rtAudioCallback, this, &options)) { |
| 184 | throw Exception("Failed to open RtAudio %s device %d", driver->getName().c_str(), deviceId); |
| 185 | } |
| 186 | |
| 187 | try { |
| 188 | INFO("Starting RtAudio %s device %d", driver->getName().c_str(), deviceId); |
| 189 | if (rtAudio->startStream()) { |
| 190 | throw Exception("Failed to start RtAudio %s device %d", driver->getName().c_str(), deviceId); |
| 191 | } |
| 192 | |
| 193 | // Update sample rate to actual value |
| 194 | sampleRate = rtAudio->getStreamSampleRate(); |
| 195 |
nothing calls this directly
no test coverage detected