def render_sounds(samples, indices, sound_samples): """Adds sound to samples at the given starting indices.""" result = np.zeros(samples.shape[0]) for onset in indices: # Crop the sound if necessary. if onset + sound_samples.shape[0] > samples.shape[0]: sound_samples = sound_samples[:samples.shape[0] - onset] # Create samples for this sound at the appropriate offset. result[onset:onset + sound_samples.shape[0]] += sound_samples # Normalize the amplitude of the resulting track. max_amplitude = np.max(result) if max_amplitude > 0: result /= max_amplitude # Merge the samples with the provided input track. sound.merge_audio(samples, result)
def test_merge_audio(self): #This merge should succeed, and do nothing note1 = sound.generate_note(1, 1, 440) note2 = numpy.zeros(len(note1)) sound.merge_audio(note2, note1) assert (note2 == note1).all() #This merge should succeed, and do something note3 = sound.generate_note(1, 1, 100) old_note_3 = deepcopy(note3) sound.merge_audio(note3, note1) assert (note3 != old_note_3).all() #This merge should fail and do nothing note4 = sound.generate_note(0.5, 1, 220) old_note_4 = deepcopy(note4) sound.merge_audio(note4, note1) assert (note4 == old_note_4).all()