Commit 221d36f5 authored by Tanguy Le Carrour's avatar Tanguy Le Carrour
Browse files

Fix pydub infrastructure.

parent 7b773488
from pydub import AudioSegment
from pydub import AudioSegment # type: ignore
from pydub.exceptions import CouldntDecodeError # type: ignore
from splito.domain.audio_editing.model import Track, Tracks
from splito.domain.audio_editing.model import Track, Tracks, Interval
class PydubTrack(Track):
......@@ -8,11 +9,21 @@ class PydubTrack(Track):
def create(cls, a_path: str) -> Track:
try:
return cls(AudioSegment.from_file(a_path))
except FileNotFoundError:
except (FileNotFoundError, CouldntDecodeError):
raise KeyError()
def __init__(self, a_pydub_object: AudioSegment) -> None:
self.__segment = a_pydub_object
def __init__(self, a_segment: AudioSegment) -> None:
self.__segment = a_segment
def __eq__(self, other: object) -> bool:
if not isinstance(other, PydubTrack):
return NotImplemented
return bool(self.__segment.raw_data == other.__segment.raw_data)
def extract(self, an_interval: Interval) -> "Track":
return PydubTrack(
self.__segment[an_interval.start * 1000 : an_interval.end * 1000]
)
def export(self, path: str) -> None:
self.__segment.export(path)
......
from mamba import describe, it, before # type: ignore
from robber import expect # type: ignore
from splito.domain.audio_editing.model import Interval
from splito.infrastructure.pydub.tracks import PydubTrack, PydubTracks
......@@ -8,11 +9,29 @@ with describe(PydubTrack):
with describe(PydubTrack.create):
with it("return a track object"):
track = PydubTrack.create("data/sample.mp3")
with it("returns a track object"):
track = PydubTrack.create("data/a_test_track.mp3")
expect(track).to.be.instanceof(PydubTrack)
with describe(PydubTrack.__eq__):
with it("returns true if the tracks are the same"):
track1 = PydubTrack.create("data/a_test_track.mp3")
track2 = PydubTrack.create("data/a_test_track.mp3")
expect(track1 == track2).to.be.true()
with describe(PydubTrack.extract):
with it("returns an extract of the original track"):
track = PydubTrack.create("data/a_test_track.mp3")
extract = track.extract(Interval(0, 10))
# Do not check what pydub does, just what our wrapper does!
expect(extract).to.be.instanceof(PydubTrack)
with describe(PydubTrack.export):
with it("is tested within PydubTracks.__setitem__"):
......@@ -27,7 +46,7 @@ with describe(PydubTracks):
with describe(PydubTracks.__getitem__):
with it("loads a track from disk"):
track = self.tracks["data/sample.mp3"]
track = self.tracks["data/a_test_track.mp3"]
expect(track).to.be.instanceof(PydubTrack)
......@@ -41,11 +60,9 @@ with describe(PydubTracks):
with describe(PydubTracks.__setitem__):
with it("saves a track to disk"):
track = PydubTrack.create("data/sample.mp3")
track = PydubTrack.create("data/a_test_track.mp3")
def action() -> None:
self.tracks["data/a_new_track.mp3"] = track
action()
expect(action).not_to.throw(Exception)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment