# Porting the AudioRecordingDemo to a Plugin

**URL:** <https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775>\
**Category:** Audio Plugins\
**Created:** [April 11, 2017, 8:40pm UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775 "2017-04-11T20:40:33Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [July 2, 2019, 3:25pm UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775/21 "2019-07-02T15:25:16Z")

</div>

> [@daniel](#):
>
> have a bool flag to signal the processBlock, if the writer is ready to write and silently ignore if it isn’t, to avoid disaster…

Probably best to make that an atomic bool too… 🙂

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 2, 2019, 3:29pm UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775/22 "2019-07-02T15:29:44Z")

</div>

Is there a platform, where bool isn’t atomic? 😉  
but for consistency, probably yes…

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [July 2, 2019, 3:32pm UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775/23 "2019-07-02T15:32:27Z")

</div>

Yes, it’s complicated…The bools may be atomic at the hardware level, but the compiler might do weird stuff with the variables if they are not explicitly handled as atomic in the code. (Atomic ensures the compiler isn’t going try anything “smart” with the variable.)

---

<div class="post-metadata">

**Author:** ![voluntaryzonkey](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/voluntaryzonkey/32/6734_2.png) [@voluntaryzonkey](https://forum.juce.com/u/voluntaryzonkey)\
**Post date:** [July 2, 2019, 5:47pm UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775/24 "2019-07-02T17:47:54Z")

</div>

So sorry for my coding noobiness but… on the bool flag - is the idea to check for errors in my functions to check in case something returns null, such as:

```
bool flag;

if (auto* wavWriter = format.createWriterFor (new FileOutputStream (file)) == nullptr
    flag == false;

```

And then in the process block have some kind of a…  
`while (flag == true)`

Is this roughly what you meant? Thanks again

EDIT: Or should I maybe make the whole function boolean, and if something goes wrong I return false?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 2, 2019, 8:06pm UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775/25 "2019-07-02T20:06:35Z")

</div>

The idea is, if opening the file is asynchronous, you have to make sure you don’t try to write while in the process of opening. I think you don’t have to worry about it, if you do that in prepareToPlay and implement that synchronously, since the processing can only start, once all prepareToPlay calls have finished. But the cleaner design would be to have such a flag, that indicates, if you are ready to record, and if you want record at all…  
That is, because the processBlock must not be held back by anything.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 22, 2023, 9:56am UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775/27 "2023-11-22T09:56:20Z")

</div>

> [@daniel](#):
>
> Is there a platform, where bool isn’t atomic? 😉

Seeing my answer of \>4 years ago I am embarrassed. I learned some stuff in the meantime:

It doesn’t matter, if it is an atomic operation/atomic type.  
ALWAYS wrap into atomic, if it can be accessed from different threads. This is to stop the optimiser from making false assumptions. The optimiser can break your code!  
See Fabian and Dave’s Realtime 101 on youtube and, if you have access to this years ADC, Fabians Realtime Confessions!

(Sorry Xenakios, thanks for correctng me back then)

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [November 22, 2023, 11:00am UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775/28 "2023-11-22T11:00:28Z")

</div>

I think asking that question is practically a right of passage to writing C++!

---

<div class="post-metadata">

**Author:** ![Qfactor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/qfactor/32/14921_2.png) [@Qfactor](https://forum.juce.com/u/Qfactor)\
**Post date:** [November 25, 2023, 2:42pm UTC](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775/29 "2023-11-25T14:42:09Z")

</div>

> [@daniel](#):
>
> It doesn’t matter, if it is an atomic operation/atomic type.  
> ALWAYS wrap into atomic

Yes, and check it is lock free too with [is\_lock\_free()](https://en.cppreference.com/w/cpp/atomic/atomic/is_lock_free) (which is usually the case in PC’s and Macs). If you want to be totally certain, for a bool you have atomic\_flag and for an int, in c++20 you can use atomic\_signed\_lock\_free and atomic\_unsigned\_lock\_free (to be honest, I haven’t tried those yet though).

[https://en.cppreference.com/w/cpp/atomic/atomic](https://en.cppreference.com/w/cpp/atomic/atomic)

You can also quickly check some [pre-defined macros](https://en.cppreference.com/w/c/atomic/ATOMIC_LOCK_FREE_consts).

[Previous page](https://forum.juce.com/t/porting-the-audiorecordingdemo-to-a-plugin/21775.md?page=1)
