def test_iter_dataframes_mono_from_function(mono_ttf_gen): fps, frequency, volume = (44100, 110, 0.5) time_to_frame = mono_ttf_gen(fps=fps, frequency=frequency, volume=volume) t_fps = 1 / fps sound = Sound.from_datatimes(time_to_frame, fps=fps).with_duration(0.5) for i, frame in enumerate(sound.iter_dataframes): assert frame == time_to_frame(i * t_fps)
def test_iter_datatimes_mono_from_function(mono_ttf_gen): fps, frequency, volume = (44100, 110, 0.5) time_to_frame = mono_ttf_gen(fps=fps, frequency=frequency, volume=volume) sound = Sound.from_datatimes(time_to_frame, fps=fps).with_duration(0.5) zipped = zip(sound.iter_datatimes, sound.time_sequence) for (id_t, frame), ts_t in zipped: assert id_t == ts_t assert time_to_frame(ts_t) == frame
def test_from_datatimes_mono(mono_ttf_gen): fps, frequency, volume = (44100, 110, 0.5) time_to_frame = mono_ttf_gen(fps=fps, frequency=frequency, volume=volume) sound = Sound.from_datatimes(time_to_frame, fps=fps) assert sound.n_bytes == 2 assert sound.n_frames is None assert sound.duration == math.inf times = [random.uniform(0, 5) for i in range(10)] for t in times: assert time_to_frame(t) == sound.time_to_frame(t)
def test_mono_ttf_gen(mono_ttf_gen, fps, frequency, volume, sample_width): time_to_frame = mono_ttf_gen( fps=fps, frequency=frequency, volume=volume, sample_width=sample_width, ) sound = Sound.from_datatimes(time_to_frame, fps=fps).with_duration(1) assert sound.fps == fps assert sound.n_channels == 1 assert sound.duration == 1 assert sound.dtype is getattr(np, f"int{sample_width << 3}")
def test_save_mono_from_function(tmp_path): fps, frequency, volume = (44100, 110, 0.5) amplitude = np.iinfo(np.int16).max * volume def time_to_frame(t): return (np.sin(frequency * 2 * np.pi * t) * amplitude).astype(np.int16) mono_sound = Sound.from_datatimes(time_to_frame, fps=fps).with_duration(0.5) filename = tmp_path / "save_mono_from_function.wav" mono_sound.save(filename) assert filename.exists() resulting_sound = Sound.from_file(filename) assert np.array_equal(resulting_sound.data, mono_sound.data)
def test_iter_dataframes_stereo_from_function(): fps, frequencies, volume = (44100, (110, 440), 0.5) amplitude, t_fps = (np.iinfo(np.int16).max * volume, 1 / fps) time_to_frame_left = lambda t: (np.sin(frequencies[0] * 2 * np.pi * t) * amplitude).astype(np.int16) time_to_frame_right = lambda t: (np.sin(frequencies[1] * 2 * np.pi * t) * amplitude).astype(np.int16) sound = Sound.from_datatimes( lambda t: [time_to_frame_left(t), time_to_frame_right(t)], fps=fps).with_duration(0.5) for i, frame in enumerate(sound.iter_dataframes): assert frame[0] == time_to_frame_left(i * t_fps) assert frame[1] == time_to_frame_right(i * t_fps)
def test_iter_datatimes_stereo_from_function(): fps, frequencies, volume = (44100, (110, 440), 0.5) amplitude = np.iinfo(np.int16).max * volume time_to_frame_left = lambda t: (np.sin(frequencies[0] * 2 * np.pi * t) * amplitude).astype(np.int16) time_to_frame_right = lambda t: (np.sin(frequencies[1] * 2 * np.pi * t) * amplitude).astype(np.int16) sound = Sound.from_datatimes( lambda t: [time_to_frame_left(t), time_to_frame_right(t)], fps=fps).with_duration(0.5) zipped = zip(sound.iter_datatimes, sound.time_sequence) for (id_t, frame), ts_t in zipped: assert id_t == ts_t assert time_to_frame_left(ts_t) == frame[0] assert time_to_frame_right(ts_t) == frame[1]
def test_save_stereo_from_function(tmp_path): fps, frequencies, volume = (44100, (110, 440), 0.5) amplitude = np.iinfo(np.int16).max * volume time_to_frame_left = lambda t: (np.sin(frequencies[0] * 2 * np.pi * t) * amplitude).astype(np.int16) time_to_frame_right = lambda t: (np.sin(frequencies[1] * 2 * np.pi * t) * amplitude).astype(np.int16) stereo_sound = Sound.from_datatimes( lambda t: [time_to_frame_left(t), time_to_frame_right(t)], fps=fps).with_duration(0.5) filename = tmp_path / "save_stereo_from_function.wav" stereo_sound.save(filename) assert filename.exists() resulting_sound = Sound.from_file(filename) assert np.array_equal(resulting_sound.data, stereo_sound.data)
def test_from_datatimes_stereo(): fps, frequencies, volume = (44100, (110, 440), 0.5) amplitude = np.iinfo(np.int16).max * volume time_to_frame_left = lambda t: (np.sin(frequencies[0] * 2 * np.pi * t) * amplitude).astype(np.int16) time_to_frame_right = lambda t: (np.sin(frequencies[1] * 2 * np.pi * t) * amplitude).astype(np.int16) sound = Sound.from_datatimes( lambda t: [time_to_frame_left(t), time_to_frame_right(t)], fps=fps) assert sound.n_bytes == 2 assert sound.n_frames is None assert sound.duration == math.inf times = [random.uniform(0, 5) for i in range(10)] for t in times: frame = sound.time_to_frame(t) assert frame[0] == time_to_frame_left(t) assert frame[1] == time_to_frame_right(t)