根据 fs/ch/sampwidth 计算 PCM 时长,避免写死 16k -> 32 bytes/ms。
(pcm_bytes: bytes, fs: int, ch: int = 1, sampwidth: int = 2)
| 139 | |
| 140 | |
| 141 | def _pcm_duration_ms(pcm_bytes: bytes, fs: int, ch: int = 1, sampwidth: int = 2) -> int: |
| 142 | """根据 fs/ch/sampwidth 计算 PCM 时长,避免写死 16k -> 32 bytes/ms。""" |
| 143 | if not pcm_bytes: |
| 144 | return 0 |
| 145 | bytes_per_ms = (fs * ch * sampwidth) / 1000.0 |
| 146 | if bytes_per_ms <= 0: |
| 147 | return 0 |
| 148 | return int(len(pcm_bytes) / bytes_per_ms) |
| 149 | |
| 150 | |
| 151 | def _safe_int(v, default): |
no outgoing calls
no test coverage detected
searching dependent graphs…