def test_music_analyser(self): amp=[5.00, 3.88, 4.33, 600.00] peak_freq_finder = SignalPeakFreqFinderV1() peak_freq_finder.call = MagicMock() peak_freq_finder.call.return_value = ([392.00, 493.88, 587.33, 600.00], amp ) sub_signal = Signal() sub_signal.get_length = MagicMock() sub_signal.get_length.return_value = 44100 * 0.2 #sub_signal.get_rate = MagicMock() #sub_signal.get_rate.return_value = 44100 the_signal = Signal() the_signal.get_sub_signal = MagicMock() the_signal.get_sub_signal.return_value = sub_signal the_signal.get_rate = MagicMock() the_signal.get_rate.return_value = 44100 ma = MusicAnalyser(the_signal, SignalSliceGenerator(44100*5, 44100), peak_freq_finder) xt, x, x_name, t = ma.generate_matrix() expected_slice = np.zeros([97]) expected_slice[[55, 59, 62]] = amp self.assertListEqual(xt[:,5].tolist(), expected_slice.tolist())
def pitchogram_from_url(url, max_byte_allowed = 1024*100, timeout = 10, filtered = True): wreader = PartialWaveReader(SimpleWebStreamer(url).set_timeout(timeout)) wreader.set_max_byte_allowed(max_byte_allowed) sample_rate, wave_data = wreader.numpy_read_wav() sig = Signal() sig.set_signal(wave_data, sample_rate) return pitchogram_from_signal(sig, filtered)
def __get_activenotes(self): """从wav文件读取能量信息""" f = wave.open(self.url, "rb") params = f.getparams() nchannels, sampwidth, framerate, nframes = params[:4] str_data = f.readframes(nframes) f.close() wave_data = np.fromstring(str_data, dtype=np.int32) sig = Signal() sig.set_signal(wave_data, framerate) return pitchogram_from_signal(sig, filtered=True)
def analyse_wav_url(url, max_byte_allowed=1024 * 100, timeout=10): wreader = PartialWaveReader(SimpleWebStreamer(url).set_timeout(timeout)) wreader.set_max_byte_allowed(max_byte_allowed) sample_rate, wave_data = wreader.numpy_read_wav() sig = Signal() sig.set_signal(wave_data, sample_rate) return analyse_wav_signal(sig)
def _generate_sample_signal(self, rate, duration, target_freqs, target_amps, noise_std): time_x = np.linspace(0, duration, rate * duration + 1) signal_y = np.zeros(time_x.size) for freq, amp in zip(target_freqs, target_amps) : signal_y = signal_y + np.sin(np.pi * 2 * time_x * 1.0 * freq) * amp signal_y = signal_y + np.random.normal(0, noise_std, signal_y.size) ss = Signal() ss.set_signal(signal_y, rate) return ss
def _generate_sample_signal(self, rate, duration, target_freqs, target_amps, noise_std): time_x = np.linspace(0, duration, rate * duration + 1) signal_y = np.zeros(time_x.size) for freq, amp in zip(target_freqs, target_amps): signal_y = signal_y + np.sin(np.pi * 2 * time_x * 1.0 * freq) * amp signal_y = signal_y + np.random.normal(0, noise_std, signal_y.size) ss = Signal() ss.set_signal(signal_y, rate) return ss
def analyse_wav_url(url, max_byte_allowed = 1024*100, timeout = 10): wreader = PartialWaveReader(SimpleWebStreamer(url).set_timeout(timeout)) wreader.set_max_byte_allowed(max_byte_allowed) sample_rate, wave_data = wreader.numpy_read_wav() sig = Signal() sig.set_signal(wave_data, sample_rate) return analyse_wav_signal(sig)
def test_music_analyser(self): amp = [5.00, 3.88, 4.33, 600.00] peak_freq_finder = SignalPeakFreqFinderV1() peak_freq_finder.call = MagicMock() peak_freq_finder.call.return_value = ([392.00, 493.88, 587.33, 600.00], amp) sub_signal = Signal() sub_signal.get_length = MagicMock() sub_signal.get_length.return_value = 44100 * 0.2 #sub_signal.get_rate = MagicMock() #sub_signal.get_rate.return_value = 44100 the_signal = Signal() the_signal.get_sub_signal = MagicMock() the_signal.get_sub_signal.return_value = sub_signal the_signal.get_rate = MagicMock() the_signal.get_rate.return_value = 44100 ma = MusicAnalyser(the_signal, SignalSliceGenerator(44100 * 5, 44100), peak_freq_finder) xt, x, x_name, t = ma.generate_matrix() expected_slice = np.zeros([97]) expected_slice[[55, 59, 62]] = amp self.assertListEqual(xt[:, 5].tolist(), expected_slice.tolist())
def _sub_test_note_peak_finder(self, rate, duration): target_freqs = [246.94, 293.66, 349.23, 415.30, 493.88] # B D F G# target_amps = [5.0, 4.0, 4.0, 3.0, 4.0] time_x = np.linspace(0, duration, rate * duration + 1) signal_y = np.zeros(time_x.size) for freq, amp in zip(target_freqs, target_amps): signal_y = signal_y + np.sin(np.pi * 2 * time_x * 1.0 * freq) * amp signal_y = signal_y + np.random.normal(0, 1, signal_y.size) ss = Signal() ss.set_signal(signal_y, rate) peaks, amplitude = SignalPeakFreqFinderV1().call(ss) self.assertEqual(len(peaks), len(target_freqs)) result = np.abs(peaks / np.array(target_freqs) - 1) < 0.01 self.assertListEqual(result.tolist(), [True] * len(target_freqs))
def _sub_test_note_peak_finder(self, rate, duration): target_freqs = [246.94, 293.66, 349.23, 415.30, 493.88] # B D F G# target_amps = [5.0, 4.0, 4.0, 3.0, 4.0] time_x = np.linspace(0, duration, rate * duration + 1) signal_y = np.zeros(time_x.size) for freq, amp in zip(target_freqs, target_amps) : signal_y = signal_y + np.sin(np.pi * 2 * time_x * 1.0 * freq) * amp signal_y = signal_y + np.random.normal(0, 1, signal_y.size) ss = Signal() ss.set_signal(signal_y, rate) peaks, amplitude = SignalPeakFreqFinderV1().call(ss) self.assertEqual(len(peaks), len(target_freqs)) result = np.abs(peaks / np.array(target_freqs) -1) < 0.01 self.assertListEqual(result.tolist(), [True]*len(target_freqs))
def test_music_analyser(self): amp=[5.00, 3.88, 4.33] pslice_generator = pitchogram.PitchosliceGenerator() pslice_generator.call = MagicMock() pslice_generator.call.return_value = ([55, 59, 62], amp ) sub_signal = Signal() sub_signal.get_length = MagicMock() sub_signal.get_length.return_value = 44100 * 0.2 the_signal = Signal() the_signal.get_sub_signal = MagicMock() the_signal.get_sub_signal.return_value = sub_signal the_signal.get_rate = MagicMock() the_signal.get_rate.return_value = 44100 pg = pitchogram.PitchogramGenerator(the_signal, SignalSliceGenerator(44100*5, 44100), pslice_generator) xt, x, x_name, t = pg.generate_matrix() expected_slice = np.zeros([97]) expected_slice[[55, 59, 62]] = amp self.assertListEqual(xt[:,5].tolist(), expected_slice.tolist())
def test_music_analyser(self): amp = [5.00, 3.88, 4.33] pslice_generator = pitchogram.PitchosliceGenerator() pslice_generator.call = MagicMock() pslice_generator.call.return_value = ([55, 59, 62], amp) sub_signal = Signal() sub_signal.get_length = MagicMock() sub_signal.get_length.return_value = 44100 * 0.2 the_signal = Signal() the_signal.get_sub_signal = MagicMock() the_signal.get_sub_signal.return_value = sub_signal the_signal.get_rate = MagicMock() the_signal.get_rate.return_value = 44100 pg = pitchogram.PitchogramGenerator( the_signal, SignalSliceGenerator(44100 * 5, 44100), pslice_generator) xt, x, x_name, t = pg.generate_matrix() expected_slice = np.zeros([97]) expected_slice[[55, 59, 62]] = amp self.assertListEqual(xt[:, 5].tolist(), expected_slice.tolist())
def analyse_wav_file(file_name): sig = Signal() sig.load_file(file_name) return analyse_wav_signal(sig)