# Read and write simultaneously from a .wav file?

**URL:** <https://forum.juce.com/t/read-and-write-simultaneously-from-a-wav-file/12340>\
**Category:** General JUCE discussion\
**Created:** [February 7, 2014, 4:04pm UTC](https://forum.juce.com/t/read-and-write-simultaneously-from-a-wav-file/12340 "2014-02-07T16:04:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![seertaak](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@seertaak](https://forum.juce.com/u/seertaak)\
**Post date:** [February 7, 2014, 4:04pm UTC](https://forum.juce.com/t/read-and-write-simultaneously-from-a-wav-file/12340/1 "2014-02-07T16:04:11Z")

</div>

Hi,

Is it possible to read and write simultaneously from a .wav file? I'm writing a looper, and my plan was to use&nbsp;AudioFormatWriter::ThreadedWriter to write the current audio data to a .wav file (one .wav file per "take", regardless of how many play/record loops there are on the track), and separately to create a&nbsp;MemoryMappedAudioFormatReader for each playback loop on the track, reading from the appropriate position in the "take" .wav file&nbsp;at the appropriate time.

I tried implementing this, but I&nbsp;WavAudioFormat::createMemoryMappedReader() fails, returning null instead of a valid reader.

Thanks!

seertaak

---

<div class="post-metadata">

**Author:** ![turntable](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@turntable](https://forum.juce.com/u/turntable)\
**Post date:** [February 7, 2014, 7:07pm UTC](https://forum.juce.com/t/read-and-write-simultaneously-from-a-wav-file/12340/2 "2014-02-07T19:07:15Z")

</div>

I would do this all in memory and save later, when there is something to save.

Like, keep a 'new' and an 'old' loop buffer...then whenever it starts over, add the new to the old, clear the new, and start again.

Reading and writing to the same file is not going to be pretty.

&nbsp;

Hope that helps.

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [February 7, 2014, 7:27pm UTC](https://forum.juce.com/t/read-and-write-simultaneously-from-a-wav-file/12340/3 "2014-02-07T19:27:54Z")

</div>

Turntable is right, writing to disk is slow. Circular buffers are you friend.&nbsp;

---

<div class="post-metadata">

**Author:** ![turntable](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@turntable](https://forum.juce.com/u/turntable)\
**Post date:** [February 7, 2014, 10:01pm UTC](https://forum.juce.com/t/read-and-write-simultaneously-from-a-wav-file/12340/4 "2014-02-07T22:01:00Z")

</div>

> Circular buffers are you friend.&nbsp;

...and so is the friendly modulo (%)!!!!

---

<div class="post-metadata">

**Author:** ![seertaak](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@seertaak](https://forum.juce.com/u/seertaak)\
**Post date:** [February 8, 2014, 9:48pm UTC](https://forum.juce.com/t/read-and-write-simultaneously-from-a-wav-file/12340/5 "2014-02-08T21:48:43Z")

</div>

Rory, turntable: thanks a lot for your advice!
