1
$\begingroup$

I have a dataset of audio files, which differ on their lengths by a couple of milliseconds. Some are little shorter than 1s and some are little longer than 1s. They all have the same sampling rate, namely 20kHz, what differs is the number of samples.

Does anyone know a way to automatically transform them into 1s audio files with 20kHz sampling rate?

EDIT: I need to transform the audios to having the same length so that I can input them into a standard fully-connected neural network.

Cutting the audio in the beginning and at the end would be the best solution, I think, if the audio is longer than 1 second. For example, if the audio is longer 1.1s, then I would like to cut the audio by 5ms in the beginning and the end to transform it to a 1s audio? And if the data is shorter, somehow putting some "silence" at the end of the data?

Thank you in advance!

$\endgroup$
  • $\begingroup$ Hey what you are asking is not very clear, there's two way to do that : either you straight cut the audio but them you'll loose some data in the end or in the beginning, but appart from that every thing you'll do will affect the audio quality. Can you afford tuning up or down the audio ? Can you afford processing a lot on the audio in order to avoid dumping samples? $\endgroup$ – YCN- Apr 30 '18 at 12:57
  • $\begingroup$ Thank you for your feedback! I need to transform the audios to having the same length so that I can input them into a standard fully-connected neural network. Therefore, cutting the audio in the beginning and at the end would be the best solution. Is there anyway to automatically do that, for example, if the audio is longer than 1 s by 10 ms, then cutting the audio by 5ms in the beginning and the end? And if the data is shorter, somehow putting some "silence" at the end of the data? $\endgroup$ – UrmLmn Apr 30 '18 at 13:03
  • $\begingroup$ Use octave you need few linea of code $\endgroup$ – Andrea Apr 30 '18 at 15:50
3
$\begingroup$

ImageMagick is a great tool to edit multiple images in command line. Thanks to your question, I just discovered SoX, or Sound eXchange:

SoX is a cross-platform (Windows, Linux, MacOS X, etc.) command line utility that can convert various formats of computer audio files in to other formats. It can also apply various effects to these sound files, and, as an added bonus, SoX can play and record audio files on most platforms.

It can trim, crop, cut multiple files (and resample too), see for instance:

For shorter files, an option is to create a silence file, happen it, and crop again, see for instance: Linux command to extend the duration of audio files.

$\endgroup$
  • 1
    $\begingroup$ Thank you for your answer! I managed to adjust audio longer than 1 second to 1 second audio clips. Need to ajdust now those that are shorter than 1s. $\endgroup$ – UrmLmn Apr 30 '18 at 14:59
  • 1
    $\begingroup$ My last edit disappeared... Here is it again, with an option to happen a silence file and crop it again $\endgroup$ – Laurent Duval Apr 30 '18 at 15:12
2
$\begingroup$

In order to make your audio wav file longer you can use sox in linux :

sox input.wav output.wav trim 0 00:01

This will take 1s of the audio input and put it in output.wav. If it's shorter than 1s it will probably work, but I have no idea.

Another solution would be to actually do what you want with a C/whatever program, that won't be very hard to do but you probably don't want to go that way.

I guess Sox has probably a simple and efficient solution to your problem.

$\endgroup$
  • 1
    $\begingroup$ Thank you for your answer! I managed to bring audios larger than 1s to 1s audio clips, using ffmpeg, but also your command worked as well. However, both of these programs are not extending the audios that are shorter than 1s to 1s. Need to further search! $\endgroup$ – UrmLmn Apr 30 '18 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.