<s> import os <EOL> try : <EOL> os . environ . pop ( '<STR_LIT>' ) <EOL> except KeyError : <EOL> pass <EOL> import matplotlib <EOL> matplotlib . use ( '<STR_LIT>' ) <EOL> import six <EOL> import glob <EOL> import numpy as np <EOL> import scipy . io <EOL> from nose . tools import eq_ , raises <EOL> import warnings <EOL> import librosa <EOL> def files ( pattern ) : <EOL> test_files = glob . glob ( pattern ) <EOL> test_files . sort ( ) <EOL> return test_files <EOL> def load ( infile ) : <EOL> DATA = scipy . io . loadmat ( infile , chars_as_strings = True ) <EOL> return DATA <EOL> def test_hz_to_mel ( ) : <EOL> def __test_to_mel ( infile ) : <EOL> DATA = load ( infile ) <EOL> z = librosa . hz_to_mel ( DATA [ '<STR_LIT:f>' ] , DATA [ '<STR_LIT>' ] ) <EOL> assert np . allclose ( z , DATA [ '<STR_LIT:result>' ] ) <EOL> for infile in files ( '<STR_LIT>' ) : <EOL> yield ( __test_to_mel , infile ) <EOL> pass <EOL> def test_mel_to_hz ( ) : <EOL> def __test_to_hz ( infile ) : <EOL> DATA = load ( infile ) <EOL> z = librosa . mel_to_hz ( DATA [ '<STR_LIT:f>' ] , DATA [ '<STR_LIT>' ] ) <EOL> assert np . allclose ( z , DATA [ '<STR_LIT:result>' ] ) <EOL> for infile in files ( '<STR_LIT>' ) : <EOL> yield ( __test_to_hz , infile ) <EOL> pass <EOL> def test_hz_to_octs ( ) : <EOL> def __test_to_octs ( infile ) : <EOL> DATA = load ( infile ) <EOL> z = librosa . hz_to_octs ( DATA [ '<STR_LIT:f>' ] ) <EOL> assert np . allclose ( z , DATA [ '<STR_LIT:result>' ] ) <EOL> for infile in files ( '<STR_LIT>' ) : <EOL> yield ( __test_to_octs , infile ) <EOL> pass <EOL> def test_melfb ( ) : <EOL> def __test ( infile ) : <EOL> DATA = load ( infile ) <EOL> wts = librosa . filters . mel ( DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> n_mels = DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> fmin = DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> fmax = DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> htk = DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> wts = np . pad ( wts , [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:0> , int ( DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> ] // <NUM_LIT:2> - <NUM_LIT:1> ) ) ] , <EOL> mode = '<STR_LIT>' ) <EOL> eq_ ( wts . shape , DATA [ '<STR_LIT>' ] . shape ) <EOL> assert np . allclose ( wts , DATA [ '<STR_LIT>' ] ) <EOL> for infile in files ( '<STR_LIT>' ) : <EOL> yield ( __test , infile ) <EOL> def test_chromafb ( ) : <EOL> def __test ( infile ) : <EOL> DATA = load ( infile ) <EOL> octwidth = DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> if octwidth == <NUM_LIT:0> : <EOL> octwidth = None <EOL> wts = librosa . filters . chroma ( DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> A440 = DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> ctroct = DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> octwidth = octwidth , <EOL> norm = <NUM_LIT:2> , <EOL> base_c = False ) <EOL> wts = np . pad ( wts , [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:0> , int ( DATA [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] // <NUM_LIT:2> - <NUM_LIT:1> ) ) ] , <EOL> mode = '<STR_LIT>' ) <EOL> eq_ ( wts . shape , DATA [ '<STR_LIT>' ] . shape ) <EOL> assert np . allclose ( wts , DATA [ '<STR_LIT>' ] ) <EOL> for infile in files ( '<STR_LIT>' ) : <EOL> yield ( __test , infile ) <EOL> def test__window ( ) : <EOL> def __test ( n , window ) : <EOL> wdec = librosa . filters . __float_window ( window ) <EOL> if n == int ( n ) : <EOL> n = int ( n ) <EOL> assert np . allclose ( wdec ( n ) , window ( n ) ) <EOL> else : <EOL> wf = wdec ( n ) <EOL> fn = int ( np . floor ( n ) ) <EOL> assert not np . any ( wf [ fn : ] ) <EOL> for n in [ <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] : <EOL> for window_name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> window = getattr ( scipy . signal . windows , window_name ) <EOL> yield __test , n , window <EOL> def test_constant_q ( ) : <EOL> def __test ( sr , fmin , n_bins , bins_per_octave , tuning , filter_scale , <EOL> pad_fft , norm ) : <EOL> F , lengths = librosa . filters . constant_q ( sr , <EOL> fmin = fmin , <EOL> n_bins = n_bins , <EOL> bins_per_octave = bins_per_octave , <EOL> tuning = tuning , <EOL> filter_scale = filter_scale , <EOL> pad_fft = pad_fft , <EOL> norm = norm ) <EOL> assert np . all ( lengths <= F . shape [ <NUM_LIT:1> ] ) <EOL> eq_ ( len ( F ) , n_bins ) <EOL> if not pad_fft : <EOL> return <EOL> eq_ ( np . mod ( np . log2 ( F . shape [ <NUM_LIT:1> ] ) , <NUM_LIT:1.0> ) , <NUM_LIT:0.0> ) <EOL> F_fft = np . abs ( np . fft . fft ( F , axis = <NUM_LIT:1> ) ) <EOL> F_fft = F_fft / np . max ( F_fft , axis = <NUM_LIT:1> , keepdims = True ) <EOL> assert not np . any ( F_fft [ : , - F_fft . shape [ <NUM_LIT:1> ] // <NUM_LIT:2> : ] > <NUM_LIT> ) <EOL> sr = <NUM_LIT> <EOL> yield ( raises ( librosa . ParameterError ) ( __test ) , sr , sr / <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:0> , <NUM_LIT:1> , True , <NUM_LIT:1> ) <EOL> yield ( raises ( librosa . ParameterError ) ( __test ) , sr , - <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:0> , <NUM_LIT:1> , True , <NUM_LIT:1> ) <EOL> yield ( raises ( librosa . ParameterError ) ( __test ) , sr , <NUM_LIT> , <NUM_LIT:1> , - <NUM_LIT:12> , <NUM_LIT:0> , <NUM_LIT:1> , True , <NUM_LIT:1> ) <EOL> yield ( raises ( librosa . ParameterError ) ( __test ) , sr , <NUM_LIT> , - <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:0> , <NUM_LIT:1> , True , <NUM_LIT:1> ) <EOL> yield ( raises ( librosa . ParameterError ) ( __test ) , sr , <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:0> , - <NUM_LIT:1> , True , <NUM_LIT:1> ) <EOL> yield ( raises ( librosa . ParameterError ) ( __test ) , sr , <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:0> , <NUM_LIT:1> , True , - <NUM_LIT:1> ) <EOL> for fmin in [ None , librosa . note_to_hz ( '<STR_LIT>' ) ] : <EOL> for n_bins in [ <NUM_LIT:12> , <NUM_LIT> ] : <EOL> for bins_per_octave in [ <NUM_LIT:12> , <NUM_LIT> ] : <EOL> for tuning in [ <NUM_LIT:0> , <NUM_LIT> ] : <EOL> for filter_scale in [ <NUM_LIT:1> , <NUM_LIT:2> ] : <EOL> for norm in [ <NUM_LIT:1> , <NUM_LIT:2> ] : <EOL> for pad_fft in [ False , True ] : <EOL> yield ( __test , sr , fmin , n_bins , <EOL> bins_per_octave , tuning , <EOL> filter_scale , pad_fft , <EOL> norm ) <EOL> def test_window_bandwidth ( ) : <EOL> eq_ ( librosa . filters . window_bandwidth ( '<STR_LIT>' ) , <EOL> librosa . filters . window_bandwidth ( scipy . signal . hann ) ) <EOL> def test_window_bandwidth_missing ( ) : <EOL> warnings . resetwarnings ( ) <EOL> with warnings . catch_warnings ( record = True ) as out : <EOL> x = librosa . filters . window_bandwidth ( '<STR_LIT>' ) <EOL> eq_ ( x , <NUM_LIT:1> ) <EOL> assert len ( out ) > <NUM_LIT:0> <EOL> assert out [ <NUM_LIT:0> ] . category is UserWarning <EOL> assert '<STR_LIT>' in str ( out [ <NUM_LIT:0> ] . message ) <EOL> def binstr ( m ) : <EOL> out = [ ] <EOL> for row in m : <EOL> line = [ '<STR_LIT:U+0020>' ] * len ( row ) <EOL> for i in np . flatnonzero ( row ) : <EOL> line [ i ] = '<STR_LIT:.>' <EOL> out . append ( '<STR_LIT>' . join ( line ) ) <EOL> return '<STR_LIT:\n>' . join ( out ) <EOL> def test_cq_to_chroma ( ) : <EOL> def __test ( n_bins , bins_per_octave , n_chroma , fmin , base_c , window ) : <EOL> if fmin is None : <EOL> midi_base = <NUM_LIT> <EOL> else : <EOL> midi_base = librosa . hz_to_midi ( fmin ) <EOL> midi_notes = np . linspace ( midi_base , <EOL> midi_base + n_bins * <NUM_LIT> / bins_per_octave , <EOL> endpoint = False , <EOL> num = n_bins ) <EOL> midi_notes = np . around ( midi_notes , decimals = <NUM_LIT:2> ) <EOL> C = np . diag ( midi_notes ) <EOL> cq2chr = librosa . filters . cq_to_chroma ( n_input = C . shape [ <NUM_LIT:0> ] , <EOL> bins_per_octave = bins_per_octave , <EOL> n_chroma = n_chroma , <EOL> fmin = fmin , <EOL> base_c = base_c , <EOL> window = window ) <EOL> chroma = cq2chr . dot ( C ) <EOL> for i in range ( n_chroma ) : <EOL> v = chroma [ i ] [ chroma [ i ] != <NUM_LIT:0> ] <EOL> v = np . around ( v , decimals = <NUM_LIT:2> ) <EOL> if base_c : <EOL> resid = np . mod ( v , <NUM_LIT:12> ) <EOL> else : <EOL> resid = np . mod ( v - <NUM_LIT:9> , <NUM_LIT:12> ) <EOL> resid = np . round ( resid * n_chroma / <NUM_LIT> ) <EOL> assert np . allclose ( np . mod ( i - resid , <NUM_LIT:12> ) , <NUM_LIT:0.0> ) , i - resid <EOL> for n_octaves in [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] : <EOL> for semitones in [ <NUM_LIT:1> , <NUM_LIT:3> ] : <EOL> for n_chroma in <NUM_LIT:12> * np . arange ( <NUM_LIT:1> , <NUM_LIT:1> + semitones ) : <EOL> for fmin in [ None ] + list ( librosa . midi_to_hz ( range ( <NUM_LIT> , <NUM_LIT> ) ) ) : <EOL> for base_c in [ False , True ] : <EOL> for window in [ None , [ <NUM_LIT:1> ] ] : <EOL> bins_per_octave = <NUM_LIT:12> * semitones <EOL> n_bins = n_octaves * bins_per_octave <EOL> if np . mod ( bins_per_octave , n_chroma ) != <NUM_LIT:0> : <EOL> tf = raises ( librosa . ParameterError ) ( __test ) <EOL> else : <EOL> tf = __test <EOL> yield ( tf , n_bins , bins_per_octave , <EOL> n_chroma , fmin , base_c , window ) </s>
