Mozzi  version v2.0
sound synthesis library for Arduino
Metronome.h
1 /*
2  * Metronome.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 
13 #ifndef METRO_H_
14 #define METRO_H_
15 
16 #include "EventDelay.h"
17 
23 class Metronome: public EventDelay
24 {
25 
26 public:
27 
32  Metronome(unsigned int delay_milliseconds = 0): EventDelay(delay_milliseconds), stopped(false) {
33  }
34 
35 
40  inline
41  void start()
42  {
43  deadline=audioTicks()+ticks;
44  stopped = false;
45  }
46 
47 
51  inline
52  void start(unsigned int delay_milliseconds)
53  {
54  set(delay_milliseconds);
55  start();
56  }
57 
58 
59 
63  inline
64  void setBPM(float bpm)
65  {
66  set((unsigned int) (60000.f/bpm));
67  }
68 
69 
70 
71 
75  inline
76  bool ready()
77  {
78  unsigned long now = audioTicks();
79  if ((now<deadline) || stopped) return false;
80 
81  deadline=now-(now-deadline)+ticks; // subtract overrun so the timing doesn't slip
82  return true;
83  }
84 
85 
86  inline
87  void stop(){
88  stopped = true;
89  }
90 
91 private:
92  bool stopped;
93 };
94 
95 
96 
97 
103 #endif /* METRO_H_ */
A non-blocking replacement for Arduino's delay() function.
Definition: EventDelay.h:21
void set(unsigned int delay_milliseconds)
Set the delay time.
Definition: EventDelay.h:40
A metronome class which is like an EventDelay which retriggers itself when the delay time is up,...
Definition: Metronome.h:24
Metronome(unsigned int delay_milliseconds=0)
Constructor.
Definition: Metronome.h:32
void setBPM(float bpm)
Set the beats per minute.
Definition: Metronome.h:64
void start()
Start the metronome.
Definition: Metronome.h:41
void start(unsigned int delay_milliseconds)
Set the time between beats and start the metronome.
Definition: Metronome.h:52
bool ready()
Call this in updateControl() or updateAudio() to check if it is time for a beat.
Definition: Metronome.h:76
unsigned long audioTicks()
An alternative for Arduino time functions like micros() and millis().
Definition: MozziGuts.hpp:301