Mozzi  version v1.1.0 sound synthesis library for Arduino
mozzi_utils.cpp
1 #include "mozzi_utils.h"
2
3 /** @ingroup util
4 Given a power of 2, work out the number to shift right by to do a divide by the number, or shift left to multiply.
5 @param a power of 2, or any other number for that matter
6 @return the number of trailing zeros on the right hand end
7 */
8
9 /* NOTE: previous version is super-nifty, but pulls in software float code on AVR (the platform where it makes a difference), leading to bloat and a compile time warning.
10  * Sine the only use-case I could find works on a compile-time constant (template-parameter), I added a constexpr function in mozzi_utils.h, instead. I renamed it, too,
11  * so, we'll learn about any use-case outside of this scope. If there are no reports of breakage, the following can probably be removed for good.
12 long trailingZeros(unsigned long v) {
13  // find the number of trailing zeros in v, from http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightFloatCast
14  // there are faster methods on the bit twiddling site, but this is short
15  float f = (float)(v & -v); // cast the least significant bit in v to a float
16  return (*(uint32_t *)&f >> 23) - 0x7f;
17 }
18
19 Here's an alternate, trivial version:
20 uint8_t trailingZeros(uint16_t v) {
21  uint8_t ret = 0;
22  while ((v % 2) == 0) {
23  v = v >> 1;
24  ++ret;
25  }
26  return ret;
27 } */
28
29
30  /** Convert BPM to milliseconds, which can be used to set the delay between beats for Metronome.
31  @param bpm beats per minute
32  */
33  unsigned int BPMtoMillis(float bpm){
34  float seconds_per_beat = 60.f/bpm;
35  return (unsigned int) (seconds_per_beat*1000);
36  }
unsigned int BPMtoMillis(float bpm)
Given a power of 2, work out the number to shift right by to do a divide by the number, or shift left to multiply.
Definition: mozzi_utils.cpp:33