|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +""" |
| 8 | +======================================== |
| 9 | +Decoding audio streams with AudioDecoder |
| 10 | +======================================== |
| 11 | +
|
| 12 | +In this example, we'll learn how to decode an audio file using the |
| 13 | +:class:`~torchcodec.decoders.AudioDecoder` class. |
| 14 | +""" |
| 15 | + |
| 16 | +# %% |
| 17 | +# First, a bit of boilerplate: we'll download an audio file from the web and |
| 18 | +# define an audio playing utility. You can ignore that part and jump right |
| 19 | +# below to :ref:`creating_decoder_audio`. |
| 20 | +import requests |
| 21 | +from IPython.display import Audio |
| 22 | + |
| 23 | + |
| 24 | +def play_audio(samples): |
| 25 | + return Audio(samples.data, rate=samples.sample_rate) |
| 26 | + |
| 27 | + |
| 28 | +# Audio source is CC0: https://opengameart.org/content/town-theme-rpg |
| 29 | +# Attribution: cynicmusic.com pixelsphere.org |
| 30 | +url = "https://opengameart.org/sites/default/files/TownTheme.mp3" |
| 31 | +response = requests.get(url, headers={"User-Agent": ""}) |
| 32 | +if response.status_code != 200: |
| 33 | + raise RuntimeError(f"Failed to download video. {response.status_code = }.") |
| 34 | + |
| 35 | +raw_audio_bytes = response.content |
| 36 | + |
| 37 | +# %% |
| 38 | +# .. _creating_decoder_audio: |
| 39 | +# |
| 40 | +# Creating a decoder |
| 41 | +# ------------------ |
| 42 | +# |
| 43 | +# We can now create a decoder from the raw (encoded) audio bytes. You can of |
| 44 | +# course use a local audio file and pass the path as input. You can also decode |
| 45 | +# audio streams from videos! |
| 46 | + |
| 47 | +from torchcodec.decoders import AudioDecoder |
| 48 | + |
| 49 | +decoder = AudioDecoder(raw_audio_bytes) |
| 50 | + |
| 51 | +# %% |
| 52 | +# The has not yet been decoded by the decoder, but we already have access to |
| 53 | +# some metadata via the ``metadata`` attribute which is an |
| 54 | +# :class:`~torchcodec.decoders.AudioStreamMetadata` object. |
| 55 | +print(decoder.metadata) |
| 56 | + |
| 57 | +# %% |
| 58 | +# Decoding samples |
| 59 | +# ---------------- |
| 60 | +# |
| 61 | +# To get decoded samples, we just need to call the |
| 62 | +# :meth:`~torchcodec.decoders.AudioDecoder.get_samples_played_in_range` method, |
| 63 | +# which returns an :class:`~torchcodec.AudioSamples` object: |
| 64 | + |
| 65 | +samples = decoder.get_samples_played_in_range(start_seconds=0) |
| 66 | + |
| 67 | +print(samples) |
| 68 | +play_audio(samples) |
| 69 | + |
| 70 | +# %% |
| 71 | +# The ``.data`` field is a tensor of shape ``(num_channels, num_samples)`` and |
| 72 | +# of float dtype with values in [-1, 1]. |
| 73 | +# |
| 74 | +# The ``.pts_seconds`` field indicates the starting time of the output samples. |
| 75 | +# Here it's 0.025 seconds, even though we asked for samples starting from 0. Not |
| 76 | +# all streams start exactly at 0! This is not a bug in TorchCodec, this is a |
| 77 | +# property of the file that was defined when it was encoded. |
| 78 | +# |
| 79 | +# We only output the *start* of the samples, not the end or the duration. Those can |
| 80 | +# be easily derived from the number of samples and the sample rate: |
| 81 | + |
| 82 | +duration_seconds = samples.data.shape[1] / samples.sample_rate |
| 83 | +print(f"Duration = {int(duration_seconds // 60)}m{int(duration_seconds % 60)}s.") |
| 84 | + |
| 85 | +# %% |
| 86 | +# Specifying a range |
| 87 | +# ------------------ |
| 88 | +# |
| 89 | +# By default, |
| 90 | +# :meth:`~torchcodec.decoders.AudioDecoder.get_samples_played_in_range` decodes |
| 91 | +# the entire audio stream, but we can specify a custom range: |
| 92 | + |
| 93 | +samples = decoder.get_samples_played_in_range(start_seconds=10, stop_seconds=70) |
| 94 | + |
| 95 | +print(samples) |
| 96 | +play_audio(samples) |
| 97 | + |
| 98 | +# %% |
| 99 | +# Custom sample rate |
| 100 | +# ------------------ |
| 101 | +# |
| 102 | +# We can also decode the samples into a desired sample rate using the |
| 103 | +# ``sample_rate`` parameter of :class:`~torchcodec.decoders.AudioDecoder`. The |
| 104 | +# ouput will sound the same, but note that the number of samples greatly |
| 105 | +# increased: |
| 106 | + |
| 107 | +decoder = AudioDecoder(raw_audio_bytes, sample_rate=16_000) |
| 108 | +samples = decoder.get_samples_played_in_range(start_seconds=0) |
| 109 | + |
| 110 | +print(samples) |
| 111 | +play_audio(samples) |
0 commit comments