如何获得每个频道的平均和最大音量的立体声wav文件?例如,每秒获取一个数组中的音量?

时间:2013-10-30 13:23:09

标签: python audio wav

有一个代码:

import wave
import numpy as np
import math

wav = wave.open("music.wav", mode="r")
(nchannels, sampwidth, framerate, nframes, comptype, compname) = wav.getparams()

content = wav.readframes(nframes)
samples = np.fromstring(content, dtype=types[sampwidth])

for n in range(nchannels):
    channel = samples[n::nchannels]
    print channel

结果:

[0 0 0 ..., 0 0 8]
[0 0 0 ..., 0 0 0]

型:

<type 'numpy.ndarray'>
<type 'numpy.ndarray'>

我无法弄清楚接下来要做什么......我会很高兴另一种解决方案:)

2 个答案:

答案 0 :(得分:1)

不确定你的第二个问题,但第一个问题......

如果样本中有nd numpy数组:

samples
array([[   1,    3],
   [   2,    2],
   [   3,    4],
   [   4,    5],
   [   5,  100],
   [   6, 1000],
   [   7,    0],
   [   8,    1]]

mean1 = samples.mean(axis=1)
max1 = samples.max(axis=1)

outputWav = numpy.vstack((mean1,max1)).T

然后写出这个文件,注意从浮点数到整数的舍入问题。

答案 1 :(得分:0)

解决方案:

# first channel
samples_o = samples[0::2]
# second channel
samples_c = samples[1::2]

# for 3 second 24000 = 8000*3
gr_size = len(samples_o) // gr_count 

lst = [lst[i:i+gr_size] for i in range(0, len(samples_o), gr_size)]

agr = []

for array in lst:
  max_el = np.argmax(array, axis=0)
  agr.append(max_el)

print np.mean(agr, axis=0) # avg max volume for first channel