Exemple #1
0
    def test_py_c_equal(self): 
        audio, sampling_rate, onsets = modal.get_audio_file('piano_G2.wav')
        audio = audio[0:4096]
        frame_size = 512
        hop_size = 512
        py_odf = LPSpectralDifferenceODF()
        py_odf.set_frame_size(frame_size)
        py_odf.set_hop_size(hop_size)
        py_odf.set_order(self.order)
        c_odf = CLPSpectralDifferenceODF()
        c_odf.set_frame_size(frame_size)
        c_odf.set_hop_size(hop_size)
        c_odf.set_order(self.order)
        # if necessary, pad the input signal
        if len(audio) % hop_size != 0:
            audio = np.hstack((audio, np.zeros(hop_size - (len(audio) % hop_size),
                                               dtype=np.double)))
        # get odf samples
        odf_size = len(audio) / hop_size
        py_samples = np.zeros(odf_size, dtype=np.double)
        c_samples = np.zeros(odf_size, dtype=np.double)
        py_odf.process(audio, py_samples)
        c_odf.process(audio, c_samples)

        assert len(py_samples) == len(c_samples)
        for i in range(len(py_samples)):
            assert_almost_equals(py_samples[i], c_samples[i],
                                 places=self.FLOAT_PRECISION)
Exemple #2
0
    def test_find_peaks(self):
        audio, sampling_rate, onsets = modal.get_audio_file('piano_G2.wav')
        frame_size = 1024
        window = np.hanning(frame_size)
        frame = audio[0:frame_size] * window
        spectrum = np.fft.rfft(frame)

        pd = mq.MQPeakDetection(self.max_peaks, sampling_rate, frame_size)
        py_peaks = pd.find_peaks(spectrum)
        py_peaks = [p.bin_number for p in py_peaks]

        mq_params = cdf.MQParameters()
        mq_params.max_peaks = self.max_peaks
        mq_params.frame_size = frame_size
        mq_params.num_bins = int(frame_size / 2) + 1
        mq_params.peak_threshold = 0.1
        mq_params.matching_interval = 100.0
        mq_params.fundamental = float(sampling_rate / frame_size)
        cdf.init_mq(mq_params)
        c_peaks = cdf.find_peaks(audio[0:frame_size], mq_params)
        cdf.destroy_mq(mq_params)

        num_peaks = 0
        current_peak = c_peaks
        while current_peak:
            num_peaks += 1
            current_peak = current_peak.next
        assert len(py_peaks) == num_peaks

        current_peak = c_peaks
        for i in range(len(py_peaks)):
            assert py_peaks[i] == current_peak.peak.bin
            current_peak = current_peak.next
        cdf.delete_peak_list(c_peaks)
Exemple #3
0
 def test_py_c_equal_rt(self):
     audio, sampling_rate, onsets = modal.get_audio_file('piano_G2.wav')
     audio = audio[0:4096]
     frame_size = 256
     hop_size = 256
     py_odf = EnergyODF()
     py_odf.set_frame_size(frame_size)
     py_odf.set_hop_size(hop_size)
     c_odf = CEnergyODF()
     c_odf.set_frame_size(frame_size)
     c_odf.set_hop_size(hop_size)
     # if necessary, pad the input signal
     if len(audio) % hop_size != 0:
         audio = np.hstack((
             audio, np.zeros(hop_size - (len(audio) % hop_size),
                             dtype=np.double)
         ))
     # get odf samples
     audio_pos = 0
     while audio_pos <= len(audio) - frame_size:
         frame = audio[audio_pos:audio_pos + frame_size]
         py_odf_value = py_odf.process_frame(frame)
         c_odf_value = c_odf.process_frame(frame)
         assert_almost_equals(py_odf_value, c_odf_value,
                              places=self.FLOAT_PRECISION)
         audio_pos += hop_size
