void Simulation::setOutputDevice(int index, int channels, float minLatency, float startLatency, float maxLatency) { if(index < -1) ERROR(Lav_ERROR_RANGE, "Index -1 is default; all other negative numbers are invalid."); if(output_device) { output_device->stop(); } std::lock_guard<std::recursive_mutex> g(mutex); auto factory = getOutputDeviceFactory(); if(factory == nullptr) ERROR(Lav_ERROR_CANNOT_INIT_AUDIO, "Failed to get output device factory."); auto sptr = std::static_pointer_cast<Simulation>(shared_from_this()); std::weak_ptr<Simulation> wptr(sptr); int blockSize=getBlockSize(); auto cb =[wptr, blockSize](float* buffer, int channels)->void { auto strong =wptr.lock(); if(strong==nullptr) memset(buffer, 0, sizeof(float)*blockSize*channels); else { std::lock_guard<Simulation> guard(*strong); strong->getBlock(buffer, channels); } }; std::shared_ptr<audio_io::OutputDevice> dev; try { dev =factory->createDevice(cb, index, channels, getSr(), getBlockSize(), minLatency, startLatency, maxLatency); if(dev == nullptr) ERROR(Lav_ERROR_CANNOT_INIT_AUDIO, "Device could not be created."); } catch(std::exception &e) { ERROR(Lav_ERROR_CANNOT_INIT_AUDIO, e.what()); } output_device=dev; }
void Simulation::writeFile(std::string path, int channels, double duration, bool mayApplyMixingMatrix) { int blocks = (duration*getSr()/block_size)+1; auto file = FileWriter(); file.open(path.c_str(), sr, channels); //vectors are guaranteed to be contiguous. Using a vector here guarantees proper cleanup. std::vector<float> block; block.resize(channels*getBlockSize()); for(int i = 0; i < blocks; i++) { getBlock(&block[0], channels, mayApplyMixingMatrix); unsigned int written= 0; while(written < getBlockSize()) written += file.write(getBlockSize(), &block[0]); } file.close(); }
void Simulation::setOutputDevice(int index, int channels, int mixahead) { if(index < -1) ERROR(Lav_ERROR_RANGE, "Index -1 is default; all other negative numbers are invalid."); auto factory = getOutputDeviceFactory(); if(factory == nullptr) ERROR(Lav_ERROR_CANNOT_INIT_AUDIO, "Failed to get output device factory."); auto sptr = std::static_pointer_cast<Simulation>(shared_from_this()); std::weak_ptr<Simulation> wptr(sptr); int blockSize=getBlockSize(); auto cb =[wptr, blockSize](float* buffer, int channels)->void { auto strong =wptr.lock(); if(strong==nullptr) memset(buffer, 0, sizeof(float)*blockSize*channels); else { std::lock_guard<Simulation> guard(*strong); strong->getBlock(buffer, channels); } }; auto dev =factory->createDevice(cb, index, channels, getSr(), getBlockSize(), mixahead); if(dev == nullptr) ERROR(Lav_ERROR_CANNOT_INIT_AUDIO, "Device could not be created."); output_device=dev; }