Mozzi  version v1.1.0
sound synthesis library for Arduino
Metronome.h
1 /*
2  * Metronome.h
3  *
4  * Copyright 2012 Tim Barrass.
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 
12 #ifndef METRO_H_
13 #define METRO_H_
14 
15 #include "EventDelay.h"
16 
17 /** A metronome class which is like an EventDelay which retriggers itself when the delay time is up, to produce a repeating beat.
18 Metronome can be set() to a number of milliseconds, then after calling start(), ready() will return true when the time is up.
19 Alternatively, start(milliseconds) will call set() and start() together.
20 This is called Metronome to avoid conflict with the Arduino Metro library.
21 */
22 class Metronome: public EventDelay
23 {
24 
25 public:
26 
27  /** Constructor.
28  Declare a Metronome object.
29  @param delay_milliseconds how long between each occasion when ready() returns true.
30  */
31  Metronome(unsigned int delay_milliseconds = 0): EventDelay(delay_milliseconds), stopped(false) {
32  }
33 
34 
35  /** Start the metronome.
36  @todo have a parameter to set whether it's single or repeating, so start doesn't have to be called for repeats.
37  Pro: simpler user programming. Con: would require an if..then every time ready() is called.
38  */
39  inline
40  void start()
41  {
42  deadline=audioTicks()+ticks;
43  stopped = false;
44  }
45 
46 
47  /** Set the time between beats and start the metronome.
48  @param delay_milliseconds delay time in milliseconds.
49  */
50  inline
51  void start(unsigned int delay_milliseconds)
52  {
53  set(delay_milliseconds);
54  start();
55  }
56 
57 
58 
59  /** Set the beats per minute.
60  @param bpm beats per minute
61  */
62  inline
63  void setBPM(float bpm)
64  {
65  set((unsigned int) (60000.f/bpm));
66  }
67 
68 
69 
70 
71  /** Call this in updateControl() or updateAudio() to check if it is time for a beat.
72  @return true if the time for one is up.
73  */
74  inline
75  bool ready()
76  {
77  unsigned long now = audioTicks();
78  if ((now<deadline) || stopped) return false;
79 
80  deadline=now-(now-deadline)+ticks; // subtract overrun so the timing doesn't slip
81  return true;
82  }
83 
84 
85  inline
86  void stop(){
87  stopped = true;
88  }
89 
90 private:
91  bool stopped;
92 };
93 
94 
95 
96 
97 /**
98 @example 02.Control/Metronome_SampleHuffman/Metronome_SampleHuffman.ino
99 This example shows how to use the Metronome class.
100 */
101 
102 #endif /* METRO_H_ */
Metronome(unsigned int delay_milliseconds=0)
Constructor.
Definition: Metronome.h:31
EventDelay(unsigned int delay_milliseconds=0)
Constructor.
Definition: EventDelay.h:29
void setBPM(float bpm)
Set the beats per minute.
Definition: Metronome.h:63
bool ready()
Call this in updateControl() or updateAudio() to check if it is time for a beat.
Definition: Metronome.h:75
A metronome class which is like an EventDelay which retriggers itself when the delay time is up...
Definition: Metronome.h:22
void start(unsigned int delay_milliseconds)
Set the time between beats and start the metronome.
Definition: Metronome.h:51
A non-blocking replacement for Arduino&#39;s delay() function.
Definition: EventDelay.h:20
void start()
Start the metronome.
Definition: Metronome.h:40
void set(unsigned int delay_milliseconds)
Set the delay time.
Definition: EventDelay.h:40