Skip to content

Commit

Permalink
Add testARealCase()
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviliz committed May 14, 2024
1 parent a6cce40 commit 3e9ce14
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion test/src/unittests/tonal/test_audio2pitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def runTest(

frames = FrameGenerator(signal, frameSize=frameSize, hopSize=hopsize)
pitchDetect = Audio2Pitch(frameSize=frameSize, sampleRate=sample_rate)
pitch, confidence, loudness, voiced = ([] for _ in range(4))
n_outputs = len(pitchDetect.outputNames())
pitch, confidence, loudness, voiced = ([] for _ in range(n_outputs))
for frame in frames:
f, conf, l, v = pitchDetect(frame)
pitch += [f]
Expand Down Expand Up @@ -140,6 +141,51 @@ def testInvalidParam(self):
{"sampleRate": 44100, "loudnessAlgorithm": "ebur128"},
)

def testARealCase(self):
# The expected values were recomputed from commit
# 2d37c0713fb6cc5f637b3d8f5d65aa90b36d4277
#
# The expeted values were compared with the vamp pYIN
# implementation of the YIN algorithm producing very
# similar values.
#
# https://code.soundsoftware.ac.uk/projects/pyin

frameSize = 1024
sample_rate = 44100
hopSize = 512
filename = join(testdata.audio_dir, "recorded", "vignesh.wav")
if sys.platform == "darwin":
import soundfile as sf

audio, _ = sf.read(filename, dtype="float32")
else:
audio = MonoLoader(filename=filename, sampleRate=44100)()
frames = FrameGenerator(audio, frameSize=frameSize, hopSize=hopSize)
pitchDetect = Audio2Pitch(
frameSize=frameSize,
sampleRate=sample_rate,
pitchConfidenceThreshold=0.15,
loudnessThreshold=0.0001,
)

n_outputs = len(pitchDetect.outputNames())
pitch, confidence, loudness, voiced = ([] for _ in range(n_outputs))
for frame in frames:
f, conf, l, v = pitchDetect(frame)
pitch += [f]
confidence += [conf]
loudness += [l]
voiced += [v]
expected_pitch = numpy.load(join(filedir(), "pitchyinfft/vignesh_pitch.npy"))
expected_conf = numpy.load(
join(filedir(), "pitchyinfft/vignesh_confidence.npy")
)
expected_voiced = [1] * len(expected_pitch)
self.assertAlmostEqualVector(pitch, expected_pitch, 1e-6)
self.assertAlmostEqualVector(confidence, expected_conf, 5e-5)
self.assertAlmostEqualVector(voiced, expected_voiced)


suite = allTests(TestAudio2Pitch)

Expand Down

0 comments on commit 3e9ce14

Please sign in to comment.