How to cut imported audio?

How to cut an audio clip imported by the Import function? E.g. I’m interested in the content starting from the 100th sample up to the 200th.

I’d like to avoid using an external editor to edit the sample.



2 Answers


The audio processing tutorial is helpful here. Without information about the sampling rate, I’m not sure what 100 and 200 mean in your case, but this example should help.

sound = Import[ “ExampleData/rule30.wav” ]
soundlist = sound[[1]];
soundlist[[1, 1]] = Take[soundlist[[1, 1]], -79380/2];

First, we import an example sound into sound. Then extract the soundlist, which is the first element of the sound. Next, we remove some data (I take the last half of the example sound in this case) and finally create a new Sound out of the soundlist.

The following snippet works with Sound objects that have just one data list (as opposed to composed Sound objects), such as imported files, and takes care of multi channel audio as well.

sound = Import[“[…].mp3”];
ClipAudio[sound_, skip_, dur_] := Module[{channels, sslist, freq},
{sslist, freq} = {sound[[1]], sound[[1, 2]]};
channels = Map[Take[Drop[#, Round[freq*skip]], Round[freq*dur]] &, First@sslist];
Sound[SampledSoundList[channels, freq]]

Ex: ClipAudio[sound, 5, 10] should drop the first 5 seconds, and be 10 seconds long.