audio
- it is simple to install tensorflow as follows:
pip install torch torchvision torchaudio
tf.audio.decode_wav
you can condense a 16-bit PCM WAV file into a float tensor.
import tensorflow as tf
contents = tf.io.read_file("audio_file.wav")
audio, sample_rate = tf.audio.decode_wav(contents, desired_channels=1)
print("Audio shape:", audio.shape)
print("Sample rate:", sample_rate.numpy())
tf.audio.encode_wav
achive the data Encoding of audio with the WAV file format.
import tensorflow as tf
import matplotlib.pyplot as plt
sample_rate = 44100
frequency = 440.0
duration = 2.0
t = tf.linspace(0.0, duration, int(sample_rate * duration))
audio = tf.sin(2 * 3.141592 * frequency * t)
plt.plot(t[:200], audio[:200])
plt.title("440 Hz 正弦波 (采样率 44.1 kHz)")
plt.xlabel("时间 (秒)")
plt.ylabel("振幅")
plt.show()
audio = tf.expand_dims(audio, axis=-1)
wav_data = tf.audio.encode_wav(audio, sample_rate)
tf.io.write_file("test.wav", wav_data)

references
- https://tensorflow.google.cn/api_docs/python/tf/all_symbols
- deepseek