def test_audio_array(document, comm): data = np.random.randint(-100, 100, 100).astype('int16') sample_rate = 10 buffer = BytesIO() wavfile.write(buffer, sample_rate, data) b64_encoded = b64encode(buffer.getvalue()).decode('utf-8') audio = Audio(data, sample_rate=sample_rate) model = audio.get_root(document, comm=comm) model_value = model.value assert model_value.split(',')[1] == b64_encoded assert model.value.startswith('data:audio/wav;base64')
def test_numpy_audio(document, comm): sps = 8000 # Samples per second duration = 0.01 # Duration in seconds modulator_frequency = 2.0 carrier_frequency = 120.0 modulation_index = 2.0 time = np.arange(sps * duration) / sps modulator = np.sin( 2.0 * np.pi * modulator_frequency * time) * modulation_index waveform = np.sin(2. * np.pi * (carrier_frequency * time + modulator)) waveform_quiet = waveform * 0.3 waveform_int = np.int16(waveform_quiet * 32767) audio = Audio(waveform_int, sample_rate=sps) model = audio.get_root(document, comm=comm) assert model.value == 'data:audio/wav;base64,UklGRsQAAABXQVZFZm10IBAAAAABAAEAQB8AAIA+AAACABAAZGF0YaAAAAAAAF4ErQjgDOgQuBRDGH0bXB7WIOMifCScJT8mYyYHJi0l1yMLIs0fJR0dGr4WFBMqDw4LzQZ1Ahf+vfl59VfxZu2z6UrmN+OD4DjeXNz42g7aotm22UjaWNvi3ODeTOEe5E3nzuqU7pXywvYO+2v/yAMZCFAMXhA1FMoXDxv7HYMgoCJJJHolLyZlJhwmVSURJFciKiCTHZoaSBeqE8oP' # noqa
def test_audio_url(document, comm): audio_url = 'http://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3' audio = Audio(audio_url) model = audio.get_root(document, comm=comm) assert audio_url == model.value
def test_local_audio(document, comm): audio = Audio(str(ASSETS / 'mp3.mp3')) model = audio.get_root(document, comm=comm) assert model.value == 'data:audio/mp3;base64,/+MYxAAAAANIAAAAAExBTUUzLjk4LjIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' # noqa
def test_audio_autoplay(document, comm): audio_url = 'http://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3' audio = Audio(audio_url, autoplay=True) model = audio.get_root(document, comm=comm) assert model.autoplay