Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decibel values at specific points in wav file

I am trying to measure the lead in and lead out of a wav file. Preferably the first and last 5 seconds or so. I am basically trying to assign a numerical value that means 'This song has a slow lead in' or 'This song has an abrupt end'.

My thinking has been to get the slope of the dB values, but I can't seem to find a linux command line tool that will give me dB values. I know they can be measured because Audacity has a waveform(db) view.

enter image description here

Basically I'm looking for a way to gather the data points to duplicate this graph so I can get the slope.

EDIT - working in java

like image 312
jgreen Avatar asked Feb 18 '23 14:02

jgreen


2 Answers

I don't know of any command-line tools to do this, but writing a python script with this functionality is fairly simple using scipy libraries.

We can use scipy.io.wavfile to do the file IO, and then calculate the dB values ourselves (note that these won't necessarily be standard dB values, as those will depend on your speakers and volume settings).

First we get the file:

from scipy.io.wavfile import read
samprate, wavdata = read('file.wav')

We then split the file into chunks, where the number of chunks depends on how finely you want to measure the volume:

import numpy as np
chunks = np.array_split(wavdata, numchunks)

Finally, we compute the volume of each chunk:

dbs = [20*log10( sqrt(mean(chunk**2)) ) for chunk in chunks]

where dbs is now a list of dB values (again, not necessarily the true SPL sound levels) for each chunk of your file.

You can also easily split up the data in a different way, using overlapping chunks, etc.

References: - scipy.io.wavfile - dB (SPL)

like image 100
Isaac Avatar answered Feb 21 '23 05:02

Isaac


For those of you trying to use the Python script in @Isaac's answer, I cleaned it up (I don't know Python either but I got it working). Some notes:

  1. You need python 3+ to use the statistics package (see this excellent article for Mac - https://opensource.com/article/19/5/python-3-default-mac)
  2. You need scipy - on a mac it's: pip install scipy
from scipy.io.wavfile import read
samprate, wavdata = read('intro.wav')
import numpy as np
import math
import statistics 
# basically taking a reading every half a second - the size of the data 
# divided by the sample rate gives us 1 second chunks so I chop 
# sample rate in half for half second chunks
chunks = np.array_split(wavdata, wavdata.size/(samprate/2))
dbs = [20*math.log10( math.sqrt(statistics.mean(chunk**2)) ) for chunk in chunks]
print(dbs)
like image 23
William Neely Avatar answered Feb 21 '23 06:02

William Neely