[英]How to get a stereo wav file of the average and maximum volume for each channel? Get the volume in an array on a per second, for example?
有一個代碼:
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'>
我不知道下一步該怎么做...我將很高興找到另一個解決方案:)
不確定第二個問題,但第一個問題...
如果樣本中有一個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
然后寫出該文件,注意將問題從四舍五入到整數。
解決方案:
# 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.