编辑使用python一个wav文件

编辑使用python一个wav文件

本文介绍了编辑使用python一个wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和WAV文件我有充分的沉默每个字之间(我六角车间检查,沉默再$ P $ 0的psented)

又能怎么样我剪不沉默的声音?

我使用Python编程

感谢


解决方案

Python有一个。你可以用它来打开一个wav文件进行读取和使用`getframes(1)命令,通过帧帧文件走。

 进口波
W = wave.open('beeps.wav','R')
因为我的range():
帧= w.readframes(1)

返回的将是一个字节串在它的十六进制值框架。如果该文件是立体声的结果会是这个样子(4字节):

 '\\ XE2 \\ XFF \\ XE2 \\ XFF

如果其单,它将有一半的数据(2字节):

 '\\ XE2 \\ XFF

每个通道为2个字节长,因为音频是16位。如果是8位,每个通道将只有一个字节。您可以使用 getsampwidth()的方法来确定这一点。此外, getchannels()将确定是否其单声道或立体声。

您可以遍历这些字节,看看他们都等于零,这意味着两个通道都沉默了。在下面的例子中我使用了 ORD()函数的'\\ XE2的十六进制值转换为整数。

 进口波
W = wave.open('beeps.wav','R')
因为我在范围内(w.getnframes()):
    ###读1帧和位置将更新###
    帧= w.readframes(1)    all_zero = TRUE
    对于j的范围(LEN(帧)):
        #检查,如果幅度大于0
        如果ORD(框架[J]。)> 0:
            all_zero =假
            打破    如果all_zero:
        #这里执行您的剪切
        打印沉默的帧发现%s'的%w.tell()
        打印沉默的第二个%s发现'%(w.tell()/ w..getframerate())

有值得注意的是沉默单帧不一定表示空的空间,因为振幅可以越过0马克正常频率。因此,建议在0一定数目的帧的决定之前,如果该区域是被观察到的,事实上,无声

and between each word in the wav file I have full silence (I checked with Hex workshop and silence is represented with 0's)

how can I cut the non-silence sound ?

I'm programming using python

thanks

解决方案

Python has a wav module. You can use it to open a wav file for reading and use the `getframes(1)' command to walk through the file frame by frame.

import wave
w = wave.open('beeps.wav', 'r')
for i in range():
frame = w.readframes(1)

The frame returned will be a byte string with hex values in it. If the file is stereo the result will look something like this (4 bytes):

'\xe2\xff\xe2\xff'

If its mono, it will have half the data (2 bytes):

'\xe2\xff'

Each channel is 2 bytes long because the audio is 16 bit. If is 8 bit, each channel will only be one byte. You can use the getsampwidth() method to determine this. Also, getchannels() will determine if its mono or stereo.

You can loop over these bytes to see if they all equal zero, meaning both channels are silent. In the following example I use the ord() function to convert the '\xe2' hex values to integers.

import wave
w = wave.open('beeps.wav', 'r')
for i in range(w.getnframes()):
    ### read 1 frame and the position will updated ###
    frame = w.readframes(1)

    all_zero = True
    for j in range(len(frame)):
        # check if amplitude is greater than 0
        if ord(frame[j]) > 0:
            all_zero = False
            break

    if all_zero:
        # perform your cut here
        print 'silence found at frame %s' % w.tell()
        print 'silence found at second %s' % (w.tell()/w..getframerate())

It is worth noting that a single frame of silence doesn't necessarily denote empty space since the amplitude may cross the 0 mark normal frequencies. Therefore, it is recommended that a certain number of frames at 0 be observed before deciding if the region is, in fact, silent.

这篇关于编辑使用python一个wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:11