Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docarray/document/mixins/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,20 @@ def load_uri_to_audio_tensor(self: 'T') -> 'T':

:return: Document itself after processed
"""
if self.uri.startswith('http'):
import io
import requests

resp = requests.get(self.uri)
resp.raise_for_status()
file = io.BytesIO()
file.write(resp.content)
file.seek(0)
else:
file = self.uri

with wave.open(
self.uri
file
) as ifile: #: note wave is Python built-in module https://docs.python.org/3/library/wave.html
samples = ifile.getnframes()
audio = ifile.readframes(samples)
Expand Down
24 changes: 14 additions & 10 deletions tests/unit/document/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ def test_video_convert_pipe_key_frame_indices(pytestconfig, tmpdir):
assert d.tags['keyframe_indices'] == [0, 95]


def test_audio_convert_pipe(pytestconfig, tmpdir):
num_d = 0
for d in from_files(f'{cur_dir}/toydata/*.wav'):
fname = str(tmpdir / f'tmp{num_d}.wav')
d.load_uri_to_audio_tensor()
d.tensor = d.tensor[::-1]
d.save_audio_tensor_to_file(fname)
assert os.path.exists(fname)
num_d += 1
assert num_d
@pytest.mark.parametrize(
'file',
[
'https://www.kozco.com/tech/piano2.wav',
f'{cur_dir}/toydata/hello.wav',
f'{cur_dir}/toydata/olleh.wav',
],
)
def test_audio_convert_pipe(file, pytestconfig, tmpdir):
d = Document(uri=file)
d.load_uri_to_audio_tensor()
fname = str(tmpdir / f'tmp.wav')
d.save_audio_tensor_to_file(fname)
assert os.path.exists(fname)


def test_image_convert_pipe(pytestconfig):
Expand Down