Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
metronome.hpp
00001 #pragma once 00002 00003 #include "mbed.h" 00004 00005 class metronome 00006 { 00007 public: 00008 enum { beat_samples = 3 }; 00009 00010 public: 00011 metronome() 00012 : m_timing(false), m_beat_count(0) {} 00013 ~metronome() {} 00014 00015 public: 00016 // Call when entering "learn" mode 00017 void start_timing() { 00018 m_timing = true; 00019 m_beat_count = 0; 00020 m_timer.reset(); 00021 m_timer.start(); 00022 } 00023 00024 // Call when leaving "learn" mode 00025 void stop_timing() { 00026 m_timing = false; 00027 m_timer.stop(); 00028 } 00029 00030 // Should only record the current time when timing 00031 // Insert the time at the next free position of m_beats 00032 void tap() { 00033 m_timer.stop(); 00034 if( m_beat_count < beat_samples ) { 00035 m_beats[m_beat_count] = m_timer.read_ms(); 00036 m_beat_count++; 00037 } else { 00038 for(int i = 0; i < beat_samples - 1; i++) { 00039 m_beats[i] = m_beats[i + 1]; 00040 } 00041 m_beats[beat_samples - 1] = m_timer.read_ms(); 00042 } 00043 m_timer.reset(); 00044 m_timer.start(); 00045 00046 } 00047 00048 bool is_timing() const { return m_timing; } 00049 00050 float get_delay() const { 00051 if (m_beat_count < beat_samples - 1) { 00052 return 0; 00053 } else { 00054 float sum = 0.0f; 00055 for(int i = 0; i < beat_samples; i++) { 00056 sum += m_beats[i]; 00057 } 00058 return (float) (sum / beat_samples); 00059 } 00060 } 00061 00062 size_t get_bpm() const { 00063 return (size_t) 60000 / get_delay(); 00064 } 00065 00066 private: 00067 bool m_timing; 00068 Timer m_timer; 00069 00070 // Insert new samples at the end of the array, removing the oldest 00071 size_t m_beats[beat_samples]; 00072 size_t m_beat_count; 00073 };
Generated on Tue Jul 12 2022 16:04:09 by
1.7.2