<p>Ok, finally I found a <a href="https://arxiv.org/abs/1706.07156" rel="nofollow noreferrer">paper</a> that talks about it. In the paper they say:</p>
<blockquote>
<p>All audio clips were standardized by padding/clipping to a 4 second
duration</p>
</blockquote>
<p>So yes, what you say that impacts on your performance is what they do in papers for what I see.</p>
<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 the 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 latter has its code open-sourced and you can see it also gets audio to 4 seconds 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>
<p><strong>How to clip audios</strong></p>
<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><strong>Mel-spectrogram</strong></p>
<p>Standard 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># Attention, I do not cut / pad this example
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>
How to handle dynamic input size for audio spectrogram used in CNN?
<p>A lot of articles are using CNNs to extract audio features. The input data is a spectrogram with two dimensions, time and frequency.</p>

<p>When creating an audio spectrogram, you need to specify the exact size of both dimensions. But they are usually not fixed. One can specify the size of the frequency dimension through the window size, but what about the time domain? The lengths of audio samples are different, but the size of the input data of CNNs should be fixed. </p>

<p>In my datasets, the audio length ranges from 1s to 8s. Padding or Cutting always impacts the results too much. </p>

<p>So I want to know more about this method.</p>