<div dir="ltr">Hi Martin,<div><br></div><div>On line 63 <a href="http://lpaste.net/147522#line63">http://lpaste.net/147522#line63</a>, when you do</div><div><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><span class="" style="color:rgb(57,116,96)">let</span> <span class="" style="color:rgb(51,51,51)">sndChan</span> <span class="" style="color:rgb(143,78,139)">=</span> <span class="" style="color:rgb(51,51,51)">newTChan</span></pre><pre style="margin-top:0px;margin-bottom:0px"><span class="" style="white-space:normal"><font face="arial, sans-serif"><br></font></span></pre><pre style="margin-top:0px;margin-bottom:0px"><font face="arial, sans-serif"><span style="white-space:normal">you're not actually creating a new TChan, but rather creating a reference to the STM action that creates new TChans.</span></font></pre><pre style="margin-top:0px;margin-bottom:0px"><font face="arial, sans-serif"><span style="white-space:normal"><br></span></font></pre><pre style="margin-top:0px;margin-bottom:0px"><font face="arial, sans-serif"><span style="white-space:normal">So this means that e.g. on line 71 </span></font><span style="font-family:arial,sans-serif"><a href="http://lpaste.net/147522#line71">http://lpaste.net/147522#line71</a></span><span style="white-space:normal;font-family:arial,sans-serif">, you are creating a new channel every time with</span></pre><pre style="margin-top:0px;margin-bottom:0px"><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><span class="" style="color:rgb(51,51,51)">ch</span> <span class="" style="color:rgb(143,78,139)"><-</span> <span class="" style="color:rgb(51,51,51)">sndChan</span></pre><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><span class="" style="color:rgb(51,51,51)"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><pre style="color:rgb(34,34,34);margin-top:0px;margin-bottom:0px"><font face="arial, sans-serif"><span style="white-space:normal">Instead, you want to do:</span></font></pre></pre><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><span class="" style="color:rgb(51,51,51)">sndChan <- newTChanIO</span></pre><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><span class="" style="color:rgb(51,51,51)">(or,</span></pre><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><span class="" style="color:rgb(51,51,51)">sndChan <- atomically newTChan</span></pre><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><span class="" style="color:rgb(51,51,51)">)</span></pre><pre style="margin-top:0px;margin-bottom:0px;color:rgb(68,68,68);font-size:13px"><br></pre><pre style="margin-top:0px;margin-bottom:0px"><font face="arial, sans-serif"><span style="white-space:normal">And then pass that value to your other functions, which will just take </span></font><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(79,67,113)">TChan</span><span style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif"> </span><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(143,78,139)">[</span><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(79,67,113)">Int32</span><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(143,78,139)">]</span><span style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif"> </span><span style="white-space:normal;font-family:arial,sans-serif">rather than </span><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(79,67,113)">STM</span><span style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif"> </span><span class="" style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif">(</span><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(79,67,113)">TChan</span><span style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif"> </span><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(143,78,139)">[</span><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(79,67,113)">Int32</span><span class="" style="font-size:13px;font-family:arial,sans-serif;color:rgb(143,78,139)">]</span><span class="" style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif">)</span></pre><pre style="margin-top:0px;margin-bottom:0px"><span class="" style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span class="" style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif">Here's what I mean:</span></pre><pre style="margin-top:0px;margin-bottom:0px"><span class="" style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif"><pre style="color:rgb(34,34,34);margin-top:0px;margin-bottom:0px"><span class="" style="color:rgb(68,68,68);font-family:arial,sans-serif"><a href="http://lpaste.net/diff/147522/147557">http://lpaste.net/diff/147522/147557</a></span></pre></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span class="" style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span class="" style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif">Hope that helps!</span></pre><pre style="margin-top:0px;margin-bottom:0px"><span class="" style="color:rgb(68,68,68);font-size:13px;font-family:arial,sans-serif"><br></span></pre></pre></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Dec 20, 2015 at 9:24 AM, Martin Vlk <span dir="ltr"><<a href="mailto:martin@vlkk.cz" target="_blank">martin@vlkk.cz</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi, I am working on a little toy project related to controlling graphics<br>
display based on data from the computer microphone.<br>
<br>
I am basing the whole thing on concepts from game programming, so I have<br>
a main loop which reads inputs, updates the world state and generates<br>
outputs.<br>
<br>
As part of inputs there is microphone. I read data from it using the<br>
pulse-simple library. The "simpleRead" function blocks if there is not<br>
enough data available so I can't use it directly in the main loop or I<br>
risk delays.<br>
<br>
So I figured I'll use a separate thread to read from the mic and write<br>
data into a TChan. The main loop in separate thread then can read from<br>
the TChan as needed and test for availability of data to avoid delaying<br>
the main loop.<br>
<br>
Here is my current code: <a href="http://lpaste.net/147522" rel="noreferrer" target="_blank">http://lpaste.net/147522</a><br>
<br>
The data is written into TChan in the "handleMic" function and read from<br>
the TChan on line 85.<br>
<br>
The problem I have is that the TChan never seems to contain any data<br>
when I read from it and that confuses me. Why?<br>
<br>
Does anyone see where is my problem?<br>
<br>
Many Thanks<br>
Martin<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>