void Exciter::ProcessParticles( const uint8_t flags, float* out, size_t size) { if (flags & EXCITER_FLAG_RISING_EDGE) { particle_state_ = RandomSample(); particle_state_ = 1.0f - 0.6f * particle_state_ * particle_state_; delay_ = 0; particle_range_ = 1.0f; } fill(&out[0], &out[size], 0.0f); if (flags & EXCITER_FLAG_GATE) { const uint32_t up_probability = uint32_t(0.7f * 4294967296.0f); const uint32_t down_probability = uint32_t(0.3f * 4294967296.0f); const float amplitude = GetPulseAmplitude(timbre_); while (size--) { if (delay_ == 0) { float amount = RandomSample(); amount = 1.05f + 0.5f * amount * amount; if (Random::GetWord() > up_probability) { particle_state_ *= amount; if (particle_state_ >= (particle_range_ + 0.25f)) { particle_state_ = particle_range_ + 0.25f; } } else if (Random::GetWord() < down_probability) { particle_state_ /= amount; if (particle_state_ <= 0.02f) { particle_state_ = 0.02f; } } delay_ = static_cast<uint32_t>(particle_state_ * 0.15f * kSampleRate); float gain = 1.0f - particle_range_; gain *= gain; *out = particle_state_ * amplitude * (1.0f - gain); float decay_factor = 1.0f - parameter_; particle_range_ *= 1.0f - decay_factor * decay_factor * 0.5f; } else { --delay_; } ++out; } } }
void Exciter::ProcessFlow( const uint8_t flags, float* out, size_t size) { float scale = parameter_ * parameter_ * parameter_ * parameter_; float threshold = 0.0001f + scale * 0.125f; if (flags & EXCITER_FLAG_RISING_EDGE) { particle_state_ = 0.5f; } while (size--) { float sample = RandomSample(); if (sample < threshold) { particle_state_ = -particle_state_; } *out++ = particle_state_ + (sample - 0.5f - particle_state_) * scale; } }
bool BuildVocabTree(const char *sift_list, const char *output_filename, int depth, int branch_num, SiftType sift_type, int thread_num) { // read sift filenames, get the total number of sift keys, and allocate memory std::vector<std::string> sift_filenames; tw::IO::ExtractLines(sift_list, sift_filenames); size_t siftfile_num = sift_filenames.size(); // sample a part of sift files size_t memory_size = tw::IO::GetAvailMem() / (1024*1024); // convert to mb size_t tree_memory_size = FDIM * sizeof(DTYPE) * (size_t)pow((double)branch_num, (double)(depth+1)) / (1024*1024); size_t max_siftfile_num = (memory_size - tree_memory_size) / 2; size_t sample_size = siftfile_num > max_siftfile_num ? max_siftfile_num : siftfile_num; std::vector<size_t> siftfile_samples = RandomSample(siftfile_num, sample_size); size_t total_keys = 0; std::vector<tw::SiftData> sift_data; sift_data.resize(sample_size); std::cout << "Reading sift (type " << (int)sift_type << ") files...\n"; for(size_t i = 0; i < sample_size; i++) { if(sift_type == E3D_SIFT) { if(sizeof(DTYPE) == 1) sift_data[i].ReadSiftFile(sift_filenames[siftfile_samples[i]]); else sift_data[i].ReadChar2DTYPE(sift_filenames[siftfile_samples[i]]); } else if(sift_type == OPENMVG_FEAT) { sift_data[i].ReadOpenmvgDesc<DTYPE, FDIM>(sift_filenames[i]); } else //if(sift_type == 1) { std::cout << "[Build Tree] Sift type is wrong (should be 0). Exit...\n"; return false; } total_keys += sift_data[i].getFeatureNum(); } std::cout << "[Build Tree] Total sift keys (Type SIFT5.0): " << total_keys << std::endl; size_t len = (size_t) total_keys * FDIM; size_t num_arrays = len / MAX_ARRAY_SIZE + ((len % MAX_ARRAY_SIZE) == 0 ? 0 : 1); // allocate a big chunk of memory to each array std::cout << "[Build Tree] Allocate " << len << " bytes memory into " << num_arrays << " arrays\n"; DTYPE **mem = new DTYPE *[num_arrays]; size_t remain_length = len; for(size_t i = 0; i < num_arrays; i++) { size_t len_curr = remain_length > MAX_ARRAY_SIZE ? MAX_ARRAY_SIZE : remain_length; mem[i] = new DTYPE [len_curr]; remain_length -= len_curr; } assert(remain_length == 0); // allocate a pointer array to sift data DTYPE **mem_pointer = new DTYPE *[total_keys]; size_t off = 0; size_t curr_key = 0; int curr_array = 0; for(size_t i = 0; i < sample_size; i++) { int num_keys = sift_data[i].getFeatureNum(); if(num_keys > 0 ) { DTYPE *dp = sift_data[i].getDesPointer(); for(int j = 0; j < num_keys; j++) { for(int k = 0; k < FDIM; k++) { mem[curr_array][off+k] = dp[j * FDIM + k]; } mem_pointer[curr_key] = mem[curr_array] + off; curr_key++; off += FDIM; if(off == MAX_ARRAY_SIZE) { off = 0; curr_array++; } } } } // build a vocabulary tree using sift keys vot::VocabTree vt; if(vt.BuildTree(total_keys, FDIM, depth, branch_num, mem_pointer, thread_num)) vt.WriteTree(output_filename); vot::VocabTree vt1; vt1.ReadTree(output_filename); if(vt1.Compare(vt)) { std::cout << "[VocabTree IO test] vt and vt1 are the same\n"; } else { std::cout << "[vocabTree IO test] vt and vt1 are different\n"; } vt.ClearTree(); // free memory delete [] mem_pointer; for(size_t i = 0; i < num_arrays; i++) delete [] mem[i]; delete [] mem; return true; }
void Exciter::ProcessNoise(const uint8_t flags, float* out, size_t size) { while (size--) { *out++ = RandomSample() - 0.5f; } }