mirror of
https://github.com/flancian/garden.git
synced 2026-08-07 23:16:21 +00:00
autopushed
This commit is contained in:
@@ -6,6 +6,7 @@ import click
|
|||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class AgoraCmd(click.Command):
|
class AgoraCmd(click.Command):
|
||||||
def format_help(self, ctx, formatter):
|
def format_help(self, ctx, formatter):
|
||||||
click.echo("""Usage:
|
click.echo("""Usage:
|
||||||
@@ -27,29 +28,40 @@ class AgoraCmd(click.Command):
|
|||||||
except SystemExit:
|
except SystemExit:
|
||||||
sys.exit(exc.exit_code)
|
sys.exit(exc.exit_code)
|
||||||
|
|
||||||
|
|
||||||
@click.command(cls=AgoraCmd)
|
@click.command(cls=AgoraCmd)
|
||||||
@click.argument('freq', type=click.FLOAT)
|
@click.argument('freq', type=click.FLOAT)
|
||||||
def hz(freq):
|
def hz(freq):
|
||||||
"""Calculates the closest note to input hz :)"""
|
"""Calculates the closest musical note to an input frequency in Hz."""
|
||||||
|
if freq <= 0:
|
||||||
|
click.echo("Frequency must be positive.")
|
||||||
|
return
|
||||||
|
|
||||||
A4 = 440
|
A4 = 440.0
|
||||||
|
|
||||||
# Number of half steps away.
|
# Exact continuous half steps from A4
|
||||||
n = 12 * math.log2(freq / A4)
|
n_exact = 12 * math.log2(freq / A4)
|
||||||
n = round(n)
|
n = round(n_exact)
|
||||||
note_index = n % 12
|
|
||||||
|
# Pitch deviation in cents (100 cents = 1 semitone)
|
||||||
|
cents = round((n_exact - n) * 100)
|
||||||
|
|
||||||
notes = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
|
notes = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
|
||||||
|
note_index = n % 12
|
||||||
note = notes[note_index]
|
note = notes[note_index]
|
||||||
|
|
||||||
octave = 4 + (n // 12)
|
octave = 4 + (n // 12)
|
||||||
if note in ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]:
|
if note in ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]:
|
||||||
# compensate for the fact that octaves are counted from C
|
# compensate for octave starting at C
|
||||||
octave += 1
|
octave += 1
|
||||||
|
|
||||||
click.echo(f"hz({freq}) is {note}{str(octave)}.")
|
note_name = f"{note}{octave}"
|
||||||
# click.echo(f"hz({freq}) is {note}.")
|
cents_str = f" ({'+' if cents > 0 else ''}{cents} cents)" if cents != 0 else " (perfect pitch)"
|
||||||
|
|
||||||
|
click.echo(f"[[hz/{freq}]] is closest to [[{note_name}]] at [[{freq}]] Hz{cents_str}.")
|
||||||
|
click.echo(f"Note class: [[{note}]], Octave: [[octave/{octave}]].")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
hz()
|
hz()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user