Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sound lib python [closed]

Tags:

python

im looking for a sound lib for python that can tall me per frame the volume of a sound file

or a software that can do a noise gate,preferd command line software

thanx!!


2 Answers

The snack library can do it. In particular, this library support WAV, AU, AIFF, MP3, CSL, SD, SMP, and NIST/Sphere files. It can play the sound, do power spectrum analysis and filtering.

like image 102
Eolmar Avatar answered May 25 '26 17:05

Eolmar


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's 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()

First-pass attempt.. needs to be optimised for files of any significant size. Hat-tip to this blog post

like image 44
pufferfish Avatar answered May 25 '26 16:05

pufferfish