| 34 | """ |
| 35 | |
| 36 | def __init__(self, uri, ctx=cpu(0), sample_rate=-1, mono=True): |
| 37 | self._handle = None |
| 38 | assert isinstance(ctx, DECORDContext) |
| 39 | is_mono = 1 if mono else 0 |
| 40 | if hasattr(uri, 'read'): |
| 41 | ba = bytearray(uri.read()) |
| 42 | uri = '{} bytes'.format(len(ba)) |
| 43 | self._handle = _CAPI_AudioReaderGetAudioReader( |
| 44 | ba, ctx.device_type, ctx.device_id, sample_rate, 2, is_mono) |
| 45 | else: |
| 46 | self._handle = _CAPI_AudioReaderGetAudioReader( |
| 47 | uri, ctx.device_type, ctx.device_id, sample_rate, 0, is_mono) |
| 48 | if self._handle is None: |
| 49 | raise RuntimeError("Error reading " + uri + "...") |
| 50 | self._array = _CAPI_AudioReaderGetNDArray(self._handle) |
| 51 | self._array = self._array.asnumpy() |
| 52 | self._duration = _CAPI_AudioReaderGetDuration(self._handle) |
| 53 | self._num_samples_per_channel = _CAPI_AudioReaderGetNumSamplesPerChannel(self._handle) |
| 54 | self._num_channels = _CAPI_AudioReaderGetNumChannels(self._handle) |
| 55 | self.sample_rate = sample_rate |
| 56 | self._num_padding = None |
| 57 | |
| 58 | def __len__(self): |
| 59 | """Get length of the audio. The length refer to the shape's first dimension. In this case, |