Compile and install libsamplerate and scikits.samplerate in "Python" windows

Source: Internet
Author: User

Librosa Reason

Librosa is a Python package for audio and music processing that I use to make audio feature extraction. However, when using, found that the Librosa.load music files into a time series process, the speed is unbearable, the CPU runs very high, the program seems to be suspended animation state.
Check the official documentation found that by default, Librosa uses scipy.signal for the resampling of audio signals, which is very slow when actually used. For high performance, the official recommendation is to install the libsamplerate and its corresponding Python module scikits.samplerate.
That is, to install under Windows, you need to compile libsamplerate to get the appropriate Lib and DLL files, and then install the Python interface module.
I tried to install it under Linux, and the process was smooth, because using Apt-get can install Libsamplerate, Pip for scikits.samplerate installation, the system can directly find the Libsamplerate compiled lib file.

Although this software is relatively small, but if the reader has encountered the same problem, I hope to help you.

Windows compiler Libsamplerate

Libsamplerate Official description
Compile steps:

  1. Unzip the Libsamplerate package to the C:\libsamplerate-0.1.8 directory
  2. Download the Libsndfile binary installation package for installation
  3. Copy the Libsndfile-1.dll, Libsndfile-1.lib, and libsndfile-1.def files in Libsndfile to the C:\libsamplerate-0.1.8 directory
  4. Copy the include/sndfile.h in the Libsndfile to the C:\libsamplerate-0.1.8 directory
  5. Ensure that the VC compiler's Bin directory is added to the system environment variable
  6. Open a win32\makefile.msvc file, modify
    • Msvcdir= (Your installation directory) \microsoft Visual Studio 11.0\VC
    • The cflags variable is added to the Include folder for Microsoft SDKs, such as my cflags=/nologo/md/w1/gx/o2/zm200/i "$ (msvcdir) \include"/i "C:\Program Fil Es\microsoft sdks\windows\v6.0a\include "/I. /i "Win32"//"src"/"WIN32"//"_usrdll"/yx/fd/zm1000
    • Prog_link_flags variable added to the Lib folder of Microsoft SDKs, my is Prog_link_flags=/nologo/incremental:no/libpath: "$ (Msvcdir) \lib"/ Libpath: "C:\Program Files\Microsoft Sdks\windows\v6.0a\lib"/PDB: "libsamplerate-0.pdb"/machine:i386/exetype: Dynamic
    • Dll_link_flags variable added to the Lib folder of Microsoft SDKs, my is Dll_link_flags=/nologo/dll/incremental:no/libpath: "$ (Msvcdir) \lib"/ Libpath: "C:\Program Files\Microsoft Sdks\windows\v6.0a\lib"/PDB: "libsamplerate-0.pdb"/implib: ". \ Libsamplerate-0.lib "/machine:i386/out:" Libsamplerate-0.dll "
  7. Enter the C:\libsamplerate-0.1.8 directory in command line mode, enter Make.bat to compile, and use make check to test the program
  8. Finally got the libsamplerate-0.dll,libsamplerate-0.lib, combined with the SRC directory below the samplerate.h
Source Installation Scikits.samplerate

If you want to install using PIP, the system may not find the Libsamplerate lib file, causing the installation to fail.
After downloading the scikits.samplerate source package, the Site.cfg file needs to be configured, the Lib file name and directory of the Samplerate library can be installed properly, otherwise it will cause failure.

In the Scikits.samplerate directory, open the Site.cfg file and modify the content to:

[samplerate]library_dirs = c:\libsamplerate-0.1.8libraries = libsamplerate-0

This will allow you to python setup.py install install it.

It is also important to note that after the installation succeeds, find scikits.samplerate in the Python third-party library folder and put the Libsamplerate compiled DLL file in the Scikits\samplerate folder so that it can be used.

Supplemental: Audio resampling

The frequency corresponds to the time axis, and the amplitude corresponds to the level axis. The wave is infinitely smooth, the string can be regarded as a myriad of points, because the storage space is relatively limited, the digital encoding process, the point of the string must be sampled. The sampling process is to extract the frequency value of a point, it is clear that in one second the more points extracted, get more frequency information more abundant, in order to restore the waveform, a vibration, must have 2 points of sampling, the human ear can feel the highest frequency of 20kHz, so to meet the hearing requirements of human ears, It takes at least 40k samples per second, expressed in 40kHz, and this 40kHz is the sample rate.

In order to reduce the computational complexity and storage complexity, the sampling rate conversion technique is necessary, and the audio resampling algorithm can be used to realize the conversion between the arbitrary sampling rate of audio signals.

For an audio signal with a sampling rate of FS and a length of M sampling points x[n], the sample number of this signal can be increased or decreased after resampling by sampling factor p/q. The process can be divided into the following three steps [17]:
(1) on the sampling: in the original signal adjacent two points inserted P-1 0 points, that is, to create a P (M-1) + 1 sampling point signal xu[n], when N=1,2,...,m, Xu[p (n-1) +1] = x[n]; otherwise, xu[n]=0.
(2) Interpolation: Xu[n] and a low-pass interpolation filter h[n] for convolution: xi[n]= Xu[n]h[n].
(3) Under sampling: Every Q-1 point, that is, to create a signal with a Q point xd[n], when N=1,2,...,+1, Xd[n]=xi[1+q (n-1)], the resampling signal y[n]=xd[n]. (the symbol indicates that the integer is taken down)
The different types of resampling algorithms can be divided into the above three processes, the only difference is that the interpolation filter used in step 2 is different.

Precautions:
1. When sampling, the image information is created, so a low-pass filter is required (the linear interpolation itself is a low-pass filter and therefore does not require additional processing).
2. When sampling, the spectrum may be confused, so the low-pass obfuscation filter is used before the next sampling.
3. The resampling algorithm is very time consuming, and using a multiphase filter and some conditions limit can greatly improve the operation speed.

reprint Please indicate the author Jason Ding and its provenance
Gitcafe Blog Home page (http://jasonding1354.gitcafe.io/)
GitHub Blog Home page (http://jasonding1354.github.io/)
CSDN Blog (http://blog.csdn.net/jasonding1354)
Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)
Baidu Search jasonding1354 access to my blog homepage

Compile and install libsamplerate and scikits.samplerate in "Python" windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.