TensorFlow2 study notes[1]

文章目录

audio

  1. it is simple to install tensorflow as follows:
bash 复制代码
pip install torch torchvision torchaudio
  1. tf.audio.decode_wav
    you can condense a 16-bit PCM WAV file into a float tensor.
python 复制代码
import tensorflow as tf

# Read the WAV file as a byte string
contents = tf.io.read_file("audio_file.wav")

# Decode the WAV file
audio, sample_rate = tf.audio.decode_wav(contents, desired_channels=1)

print("Audio shape:", audio.shape)  # [samples, channels]
print("Sample rate:", sample_rate.numpy())  # e.g., 44100 (Hz)
  1. tf.audio.encode_wav achive the data Encoding of audio with the WAV file format.
python 复制代码
import tensorflow as tf
import matplotlib.pyplot as plt

# Generate a simple sine wave (1 second, 440 Hz, mono)
sample_rate = 44100  # 采样率 (44.1 kHz)
frequency = 440.0    # 频率 (440 Hz, A4 音)
duration = 2.0       # 时长 (2 秒)

# 生成时间轴 (0 到 1 秒)
t = tf.linspace(0.0, duration, int(sample_rate * duration))

# 生成 440 Hz 正弦波
audio = tf.sin(2 * 3.141592 * frequency * t)

# 绘制前 5 毫秒的波形(约 2 个周期)
plt.plot(t[:200], audio[:200])  # 44100 Hz / 440 Hz ≈ 100 点/周期
plt.title("440 Hz 正弦波 (采样率 44.1 kHz)")
plt.xlabel("时间 (秒)")
plt.ylabel("振幅")
plt.show()
# Encode to WAV format
audio = tf.expand_dims(audio, axis=-1) 
wav_data = tf.audio.encode_wav(audio, sample_rate)

# Save to a file
tf.io.write_file("test.wav", wav_data)

references

  1. https://tensorflow.google.cn/api_docs/python/tf/all_symbols
  2. deepseek
相关推荐
喝拿铁写前端5 小时前
别再让 AI 直接写页面了:一种更稳的中后台开发方式
前端·人工智能
Swizard6 小时前
别再让你的 Python 傻等了:三分钟带你通过 asyncio 实现性能起飞
python
tongxianchao6 小时前
UPDP: A Unified Progressive Depth Pruner for CNN and Vision Transformer
人工智能·cnn·transformer
塔能物联运维6 小时前
设备边缘计算任务调度卡顿 后来动态分配CPU/GPU资源
人工智能·边缘计算
过期的秋刀鱼!7 小时前
人工智能-深度学习-线性回归
人工智能·深度学习
木头左7 小时前
高级LSTM架构在量化交易中的特殊入参要求与实现
人工智能·rnn·lstm
IE067 小时前
深度学习系列84:使用kokoros生成tts语音
人工智能·深度学习
欧阳天羲7 小时前
#前端开发未来3年(2026-2028)核心趋势与AI应用实践
人工智能·前端框架
IE067 小时前
深度学习系列83:使用outetts
人工智能·深度学习
水中加点糖7 小时前
源码运行RagFlow并实现AI搜索(文搜文档、文搜图、视频理解)与自定义智能体(一)
人工智能·二次开发·ai搜索·文档解析·ai知识库·ragflow·mineru