Python lib sound

im looking for a sound library for python that can lift me per frame by the volume of the sound file

or software that can perform noise shutter, prefers command line software

thanks!!

+3
source share
4 answers

The snack library can do this. In particular, this library supports WAV, AU, AIFF, MP3, CSL, SD, SMP, and NIST / Sphere files. It can reproduce sound, analyze and filter the power spectrum.

+3
source

Python has a built-in wave module

import wave
import struct
import numpy

# read in the data string   
fin = wave.open("input.wav", "rb")
data_string = fin.readframes(fin.getnframes())
wav_params = fin.getparams()
fin.close()

# convert to volume
unpacked = struct.unpack("%dB"%(len(data_string)), data_string)
unpacked = [x**2 for x in unpacked]
# here the volume
volume = [20 * numpy.log10(numpy.sqrt(i)) for i in unpacked]

noise_level = 40 # 'noise' level  

# filter out values below limit
outstring = ""
for i in range(len(data_string)):
    if volume[i] > noise_level:
        outstring += data_string[i]
    else:
        outstring += "\0"

# write result to new file
fout = wave.open("output.wav", "wb")
fout.setparams(wav_params)
fout.writeframes(outstring)
fout.close()

* . Hat-tip to

+3

pyo .

from pyo import *

s = Server().boot()
s.start()
sf = SfPlayer('input.aif', speed=[1,.5], loop=True)
gt = Gate(sf, thresh=-24, risetime=0.005, falltime=0.01, lookahead=5, mul=.4).out()
0

, , , , .

, 44,100 , 44 100 ( 1000 ), 22,67 . . , Audio - , , , , ..

In any case, if you want 1 ms resolution for this purpose, pydub , the module can be used for this purpose. Not sure if this is necessary.

Code example

from pydub import AudioSegment
from pydub import utils

chunksize = 1 #ms

fname = "C:\\PATH_TO_THE_FILE\\myAudio.wav"
mysong = AudioSegment.from_wav(fname)
myAudioChunks = utils.make_chunks(mysong,chunksize)
#print "myAudioChunks =", myAudioChunks

for audioChunks in myAudioChunks:    
    loudness = audioChunks.dBFS
    print " loudness in dBFS =", loudness  

Above, the volume will be heard in DBFS, if you want the volume in different parameters, here are a few examples.

1) To get the volume in RMS

replace loudness = audioChunks.dBFSwithloudness = audioChunks.rms

2) To get the maximum volume in the sample [that is, the maximum amplitude in any set of samples]

replace loudness = audioChunks.dBFSwithloudness = audioChunks.max

0
source

Source: https://habr.com/ru/post/1711213/


All Articles