mirror of
https://github.com/iluvcapra/wavinfo.git
synced 2025-12-31 08:50:41 +00:00
Adding tests for cues
This commit is contained in:
39
tests/test_cue.py
Normal file
39
tests/test_cue.py
Normal file
@@ -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}")
|
||||
|
||||
|
||||
|
||||
BIN
tests/test_files/cue_chunks/izotoperx_cues_test.wav
Normal file
BIN
tests/test_files/cue_chunks/izotoperx_cues_test.wav
Normal file
Binary file not shown.
@@ -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
|
||||
@@ -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 = []
|
||||
@@ -200,13 +202,21 @@ class WavCuesReader:
|
||||
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])
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -198,11 +198,13 @@ class WavInfoReader:
|
||||
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()"
|
||||
|
||||
Reference in New Issue
Block a user