<p>In <strong>Matlab</strong>, you can do this. Can be modified if all the files in your folder are continuous.</p>
<pre><code>clear
filepath = 'C:\[insert your file path]\';
files = dir([filepath '*.wav']);

%% Loop within a folder containing multiple files
for rr = 1:length(files);
    info = audioinfo([filepath files(rr).name]);
    fs   = info.SampleRate; % samples per second
    filelength = info.TotalSamples;
    
    %%define chunk length
    tenmins_samples = 10*60*fs; %10 mins * 60 seconds in a min * number of sample per sec
    numloops = ceil(filelength/tenmins_samples); %round up, last output file may be &lt;10 mins
    
    %% Loop over number of ten min iterations within a file
    for qq = 1:numloops; 
        %% Define the sample numbers for start and stop of each 10 min chunk
        if qq==1
            samples = [1, tenmins_samples];
        elseif qq==numloops
            samples = [(qq-1)*tenmins_samples+1, filelength];
        else
            samples = [(qq-1)*tenmins_samples+1, qq*tenmins_samples];
        end
        chunk2keep = audioread([filepath files(1).name], samples);

        %% name and write the output file
        outputfilename = [files(rr).name(1:end-4) '_chunk' num2str(qq) '.wav'];
        audiowrite([filepath outputfilename], chunk2keep, fs)
        clear samples chunk2keep outputfilename
    end %qq
    
    clear numloops info filelength fs
end %rr
</code></pre>

