Read and write raw PCM using GStreamer

本文介绍了如何利用GStreamer管道进行PCM音频的编码与解码操作。具体包括将WAV文件转换为PCM格式、批量转换多个WAV文件、将PCM转换回WAV格式、播放PCM文件以及使用xxd工具创建PCM数据的C数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Embedded developers have a frequent need to encode or decode PCM audio. In this post I show some GStreamer pipelines that can help with that task.

Convert WAV to PCM

1
gst-launch filesrc location=file.wav ! wavparse ! audioresample ! audioconvert ! audio/x-raw-int, rate=8000, channels=1, endianness=4321, width=16, depth=16, signed=true ! filesink location=file.pcm

For bulk conversion

1
ls *.wav | xargs -i -n 1 gst-launch filesrc location='{}' ! wavparse ! audioresample ! audioconvert ! audio/x-raw-int, rate=8000, channels=1, endianness=4321, width=16, depth=16, signed=true ! filesink location='{}'.pcm

Convert PCM to WAV

1
gst-launch filesrc location=file.pcm ! audio/x-raw-int, rate=8000, channels=1, endianness=4321, width=16, depth=16, signed=true ! audioconvert ! audio/x-raw-int, rate=8000, channels=1, endianness=1234, width=16, depth=16, signed=true ! wavenc ! filesink location=file.wav

Play PCM

1
gst-launch filesrc location=file.pcm ! audio/x-raw-int, rate=8000, channels=1, endianness=4321, width=16, depth=16, signed=true ! pulsesink

Use xxd to create C array of PCM data

1
xxd -i file.pcm > voice.c


转自:https://delog.wordpress.com/2011/10/04/read-and-write-raw-pcm-using-gstreamer/

### 使用 GStreamerPCM 转换为 WAV 的方法 GStreamer 是一个功能强大的多媒体框架,支持多种音频和视频格式的处理。以下是使用 GStreamerPCM 格式转换为 WAV 格式的示例[^1]。 #### 示例命令 以下是一个基于 `gst-launch-1.0` 的命令行示例,展示如何将 PCM 数据转换为 WAV 文件: ```bash gst-launch-1.0 filesrc location=input.pcm ! capsfilter caps="audio/x-raw, format=S16LE, rate=44100, channels=2" ! audioconvert ! wavenc ! filesink location=output.wav ``` - `filesrc location=input.pcm`:指定输入文件为 PCM 格式的 `input.pcm`。 - `capsfilter caps="audio/x-raw, format=S16LE, rate=44100, channels=2"`:定义 PCM 数据的属性,包括采样率为 44100 Hz、位深为 16 位小端 (S16LE) 和双声道。 - `audioconvert`:用于确保音频数据格式与后续处理模块兼容。 - `wavenc`:将音频数据编码为 WAV 格式。 - `filesink location=output.wav`:将输出保存为 `output.wav` 文件。 #### Python 示例代码 如果需要通过编程方式实现 PCM 到 WAV 的转换,可以使用 GStreamer 的 Python 绑定(PyGObject)。以下是一个 Python 示例代码: ```python import gi gi.require_version('Gst', '1.0') from gi.repository import Gst # 初始化 GStreamer Gst.init(None) # 创建管道 pipeline = Gst.parse_launch( "filesrc location=input.pcm ! " "capsfilter caps=\"audio/x-raw, format=S16LE, rate=44100, channels=2\" ! " "audioconvert ! wavenc ! filesink location=output.wav" ) # 开始播放 pipeline.set_state(Gst.State.PLAYING) # 等待管道完成 bus = pipeline.get_bus() msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE, Gst.MessageType.ERROR | Gst.MessageType.EOS) # 停止管道 pipeline.set_state(Gst.State.NULL) ``` - `Gst.parse_launch`:解析并创建 GStreamer 管道。 - `pipeline.set_state(Gst.State.PLAYING)`:启动管道以执行转换操作。 - `bus.timed_pop_filtered`:等待管道完成或出现错误。 - `pipeline.set_state(Gst.State.NULL)`:停止并释放管道资源。 #### 注意事项 - 在使用上述命令或代码时,请确保输入 PCM 文件的实际参数(如采样率、位深和声道数)与 `capsfilter` 中定义的参数一致[^1]。 - 如果 PCM 文件的参数不同,需根据实际情况调整 `capsfilter` 的设置。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值