Look up the index of the frequency f in the frequency table. Return the nearest index.
(f)
| 47 | |
| 48 | |
| 49 | def _find_log_index(f): |
| 50 | """Look up the index of the frequency f in the frequency table. |
| 51 | |
| 52 | Return the nearest index. |
| 53 | """ |
| 54 | global _last_asked, _log_cache |
| 55 | (begin, end) = (0, 128) |
| 56 | |
| 57 | # Most calls are sequential, this keeps track of the last value asked for so |
| 58 | # that we need to search much, much less. |
| 59 | if _last_asked is not None: |
| 60 | (lastn, lastval) = _last_asked |
| 61 | if f >= lastval: |
| 62 | if f <= _log_cache[lastn]: |
| 63 | _last_asked = (lastn, f) |
| 64 | return lastn |
| 65 | elif f <= _log_cache[lastn + 1]: |
| 66 | _last_asked = (lastn + 1, f) |
| 67 | return lastn + 1 |
| 68 | begin = lastn |
| 69 | |
| 70 | # Do some range checking |
| 71 | if f > _log_cache[127] or f <= 0: |
| 72 | return 128 |
| 73 | |
| 74 | # Binary search related algorithm to find the index |
| 75 | while begin != end: |
| 76 | n = (begin + end) // 2 |
| 77 | c = _log_cache[n] |
| 78 | cp = _log_cache[n - 1] if n != 0 else 0 |
| 79 | if cp < f <= c: |
| 80 | _last_asked = (n, f) |
| 81 | return n |
| 82 | if f < c: |
| 83 | end = n |
| 84 | else: |
| 85 | begin = n |
| 86 | _last_asked = (begin, f) |
| 87 | return begin |
| 88 | |
| 89 | |
| 90 | def find_frequencies(data, freq=44100, bits=16): |