使用 sox stats 批量测量 .wav 文件 [英] Batch measurements of .wav files with sox stats

查看:64
本文介绍了使用 sox stats 批量测量 .wav 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于之前关于get-mean-amplitude-of-wav-from-sox"的问题:

My question is similar to a previous question about "get-mean-amplitude-of-wav-from-sox":

从 sox 获取 .wav 的平均振幅(仅)

我希望能够使用 stats sox 对目录中的 1,000 个 .wav 文件进行批量测量,并将结果存储在数据框或一些类似的结构中,我可以将其另存为 csv 文本文件.

I would like to be able to use the stats sox to do batch measurements of 1,000's of .wav files in a directory, and store the results in a data frame or some similar structure I can save as a csv text file.

对于一个声音文件,代码是:

For one sound file, the code would be:

./sox SampleSound.wav -n stat

./sox SampleSound.wav -n stat

产生以下输出:

Samples read:          72000000
Length (seconds):   3600.000000
Scaled by:         2147483647.0
Maximum amplitude:     0.778809
Minimum amplitude:    -1.000000
Midline amplitude:    -0.110596
Mean    norm:          0.062671
Mean    amplitude:    -0.008131
RMS     amplitude:     0.172914
Maximum delta:         1.778809
Minimum delta:         0.000000
Mean    delta:         0.014475
RMS     delta:         0.057648
Rough   frequency:         1061
Volume adjustment:        1.000

我想:- 对给定目录中的 1,000 个声音文件进行批量测量,- 捕获列中的统计输出以及测量的声音文件名,- 并导出以用作 R 分析中的协变量.

I would like to: - take batch measurements on 1,000's of sound files in a given directory, - capture the stats output in columns along with the sound file name that was measured, - and export for use as covariates in an analysis in R.

谢谢!

马修

推荐答案

首先,您需要对 sox 执行系统调用,并捕获其输出.例如:

First you will need to perform a system call to sox, and capture its output. For example:

> spam = system("sox worf.wav -n stat 2>&1", intern = TRUE)
> spam
 [1] "Samples read:             34000" "Length (seconds):      3.083900"
 [3] "Scaled by:         2147483647.0" "Maximum amplitude:     0.999969"
 [5] "Minimum amplitude:    -0.938721" "Midline amplitude:     0.030624"
 [7] "Mean    norm:          0.190602" "Mean    amplitude:    -0.004302"
 [9] "RMS     amplitude:     0.244978" "Maximum delta:         1.340240"
[11] "Minimum delta:         0.000000" "Mean    delta:         0.051444"
[13] "RMS     delta:         0.099933" "Rough   frequency:          715"
[15] "Volume adjustment:        1.000"

设置 intern = TRUE 将命令的输出返回到一个变量.奇怪的是,sox 将其输出提供给 stderr 而不是 stdout,因此需要 2>&1.现在最好的方法是将其包装在一个函数中,该函数也对 system 的输出进行后处理:

Setting intern = TRUE returns the output of the command to a variable. Strangely, sox provides its output to stderr and not stdout, hence the need for 2>&1. The best way forward now is to wrap this in a function which also postprocesses the output of system:

get_wav_stats = function(wav_file) {
   rough_wav_stats = system(sprintf("sox %s -n stat 2>&1", wav_file), intern = TRUE)
   wav_stats = data.frame(do.call("rbind", strsplit(rough_wav_stats, split = ":")))
   names(wav_stats) = c("variable", "value")
   wav_stats = transform(wav_stats, value = as.numeric(as.character(value)))
   return(wav_stats)
}
> spam = get_wav_stats("worf.wav")
> spam
            variable         value
1       Samples read  3.400000e+04
2   Length (seconds)  3.083900e+00
3          Scaled by  2.147484e+09
4  Maximum amplitude  9.999690e-01
5  Minimum amplitude -9.387210e-01
6  Midline amplitude  3.062400e-02
7       Mean    norm  1.906020e-01
8  Mean    amplitude -4.302000e-03
9  RMS     amplitude  2.449780e-01
10     Maximum delta  1.340240e+00
11     Minimum delta  0.000000e+00
12     Mean    delta  5.144400e-02
13     RMS     delta  9.993300e-02
14 Rough   frequency  7.150000e+02
15 Volume adjustment  1.000000e+00

接下来,您可以将其包装在一个应用循环中以从给定目录中获取所有统计信息:

Next you can wrap this in an apply loop to get all the stats from a given directory:

# files_dir = list.files("path", full.names = TRUE)
# For this example I create a mock list:
files_dir = rep("worf.wav", 10)
stat_wavs = lapply(files_dir, get_wav_stats)
> str(stat_wavs)
    List of 10
     $ :'data.frame':   15 obs. of  2 variables:
      ..$ variable: Factor w/ 15 levels "Length (seconds)",..: 13 1 14 2 8 7 6 4 10 3 ...
      ..$ value   : num [1:15] 3.40e+04 3.08 2.15e+09 1.00 -9.39e-01 ...
     $ :'data.frame':   15 obs. of  2 variables:
      ..$ variable: Factor w/ 15 levels "Length (seconds)",..: 13 1 14 2 8 7 6 4 10 3 ...
      ..$ value   : num [1:15] 3.40e+04 3.08 2.15e+09 1.00 -9.39e-01 ...
<< snip >> 
     $ :'data.frame':   15 obs. of  2 variables:
      ..$ variable: Factor w/ 15 levels "Length (seconds)",..: 13 1 14 2 8 7 6 4 10 3 ...
      ..$ value   : num [1:15] 3.40e+04 3.08 2.15e+09 1.00 -9.39e-01 ...

仅提取包含您需要的统计信息的 value 列:

To extract only the value column, which contains the stats you need:

stats4files = data.frame(do.call("rbind", lapply(stat_wavs, "[[", 2)))
names(stats4files) = stat_wavs[[1]][[1]]
rownames(stats4files) = files_dir # this doesn't work actually because I have repeated the same file multiple times :)

> stats4files
   Samples read Length (seconds)  Scaled by Maximum amplitude Minimum amplitude Midline amplitude
1         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
2         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
3         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
4         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
5         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
6         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
7         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
8         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
9         34000           3.0839 2147483647          0.999969         -0.938721          0.030624
10        34000           3.0839 2147483647          0.999969         -0.938721          0.030624
   Mean    norm Mean    amplitude RMS     amplitude Maximum delta Minimum delta Mean    delta
1      0.190602         -0.004302          0.244978       1.34024             0      0.051444
2      0.190602         -0.004302          0.244978       1.34024             0      0.051444
3      0.190602         -0.004302          0.244978       1.34024             0      0.051444
4      0.190602         -0.004302          0.244978       1.34024             0      0.051444
5      0.190602         -0.004302          0.244978       1.34024             0      0.051444
6      0.190602         -0.004302          0.244978       1.34024             0      0.051444
7      0.190602         -0.004302          0.244978       1.34024             0      0.051444
8      0.190602         -0.004302          0.244978       1.34024             0      0.051444
9      0.190602         -0.004302          0.244978       1.34024             0      0.051444
10     0.190602         -0.004302          0.244978       1.34024             0      0.051444
   RMS     delta Rough   frequency Volume adjustment
1       0.099933               715                 1
2       0.099933               715                 1
3       0.099933               715                 1
4       0.099933               715                 1
5       0.099933               715                 1
6       0.099933               715                 1
7       0.099933               715                 1
8       0.099933               715                 1
9       0.099933               715                 1
10      0.099933               715                 1

这篇关于使用 sox stats 批量测量 .wav 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