title: Efficiently split a large audio file in R
body: <p><br> 
  Previously I asked <a href="https://stackoverflow.com/questions/20696681/split-an-audio-file-into-pieces-of-an-arbitrary-size">this question on SO about splitting an audio file</a>.  The answer I got from @Jean V. Adams worked relatively (downside: input was stereo and output was mono, not stereo) well for small sound objects:</p>
  
  <pre><code>library(seewave)
  
  # your audio file (using example file from seewave package)
  data(tico)
  audio &lt;- tico # this is an S4 class object
  # the frequency of your audio file
  freq &lt;- 22050
  # the length and duration of your audio file
  totlen &lt;- length(audio)
  totsec &lt;- totlen/freq
  
  # the duration that you want to chop the file into
  seglen &lt;- 0.5
  
  # defining the break points
  breaks &lt;- unique(c(seq(0, totsec, seglen), totsec))
  index &lt;- 1:(length(breaks)-1)
  # a list of all the segments
  subsamps &lt;- lapply(index, function(i) cutw(audio, f=freq, from=breaks[i], to=breaks[i+1]))
  </code></pre>
  
  <p>I applied this solution to one (out of around 300) of the files I'm preparing for analysis (~150 MB), and my computer worked on it for ( > 5 hours now), but I ended up closing the session before it finished.</p>
  
  <p>Does anyone have any thoughts or solutions to efficiently perform this task of splitting up a large audio file (specifically, an S4 class Wave object) into smaller pieces using R?  I'm hoping to cut down drastically on the time it takes to make smaller files out of these larger files, and I'm hoping to use R.  However, if I cannot get R to do the task efficiently, I would appreciate suggestions of other tools for the job.  The example data above is mono, but my data is in stereo.  The example data can be made to be stereo using:</p>
  
  <pre><code>tico@stereo &lt;- TRUE
  tico@right &lt;- tico@left
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <p>I identified another solution that builds on work from the first solution:</p>
  
  <pre><code>lapply(index, function(i) audio[(breaks[i]*freq):(breaks[i+1]*freq)])
  </code></pre>
  
  <p>Comparing the performance of three solutions:</p>
  
  <pre><code># Solution suggested by @Jean V. Adams
  system.time(replicate(100,lapply(index, function(i) cutw(audio, f=freq, from=breaks[i], to=breaks[i+1], output="Wave"))))
  user  system elapsed 
  1.19    0.00    1.19 
  # my modification of the previous solution
  system.time(replicate(100,lapply(index, function(i) audio[(breaks[i]*freq):(breaks[i+1]*freq)])))
  user  system elapsed 
  0.86    0.00    0.85 
  
  # solution suggested by @CarlWitthoft 
  audiomod &lt;- audio[(freq*breaks[1]):(freq*breaks[length(breaks)-1])] # remove unequal part at end
  system.time(replicate(100,matrix(audiomod@left,ncol=length(breaks))))+
  system.time(replicate(100,matrix(audiomod@right,ncol=length(breaks))))
  user  system elapsed 
  0.25    0.00    0.26 
  </code></pre>
  
  <p>The method using indexing (i.e. <code>[</code>) seems to faster (3-4x).  @CarlWitthoft's solution is even faster, the downside is that it puts the data into a matrix rather than multiple <code>Wave</code> objects, which I will be saving using <code>writeWave</code>.  Presumably, convert from the matrix format to a separate <code>Wave</code> objects will be relatively trivial if I properly understand how to create this type of S4 object.  Any further room for improvement?</p>