Skip to content

Commit

Permalink
dealing with empty midi file
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyuehchen committed Nov 26, 2023
1 parent fbed6cd commit f312f21
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions miditok/midi_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ def _tokens_to_ids(
:param tokens: list of tokens (str) to convert.
:return: list of corresponding ids (int).
"""
if len(tokens) == 0:
return []
if isinstance(tokens[0], (list, tuple)):
ids = []
for seq in tokens:
Expand Down Expand Up @@ -1065,6 +1067,8 @@ def _events_to_tokens(
:return: the sequence of corresponding tokens (str).
"""
tokens = []
if len(events) == 0:
return tokens
if isinstance(events[0], list): # multiple vocabularies
for (
multi_event
Expand All @@ -1090,6 +1094,8 @@ def _ids_to_bytes(
:param as_one_str: will return the bytes all concatenated into one string. (default: False)
:return: the tokens converted into strings of unique bytes.
"""
if len(ids) == 0:
return ""
if isinstance(ids[0], list):
return [self._ids_to_bytes(item, as_one_str) for item in ids]
bytes_ = [self._vocab_base_id_to_byte[i] for i in ids]
Expand All @@ -1104,6 +1110,8 @@ def _bytes_to_tokens(
:param as_str: return the events as string objects, otherwise Event objects (default: True)
:return: the sequence of corresponding tokens (str).
"""
if len(bytes_) == 0:
return []
if isinstance(bytes_[0], list): # multiple vocabularies
return [self._bytes_to_tokens(byte_) for byte_ in bytes_]

Expand Down

0 comments on commit f312f21

Please sign in to comment.