Skip to content

Commit

Permalink
QA Unit tests: Add test on real Audio for tonal extractor algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
cvf-bcn-gituser committed Feb 27, 2024
1 parent e5f720f commit 2d56ff2
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions test/src/unittests/extractor/test_tonalextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#
# You should have received a copy of the Affero GNU General Public License
# version 3 along with this program. If not, see http://www.gnu.org/licenses/
import os.path

from essentia_test import *
import numpy as np


class TestTonalExtractor(TestCase):

def testEmpty(self):
Expand Down Expand Up @@ -83,20 +85,20 @@ def testRandomInput(self):
self.assertAlmostEqual(result[0], expected_result, 9.999e+02)

def testRegression(self):
frameSizes = [128, 256, 512, 1024, 2048, 4096]
hopSizes = [256, 512, 1024, 2048, 4096, 8192]
frameSizes = [256, 512, 1024, 2048, 4096, 8192]
hopSizes = [128, 256, 512, 1024, 2048, 4096]

input_filename = join(testdata.audio_dir, "recorded", "dubstep.wav") # Replace 'testdata' with actual path
real_audio_input = MonoLoader(filename=input_filename)()

realAudio = MonoLoader(filename=input_filename)()

# Iterate through pairs of frameSize and corresponding hopSize
# TODO: Extend loop to try different tuningFrequency values
for fs, hs in zip(frameSizes, hopSizes):
with self.subTest(frameSize=fs, hopSize=hs):
# Process the algorithm on real audio with the current frameSize and hopSize
te = TonalExtractor()
te.configure(frameSize=fs, hopSize=hs)
chords_changes_rate, _, _, chords_number_rate, _, _, _, _, _, _, _, key_strength= te(real_audio_input)
chords_changes_rate, _, _, chords_number_rate, _, _, _, _, _, _, _, key_strength= te(realAudio)

# Perform assertions on one or more outputs
# Example: Assert that chords_changes_rate is a non-negative scalar
Expand All @@ -108,6 +110,43 @@ def testRegression(self):
self.assertGreaterEqual(key_strength, 0)
# You can add more assertions on other outputs as needed

def testRealAudio(self):

# These reference values could also be compared with th results of tonal extractors of alternative a
# audio libraries (e.g. MadMom, libs fromn Alexander Lerch etc.)
# ccr = chord changes rate ; cnr = chord number rate; ks = key strength
mozart_ccr = 0.03400309011340141
mozart_cnr = 0.010819165036082268
mozart_ks = 0.8412253260612488

vivaldi_ccr = 0.052405908703804016
vivaldi_cnr = 0.004764173645526171
vivaldi_ks = 0.7122617959976196

thresh = 0.5

def test_on_real_audio(path, ccr, cnr, ks):
realAudio = MonoLoader(filename=path)()

# Use default configuration of algorothm
# This function could be extended to test for more outputs
# TODO: Extend to test non-scalar and string outputs:
# i.e. chords_histogram, chords_progression, chords_scale, chords_strength
# hpcp, hpcp_highres, key_key and key_scale
te = TonalExtractor()
chords_changes_rate, _, _, chords_number_rate, _, _, _, _, _, _, _, key_strength= te(realAudio)
self.assertIsInstance(chords_changes_rate, (int, float))
self.assertGreaterEqual(chords_changes_rate, 0)
self.assertAlmostEqual(chords_changes_rate, ccr, thresh)
self.assertIsInstance(chords_number_rate, (int, float))
self.assertGreaterEqual(chords_number_rate, 0)
self.assertAlmostEqual(chords_number_rate, cnr, thresh)
self.assertIsInstance(key_strength, (int, float))
self.assertGreaterEqual(key_strength, 0)
self.assertAlmostEqual(key_strength, ks, thresh)

test_on_real_audio(join(testdata.audio_dir, "recorded", "mozart_c_major_30sec.wav"), mozart_ccr, mozart_cnr, mozart_ks)
test_on_real_audio(join(testdata.audio_dir, "recorded", "Vivaldi_Sonata_5_II_Allegro.wav"), vivaldi_ccr, vivaldi_cnr, vivaldi_ks)

suite = allTests(TestTonalExtractor)

Expand Down

0 comments on commit 2d56ff2

Please sign in to comment.