<blockquote>
<p>In my (limited) understanding this seems to be issue because the train
data has different sizes and hence numpy can't convert it into a
matrix,</p>
</blockquote>
<p>True, that's exactly the issue.</p>
<blockquote>
<p>so how do I fix this? Can I pad it? If so whats the size I should use?
Or is this a mistake on my part?</p>
</blockquote>
<p>There are 2 options I can think of, one is to use a network that supports different size matrices (Option1). Other is to pad with zeros. (Option 2). One can also change the length of the audio with other methods (maintaining or not the pitch) but I have not find any applications of that in any paper so I will not post it as an option.</p>
<p><strong>Option 1: Use a network that can deal with different sizes</strong></p>
<p>Normally, one uses <a href="https://en.wikipedia.org/wiki/Recurrent_neural_network" rel="nofollow noreferrer">Recurrent Neural Network</a> (RNN) as it can cope with different sizes of audio.</p>
<p><strong>Option 2: zero-pad / truncate</strong></p>
<p>I couldn't honestly find a standard here. You could choose a fix duration and then:</p>
<ul>
<li>For shorter audios: Add silence at the end and/or start of the audio</li>
<li>For longer audios: Cut them</li>
</ul>
<pre><code>from pydub import AudioSegment

audio = pydub.AudioSegment.silent(duration=duration_ms)    # The length you want
audio = audio.overlay(pydub.AudioSegment.from_wav(path))
raw = audio.split_to_mono()[0].get_array_of_samples()      # I only keep the left sound 
</code></pre>
<p>An example of this kind of application can be <a href="https://urbansounddataset.weebly.com/" rel="nofollow noreferrer">UrbanSoundDataset</a>. It's a dataset of different lengths audio and therefore any paper that uses it (for a non-RNN network) will be forced to use this or another approach that converts sounds to a same length vector/matrix. I recommend the paper <a href="https://arxiv.org/abs/1608.04363" rel="nofollow noreferrer">Deep Convolutional Neural Networks and Data Augmentation for Environmental Sound Classification</a> or <a href="https://ieeexplore.ieee.org/document/7324337" rel="nofollow noreferrer">ENVIRONMENTAL SOUND CLASSIFICATION WITH CONVOLUTIONAL NEURAL NETWORKS</a>. The later has it's code open sourced and you can see it uses the method I explained (kind of) on function <code>_load_audio</code> in <a href="https://github.com/karolpiczak/paper-2015-esc-convnet/blob/master/Code/_Datasets/Setup.ipynb" rel="nofollow noreferrer">this</a> notebook</p>
<hr />
<p>A bit off topic, but for these kind of applications is highly recommended to use <strong>mel-spectrum</strong></p>
<p>Standard (as far as I know) is to use a <a href="https://medium.com/analytics-vidhya/understanding-the-mel-spectrogram-fca2afa2ce53" rel="nofollow noreferrer">mel-spectrum</a> for this kind of applications. You could use the Python library <a href="https://pypi.org/project/essentia/" rel="nofollow noreferrer">Essentia</a> and follow <a href="https://essentia.upf.edu/essentia_python_tutorial.html#computing-spectrum-mel-bands-energies-and-mfccs" rel="nofollow noreferrer">this</a> example or use librosa like this:</p>
<pre><code>y, sr = librosa.load('your-wav-file.wav')
mel_spect = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=1024)
</code></pre>
Using sklearn on audio files of different lengths
<p>I'm new to ML and wanted to try a project myself to learn, so please excuse any blatant mistakes. I'm trying to classify a few files (ringtones and such) using audiolab and sklearn in python. </p>

<p>Here's the code:</p>

<pre><code>from scikits.audiolab.pysndfile.matapi import oggread, wavread

import numpy as np

from sklearn import svm

files = ["Basic_Bell.ogg", "Beep-Beep.ogg", "Beep_Once.ogg", "Calling_You.ogg",  "Time_Up.ogg"]
labels = [2,1,1,2,2]
train = []

for f in files:
    data, fs, enc = oggread("Tones/"+f)
    train.append(data)

clf = svm.SVC()
clf.fit(train, labels)
</code></pre>

<p>I'm getting an error message:</p>

<pre><code>Traceback (most recent call last):
  File "/home/athul/Projects/Audio Analysis/read.py", line 18, in &lt;module&gt;
    clf.fit(train, labels)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 150, in fit
    X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
  File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 373, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: setting an array element with a sequence.
</code></pre>

<p>In my (limited) understanding this seems to be issue because the train data has different sizes and hence numpy can't convert it into a matrix, so how do I fix this? Can I pad it? If so whats the size I should use? Or is this a mistake on my part? </p>