【发布时间】:2011-08-15 03:56:23
【问题描述】:
如何使用 gnuplot 将音频文件(如 aiff)转换为 svg?我使用 sox(声音交换)将 .aiff 转换为 .dat,现在我可以将其加载到 gnuplot 中。
我做了类似的事情:
set terminal svg
set output "test.svg"
plot "test.dat"
我得到一个 svg 文件,但只有点 / 或很多 x。 我怎么能把这些点联系起来?
【问题讨论】:
如何使用 gnuplot 将音频文件(如 aiff)转换为 svg?我使用 sox(声音交换)将 .aiff 转换为 .dat,现在我可以将其加载到 gnuplot 中。
我做了类似的事情:
set terminal svg
set output "test.svg"
plot "test.dat"
我得到一个 svg 文件,但只有点 / 或很多 x。 我怎么能把这些点联系起来?
【问题讨论】:
只是想记录一下——好吧,我一直在寻找 Linux 命令行音频波形查看器,它可以从命令行调用,以原始二进制文件作为输入,其中可以在命令行上指定数据。
Audacity 可以导入原始数据,但只能从 GUI(无法通过其命令行选项指定原始数据文件格式);而像gwave、gtkwave 或Gaw - Gtk Analog Wave viewer 这样的波形查看器可以读取正确的.wav 或基于SPICE 的格式。
感谢answer by @Thor,现在我知道我可以使用gnuplot 来达到目的。这是一个示例命令行,它将原始二进制数据解释为 16 位立体声:
gnuplot -p -e "set terminal x11 ; set multiplot layout 2,1 ; plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:1 with lines ls 1; plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:2 with lines ls 1 ; unset multiplot"
...或分成几行:
gnuplot -p -e "set terminal x11 ; set multiplot layout 2,1 ; \
plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:1 with lines ls 1; \
plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:2 with lines ls 1; \
unset multiplot"
注意:
"< ..." - 如果你有一个文件(如上),不要使用管道(否则获得权限被拒绝)'%int16%int16' 格式将导致字节流被“分组”为 2 个字节,代表列(通道)1,接下来的 2 个字节作为列(通道)2,接下来的 2 个字节再次作为第 1 列,并且等等...见gnuplot docs_4.2: Binary General - Format(也相关:Gnuplot: How to plot multiple time series from a binary format)plots,一个using 0:1,另一个using 0:2,我们可以得到一个典型的波形渲染(如接受的答案)-一个通道在另一个之上--persist 选项,gnuplot 将退出,而(x11 或wxt)窗口将保留 - 因此,典型的gnuplot 与窗口的交互将 不工作不管怎样,很高兴我找到了这个,这会为我节省不少时间,我想:)
【讨论】:
请注意,您也可以直接绘制二进制数据:
set terminal svg
set output "test.svg"
plot '< sox test.aiff -t s32 -' binary format='%int32' using 0:1 with lines
【讨论】:
要在点之间画线,请使用
plot "test.dat" with lines
或者要保留点标记以及线条,请使用
plot "test.dat" with linespoints
所以你的例子变成了
set terminal svg
set output "test.svg"
plot "test.dat" with lines
更多提示:
不要考虑每个样本:
对于大文件,您可能还会发现使用“every n”仅绘制每第 n 个样本很有用。这将使绘图的生成速度更快,并且还会生成更小(但不太详细)的 svg 文件。
例如
plot "test.dat" every 100 with lines
忽略 .dat 文件头:
如果您的 sox 生成的 .dat 文件有一些介绍性元数据行,例如
; Sample Rate 44100
; Channels 2
您可以添加以下内容让 gnuplot 考虑这些行 cmets 并忽略它们。
set datafile commentschars ";"
这将使您不必预处理 .dat 文件,以便在 gnuplot 阻塞之前删除这些行。
绘制立体声音频的左右声道:
如果您使用的是立体声文件,您可能希望同时查看两个声道。
我们可以使用“multiplot”在共享的 x 轴上布置以下两个图(从左声道到右声道),一个在另一个之上,就像许多声音编辑程序所做的那样。
set multiplot layout 2,1
plot "test.dat" using 1:2 with lines
plot "" using 1:3 with lines
1:2 和 1:3 指示 gnuplot 将 dat 文件的哪些列用作 x 和 y 源。我假设你的 sox 生成的立体声 .dat 文件看起来和我的一样,列 - 1:自第一个样本开始以来的时间 - 2:左声道的归一化样本值 - 3:右声道归一化样本值
示例 sn-p:
10.840113 0.20101929 0.17840576
10.840136 0.26062012 0.14831543
10.840159 0.23779297 0.13146973
放在一起: 这是一个将以上所有内容放在一起的脚本。如果您没有立体数据文件来尝试此操作,则需要删除 1:3 的绘图和多绘图设置。
#!/usr/bin/env gnuplot
set datafile commentschars ";"
set terminal svg
set output "test.svg"
set multiplot layout 2,1
plot "test.dat" using 1:2 every 100 with lines
plot "" using 1:3 every 100 with lines
unset multiplot
美化
最后,我调整了演示文稿的脚本(大量借鉴 Philipp K. Janert 的优秀“gnuplot in action”一书):
#!/usr/bin/env gnuplot
set datafile commentschars ";"
set terminal svg
set output "test.svg"
set multiplot layout 2,1
set ylabel "sample value"
set bmargin 0
set format x ""
set ytics -0.8,0.2
set key bottom
plot "test.dat" using 1:2 every 100 with lines lc rgbcolor "#a0a0b0" title "left channel"
set xlabel "time (s)"
set bmargin
set tmargin 0
set format x "%g"
set ytics -1.0,0.2,0.8
set key top
plot "" using 1:3 every 100 with lines lc rgbcolor "#a0a0b0" title "right channel"
unset multiplot
这是一个示例输出(尽管是 png):
如何制作 .dat 文件
对于在家跟随的任何人,您可以使用 sox 通过以下命令从音频文件生成 .dat 文件:
sox input.wav output.dat
大文件警告:即使仅转换 10 秒的 40kHz 立体声音频也会产生 25Mb 的输出文件。
【讨论】:
sox input.wav output.dat。我也有sox input.wav -n stat -freq 输出。我不知道如何处理这些。你的建议对我很有帮助