MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / sample_point_indices

Function sample_point_indices

src/framework/debug/trace.cpp:166–221  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

164 oss << "]";
165 return oss.str();
166}
167
168std::vector<size_t> sample_point_indices(size_t count, size_t target) {
169 if (count == 0) {
170 return {};
171 }
172 if (count <= target) {
173 std::vector<size_t> all(count);
174 for (size_t i = 0; i < count; ++i) {
175 all[i] = i;
176 }
177 return all;
178 }
179
180 const size_t first_count = 14;
181 const size_t middle_count = 12;
182 const size_t last_count = 14;
183 const size_t first_end = count / 3;
184 const size_t middle_end = (count * 2) / 3;
185
186 auto append_range = [](std::vector<size_t> & dst, size_t begin, size_t end, size_t samples) {
187 if (begin >= end || samples == 0) {
188 return;
189 }
190 const size_t span = end - begin;
191 if (span <= samples) {
192 for (size_t i = begin; i < end; ++i) {
193 if (dst.empty() || dst.back() != i) {
194 dst.push_back(i);
195 }
196 }
197 return;
198 }
199 for (size_t i = 0; i < samples; ++i) {
200 const double pos = samples == 1 ? 0.0 : static_cast<double>(i) / static_cast<double>(samples - 1);
201 const size_t offset = static_cast<size_t>(pos * static_cast<double>(span - 1));
202 const size_t index = begin + offset;
203 if (dst.empty() || dst.back() != index) {
204 dst.push_back(index);
205 }
206 }
207 };
208
209 std::vector<size_t> points;
210 points.reserve(target);
211 append_range(points, 0, first_end, first_count);
212 append_range(points, first_end, middle_end, middle_count);
213 append_range(points, middle_end, count, last_count);
214
215 if (points.empty() || points.front() != 0) {
216 points.insert(points.begin(), 0);
217 }
218 if (points.back() != count - 1) {
219 points.push_back(count - 1);
220 }
221 points.erase(std::unique(points.begin(), points.end()), points.end());
222 return points;
223}

Callers 2

sample_points_f32Function · 0.85
sample_points_i32Function · 0.85

Calls 4

emptyMethod · 0.45
beginMethod · 0.45
eraseMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected