Converting into WAV format

Sure enough, if we want to test our classifier on our private MP3 collection, we would not be able to extract much meaning. This is because MP3 is a lossy music compression format that cuts out parts that the human ear cannot perceive. This is nice for storing because, with MP3, you can fit 10 times as many songs on your device. For our endeavor, however, it is not so nice. For classification, we will have an easier time with WAV files because they can be directly read by the scipy.io.wavfile package. We would, therefore, have to convert our MP3 files, if we want to use them with our classifier.

If don't have a conversion tool nearby, you might want to check out SoX: http://sox.sourceforge.net. It claims to be the Swiss Army knife of sound processing, and we agree with this bold claim.

The GTZAN dataset, however, comes with music files not in MP3 but in AU format, which means we have to convert it file by file. The following snippet is a neat trick that is possible in Jupyter notebooks: it conveniently allows us to run system commands, such as the sox sound converter within a Python environment. We simply prepend the command line with an exclamation mark (!) and use curly brackets to pass on Python expressions:

GENRE_DIR = Path(DATA_DIR) / 'genres' 
# You need to adapt the SOX_PATH accordingly on your system
SOX_PATH = r'C:Program Files (x86)sox-14-4-2'
for au_fn in Path(GENRE_DIR).glob('**/*.au'):
print(au_fn)
!"{SOX_PATH}/sox.exe" {au_fn} {au_fn.with_suffix('.wav')}

Of course, all of this can be done in a normal Linux or Windows shell as well, but will require a bit more shell expertise.

One advantage of having all of our music files in WAV format is that it is directly readable by the SciPy toolkit:

>>> sample_rate, X = scipy.io.wavfile.read(wave_filename)

X now contains the samples and sample_rate is the rate at which they were taken. Let's use that information to peek into some music files to get an idea of what the data looks like.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset