GitHub issue #6239: Load local audio data doesn't work
State: closed | Created: 2023-09-13T22:30:01Z

### Describe the bug

I get a RuntimeError from the following code:

```python
 audio_dataset = Dataset.from_dict({"audio": ["/kaggle/input/bengaliai-speech/train_mp3s/000005f3362c.mp3"]}).cast_column("audio", Audio())
 audio_dataset[0]
 ```
 
 ### Traceback

<details>

```python
RuntimeError                              Traceback (most recent call last)
Cell In[33], line 1
----> 1 train_dataset[0]

File /opt/conda/lib/python3.10/site-packages/datasets/arrow_dataset.py:1764, in Dataset.__getitem__(self, key)
   1762 def __getitem__(self, key):  # noqa: F811
   1763     """Can be used to index columns (by string names) or rows (by integer index or iterable of indices or bools)."""
-> 1764     return self._getitem(
   1765         key,
   1766     )

File /opt/conda/lib/python3.10/site-packages/datasets/arrow_dataset.py:1749, in Dataset._getitem(self, key, decoded, **kwargs)
   1747 formatter = get_formatter(format_type, features=self.features, decoded=decoded, **format_kwargs)
   1748 pa_subtable = query_table(self._data, key, indices=self._indices if self._indices is not None else None)
-> 1749 formatted_output = format_table(
   1750     pa_subtable, key, formatter=formatter, format_columns=format_columns, output_all_columns=output_all_columns
   1751 )
   1752 return formatted_output

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:532, in format_table(table, key, formatter, format_columns, output_all_columns)
    530 python_formatter = PythonFormatter(features=None)
    531 if format_columns is None:
--> 532     return formatter(pa_table, query_type=query_type)
    533 elif query_type == "column":
    534     if key in format_columns:

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:281, in Formatter.__call__(self, pa_table, query_type)
    279 def __call__(self, pa_table: pa.Table, query_type: str) -> Union[RowFormat, ColumnFormat, BatchFormat]:
    280     if query_type == "row":
--> 281         return self.format_row(pa_table)
    282     elif query_type == "column":
    283         return self.format_column(pa_table)

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:312, in PythonFormatter.format_row(self, pa_table)
    310 row = self.python_arrow_extractor().extract_row(pa_table)
    311 if self.decoded:
--> 312     row = self.python_features_decoder.decode_row(row)
    313 return row

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:221, in PythonFeaturesDecoder.decode_row(self, row)
    220 def decode_row(self, row: dict) -> dict:
--> 221     return self.features.decode_example(row) if self.features else row

File /opt/conda/lib/python3.10/site-packages/datasets/features/features.py:1386, in Features.decode_example(self, example)
   1376 def decode_example(self, example: dict):
   1377     """Decode example with custom feature decoding.
   1378 
   1379     Args:
   (...)
   1383         :obj:`dict[str, Any]`
   1384     """
-> 1386     return {
   1387         column_name: decode_nested_example(feature, value)
   1388         if self._column_requires_decoding[column_name]
   1389         else value
   1390         for column_name, (feature, value) in zip_dict(
   1391             {key: value for key, value in self.items() if key in example}, example
   1392         )
   1393     }

File /opt/conda/lib/python3.10/site-packages/datasets/features/features.py:1387, in <dictcomp>(.0)
   1376 def decode_example(self, example: dict):
   1377     """Decode example with custom feature decoding.
   1378 
   1379     Args:
   (...)
   1383         :obj:`dict[str, Any]`
   1384     """
   1386     return {
-> 1387         column_name: decode_nested_example(feature, value)
   1388         if self._column_requires_decoding[column_name]
   1389         else value
   1390         for column_name, (feature, value) in zip_dict(
   1391             {key: value for key, value in self.items() if key in example}, example
   1392         )
   1393     }

File /opt/conda/lib/python3.10/site-packages/datasets/features/features.py:1087, in decode_nested_example(schema, obj)
   1085 # Object with special decoding:
   1086 elif isinstance(schema, (Audio, Image)):
-> 1087     return schema.decode_example(obj) if obj is not None else None
   1088 return obj

File /opt/conda/lib/python3.10/site-packages/datasets/features/audio.py:103, in Audio.decode_example(self, value)
    101     raise ValueError(f"An audio sample should have one of 'path' or 'bytes' but both are None in {value}.")
    102 elif path is not None and path.endswith("mp3"):
--> 103     array, sampling_rate = self._decode_mp3(file if file else path)
    104 elif path is not None and path.endswith("opus"):
    105     if file:

File /opt/conda/lib/python3.10/site-packages/datasets/features/audio.py:241, in Audio._decode_mp3(self, path_or_file)
    238 except RuntimeError as err:
    239     raise ImportError("To support decoding 'mp3' audio files, please install 'sox'.") from err
--> 241 array, sampling_rate = torchaudio.load(path_or_file, format="mp3")
    242 if self.sampling_rate and self.sampling_rate != sampling_rate:
    243     if not hasattr(self, "_resampler") or self._resampler.orig_freq != sampling_rate:

File /opt/conda/lib/python3.10/site-packages/torchaudio/backend/sox_io_backend.py:256, in load(filepath, frame_offset, num_frames, normalize, channels_first, format)
    254 if ret is not None:
    255     return ret
--> 256 return _fallback_load(filepath, frame_offset, num_frames, normalize, channels_first, format)

File /opt/conda/lib/python3.10/site-packages/torchaudio/backend/sox_io_backend.py:30, in _fail_load(filepath, frame_offset, num_frames, normalize, channels_first, format)
     22 def _fail_load(
     23     filepath: str,
     24     frame_offset: int = 0,
   (...)
     28     format: Optional[str] = None,
     29 ) -> Tuple[torch.Tensor, int]:
---> 30     raise RuntimeError("Failed to load audio from {}".format(filepath))

RuntimeError: Failed to load audio from /kaggle/input/bengaliai-speech/train_mp3s/000005f3362c.mp3
```

</details>


### Steps to reproduce the bug

1. - Create a custom dataset using Local files of type mp3.
3. - Try to read the first audio item.

### Expected behavior

Expected output
```python
audio_dataset[0]["audio"]
{'array': array([ 0.        ,  0.00024414, -0.00024414, ..., -0.00024414,
         0.        ,  0.        ], dtype=float32),
 'path': 'path/to/audio_1',
 'sampling_rate': 16000}
 ```

### Environment info

N/A