Skip to content
Permalink
Browse files

Some cleanup to tune pitch translators, add ntm routine

  • Loading branch information
hecanjog committed Jan 13, 2020
1 parent 48d5335 commit 5a86721cbc8126dae217259cd43f3442b677d674
Showing with 27 additions and 23 deletions.
  1. +4 −1 Makefile
  2. +16 −22 pippi/tune/__init__.py
  3. +7 −0 tests/test_pitches.py
@@ -1,4 +1,4 @@
.PHONY: test test-fft test-soundfont test-grains test-wavesets test-fx test-noise test-shapes test-oscs test-soundbuffer test-lists build
.PHONY: test test-fft test-soundfont test-grains test-wavesets test-fx test-noise test-shapes test-oscs test-soundbuffer test-lists test-pitches build

test:
python -m unittest discover -s tests -p 'test_*.py' -v
@@ -39,6 +39,9 @@ test-soundpipe:
test-lists:
python -m unittest tests/test_lists.py -v

test-pitches:
python -m unittest tests/test_pitches.py -v

clean:
rm -rf build/
rm -rf pippi/*.c
@@ -213,23 +213,24 @@ def ftom(freq):
def ftomi(freq):
return int(round(ftom(freq)))

def extractPitchClass(pitch):
parsed = re.match('([a-zA-Z]#?b?)(\d+)', pitch)

try:
pitch_class = parsed.group(1)
except AttributeError:
return pitch
def parse_pitch_class(note, octave=None):
note = note.lower()
if re.match('[a-zA-Z]#?b?\d+', note) is not None:
parsed = re.match('([a-zA-Z]#?b?)(\d+)', note)
note = parsed.group(1)
octave = int(parsed.group(2))

return pitch_class
if octave is None:
octave = 4

def ptom(pitch):
pitch_class = extractPitchClass(pitch.lower())
register = int(pitch[-1])
midi_note = midi_map[pitch_class]
midi_note += register * 12
return note, octave

return midi_note
def ntm(note, octave=None):
note, octave = parse_pitch_class(note, octave)
note_index = nti(note)
if note_index >= 3:
octave -= 1
return note_index + 21 + (octave * 12)

def edo(degree, divs=12):
return 2**(degree/float(divs))
@@ -250,17 +251,10 @@ def nti(note):
def ntf(note, octave=None, ratios=None):
""" Note to freq
"""
note = note.lower()
if re.match('[a-zA-Z]#?b?\d+', note) is not None:
parsed = re.match('([a-zA-Z]#?b?)(\d+)', note)
note = parsed.group(1)
octave = int(parsed.group(2))

if ratios is None:
ratios = TERRY

if octave is None:
octave = 4
note, octave = parse_pitch_class(note, octave)

note_index = nti(note)
mult = ratios[note_index][0] / ratios[note_index][1]
@@ -0,0 +1,7 @@
from unittest import TestCase
from pippi import tune

class TestPitches(TestCase):
def test_ntm(self):
notes = [57, 58, 59, 60, 61, 62, 63, 64, 65, 66]
self.assertEqual(notes, [tune.ntm(n) for n in ['a3', 'a#3', 'b3', 'c4', 'c#4', 'd4', 'd#4', 'e4', 'f4', 'f#4']])

0 comments on commit 5a86721

Please sign in to comment.
You can’t perform that action at this time.