Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load FLAC file in python same as scipy or librosa

I would like to feed some flac sound files into a keras model. With wavfiles I can do (contrived example with one audio file used twice)

import scipy.io.wavfile
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

path = 'path/to/file.wav'
_, audio = scipy.io.wavfile.read(path)
dataset = [audio, audio]
x_train = np.array(dataset)
y_train = keras.utils.to_categorical([0, 1], num_classes=2)

model = Sequential()
model.add(Dense(32, activation='relu', input_shape=x_train[0].shape))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=32)

How do I do this with flac files instead?

like image 269
Harry Moreno Avatar asked Jun 11 '18 18:06

Harry Moreno


People also ask

How do I read a FLAC file in Python?

Data can be written to the file using soundfile. write() , or read from the file using soundfile. read() . PySoundFile can open all file formats that libsndfile supports, for example WAV, FLAC, OGG and MAT files.

What does librosa load return?

It returns floating type audio time series.

How do I import an audio file into librosa?

Load an audio file as a floating point time series. Audio will be automatically resampled to the given rate (default sr=22050 ). To preserve the native sampling rate of the file, use sr=None . path to the input file.

Can librosa load mp4?

Since librosa can load MPEG4 audio (when suitable dependencies are installed), I would recommend downloading that, and loading it directly.


1 Answers

The soundfile package can load flac files in a numpy array compatible format

import numpy as np                                                             
import soundfile as sf                                                      
import keras                                                                
from keras.models import Sequential                                         
from keras.layers import Dense, Dropout, Activation                         
from keras.optimizers import SGD                                            

path = 'path/to/file.flac'                                                  
data, samplerate = sf.read(path)                                            
dataset = [data, data]                                                      
x_train = np.array(dataset)                                                 
y_train = keras.utils.to_categorical([0, 1], num_classes=2)                 

model = Sequential()                                                        
model.add(Dense(32, activation='relu', input_shape=x_train[0].shape))       
model.add(Dense(2, activation='softmax'))                                   
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=32)    

forkable sscce https://www.kaggle.com/morenoh149/flac-keras-hello-world

like image 123
Harry Moreno Avatar answered Sep 27 '22 02:09

Harry Moreno