(
content: str,
lengths: list[int],
n_vq: int,
gen_slot_token: str,
delay_slot_token: str,
audio_start_token: str,
audio_end_token: str,
)
| 136 | |
| 137 | |
| 138 | def _replace_audio_placeholders( |
| 139 | content: str, |
| 140 | lengths: list[int], |
| 141 | n_vq: int, |
| 142 | gen_slot_token: str, |
| 143 | delay_slot_token: str, |
| 144 | audio_start_token: str, |
| 145 | audio_end_token: str, |
| 146 | ) -> str: |
| 147 | num_ph = content.count(AUDIO_PLACEHOLDER) |
| 148 | if num_ph != len(lengths): |
| 149 | raise ValueError( |
| 150 | f"Placeholder count ({num_ph}) != lengths count ({len(lengths)})" |
| 151 | ) |
| 152 | |
| 153 | lengths_iter = iter(lengths) |
| 154 | |
| 155 | def _build_block(length: int) -> str: |
| 156 | if length == 0: |
| 157 | return f"{audio_start_token}{audio_end_token}" |
| 158 | step_tokens = gen_slot_token * length + delay_slot_token * (n_vq - 1) |
| 159 | return f"{audio_start_token}{step_tokens}{audio_end_token}" |
| 160 | |
| 161 | def replacer(match: re.Match) -> str: |
| 162 | return _build_block(next(lengths_iter)) |
| 163 | |
| 164 | return re.sub(re.escape(AUDIO_PLACEHOLDER), replacer, content) |
| 165 | |
| 166 | |
| 167 | def _get_unified_codes( |
no outgoing calls
no test coverage detected