Line data Source code
1 : /** 2 : * \file CachedSinusGeneratorFilter.cpp 3 : */ 4 : 5 : #include "CachedSinusGeneratorFilter.h" 6 : 7 : #include <boost/math/constants/constants.hpp> 8 : 9 : #include <cmath> 10 : #include <cstdint> 11 : #include <cstring> 12 : 13 : namespace ATK 14 : { 15 : template<typename DataType_> 16 6 : CachedSinusGeneratorFilter<DataType_>::CachedSinusGeneratorFilter(int periods, int seconds) 17 6 : :Parent(0, 1), periods(periods), seconds(seconds) 18 : { 19 6 : } 20 : 21 : template<typename DataType_> 22 2 : void CachedSinusGeneratorFilter<DataType_>::set_frequency(int periods, int seconds) 23 : { 24 2 : if(periods <= 0) 25 : { 26 1 : throw std::out_of_range("Periods must be strictly positive"); 27 : } 28 1 : this->periods = periods; 29 1 : this->seconds = seconds; 30 1 : setup(); 31 1 : } 32 : 33 : template<typename DataType_> 34 2 : std::pair<int, int> CachedSinusGeneratorFilter<DataType_>::get_frequency() const 35 : { 36 2 : return std::make_pair(periods, seconds); 37 : } 38 : 39 : template<typename DataType_> 40 2 : void CachedSinusGeneratorFilter<DataType_>::set_volume(DataType_ volume) 41 : { 42 2 : this->volume = volume; 43 2 : } 44 : 45 : template<typename DataType_> 46 1 : DataType_ CachedSinusGeneratorFilter<DataType_>::get_volume() const 47 : { 48 1 : return volume; 49 : } 50 : 51 : template<typename DataType_> 52 2 : void CachedSinusGeneratorFilter<DataType_>::set_offset(DataType_ offset) 53 : { 54 2 : this->offset = offset; 55 2 : } 56 : 57 : template<typename DataType_> 58 1 : DataType_ CachedSinusGeneratorFilter<DataType_>::get_offset() const 59 : { 60 1 : return offset; 61 : } 62 : 63 : template<typename DataType_> 64 3 : void CachedSinusGeneratorFilter<DataType_>::setup() 65 : { 66 3 : indice = 0; 67 3 : cache.resize(output_sampling_rate * seconds); 68 528003 : for(gsl::index i = 0; i < cache.size(); ++i) 69 : { 70 528000 : cache[i] = static_cast<DataType>(std::sin(2 * boost::math::constants::pi<double>() * (i+1) * periods / seconds / output_sampling_rate)); 71 : } 72 3 : } 73 : 74 : template<typename DataType_> 75 5 : void CachedSinusGeneratorFilter<DataType_>::process_impl(gsl::index size) const 76 : { 77 5 : DataType* ATK_RESTRICT output = outputs[0]; 78 5 : gsl::index processed = 0; 79 10 : while (processed < size) 80 : { 81 5 : auto to_copy = std::min(size - processed, static_cast<gsl::index>(cache.size()) - indice); 82 5 : memcpy(reinterpret_cast<void*>(output + processed), reinterpret_cast<const void*>(cache.data() + indice), to_copy * sizeof(DataType_)); 83 5 : indice += to_copy; 84 5 : processed += to_copy; 85 5 : if (indice >= cache.size()) 86 : { 87 0 : indice = 0; 88 : } 89 : } 90 66565 : for (gsl::index i = 0; i < size; ++i) 91 : { 92 66560 : output[i] = static_cast<DataType>(offset + volume * output[i]); 93 : } 94 5 : } 95 : 96 : #if ATK_ENABLE_INSTANTIATION 97 : template class CachedSinusGeneratorFilter<std::int16_t>; 98 : template class CachedSinusGeneratorFilter<std::int32_t>; 99 : template class CachedSinusGeneratorFilter<int64_t>; 100 : template class CachedSinusGeneratorFilter<float>; 101 : #endif 102 : template class CachedSinusGeneratorFilter<double>; 103 : }