Mozzi  version 2016-12-11-17:03
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 
22 class Metronome: public EventDelay
23 {
24 
25 public:
26 
31  Metronome(unsigned int delay_milliseconds = 0): EventDelay(delay_milliseconds), stopped(false) {
32  }
33 
34 
39  inline
40  void start()
41  {
42  deadline=audioTicks()+ticks;
43  stopped = false;
44  }
45 
46 
50  inline
51  void start(unsigned int delay_milliseconds)
52  {
53  set(delay_milliseconds);
54  start();
55  }
56 
57 
58 
62  inline
63  void setBPM(float bpm)
64  {
65  set((unsigned int) (60000.f/bpm));
66  }
67 
68 
69 
70 
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 
102 #endif /* METRO_H_ */
Metronome(unsigned int delay_milliseconds=0)
Constructor.
Definition: Metronome.h:31
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 (which is disabled by Mozzi).
Definition: EventDelay.h:20
void start()
Start the metronome.
Definition: Metronome.h:40
unsigned long audioTicks()
An alternative for Arduino time funcitions like micros() which are disabled by Mozzi when it takes ov...
Definition: MozziGuts.cpp:629