Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read .sph files in Python

Tags:

python

audio

I am working on a project where I need to extract the Mel-Cepstral Frequency Coefficients (MFCC) from audio signals. The first step for this process is to read the audio file into Python.

The audio files I have are stored in a .sph format. I am unable to find a method to read these files directly into Python. I would like to have the sampling rate, and a NumPy array with the data, similar to how wav read works.

Since the audio files I will be dealing with are large in size, I would prefer not to convert to .wav format for reading. Could you please suggest a possible method to do so?

like image 580
therainmaker Avatar asked May 26 '15 05:05

therainmaker


2 Answers

I was against converting to a .wav file as I assumed it would take a lot of time. That is not the case. So, converting using SoX suited my needs.

The following script when run in a windows folder converts all the files in that folder to a .wav file.

cd %~dp0
for %%a in (*.sph) do sox "%%~a" "%%~na.wav"
pause

After this, the following command can be used to read the file.

import scipy.io.wavfile as wav
(rate,sig) = wav.read("file.wav")
like image 92
therainmaker Avatar answered Oct 13 '22 13:10

therainmaker


Based on The answer of ben, I was able to read a .sph file with librosa, as it can read everything that audioread and ffmpeg can read.

import librosa
import librosa.display # You need this in librosa to be able to plot

import matplotlib.pyplot as plt

clip_dir = os.path.join("..","babel","LDC2016S10.sph") 
audio,sr = librosa.load(clip_dir,sr=16000) # audio is a numpy array

fig, ax = plt.subplots(figsize=(15,8))
librosa.display.waveplot(audio, sr=sr, ax=ax) 
ax.set(title="LDC2016S10.sph waveform")

Waveform of the .sph file

like image 22
Oguz Avatar answered Oct 13 '22 14:10

Oguz