CC BY-SA 4.0
2022-09-23T16:14:31.957Z
Hi @chloe this code works great for splitting. However, I would also like to rename the files as they are created using their start time in the new filename, i.e. the first sound file would keep its original name, the second would start 10 minutes later, the next one 20 minutes later and so on. I am a bit stuck to change the script accordingly. Would you be able to help with this? My filename looks like this for example: 5122.220531103154.wav with date/time information in the file name after the first 5 characters.
DeniseR
CC BY-SA 4.0
2022-10-22T13:50:10.667Z
found the solution eventually, in case it helps anyone else. replace the section on naming and writing output file with the following:                                                                                                   %% name and write the output files
        thisstarttime = starttime+seconds((qq-1)*fifteenmins_samples/fs);
        outputfilename = [files(rr).name(1:4) '.' char(thisstarttime,'yyMMddHHmmss') '.wav'];
        audiowrite([filepath '15min\', outputfilename], chunk2keep, fs)
        clear samples chunk2keep outputfilename
DeniseR
CC BY-SA 4.0
2022-07-05T14:10:41.390Z
This is my "chunking" approach of choice! Excellent bit of code, @chloe
selene
CC BY-SA 4.0
2022-07-05T14:08:21.977Z
2022-07-05T14:08:21.977Z
Chloe
<p>If you have (or install) <a href="http://sox.sourceforge.net/sox.html" rel="nofollow noreferrer">sox</a>, a command line tool, you can use the following to split <code>input.wav</code> into 10-minute files (named <code>output001.wav</code>, <code>output002.wav</code>, etc):</p>
<p><code>sox input.wav output%3n.wav trim 0 600 : newfile : restart</code></p>
<p>Details:</p>
<p><code>sox</code> - the name of the application</p>
<p><code>input.wav</code> - the name of the sound file (in the current directory) you wish to split. Can also supply as <code>/path/to/input.wav</code></p>
<p><code>output%3n.wav</code> - the pattern of the name to use for the output files. <code>%3n</code> will get converted into an incrementing three-digit number (<code>001</code> for the first file, <code>002</code> for the second, etc)</p>
<p><code>trim</code> - the action that <code>sox</code> will take</p>
<p><code>0 600</code> - start at time 0 and extract 600 seconds (10 mins)</p>
<p><code>: newfile : restart</code> - tells <code>sox</code> to enter multiple output file mode and repeat the <code>trim</code> command until it reaches the end of the input file</p>

CC BY-SA 4.0
2022-07-29T19:39:22.717Z
Even though the handling may look a bit daunting at first glance, I can absolutely recommend SoX as well. It is remarkably fast and it is possible to do batch processing (requires bash scripting), for example to process all audio files in a directory in some way.
Staphylosaurus
CC BY-SA 4.0
2022-07-05T09:01:41.830Z
2022-07-29T01:31:43.550Z
2022-07-29T01:31:43.550Z
Stewart Macdonald
<p>Here is a broad solution based in Python with the <a href="https://pysoundfile.readthedocs.io/en/latest/index.html?highlight=read#soundfile.read" rel="noreferrer">SoundFile package</a></p>
<pre><code>import soundfile as sf
file_path = '&lt;path_to_your_file_here&gt;'
# get the frequency of sampling
fs = sf.info(file_path).samplerate 

# make up some start and end times in seconds
start_times , end_times = [0.01, 200, 300], [140, 190, 500]

for start_s, end_s in zip(start_times, end_times):
    short_section, _ = sf.read(file_path , start=int(fs*start_s), stop=int(fs*end_s))
    sf.write(&lt;output_file_name_that_changes_every_loop&gt;, short_section,  samplerate=fs)
</code></pre>

CC BY-SA 4.0
2022-07-05T08:24:12.520Z
2022-07-05T14:43:58.013Z
2022-07-05T14:43:58.013Z
Thejasvi
<p>In R you can also use the function <a href="https://marce10.github.io/warbleR/reference/split_sound_files.html" rel="noreferrer">split_sound_files</a> from the package warbleR. For instance splitting in 1 s segments:</p>
<pre><code>library(warbleR)

#load example sound files and save to temporary working directory
data(list = c(&quot;Phae.long1&quot;, &quot;Phae.long2&quot;))
writeWave(Phae.long1, file.path(tempdir(), &quot;Phae.long1.wav&quot;))
writeWave(Phae.long2, file.path(tempdir(), &quot;Phae.long2.wav&quot;))

#split files in 1 s files
split_sound_files(sgmt.dur = 1, path = tempdir())

# Check this folder
tempdir()
</code></pre>
<p>You can also split all files in a fixed number of segments with the argument 'sgmts'</p>

CC BY-SA 4.0
2022-07-05T19:44:31.910Z
2022-07-05T21:41:28.020Z
2022-07-05T21:41:28.020Z
Marcelo Araya-Salas
<p>There are already a number of good answers on how to do the splitting.  You might however prefer to use solutions that do not split files.  Most languages will let you read small sections of an audio file, and I would suggest keeping a list of times you want to process, reading these chunks, and then processing them.  There are multiple advantages to doing this:</p>
<ol>
<li>you do not create multiple copies of your data,</li>
<li>it remains easy to examine the context of your data (e.g., what happened a few seconds before),</li>
<li>and if you are using a library that let's you keep your audio file open between reads, multiple reads from the same file will be much faster.  There is a large overhead for opening and closing files.</li>
</ol>
<p>Best of luck.</p>

CC BY-SA 4.0
2022-07-08T12:34:23.450Z
2022-07-08T12:34:23.450Z
Marie R
<p>Another option: my computer programmer colleague Yukio Fukuzawa made a tool that splits wav files; you simply specify how long you want the audio sections to be, and how many seconds of overlap between sections.</p>
<p>The overlap option is a nice feature in case there is an acoustic unit at the breakpoint -- you can make sure you have the entire unit in the first or second segment to take measurements on (rather than spoiling the unit through splitting).</p>
<p>To run the tool you will need to install Python. After that it's just a simple line of code that you type into your command prompt.</p>
<p>The tool and instructions are found here: <a href="https://github.com/fzyukio/split-songs" rel="nofollow noreferrer">https://github.com/fzyukio/split-songs</a></p>

CC BY-SA 4.0
2022-07-07T00:21:19.297Z
2022-07-07T10:13:41.980Z
2022-07-07T10:13:41.980Z
BirdGuyWes
<p>If you are in the world of Intel-processor macOS, give a try at <strong>xACT</strong> (<strong>x</strong> <strong>A</strong>udio <strong>C</strong>ompression <strong>T</strong>oolkit), a nice app that does many different batch tasks with audio files. It was built on top of many of the tasks performed by SoX. Once there, check the <strong>shntool</strong> tab for the splitting file function.</p>
<p><a href="http://xact.scottcbrown.org/xACT2.50.zip" rel="nofollow noreferrer">http://xact.scottcbrown.org/xACT2.50.zip</a></p>

CC BY-SA 4.0
2022-07-07T14:46:29.083Z
2022-07-07T14:46:29.083Z
Mario Rollo
<p>If you're using Audiomoths, the configuration app has a built-in tool to do this as well-</p>
<p><a href="https://i.sstatic.net/TdvhC.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TdvhC.png" alt="Split WAV files using Audiomoth config app" /></a>
<a href="https://i.sstatic.net/iX8TE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/iX8TE.png" alt="Split WAV files screen in app" /></a></p>

CC BY-SA 4.0
2022-07-06T15:58:12.990Z
2022-07-06T15:58:12.990Z
Carly Batist
<p><a href="https://ap.qut.ecoacoustics.info/" rel="nofollow noreferrer">QUT's Analysis Programs</a> has an audio cutter feature.</p>
<p>Usage:</p>
<pre><code>AP audiocutter -d 60 VeryLongFile.wav ./CutFiles
</code></pre>
<p>From the help:
<a href="https://i.sstatic.net/he27z.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/he27z.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2022-07-28T10:17:48.637Z
2022-07-28T10:17:48.637Z
Anthony Truskinger
<p>A agree with Marie R that often the best solution is not to split your sound files for analysis but use software that facilitates annotating long recordings and quickly jumping among annotations compare spectrograms, audio playback, and measurements.</p>
<p><a href="https://ravensoundsoftware.com/software/raven-lite/" rel="nofollow noreferrer">Raven Lite</a> is one example of such software. If you really do need to excerpt short audio clips from long recordings, Raven Lite allows you to &quot;Save Active Selection As&quot; or &quot;Save All Selections In Current Table As&quot; from the file menu if you have annotated the bits you want to excerpt.</p>
<p><a href="https://i.sstatic.net/7YVXv.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7YVXv.jpg" alt="Annotations in Raven Lite" /></a></p>

CC BY-SA 4.0
2022-08-14T20:59:16.063Z
2022-08-14T20:59:16.063Z
Michael Pitzrick
<p>I'm fully aware that this answer strays into 'self-promotion' territory, but it does address the question. A little while back, I developed a workflow incorporating R and Audacity to split large audio files. An overview with a link to the R code (via GitHub) is published on my data science blog, Wrangling In The Antipodes (<a href="https://wranglingintheantipodes.wordpress.com/2021/10/20/preparing-big-audio-files-for-analysis/" rel="nofollow noreferrer">https://wranglingintheantipodes.wordpress.com/2021/10/20/preparing-big-audio-files-for-analysis/</a>)</p>
<p>A function written in R creates a table of labels that can then be imported into an Audacity session, applied to an audio file of a given length and then exported as multiple files. The function written is R is very straightforward, so this could very easily be rewritten in Python, C, etc.</p>
<p>Reading some of the previous answers to this question, my approach might not be the most efficient workflow but I thought I'd put it out there as an alternative.</p>

CC BY-SA 4.0
2022-08-15T00:52:06.040Z
2022-08-15T00:52:06.040Z
Tristan Louth-Robins
<p>In R you have the option to only upload a section of a wave file with arguments &quot;from&quot; and &quot;to&quot; within the function readWave (tuneR package). for example:</p>
<pre><code>library (tuneR)
NewSection&lt;-readWave(&quot;PathToMyWav&quot;,from=0, to=2, units=&quot;seconds&quot;)
</code></pre>
<p>This will only take the two first seconds of your file. You can then directly work on your this section in R or save the section in a new Wave file with the function writeWave. With &quot;FOR&quot; loops or parallel calculation in R you can then change automatically the arguments &quot;from&quot; and &quot;to&quot; without doing it manually:</p>
<pre><code>library (tuneR)
start_times = c (0, 10, 20)
end_times = c (10, 20, 30)
Path= &quot;PathToMyWav&quot;
PathResult=&quot;PathToResultFolder&quot;

for (i in 1:length(start_times))
 {
 NewSection&lt;-readWave(Path,from=start_times[i], to=end_times[i], units=&quot;minutes&quot;)
 writeWave(NewSection, file=paste(PathResult,&quot;/&quot;,basename(Path),&quot;_&quot;,start_times[i],&quot;_&quot;,end_times[i],sep=&quot;&quot;))
 }
</code></pre>

CC BY-SA 4.0
2022-07-05T14:16:34.300Z
I'd just add to this that you can also read in the wav all together and then use the cutw() function to cut it after the fact and write out those cut sections. Reading once, cutting, then writing may be a bit faster than a higher number of read/write operations.

This also does rely on the sections of your wav files you want to cut being constant across all the files (for example, if you always want the time from 30 seconds to 40 seconds). If you need more varied timestamps for your clips, a different approach would be needed.
dtsavage
CC BY-SA 4.0
2022-07-05T15:53:52.003Z
Yes, keeping in mind that loading a one-hour file in R can be really long but I agree that it is an option
Amandine Gasc
CC BY-SA 4.0
2022-07-05T08:55:59.707Z
2022-07-05T08:55:59.707Z
Amandine Gasc
<p>I have some very long .wav files that are taking a long time to process. There are only short sections within this longer file that I'm interested in analysing. Is there an efficient way to split these up into, for example, 10 minute sections, without having to manually open each file and do it by hand? I'm sure there must be some simple R/Matlab code.</p>

<analysis><r><wav><matlab>
What is an easy way to split .wav files using R or Matlab?
# What is an easy way to split .wav files using R or Matlab?

(Asked by: username_0 on 2022-07-05)

I have some very long .wav files that are taking a long time to process. There are only short sections within this longer file that I'm interested in analysing. Is there an efficient way to split these up into, for example, 10 minute sections, without having to manually open each file and do it by hand? I'm sure there must be some simple R/Matlab code.

**Tags:** analysis, r, wav, matlab


---

## 12 answers

### Answer by username_15 on 2022-07-05. Score: 19 | Accepted answer

In R you have the option to only upload a section of a wave file with arguments "from" and "to" within the function readWave (tuneR package). for example:

```
library (tuneR)
NewSection<-readWave("PathToMyWav",from=0, to=2, units="seconds")

```

This will only take the two first seconds of your file. You can then directly work on your this section in R or save the section in a new Wave file with the function writeWave. With "FOR" loops or parallel calculation in R you can then change automatically the arguments "from" and "to" without doing it manually:

```
library (tuneR)
start_times = c (0, 10, 20)
end_times = c (10, 20, 30)
Path= "PathToMyWav"
PathResult="PathToResultFolder"

for (i in 1:length(start_times))
 {
 NewSection<-readWave(Path,from=start_times[i], to=end_times[i], units="minutes")
 writeWave(NewSection, file=paste(PathResult,"/",basename(Path),"_",start_times[i],"_",end_times[i],sep=""))
 }

```

> **Comments:**
> **username_16**: I'd just add to this that you can also read in the wav all together and then use the cutw() function to cut it after the fact and write out those cut sections. Reading once, cutting, then writing may be a bit faster than a higher number of read/write operations.  This also does rely on the sections of your wav files you want to cut being constant across all the files (for example, if you always want the time from 30 seconds to 40 seconds). If you need more varied timestamps for your clips, a different approach would be needed.  
> **username_15**: Yes, keeping in mind that loading a one-hour file in R can be really long but I agree that it is an option  

---

### Answer by username_1 on 2022-07-05. Score: 19

In **Matlab**, you can do this. Can be modified if all the files in your folder are continuous.

```
clear
filepath = 'C:\[insert your file path]\';
files = dir([filepath '*.wav']);

%% Loop within a folder containing multiple files
for rr = 1:length(files);
    info = audioinfo([filepath files(rr).name]);
    fs   = info.SampleRate; % samples per second
    filelength = info.TotalSamples;
    
    %%define chunk length
    tenmins_samples = 10*60*fs; %10 mins * 60 seconds in a min * number of sample per sec
    numloops = ceil(filelength/tenmins_samples); %round up, last output file may be <10 mins
    
    %% Loop over number of ten min iterations within a file
    for qq = 1:numloops; 
        %% Define the sample numbers for start and stop of each 10 min chunk
        if qq==1
            samples = [1, tenmins_samples];
        elseif qq==numloops
            samples = [(qq-1)*tenmins_samples+1, filelength];
        else
            samples = [(qq-1)*tenmins_samples+1, qq*tenmins_samples];
        end
        chunk2keep = audioread([filepath files(1).name], samples);

        %% name and write the output file
        outputfilename = [files(rr).name(1:end-4) '_chunk' num2str(qq) '.wav'];
        audiowrite([filepath outputfilename], chunk2keep, fs)
        clear samples chunk2keep outputfilename
    end %qq
    
    clear numloops info filelength fs
end %rr

```

> **Comments:**
> **username_3**: This is my "chunking" approach of choice! Excellent bit of code, @username_1  
> **username_2**: Hi @username_1 this code works great for splitting. However, I would also like to rename the files as they are created using their start time in the new filename, i.e. the first sound file would keep its original name, the second would start 10 minutes later, the next one 20 minutes later and so on. I am a bit stuck to change the script accordingly. Would you be able to help with this? My filename looks like this for example: 5122.220531103154.wav with date/time information in the file name after the first 5 characters.  
> **username_2**: found the solution eventually, in case it helps anyone else. replace the section on naming and writing output file with the following:                                                                                                   %% name and write the output files         thisstarttime = starttime+seconds((qq-1)*fifteenmins_samples/fs);         outputfilename = [files(rr).name(1:4) '.' char(thisstarttime,'yyMMddHHmmss') '.wav'];         audiowrite([filepath '15min\', outputfilename], chunk2keep, fs)         clear samples chunk2keep outputfilename  

---

### Answer by username_4 on 2022-07-05. Score: 14

If you have (or install) [sox](http://sox.sourceforge.net/sox.html), a command line tool, you can use the following to split `input.wav` into 10-minute files (named `output001.wav`, `output002.wav`, etc):

`sox input.wav output%3n.wav trim 0 600 : newfile : restart`

Details:

`sox` - the name of the application

`input.wav` - the name of the sound file (in the current directory) you wish to split. Can also supply as `/path/to/input.wav`

`output%3n.wav` - the pattern of the name to use for the output files. `%3n` will get converted into an incrementing three-digit number (`001` for the first file, `002` for the second, etc)

`trim` - the action that `sox` will take

`0 600` - start at time 0 and extract 600 seconds (10 mins)

`: newfile : restart` - tells `sox` to enter multiple output file mode and repeat the `trim` command until it reaches the end of the input file

> **Comments:**
> **username_5**: Even though the handling may look a bit daunting at first glance, I can absolutely recommend SoX as well. It is remarkably fast and it is possible to do batch processing (requires bash scripting), for example to process all audio files in a directory in some way.  

---

### Answer by username_6 on 2022-07-05. Score: 10

Here is a broad solution based in Python with the [SoundFile package](https://pysoundfile.readthedocs.io/en/latest/index.html?highlight=read#soundfile.read)

```
import soundfile as sf
file_path = '<path_to_your_file_here>'
# get the frequency of sampling
fs = sf.info(file_path).samplerate 

# make up some start and end times in seconds
start_times , end_times = [0.01, 200, 300], [140, 190, 500]

for start_s, end_s in zip(start_times, end_times):
    short_section, _ = sf.read(file_path , start=int(fs*start_s), stop=int(fs*end_s))
    sf.write(<output_file_name_that_changes_every_loop>, short_section,  samplerate=fs)

```


---

### Answer by username_7 on 2022-07-05. Score: 8

In R you can also use the function [split\_sound\_files](https://marce10.github.io/warbleR/reference/split_sound_files.html) from the package warbleR. For instance splitting in 1 s segments:

```
library(warbleR)

#load example sound files and save to temporary working directory
data(list = c("Phae.long1", "Phae.long2"))
writeWave(Phae.long1, file.path(tempdir(), "Phae.long1.wav"))
writeWave(Phae.long2, file.path(tempdir(), "Phae.long2.wav"))

#split files in 1 s files
split_sound_files(sgmt.dur = 1, path = tempdir())

# Check this folder
tempdir()

```

You can also split all files in a fixed number of segments with the argument 'sgmts'


---

### Answer by username_8 on 2022-07-08. Score: 7

There are already a number of good answers on how to do the splitting. You might however prefer to use solutions that do not split files. Most languages will let you read small sections of an audio file, and I would suggest keeping a list of times you want to process, reading these chunks, and then processing them. There are multiple advantages to doing this:

1. you do not create multiple copies of your data,
2. it remains easy to examine the context of your data (e.g., what happened a few seconds before),
3. and if you are using a library that let's you keep your audio file open between reads, multiple reads from the same file will be much faster. There is a large overhead for opening and closing files.

Best of luck.


---

### Answer by username_9 on 2022-07-07. Score: 6

Another option: my computer programmer colleague Yukio Fukuzawa made a tool that splits wav files; you simply specify how long you want the audio sections to be, and how many seconds of overlap between sections.

The overlap option is a nice feature in case there is an acoustic unit at the breakpoint -- you can make sure you have the entire unit in the first or second segment to take measurements on (rather than spoiling the unit through splitting).

To run the tool you will need to install Python. After that it's just a simple line of code that you type into your command prompt.

The tool and instructions are found here: <https://github.com/fzyukio/split-songs>


---

### Answer by username_10 on 2022-07-07. Score: 3

If you are in the world of Intel-processor macOS, give a try at **xACT** (**x** **A**udio **C**ompression **T**oolkit), a nice app that does many different batch tasks with audio files. It was built on top of many of the tasks performed by SoX. Once there, check the **shntool** tab for the splitting file function.

<http://xact.scottcbrown.org/xACT2.50.zip>


---

### Answer by username_11 on 2022-07-06. Score: 2

If you're using Audiomoths, the configuration app has a built-in tool to do this as well-

[![Split WAV files using Audiomoth config app](https://i.sstatic.net/TdvhC.png)](https://i.sstatic.net/TdvhC.png)
[![Split WAV files screen in app](https://i.sstatic.net/iX8TE.png)](https://i.sstatic.net/iX8TE.png)


---

### Answer by username_12 on 2022-07-28. Score: 1

[QUT's Analysis Programs](https://ap.qut.ecoacoustics.info/) has an audio cutter feature.

Usage:

```
AP audiocutter -d 60 VeryLongFile.wav ./CutFiles

```

From the help:
[![enter image description here](https://i.sstatic.net/he27z.png)](https://i.sstatic.net/he27z.png)


---

### Answer by username_13 on 2022-08-14. Score: 0

A agree with username_8 R that often the best solution is not to split your sound files for analysis but use software that facilitates annotating long recordings and quickly jumping among annotations compare spectrograms, audio playback, and measurements.

[Raven Lite](https://ravensoundsoftware.com/software/raven-lite/) is one example of such software. If you really do need to excerpt short audio clips from long recordings, Raven Lite allows you to "Save Active Selection As" or "Save All Selections In Current Table As" from the file menu if you have annotated the bits you want to excerpt.

[![Annotations in Raven Lite](https://i.sstatic.net/7YVXv.jpg)](https://i.sstatic.net/7YVXv.jpg)


---

### Answer by username_14 on 2022-08-15. Score: 0

I'm fully aware that this answer strays into 'self-promotion' territory, but it does address the question. A little while back, I developed a workflow incorporating R and Audacity to split large audio files. An overview with a link to the R code (via GitHub) is published on my data science blog, Wrangling In The Antipodes (<https://wranglingintheantipodes.wordpress.com/2021/10/20/preparing-big-audio-files-for-analysis/>)

A function written in R creates a table of labels that can then be imported into an Audacity session, applied to an audio file of a given length and then exported as multiple files. The function written is R is very straightforward, so this could very easily be rewritten in Python, C, etc.

Reading some of the previous answers to this question, my approach might not be the most efficient workflow but I thought I'd put it out there as an alternative.


---

