Skip to content

Instantly share code, notes, and snippets.

@hevertonfreitas
Last active April 20, 2017 14:37
Show Gist options
  • Save hevertonfreitas/8df5dd03a060c9bc61e823a935137ef3 to your computer and use it in GitHub Desktop.
Save hevertonfreitas/8df5dd03a060c9bc61e823a935137ef3 to your computer and use it in GitHub Desktop.
PHP Metronomme
<?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