if num_uttrs < index and index < 0: raise KeyError('Interger index out of range, {:d} vs {:d}'.format(index, num_uttrs))
时间: 2024-03-29 22:37:56 浏览: 17
这行代码的作用是检查索引 `index` 是否越界。如果 `index` 小于 0 或大于等于音频数据的数量(由 `num_uttrs` 表示),则抛出 `KeyError` 异常,提示整数索引越界。其中,`{:d}` 表示将后面的参数格式化为整数,并插入到字符串中。`format` 方法会将这个格式化字符串中的 `{}` 替换为前面传入的参数。
相关问题
def __getitem__(self, index): if type(index) not in [int, str]: raise IndexError('Unsupported index type: {}'.format(type(index))) if type(index) == int: num_uttrs = len(self.keys) if num_uttrs < index and index < 0: raise KeyError('Interger index out of range, {:d} vs {:d}'.format(index, num_uttrs)) index = self.keys[index] if index not in self.index_dict: raise KeyError("Missing utterance {}!".format(index)) return self._load(index)
这是一个类方法,用于支持使用索引或键名获取数据。函数名为 `__getitem__`,接收一个参数 `index`,表示要获取的数据的索引或键名。首先,该函数会判断 `index` 的类型是否为 `int` 或 `str`,如果不是则抛出异常。接着,如果 `index` 是 `int` 类型,则判断它是否超出了范围,并将其转换为相应的键名。如果 `index` 是键名,则判断该键名是否存在于索引字典中,如果不存在则抛出异常。最后,函数返回使用 `_load` 方法读取 `index` 对应的音频数据。
class AudioReader(object): ''' Class that reads Wav format files Input as a different scp file address Output a matrix of wav files in all scp files. ''' def __init__(self, scp_path, sample_rate=8000): super(AudioReader, self).__init__() self.sample_rate = sample_rate self.index_dict = handle_scp(scp_path) self.keys = list(self.index_dict.keys()) def _load(self, key): src, sr = read_wav(self.index_dict[key], return_rate=True) if self.sample_rate is not None and sr != self.sample_rate: raise RuntimeError('SampleRate mismatch: {:d} vs {:d}'.format( sr, self.sample_rate)) return src def __len__(self): return len(self.keys) def __iter__(self): for key in self.keys: yield key, self._load(key) def __getitem__(self, index): if type(index) not in [int, str]: raise IndexError('Unsupported index type: {}'.format(type(index))) if type(index) == int: num_uttrs = len(self.keys) if num_uttrs < index and index < 0: raise KeyError('Interger index out of range, {:d} vs {:d}'.format( index, num_uttrs)) index = self.keys[index] if index not in self.index_dict: raise KeyError("Missing utterance {}!".format(index)) return self._load(index)
这段代码定义了一个 AudioReader 类,用于读取多个 wav 格式的音频文件。构造函数的参数为一个 scp 文件的路径和采样率,默认为 8000。类中定义了 `_load` 函数来读取每个 wav 文件,并进行采样率匹配。另外,类还实现了 `__len__`、`__iter__` 和 `__getitem__` 等函数,以支持对音频文件的遍历和索引访问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)