Last active
April 20, 2017 14:37
-
-
Save hevertonfreitas/8df5dd03a060c9bc61e823a935137ef3 to your computer and use it in GitHub Desktop.
PHP Metronomme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Metronome | |
* | |
* @see Based on original Java code located at http://rosettacode.org/wiki/Metronome#Java | |
* | |
* @author Heverton Coneglian <[email protected]> | |
*/ | |
class Metronome | |
{ | |
/** | |
* @var float | |
*/ | |
private $bpm; | |
/** | |
* @var int | |
*/ | |
private $measure; | |
/** | |
* @var int | |
*/ | |
private $counter; | |
/** | |
* Metronome constructor. | |
* | |
* @param float $bpm | |
* @param int $measure | |
*/ | |
public function __construct($bpm, $measure) | |
{ | |
$this->bpm = $bpm; | |
$this->measure = $measure; | |
} | |
/** | |
* Starts the metronome | |
* | |
* @return void | |
*/ | |
public function start() | |
{ | |
while (true) { | |
usleep(1000000 * (60 / $this->bpm)); | |
$this->counter++; | |
if ($this->counter % $this->measure == 0) { | |
echo "TICK\n"; | |
} else { | |
echo "TOCK\n"; | |
} | |
} | |
} | |
} | |
$metro = new Metronome(120, 4); | |
$metro->start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment