Created
March 8, 2023 15:34
-
-
Save gustheman/1ebff43c95579f2866033986785c3bb1 to your computer and use it in GitHub Desktop.
TF to load an audio file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!pip install -q "tensorflow_io==0.28.*" | |
!curl -O https://storage.googleapis.com/audioset/miaow_16k.wav | |
import tensorflow as tf | |
import tensorflow_io as tfio | |
from IPython.display import Audio | |
audio = tfio.audio.AudioIOTensor('miaow_16k.wav') | |
print(audio) | |
print(audio.rate.numpy()) | |
audio_tensor = tf.squeeze(audio.to_tensor(), axis=[-1]) | |
Audio(audio_tensor.numpy(), rate=audio.rate.numpy()) | |
duration = len(audio_tensor) / audio.rate.numpy() | |
print(f'audio duration {duration}') | |
plt.title('waveform') | |
plt.xlabel('Time (samples)') | |
plt.ylabel('Amplitude') | |
plt.plot(audio_tensor.numpy()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment