Skip to content

Commit

Permalink
Adding tests for cues
Browse files Browse the repository at this point in the history
  • Loading branch information
iluvcapra committed Nov 7, 2023
1 parent b87f4e1 commit f963daa
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
39 changes: 39 additions & 0 deletions tests/test_cue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest import TestCase
from glob import glob

import wavinfo

class TestCue(TestCase):
def setUp(self) -> None:
self.test_files = glob("tests/test_files/cue_chunks/*.wav")
return super().setUp()

def test_encoding_fallback(self):
file = "tests/test_files/cue_chunks/izotoperx_cues_test.wav"
w = wavinfo.WavInfoReader(file, info_encoding='utf-8')
expected = ("Лорем ипсум долор сит амет, тимеам вивендум хас ет, "
"цу адолесценс дефинитионес еам.")

note = [n for n in w.cues.notes if n.name == 3]
self.assertEqual(len(note), 1)
self.assertEqual(note[0].text, expected)

def test_label(self):
file = "tests/test_files/cue_chunks/izotoperx_cues_test.wav"
w = wavinfo.WavInfoReader(file)

self.assertIsNotNone(w.cues)

self.assertEqual(len(w.cues.labels), 3)
for label in w.cues.labels:
if label.name == 1:
self.assertEqual(label.text, "Marker 1")
elif label.name == 2:
self.assertEqual(label.text, "Marker 2")
elif label.name == 3:
self.assertEqual(label.text, "Marker 3")
else:
self.fail(f"Encountered unexpected label id {label.name}")

Check warning on line 36 in tests/test_cue.py

View check run for this annotation

Codecov / codecov/patch

tests/test_cue.py#L36

Added line #L36 was not covered by tests



Binary file not shown.
16 changes: 13 additions & 3 deletions wavinfo/wave_cues_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class LabelEntry(NamedTuple):
@classmethod
def read(cls, data: bytes, encoding: str):
return cls(name=unpack("<I", data[0:4])[0],
text=data[4:].decode(encoding))
text=data[4:].decode(encoding).rstrip("\0"))


NoteEntry = LabelEntry
Expand Down Expand Up @@ -166,12 +166,14 @@ class WavCuesReader:
cues: List[CueEntry]
labels: List[LabelEntry]
ranges: List[RangeLabel]
notes: List[NoteEntry]

@classmethod
def merge(cls, f,
cues: Optional[ChunkDescriptor],
labls: List[ChunkDescriptor],
ltxts: List[ChunkDescriptor],
notes: List[ChunkDescriptor],
fallback_encoding: str) -> 'WavCuesReader':

cue_list = []
Expand Down Expand Up @@ -200,13 +202,21 @@ def merge(cls, f,
fallback_encoding=fallback_encoding)
)

note_list = []
for note in notes:
note_list.append(
NoteEntry.read(note.read_data(f),
encoding=fallback_encoding)
)

return WavCuesReader(cues=cue_list, labels=label_list,
ranges=range_list)
ranges=range_list, notes=note_list)

def to_dict(self) -> Dict[str, Any]:
return dict(cues=[c.__dict__ for c in self.cues],
labels=[l.__dict__ for l in self.labels],
ranges=[r.__dict__ for r in self.ranges])
ranges=[r.__dict__ for r in self.ranges],
notes=[n.__dict__ for n in self.notes])



Expand Down
8 changes: 5 additions & 3 deletions wavinfo/wave_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ def _get_cue(self, f):
adtl = self._find_list_chunk(b'adtl')
labls = []
ltxts = []
notes = []
if adtl is not None:
labls = [child for child in adtl.children if child.ident == b'labl']
ltxts = [child for child in adtl.children if child.ident == b'ltxt']
labls = [c for c in adtl.children if c.ident == b'labl']
ltxts = [c for c in adtl.children if c.ident == b'ltxt']
notes = [c for c in adtl.children if c.ident == b'note']

return WavCuesReader.merge(f, cue, labls, ltxts,
return WavCuesReader.merge(f, cue, labls, ltxts, notes,
fallback_encoding=self.info_encoding)

def walk(self) -> Generator[str,str,Any]: #FIXME: this should probably be named "iter()"
Expand Down

0 comments on commit f963daa

Please sign in to comment.