yamnet / test_mp3.py
Luis
test mp3
2f2cd12
# https://tfhub.dev/google/lite-model/yamnet/classification/tflite/1
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import csv
# import matplotlib.pyplot as plt
# from IPython.display import Audio
from scipy.io import wavfile
import scipy
# import soundfile as sf
# import audio2numpy as a2n
import os
import gradio as gr
# import audio2numpy
# import numpy as np
from pydub import AudioSegment
from matplotlib import pyplot as plt
# https://stackoverflow.com/questions/16634128/how-to-extract-the-raw-data-from-a-mp3-file-using-python
# This will open and read the audio file with pydub. Replace the file path with
# your own file.
audio_file = AudioSegment.from_file('miaow_16k.mp3')
# Set up a list for us to dump PCM samples into, and create a 'data' variable
# so we don't need to type audio_file._data again
data = audio_file._data
pcm16_signed_integers = []
# This loop decodes the bytestring into PCM samples.
# The bytestring is a stream of little-endian encoded signed integers.
# This basically just cuts each two-byte sample out of the bytestring, converts
# it to an integer, and appends it to the list of samples.
for sample_index in range(len(data) // 2):
sample = int.from_bytes(data[sample_index * 2:sample_index * 2 + 2], 'little', signed=True)
pcm16_signed_integers.append(sample / 255)
wav_data = np.array([x for x in pcm16_signed_integers])
sample_rate = 16000
if debug: print(f'pcm16_signed_integers: {len(pcm16_signed_integers)}')
# Now plot the samples!
plt.plot(pcm16_signed_integers)
plt.show()