Exemple #4
0
    def test_py_c_equal(self):
        audio, sampling_rate, onsets = modal.get_audio_file('piano_G2.wav')
        audio = audio[0:4096]
        frame_size = 512
        hop_size = 512
        py_odf = LPEnergyODF()
        py_odf.set_frame_size(frame_size)
        py_odf.set_hop_size(hop_size)
        py_odf.set_order(self.order)
        c_odf = CLPEnergyODF()
        c_odf.set_frame_size(frame_size)
        c_odf.set_hop_size(hop_size)
        c_odf.set_order(self.order)
        # if necessary, pad the input signal
        if len(audio) % hop_size != 0:
            audio = np.hstack((audio,
                               np.zeros(hop_size - (len(audio) % hop_size),
                                        dtype=np.double)))
        # get odf samples
        odf_size = len(audio) / hop_size
        py_samples = np.zeros(odf_size, dtype=np.double)
        c_samples = np.zeros(odf_size, dtype=np.double)
        py_odf.process(audio, py_samples)
        c_odf.process(audio, c_samples)

        assert len(py_samples) == len(c_samples)
        for i in range(len(py_samples)):
            assert_almost_equals(py_samples[i],
                                 c_samples[i],
                                 places=self.FLOAT_PRECISION)
Exemple #5
0
 def test_py_c_equal_rt(self):
     audio, sampling_rate, onsets = modal.get_audio_file('piano_G2.wav')
     audio = audio[0:4096]
     frame_size = 256
     hop_size = 256
     py_odf = EnergyODF()
     py_odf.set_frame_size(frame_size)
     py_odf.set_hop_size(hop_size)
     c_odf = CEnergyODF()
     c_odf.set_frame_size(frame_size)
     c_odf.set_hop_size(hop_size)
     # if necessary, pad the input signal
     if len(audio) % hop_size != 0:
         audio = np.hstack((audio,
                            np.zeros(hop_size - (len(audio) % hop_size),
                                     dtype=np.double)))
     # get odf samples
     audio_pos = 0
     while audio_pos <= len(audio) - frame_size:
         frame = audio[audio_pos:audio_pos + frame_size]
         py_odf_value = py_odf.process_frame(frame)
         c_odf_value = c_odf.process_frame(frame)
         assert_almost_equals(py_odf_value,
                              c_odf_value,
                              places=self.FLOAT_PRECISION)
         audio_pos += hop_size
Exemple #6
0
    def test_track_peaks(self):
        audio, sampling_rate, onsets = modal.get_audio_file('piano_G2.wav')
        frame_size = 1024
        hop_size = 512
        num_frames = 9
        window = np.hanning(frame_size)

        pd = mq.MQPeakDetection(self.max_peaks, sampling_rate, frame_size)
        pt = mq.MQPartialTracking(self.max_peaks)

        mq_params = cdf.MQParameters()
        mq_params.max_peaks = self.max_peaks
        mq_params.frame_size = frame_size
        mq_params.num_bins = int(frame_size / 2) + 1
        mq_params.peak_threshold = 0.1
        mq_params.matching_interval = 200.0
        mq_params.fundamental = float(sampling_rate / frame_size)
        cdf.init_mq(mq_params)

        for i in range(num_frames):
            frame = audio[i * hop_size:(i * hop_size) + frame_size]
            spectrum = np.fft.rfft(frame * window)

            py_peaks = pd.find_peaks(spectrum)
            py_partial = pt.track_peaks(py_peaks)

            c_peaks = cdf.find_peaks(frame, mq_params)
            c_partial = cdf.track_peaks(c_peaks, mq_params)
            current_peak = c_partial

            current_peak = c_partial
            for peak in py_partial:
                if peak.prev_peak:
                    assert_almost_equals(peak.prev_peak.frequency,
                                         current_peak.peak.prev.frequency,
                                         places=self.FLOAT_PRECISION)
                else:
                    assert current_peak.peak.prev == None
                assert_almost_equals(peak.frequency,
                                     current_peak.peak.frequency,
                                     places=self.FLOAT_PRECISION)
                current_peak = current_peak.next

        cdf.destroy_mq(mq_params)