<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Matthieu Brucher&#039;s blog &#187; C++</title> <atom:link href="http://matt.eifelle.com/category/cpp/feed/" rel="self" type="application/rss+xml" /><link>http://matt.eifelle.com</link> <description></description> <lastBuildDate>Tue, 27 Jul 2010 07:04:23 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.1</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>QtVST: a Chamberlin Variable Filter</title><link>http://matt.eifelle.com/2010/07/06/qtvst-a-chamberlin-variable-filter/</link> <comments>http://matt.eifelle.com/2010/07/06/qtvst-a-chamberlin-variable-filter/#comments</comments> <pubDate>Tue, 06 Jul 2010 07:17:52 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Digital filters]]></category> <category><![CDATA[Music]]></category> <category><![CDATA[digital filter]]></category> <category><![CDATA[pyVST]]></category> <category><![CDATA[Qt]]></category> <category><![CDATA[VST]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=1251</guid> <description><![CDATA[After my last post on QtAgain, I&#8217;ve decided to test a few simple digital filters. I&#8217;ve tried to make them as generic as possible, and with a VST interface.A templated Chamberlin filter
A Chamberlin filter is a IIR (Infinite Impulse Response) filter, and it is possible to make it output high-, band- or low-pass signals. It [...]]]></description> <content:encoded><![CDATA[<p>After <a
href="http://matt.eifelle.com/2010/03/02/fixing-the-qtagain-plugin/">my last post on QtAgain</a>, I&#8217;ve decided to test a few simple digital filters. I&#8217;ve tried to make them as generic as possible, and with a VST interface.<br
/> <span
id="more-1251"></span></p><h4>A templated Chamberlin filter</h4><p>A Chamberlin filter is a IIR (Infinite Impulse Response) filter, and it is possible to make it output high-, band- or low-pass signals. It has only two parameters that are independent.</p><p>The equations are very simple:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2010/07/equations.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2010/07/equations.png" alt="" title="Chamberlin Equations" class="aligncenter size-full wp-image-1279" /></a></center></p><p>Each of the series is either the low-pass, the band-pass or the high-pass output.</p><p>There are some factors that are pre-computed:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2010/07/factors.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2010/07/factors.png" alt="" title="Digital Factors" class="aligncenter size-full wp-image-1280" /></a></center></p><p>The first is a function of the <strong>C</strong>ut frequency and the <strong>S</strong>ampling frequency, a &#8220;numerical frequency&#8221;. The second one is the numerical attenuation, between 0 and 2.</p><p>This is an excerpt of the actual code, I&#8217;ve only displayed the relevant code.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=1251&amp;download=variable_filter.h">variable_filter.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p12512"><td
class="code" id="p1251code2"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Data_Type<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> VariableFilter
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">typedef</span> Data_Type DataType<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">void</span> process<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> DataType<span style="color: #000040;">*</span> in, DataType<span style="color: #000040;">*</span> out, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> nb_samples<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> nb_samples<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      yh <span style="color: #000080;">=</span> in<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">-</span> yl <span style="color: #000040;">-</span> numerical_attenuation <span style="color: #000040;">*</span> yb<span style="color: #008080;">;</span>
      yb <span style="color: #000080;">=</span> numerical_frequency <span style="color: #000040;">*</span> yh <span style="color: #000040;">+</span> yb<span style="color: #008080;">;</span>
      yl <span style="color: #000080;">=</span> numerical_frequency <span style="color: #000040;">*</span> yb <span style="color: #000040;">+</span> yl<span style="color: #008080;">;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>selected <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        out<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> yl<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>selected <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        out<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> yb<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">else</span>
      <span style="color: #008000;">&#123;</span>
        out<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> yh<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span> 
&nbsp;
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">void</span> compute_factors<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    numerical_frequency <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">sin</span><span style="color: #008000;">&#40;</span>M_PI <span style="color: #000040;">*</span> cutoff_frequency <span style="color: #000040;">/</span> sampling_frequency<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    numerical_attenuation <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> attenuation<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>Now, I&#8217;ve written a simple GUI for this plugin.</p><h4>GUI with Qt</h4><p>I&#8217;ve used the QtAgain skeletton to wrap the filter and to make it available to <a
href="http://matt.eifelle.com/2010/03/09/annoucement-pyvst-0-1/">PyVST</a>. It&#8217;s really easy to add other parameters to the skeletton, so I will not write everything here again. There are three parameters, 2 numerical ones, and one three-state variable.</p><p><center><a
href="http://matt.eifelle.com/wp-content/uploads/2010/06/editor.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2010/06/editor-300x102.png" alt="" title="Chamberlin editor" width="300" height="102" class="aligncenter size-medium wp-image-1274" /></a></center></p><h4>Results</h4><p>I&#8217;ve recorded the filter&#8217;s outputs for 6kHz as central frequency, and an attenuation factor of .3. The output signal was generated from a random signal, and then an FFT was performed on the inputs and the outputs and displayed.</p><p>Note: I didn&#8217;t convert the scale to dB.</p><p><center><a
href="http://matt.eifelle.com/wp-content/uploads/2010/06/HighPass.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2010/06/HighPass-300x172.png" alt="" title="High Pass" width="300" height="172" class="aligncenter size-medium wp-image-1273" /></a></center><br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2010/06/BandPass.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2010/06/BandPass-300x172.png" alt="" title="Band Pass" width="300" height="172" class="aligncenter size-medium wp-image-1276" /></a></center><br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2010/06/LowPass.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2010/06/LowPass-300x172.png" alt="" title="Low Pass" width="300" height="172" class="aligncenter size-medium wp-image-1275" /></a></center></p><p>As you may have noted, the filter amplifies the signal near the central frequency for each output for the attenuation I&#8217;ve selected. The issue is that selecting a higher one has also an impact on the stability of the filter at higher frequencies. The sum of the numerical attenuation and the numerical frequency should always be inferior to 2.</p><h4>Conclusion</h4><p>This numerical is really simple to implement, and it has few parameters. The dark side of this filter is that is not always stable and the central frequency seems to be always amplified. Other filters can be better balanced.</p><p>The code is available on <a
href="https://code.launchpad.net/~matthieu-brucher/+junk/QtVST">Launchpad</a>.</p>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2010/07/06/qtvst-a-chamberlin-variable-filter/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Book review: Modern C++ Design: Generic Programming and Design Patterns Applied</title><link>http://matt.eifelle.com/2010/03/16/book-review-modern-c-design-generic-programming-and-design-patterns-applied/</link> <comments>http://matt.eifelle.com/2010/03/16/book-review-modern-c-design-generic-programming-and-design-patterns-applied/#comments</comments> <pubDate>Tue, 16 Mar 2010 08:22:41 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[Addison-Wesley]]></category> <category><![CDATA[Book review]]></category> <category><![CDATA[C++]]></category> <category><![CDATA[Code quality]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=421</guid> <description><![CDATA[This book may be a little bit old (2001), but it&#8217;s still very relevant today. A lot of the material in the book is still not applied in C++ development, it may be time to apply it, doesn&#8217;t it?Content and opinions
The book is split in two parts: the basis and the more complex blocks.
For Alexandrescu, [...]]]></description> <content:encoded><![CDATA[<p>This book may be a little bit old (2001), but it&#8217;s still very relevant today. A lot of the material in the book is still not applied in C++ development, it may be time to apply it, doesn&#8217;t it?<br
/> <span
id="more-421"></span></p><h4>Content and opinions</h4><p>The book is split in two parts: the basis and the more complex blocks.</p><p>For Alexandrescu, there are four basis that should be covered: polycies, type lists, allocators and general techniques. Polycies is tightly related to the strategy pattern. The author discusses the elegant and complex architecture that can be written using them. Type lists is, in my opinion, the first step in using metaprogramming. Since Alexandrescu wrote this book, <a
href="http://www.boost.org/">the Boost library</a> implemented a lot of tools to work on type lists (as well as different types of type lists). This book explains very well how it can work and how to make the most of it. Allocators is perhaps one of the toughest topics in C++. Fortunately, the book provides a good implementation for small objects, and it is stable and efficient. I don&#8217;t think it is sound to implement your own, so using this one is perhaps best! Finally, the second chapter tackles different small tricks that can be easily implemented in every C++ code.</p><p>The second part is much more complex. Using the different basis blocks from the first part, Alexandrescu starts by creating a generic functor. It&#8217;s perhaps too much for 99% of the projects, but it still is a great proof of concept for elegant C++ architecture. The second topic in this part is singletons. Alexandrescu tries to address every side of this question, and although he provides an implementation, it is unfortunately not yet available/used in every framework. Next come smart pointers. They will be available in the next C++ standard, but only in a reduced form than what is exposed here. It is interesting to know the different issues that such a &#8220;simple&#8221; class has to solve. The next two chapters are dedicated to factories and abstract factories. As usual, one can create its own factory, but Alexandrescu tries to solve the more general problem. Before my favorite chapter, implementing the visitor pattern is addressed. It&#8217;s not the most widely used pattern, but it solves some tricky cases. Finally, even less used is the multiple dispatch function. Imagine you have to instanciate several functions with different types. You wan use type lists for this if you have only one type for each function. But if you have a more general case with two or three types that have to be instanciated, you have to use the multiple dispatch. It can be found in the intersection sample that is used in the book, but also in steering code (your favorite template class can be used with objects that can be of different types, like int matrices or float matrices, and you only have access to the base class of this type, and the class instance must call the correct instanciation).</p><p>Each time, the main focus is on automation, less code, more readability. Of course, this last point is only achieved if the reader has some knowledge of this modern C++ design&#8230;</p><h4>Conclusion</h4><p>Alexandrescu wrote Loki as a proof of concept of modern C++. The book is the result of the different solutions he thought of. Loki may be outdate by Boost in several fields, but it is also the foundations of the Boost library. If you want to write good professional-grade C++, you should read at least once this book and appropriate its content.</p><div
style="border: 1px solid #000; padding: 5px; margin-bottom: 15px; background: url(http://matt.eifelle.com/wp-content/uploads/2009/12/BN_Logo_3tier.jpg) right bottom no-repeat #ffffff;"><a
rel="nofollow" href="http://r.popshops.com/pp/73254/modern-c-design-generic-programming-and-design-patterns-applied"><img
style="width: 150px;" src="http://images.barnesandnoble.com/images/14560000/14569343.JPG" border="0" alt="Modern C++ Design: Generic Programming and Design Patterns Applied" /></a><br
/> <a
rel="nofollow" href="http://r.popshops.com/pp/73254/modern-c-design-generic-programming-and-design-patterns-applied">Modern C++ Design: Generic Programming and Design Patterns Applied</a><br
/> Price: $53.99</div><div
class="subcolumns"><div
style="border: 1px solid #000; padding: 5px; margin-bottom: 15px; background: url(http://matt.eifelle.com/wp-content/plugins/amazonsimpleadmin/img/amazon_US_small.gif) right bottom no-repeat #ffffff;"><div
style="width: 60px; float: left; margin-right: 5px;"> <a
href="http://www.amazon.com/exec/obidos/ASIN/0201704315/masbl03-20" target="_blank"><img
src="http://ecx.images-amazon.com/images/I/516030XDD8L._SL75_.jpg" width="60" height="75" border="0" /></a></div><div><p><a
href="http://www.amazon.com/exec/obidos/ASIN/0201704315/masbl03-20" target="_blank">Modern C++ Design: Generic Programming and Design Patterns Applied</a> (Paperback)<br
/> <span
style="font-size: 0.8em;">by <strong>Andrei Alexandrescu</strong></span><br
/> ISBN: 0201704315</p><p><strong>Price:</strong> <span
style="color: #990000; font-weight: bold;">USD 48.45</span><br
/> <strong>54 used &#038; new</strong> available from <span
style="color: #990000; font-weight: bold;">USD 15.04</span></p><p> <img
src="http://matt.eifelle.com/wp-content/plugins/amazonsimpleadmin/img/stars-4.5.gif" class="asa_rating_stars" /> | 4.5 | 70</div><div
style="clear: both;"></div></div></div>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2010/03/16/book-review-modern-c-design-generic-programming-and-design-patterns-applied/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Fixing the QtAGain plugin</title><link>http://matt.eifelle.com/2010/03/02/fixing-the-qtagain-plugin/</link> <comments>http://matt.eifelle.com/2010/03/02/fixing-the-qtagain-plugin/#comments</comments> <pubDate>Tue, 02 Mar 2010 08:21:58 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Music]]></category> <category><![CDATA[Qt]]></category> <category><![CDATA[VST]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=1130</guid> <description><![CDATA[Some months ago, I&#8217;ve modified the AGain plugin sample from the VST SDK to add a Qt window. At that time, I encountered an issue with Vsthost, which is a common VST host. The issue was that in windowed mode, the plugin&#8217;s UI wasn&#8217;t displayed. With Traktion, I didn&#8217;t have this problem, but the minihost [...]]]></description> <content:encoded><![CDATA[<p>Some months ago, <a
href="http://matt.eifelle.com/2009/12/01/vst-plugin-again-reloaded-with-a-qt-gui/">I&#8217;ve modified the AGain plugin sample</a> from the VST SDK to add a Qt window. At that time, I encountered an issue with <a
href="http://www.hermannseib.com/english/vsthost.htm">Vsthost</a>, which is a common VST host. The issue was that in windowed mode, the plugin&#8217;s UI wasn&#8217;t displayed. With Traktion, I didn&#8217;t have this problem, but the minihost (a sample from the SDK) also didn&#8217;t use the UI size.</p><p>When developing <a
href="http://matt.eifelle.com/2010/02/09/pyvst-another-ctypes-based-python-vst-wrapper/">pyvst</a>, I has to implement the retrieval of the size of the plugin, and I&#8217;ve decided to add this to QtAGain. I was surprised to see that it actually work with just giving back the UI size (so fixing this was less than 5 lines).</p><p>So now, I know that to impelment an UI for a VST plugin, I have to implement:</p><ul><li><strong>open()</strong></li><li><strong>close()</strong></li><li>but also <strong>getRect()</strong></li></ul><p>Don&#8217;t make the same mistake as I did, do implement all three of them, even if your favorite VST host can live without <strong>getRect()</strong>.</p><p>P.S.: Mixing Qt for VST UIs and wxPython for pyvst works really fine!</p>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2010/03/02/fixing-the-qtagain-plugin/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>PyVST: another ctypes-based Python VST wrapper</title><link>http://matt.eifelle.com/2010/02/09/pyvst-another-ctypes-based-python-vst-wrapper/</link> <comments>http://matt.eifelle.com/2010/02/09/pyvst-another-ctypes-based-python-vst-wrapper/#comments</comments> <pubDate>Tue, 09 Feb 2010 08:06:47 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[ctypes]]></category> <category><![CDATA[VST]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=1075</guid> <description><![CDATA[In a previous post, I&#8217;ve tried to use Qt for the editor window of a VST plugin. The thing is, I want to do more than just play with a GUI, I also want to see what is done to an audio stream by a plugin.
To do so, I&#8217;ve decided to expose the VST interface [...]]]></description> <content:encoded><![CDATA[<p>In <a
href="http://matt.eifelle.com/2009/12/01/vst-plugin-again-reloaded-with-a-qt-gui/">a previous post</a>, I&#8217;ve tried to use Qt for the editor window of a VST plugin. The thing is, I want to do more than just play with a GUI, I also want to see what is done to an audio stream by a plugin.</p><p>To do so, I&#8217;ve decided to expose the VST interface to Python. There are some implementation I&#8217;ve heard of, but they are based on Cython or other wrapping tools. Ctypes has the advantage of not needing a compilation step. There are also every functionality needed, as callback creation (plugins use a callback to ask the host some stuffs), and Python provides the additional mathematical tools to display what the plugin does. It may not be perfect, but it will be enough for a starter.<br
/> <span
id="more-1075"></span></p><h4>Wrapping the VST effect class</h4><p>Wrapping a VST class is not an easy task. The plugin is accessed by a C structure with pointer functions for the main functionalities: processing an audio flow (with floats or doubles), setting and getting parameters, and a general function for setting and getting information. Additionaly, when instantiating a plugin, a callback must be given. This ctypes callback will have to be stored inside the wrapper so that it stays valid through the plugin lifetime.</p><p>So the C structure is created as a class that inherits from ctypes.Structure. Then I have to populate a class that will call the correct function or return the appropriate element inside this structure.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=1075&amp;download=aeffect.py">aeffect.py</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p10755"><td
class="code" id="p1075code5"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> AEffect<span style="color: black;">&#40;</span>Structure<span style="color: black;">&#41;</span>:
  _fields_ = <span style="color: black;">&#91;</span>
                    <span style="color: black;">&#40;</span><span style="color: #483d8b;">'magic'</span>, c_int<span style="color: black;">&#41;</span>,
                    <span style="color: black;">&#40;</span><span style="color: #483d8b;">'dispatcher'</span>, c_void_p<span style="color: black;">&#41;</span>,
                    <span style="color: black;">&#40;</span><span style="color: #483d8b;">'process'</span>, c_void_p<span style="color: black;">&#41;</span>,
                    <span style="color: black;">&#40;</span><span style="color: #483d8b;">'setParameter'</span>, c_void_p<span style="color: black;">&#41;</span>,
                    <span style="color: black;">&#40;</span><span style="color: #483d8b;">'getParameter'</span>, c_void_p<span style="color: black;">&#41;</span>,
<span style="color: black;">&#40;</span>...<span style="color: black;">&#41;</span>
                    <span style="color: black;">&#93;</span>
&nbsp;
audiomaster_callback = CFUNCTYPE<span style="color: black;">&#40;</span>c_void_p, POINTER<span style="color: black;">&#40;</span>AEffect<span style="color: black;">&#41;</span>, c_int, c_int, c_long, c_void_p, c_float<span style="color: black;">&#41;</span></pre></td></tr></table></div><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=1075&amp;download=vstplugin.py">vstplugin.py</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p10756"><td
class="code" id="p1075code6"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> VSTPlugin<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, filename, audio_callback = basic_callback<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Constructor
    Parameters:
      filename is the name of the plugin to load
      audio_callback is the Python function to call (optional)
    &quot;&quot;&quot;</span>
    <span style="color: #008000;">self</span>.__lib = CDLL<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.__callback = audiomaster_callback<span style="color: black;">&#40;</span>audio_callback<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      <span style="color: #008000;">self</span>.__lib.<span style="color: black;">VSTPluginMain</span>.<span style="color: black;">argtypes</span> = <span style="color: black;">&#91;</span>audiomaster_callback, <span style="color: black;">&#93;</span>
      <span style="color: #008000;">self</span>.__lib.<span style="color: black;">VSTPluginMain</span>.<span style="color: black;">restype</span> = POINTER<span style="color: black;">&#40;</span>AEffect<span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>.__effect = <span style="color: #008000;">self</span>.__lib.<span style="color: black;">VSTPluginMain</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.__callback<span style="color: black;">&#41;</span>.<span style="color: black;">contents</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AttributeError</span>:
      <span style="color: #008000;">self</span>.__lib.<span style="color: black;">main</span>.<span style="color: black;">argtypes</span> = <span style="color: black;">&#91;</span>audiomaster_callback, <span style="color: black;">&#93;</span>
      <span style="color: #008000;">self</span>.__lib.<span style="color: black;">main</span>.<span style="color: black;">restype</span> = POINTER<span style="color: black;">&#40;</span>AEffect<span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>.__effect = <span style="color: #008000;">self</span>.__lib.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.__callback<span style="color: black;">&#41;</span>.<span style="color: black;">contents</span></pre></td></tr></table></div><p>The main VST function must be called through a dispatch function available in the structure. I will only show of them:</p><h4>Rewritting the minihost sample</h4><p>The minihost sample prints some details of the VST plugin and then displays them. Here, I&#8217;ll do the same, but I&#8217;ve processed a sine-sweep signal and then display the result. If the 64bits processing is available, I will use it.</p><p>Here are some info that are displayed prior to the processing for the <a
href="http://bigtick.pastnotecut.org/index.php?action=PROD&#038;pcode=200&#038;lang=EN">Big Tick NastyShaper plugin</a>:</p><pre>
Plugin name:
Vendor name:
Product name:
numPrograms = 16
numParams = 11
numInputs = 2
numOutputs = 2

Program 000: Nasty Shaper
(...)
Program 015: Nasty Shaper
Param 000: Pre-Gain [0.00 dB] (normalized = 0.500000)
Param 001: Post-Gain [0.00 dB] (normalized = 0.500000)
Param 002: WS1 [25.00  %] (normalized = 0.250000)
Param 003: WS2 [35.00  %] (normalized = 0.350000)
Param 004: WS3 [45.00  %] (normalized = 0.450000)
Param 005: WS4 [55.00  %] (normalized = 0.550000)
Param 006: WS5 [20.00  %] (normalized = 0.200000)
Param 007: WS6 [60.00  %] (normalized = 0.600000)
Param 008: WS7 [30.00  %] (normalized = 0.300000)
Param 009: WS8 [40.00  %] (normalized = 0.400000)
Param 010: Oversample [OFF  ] (normalized = 0.000000)
Testing with floats (32bits)
</pre><p>Here is a graphical result. On the first row, I display the original input, on the second row is the associated output.</p><p><center><a
href="http://matt.eifelle.com/wp-content/uploads/2010/02/Big-Tick-NastyShaper.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2010/02/Big-Tick-NastyShaper-300x225.png" alt="" title="Big Tick NastyShaper spectrogram" width="300" height="225" class="aligncenter size-medium wp-image-1101" /></a</center></p><h4>To be continued</h4><p>The whole 2.4 standard is not yet wrapped, far from it. There is still much to do to be able to use this wrapper class for every plugin (how do I load an impulse for a convolution reverb for instance), but it can still help analyze how a lot of plugins change the audio signal.</p><p>Some plugins are not working yet (mainly because every input and output must be connected), but I&#8217;m working on it <img
src='http://matt.eifelle.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>The code is available on <a
href="https://launchpad.net/pyvst">Launchpad</a>.</p><form
action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input
type="hidden" name="cmd" value="_xclick" /> <input
type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input
type="hidden" name="item_name" value="Buy Me a Coffee!" /><input
type="hidden" name="currency_code" value="USD" /><span
style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br
/><br
/><select
id="amount" name="amount" class=""><option
value="3">Capuccino - 3$</option><option
value="6">Frappuccino - 6$</option><option
value="10">Hot Chocolate - 10$</option><option
value="20">Expensive Coffee - 20$</option><option
value="50">Alien Coffee - 50$</option></select><br
/><br
/><strong>Other Amount:</strong><br
/><br
/><input
type="text" name="amount" size="10" title="Other donate" value="" /><br
/><br
/><strong> Your Email Address :</strong><input
type="hidden" name="on0" value="Reference" /><br
/><br
/><input
type="text" name="os0" maxlength="60" /> <br
/><br
/> <input
type="hidden" name="no_shipping" value="2" /> <input
type="hidden" name="no_note" value="1" /> <input
type="hidden" name="mrb" value="3FWGC6LFTMTUG" /> <input
type="hidden" name="bn" value="IC_Sample" /> <input
type="hidden" name="return" value="http://matt.eifelle.com" /><input
type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2010/02/09/pyvst-another-ctypes-based-python-vst-wrapper/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Thinking of good practices when developing with accelerators</title><link>http://matt.eifelle.com/2010/01/05/thinking-of-good-practices-when-developing-with-accelerators/</link> <comments>http://matt.eifelle.com/2010/01/05/thinking-of-good-practices-when-developing-with-accelerators/#comments</comments> <pubDate>Tue, 05 Jan 2010 08:48:57 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Design Patterns]]></category> <category><![CDATA[Development process]]></category> <category><![CDATA[Distributed Computing]]></category> <category><![CDATA[High Performance Computing]]></category> <category><![CDATA[Tools]]></category> <category><![CDATA[CUDA]]></category> <category><![CDATA[Fortran]]></category> <category><![CDATA[Grid computing]]></category> <category><![CDATA[HMPP]]></category> <category><![CDATA[MPI]]></category> <category><![CDATA[Multithreaded applications]]></category> <category><![CDATA[Scientific computing]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=997</guid> <description><![CDATA[Due to the end of the free lunch, manufacturers started to provide differents processing units and developers started to go parallel. It&#8217;s kind of back to the future, as accelerators existed before today (the x87 FPU started as a coprocessor, for instance). If those accelerators were integrated into the CPU, their instruction set were also.
Today&#8217;s [...]]]></description> <content:encoded><![CDATA[<p>Due to the end of the <a
href="http://www.gotw.ca/publications/concurrency-ddj.htm">free lunch</a>, manufacturers started to provide differents processing units and developers started to go parallel. It&#8217;s kind of back to the future, as accelerators existed before today (the x87 FPU started as a coprocessor, for instance). If those accelerators were integrated into the CPU, their instruction set were also.</p><p>Today&#8217;s accelerators are not there yet. The tools are not ready yet (code translators) and usual programming practices may not be adequate. All the ecosystem will evolve, accelerators will change (GPUs are the main trend, but they will be different in a few years), so what you will do today needs to be shaped with these changes in mind. How is it possible to do so? Is it even possible?<br
/> <span
id="more-997"></span></p><h4>Available code translators</h4><p>Code translators are the easiest path to solution. I know two of them.</p><p>The first is the <a
href="http://www.pgroup.com/resources/accel.htm">PGI compiler</a>. It only supports CUDA and the Fortran and C99 language. I didn&#8217;t use it yet, also I plan of testing it in the near future. It is based on pragmas, and the compiler generates the CUDA microcode.</p><p>The second solution is <a
href="http://www.caps-entreprise.com/fr/page/index.php?id=49&amp;p_p=36">HMPP</a>. It supports more than just CUDA (also CAL/IL or OpenCL) and Fortran/C (also Java now). As the PGI compiler, it is based on pragmas, and a excellent thing is that it detects the available accelerators and launches the correct kernel (if you authorized it) or the original code. You can also modify the generated code to put your own (you can tune the code for instance, which may give you an additional x2 factor). Unfortunately, it is not possible to call functions inside the parallelized kernels, which means that only simple or badly-written (too many lines or duplicated code) kernels can be called. I think this is the same for the PGI compiler.</p><p>It seems that code translators still need work:</p><ul><li>only few accelerators are supported (CUDA, and sometimes CAL/IL or OpenCL),</li><li>almost no langage (Fortran/C/Java, a lot of Virtual Machines should be able to use them natively, without developers using specific tools),</li><li>only one function can be parallelized at a time.</li></ul><p>The last point is currently the biggest issue. You need to cut your function int pieces to have clean code and a good portability/evolutivity for the future.</p><p>This is why one still need to program a lot for those accelerators, and so we need to adapt our programming practices, develop in the accelerators&#8217; native langages (even if we know that they may disappear in a few years).</p><h4>Developping your own &#8220;tool chain&#8221; for accelerators</h4><p>For accelerators, there are a lot of things that needs to be done each time: copying some data, computing and getting some data back. These are the steps that code translators automate, in fact it is a common practice to use tools to automate stuff. The issue is that complex kernels are not supported by those translators. So what?</p><p>Creating automatic functions that will copy the data you need is in fact very common in metaprogramming. Coding the kernel on an accelerator is in fact not that difficult: the manufacturers provide the needed compilers (that&#8217;s what nVidia does and the success of the tool chain cannot be denied), and this is really the cornerstone. One has to write more code, some parts are less portable (because they are written in one of the accelerator&#8217;s languages), but in the end, with metaprogramming, the code can be better tuned, enhanced and read. This is the leverage of the accelerators.</p><h4>Conclusion</h4><p>Why do we care developing for accelerators? We know that they will go away. Before they do, they are the only way of speeding up our software. Code translators are the best tools to develop in a portable way, but they need time to support more accelerators, languages and method of programming. When CPUs will be on a par with accelerators, their progress will help compilers to target them correctly. It&#8217;s just a matter of time.<br
/> Meanwhile, metaprogrammin is the next best solution to automate processes that code translators cannot support yet.</p>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2010/01/05/thinking-of-good-practices-when-developing-with-accelerators/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Book review: The Art of Concurrency: A Thread Monkey&#8217;s Guide to Writing Parallel Applications</title><link>http://matt.eifelle.com/2009/12/08/book-review-the-art-of-concurrency-a-thread-monkeys-guide-to-writing-parallel-applications/</link> <comments>http://matt.eifelle.com/2009/12/08/book-review-the-art-of-concurrency-a-thread-monkeys-guide-to-writing-parallel-applications/#comments</comments> <pubDate>Tue, 08 Dec 2009 07:57:54 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[Book review]]></category> <category><![CDATA[C++]]></category> <category><![CDATA[Debugger]]></category> <category><![CDATA[O'Reilly]]></category> <category><![CDATA[Profiler]]></category> <category><![CDATA[Tools]]></category> <category><![CDATA[Multithreaded applications]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=847</guid> <description><![CDATA[Free lunch is over, it&#8217;s time to go concurrent. The Art of Concurrency addresses the need for a workflow to develop concurrent/parallel applications.Mainly based on multithreaded applications, the book covers pthreads, Windows threads, OpenMP or Intel Threading Building Blocks library. It also covers some part of multiprocess applications if there are differences with threaded ones.
Content [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://www.gotw.ca/publications/concurrency-ddj.htm">Free lunch is over</a>, it&#8217;s time to go concurrent. <span
style="text-decoration: underline;">The Art of Concurrency</span> addresses the need for a workflow to develop concurrent/parallel applications.<br
/> <span
id="more-847"></span><br
/> Mainly based on multithreaded applications, the book covers pthreads, Windows threads, OpenMP or <a
href="http://matt.eifelle.com/2008/07/09/book-review-intel-threading-building-blocks-outfitting-c-for-multi-core-processor-parallelism/">Intel Threading Building Blocks library</a>. It also covers some part of multiprocess applications if there are differences with threaded ones.</p><h4>Content and opinions</h4><p>The book starts with two chapers on what actions to take before parallelizing and what can and what cannot. Before the usual algorithms that can be parallelized, the author takes three chapters to explain how you may achieve your goal. Ensuring correctness is a difficult task, so the book gives 8 rules to help and then an explanation of several support libraries that can be used.</p><p>The biggest part of the book, as I&#8217;ve hinted, is dedicated to simple but usual algorithms that may be parallelized: sums and scans, mapreduce, sorts, searches, and graph algorithms. Each time, several different algorithms are first coded in a serial way and then parallelized with possibly different support libraries. Also each time, the efficiency, the simplicity, the portability and the scalability conclude the sub art: it helps standing back.</p><p>The last chapter is a small overview of the additional tools that you may use (but they are not mandatory). They are mainly Intel&#8217;s tools, but it&#8217;s mainly because Intel provides the developer with some of the best tools.</p><h4>Conclusion</h4><p>Although the author works for Intel, he doesn&#8217;t expose Intel tools more than others. The book tone is adequate, not too much serious, not like a &#8220;For Dummies&#8221;, so just enjoyable.</p><p>If you need advices to parallelize your applications and you don&#8217;t want to buy <a
href="http://matt.eifelle.com/2009/03/10/book-review-patterns-for-parallel-programming/">Patterns for Parallel Programming</a>, buy this one (well, buy it anyway).</p><div
style="border: 1px solid #000; padding: 5px; margin-bottom: 15px; background: url(http://matt.eifelle.com/wp-content/uploads/2009/12/BN_Logo_3tier.jpg) right bottom no-repeat #ffffff;"> <a
rel="nofollow" href="http://r.popshops.com/pp/69253/the-art-of-concurrency-a-thread-monkey-s-guide-to-writing-parallel-applications"><img
style="width: 150px;" src="http://images.barnesandnoble.com/images/37180000/37189689.JPG" border="0" alt="The Art of Concurrency: A Thread Monkey's Guide to Writing Parallel Applications" /></a><br
/> <a
rel="nofollow" href="http://r.popshops.com/pp/69253/the-art-of-concurrency-a-thread-monkey-s-guide-to-writing-parallel-applications">The Art of Concurrency: A Thread Monkey&#8217;s Guide to Writing Parallel Applications</a><br
/> Price: $40.49</div><div
class="subcolumns"><div
style="border: 1px solid #000; padding: 5px; margin-bottom: 15px; background: url(http://matt.eifelle.com/wp-content/plugins/amazonsimpleadmin/img/amazon_US_small.gif) right bottom no-repeat #ffffff;"><div
style="width: 57px; float: left; margin-right: 5px;"> <a
href="http://www.amazon.com/exec/obidos/ASIN/0596521537/masbl03-20" target="_blank"><img
src="http://ecx.images-amazon.com/images/I/51QaJYFLmGL._SL75_.jpg" width="57" height="75" border="0" /></a></div><div><p><a
href="http://www.amazon.com/exec/obidos/ASIN/0596521537/masbl03-20" target="_blank">The Art of Concurrency: A Thread Monkey&#8217;s Guide to Writing Parallel Applications</a> (Paperback)<br
/> <span
style="font-size: 0.8em;">by <strong>Clay Breshears</strong></span><br
/> ISBN: 0596521537</p><p><strong>Price:</strong> <span
style="color: #990000; font-weight: bold;">USD 38.48</span><br
/> <strong>40 used &#038; new</strong> available from <span
style="color: #990000; font-weight: bold;">USD 17.99</span></p><p> <img
src="http://matt.eifelle.com/wp-content/plugins/amazonsimpleadmin/img/stars-3.5.gif" class="asa_rating_stars" /> | 3.5 | 6</div><div
style="clear: both;"></div></div></div>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/12/08/book-review-the-art-of-concurrency-a-thread-monkeys-guide-to-writing-parallel-applications/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>VST plugin AGain reloaded with a Qt GUI</title><link>http://matt.eifelle.com/2009/12/01/vst-plugin-again-reloaded-with-a-qt-gui/</link> <comments>http://matt.eifelle.com/2009/12/01/vst-plugin-again-reloaded-with-a-qt-gui/#comments</comments> <pubDate>Tue, 01 Dec 2009 09:19:54 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Music]]></category> <category><![CDATA[Qt]]></category> <category><![CDATA[VST]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=793</guid> <description><![CDATA[Years ago, I&#8217;ve tried to use the GPL version of Qt, but it couldn&#8217;t be done without a Qt Solution that was at the time non-free. Now, Nokia has freed and Qt and the appropriate Qt Solution.
I&#8217;ve searched if someone has already used this new version to create a VST plugin. The only blog post [...]]]></description> <content:encoded><![CDATA[<p>Years ago, I&#8217;ve <a
href="http://lists.trolltech.com/qt-interest/2006-01/thread00332-0.html">tried to use the GPL version of Qt</a>, but it couldn&#8217;t be done without a Qt Solution that was at the time non-free. Now, Nokia has freed and Qt and <a
href="http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Windows/qtwinmigrate/">the appropriate Qt Solution</a>.</p><p>I&#8217;ve searched if someone has already used this new version to create a VST plugin. <a
href="http://vokicodder.blogspot.com/2009/04/vst-plugins-with-qt-user-interface.html">The only blog post I&#8217;ve found</a> does not use the Qt Solution and is not perfect. According to the documentation what is missing in this solution is precisely what the Solution should do. So let&#8217;s try it.<br
/> <span
id="more-793"></span></p><p>So I&#8217;ve download the 2.4 VST SDK as well as Qt 4.5 and the WinMigrate Solution. As I&#8217;ll be using Qt with signals and slots, I used SCons to build my small plugin.</p><h4>Designing the interface and testing it</h4><p>The interface is really simple: a label with a name, the value of the gain and a slider. Here is what it looks like in Tracktion 3:<br
/> <a
href="http://matt.eifelle.com/wp-content/uploads/2009/11/Tracktion-qtagain.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/11/Tracktion-qtagain.png" alt="QtAgain in Tracktion 3" title="QtAgain in Tracktion 3" width="300" height="178" class="aligncenter size-full wp-image-835" /></a><br
/> The slider takes values from 0 to 99, value that will be displayed in a label. This will be the gain factor in percentage.</p><p>Nothing fancy here with the class header:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=793&amp;download=QVstPanel.h">QVstPanel.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p79315"><td
class="code" id="p793code15"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> QVstPanel <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> QWinWidget
<span style="color: #008000;">&#123;</span>
  HWND h_parent<span style="color: #008080;">;</span>
  AudioEffectX <span style="color: #000040;">*</span>again<span style="color: #008080;">;</span>
&nbsp;
  QLabel <span style="color: #000040;">*</span>valueLabel<span style="color: #008080;">;</span>
  QSlider <span style="color: #000040;">*</span>slider<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  QVstPanel<span style="color: #008000;">&#40;</span>AudioEffectX <span style="color: #000040;">*</span>again, HWND h_parent <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>The panel is created with a link to the actual audio effect (needed in the future) and the parent HWND given by the host.</p><p>The actual code is even simplier:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=793&amp;download=QVstPanel.cpp">QVstPanel.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p79316"><td
class="code" id="p793code16"><pre class="cpp" style="font-family:monospace;">QVstPanel<span style="color: #008080;">::</span><span style="color: #007788;">QVstPanel</span><span style="color: #008000;">&#40;</span>AudioEffectX <span style="color: #000040;">*</span>again, HWND h_parent<span style="color: #008000;">&#41;</span>
<span style="color: #008080;">:</span>QWinWidget<span style="color: #008000;">&#40;</span>h_parent, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span>, again<span style="color: #008000;">&#40;</span>again<span style="color: #008000;">&#41;</span>, h_parent<span style="color: #008000;">&#40;</span>h_parent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  setAttribute<span style="color: #008000;">&#40;</span>Qt<span style="color: #008080;">::</span><span style="color: #007788;">WA_DeleteOnClose</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  QLabel <span style="color: #000040;">*</span>label <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QLabel<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Gain&quot;</span>, <span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  valueLabel <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QLabel<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;0&quot;</span>, <span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  slider <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QSlider<span style="color: #008000;">&#40;</span>Qt<span style="color: #008080;">::</span><span style="color: #007788;">Horizontal</span>, <span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  QHBoxLayout <span style="color: #000040;">*</span>layout <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QHBoxLayout<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  layout<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>addWidget<span style="color: #008000;">&#40;</span>label<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  layout<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>addWidget<span style="color: #008000;">&#40;</span>valueLabel<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  layout<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>addWidget<span style="color: #008000;">&#40;</span>slider<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  setLayout<span style="color: #008000;">&#40;</span>layout<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Now this is the class that will be opened by the VST editor this way:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=793&amp;download=guiagain.h">guiagain.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p79317"><td
class="code" id="p793code17"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> QtAGain <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> AEffEditor
<span style="color: #008000;">&#123;</span>
  QWinWidget<span style="color: #000040;">*</span> widget<span style="color: #008080;">;</span>
  AudioEffectX<span style="color: #000040;">*</span> effect<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  QtAGain<span style="color: #008000;">&#40;</span>AudioEffectX<span style="color: #000040;">*</span> effect<span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span>widget<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span>, effect<span style="color: #008000;">&#40;</span>effect<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ~QtAGain<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">bool</span> open<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> close<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=793&amp;download=guiagain.cpp">guiagain.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p79318"><td
class="code" id="p793code18"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> QtAGain<span style="color: #008080;">::</span><span style="color: #007788;">open</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  AEffEditor<span style="color: #008080;">::</span><span style="color: #007788;">open</span> <span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  widget <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QVstPanel<span style="color: #008000;">&#40;</span>effect, <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>HWND<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  widget<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>move<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  widget<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>adjustSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  widget<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setMinimumSize<span style="color: #008000;">&#40;</span>widget<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>size<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  widget<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>show<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> QtAGain<span style="color: #008080;">::</span><span style="color: #007788;">close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000dd;">delete</span> widget<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Now, in AGain&#8217;s constructor, I can add my custom editor:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=793&amp;download=again.cpp">again.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p79319"><td
class="code" id="p793code19"><pre class="cpp" style="font-family:monospace;">  QtAGain<span style="color: #000040;">*</span> again <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> QtAGain<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  setEditor<span style="color: #008000;">&#40;</span>again<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>So there isn&#8217;t anything really fancy, it kind of works out of the box. Unfortunately, it doesn&#8217;t work as well as in T3 in every host. I cannot resize the window VSTHost gives to the <em>open()</em> method, even if I tested it with almost all solutions I&#8217;ve found on MSDN.</p><h4>Adding interactivity</h4><p>Now, I will eventually use the power of Qt. The <em>QtAGain</em> class will be a <em>QObject</em>, as will be the <em>AGain</em> effect. This way, I can connect signals from the GUI to the effect when a parameter is modified by the user, and vice-versa, the UI will be updated when the effect sees a parameter change (indicated by the host for instance).</p><p>So I add a connection inside the <em>QVstPanel</em> constructor to a new slot that will update the underlying effect:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=793&amp;download=QVstPanel.cpp">QVstPanel.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p79320"><td
class="code" id="p793code20"><pre class="cpp" style="font-family:monospace;">connect<span style="color: #008000;">&#40;</span>slider, SIGNAL<span style="color: #008000;">&#40;</span>sliderMoved<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">this</span>, SLOT<span style="color: #008000;">&#40;</span>update<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> QVstPanel<span style="color: #008080;">::</span><span style="color: #007788;">update</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  valueLabel<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setText<span style="color: #008000;">&#40;</span>QString<span style="color: #008080;">::</span><span style="color: #007788;">number</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  again<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setParameter<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, value <span style="color: #000040;">/</span> <span style="color: #0000dd;">100</span>.<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>The trouble arises from the effect: what should I do if the effect gets a <em>setParameter</em> that didn&#8217;t come from the UI? If there is no UI, everything is fine though. So I will add a signal inside <em>QtAgain</em>, the editor, that will forward a signal emitted by <em>AGain</em> only if the UI is up.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=793&amp;download=again.cpp">again.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p79321"><td
class="code" id="p793code21"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> AGain<span style="color: #008080;">::</span><span style="color: #007788;">setParameter</span> <span style="color: #008000;">&#40;</span>VstInt32 index, <span style="color: #0000ff;">float</span> value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  fGain <span style="color: #000080;">=</span> value<span style="color: #008080;">;</span>
  emit update<span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>So now, the signal that the UI will get is a float, so I can reuse the name <em>update()</em> for my slot:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=793&amp;download=QVstPanel.cpp">QVstPanel.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p79322"><td
class="code" id="p793code22"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> QVstPanel<span style="color: #008080;">::</span><span style="color: #007788;">update</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> intValue <span style="color: #000080;">=</span> value <span style="color: #000040;">*</span> <span style="color: #0000dd;">100</span><span style="color: #008080;">;</span>
  valueLabel<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setText<span style="color: #008000;">&#40;</span>QString<span style="color: #008080;">::</span><span style="color: #007788;">number</span><span style="color: #008000;">&#40;</span>intValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  slider<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>setValue<span style="color: #008000;">&#40;</span>intValue<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Of course, you will have several parameters inside your VST plugin, I suggest you use the editor instance to the mapping between the effect and the different slots you may have inside your UI.</p><h4>Conclusion</h4><p>Although I couldn&#8217;t get it to work with VSTHost (the host widget is not resized), I think the <em>QWinWidget</em> is a good class to help developing Qt VST plugins. I may want to use this skeletton to test some digital effects with a cool UI.</p><p>If you have a solution to make VSTHost work, I&#8217;m all ears.</p><p>The code is available on <a
href="https://code.launchpad.net/~matthieu-brucher/+junk/QtAGain">Launchpad</a>.</p><form
action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input
type="hidden" name="cmd" value="_xclick" /> <input
type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input
type="hidden" name="item_name" value="Buy Me a Coffee!" /><input
type="hidden" name="currency_code" value="USD" /><span
style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br
/><br
/><select
id="amount" name="amount" class=""><option
value="3">Capuccino - 3$</option><option
value="6">Frappuccino - 6$</option><option
value="10">Hot Chocolate - 10$</option><option
value="20">Expensive Coffee - 20$</option><option
value="50">Alien Coffee - 50$</option></select><br
/><br
/><strong>Other Amount:</strong><br
/><br
/><input
type="text" name="amount" size="10" title="Other donate" value="" /><br
/><br
/><strong> Your Email Address :</strong><input
type="hidden" name="on0" value="Reference" /><br
/><br
/><input
type="text" name="os0" maxlength="60" /> <br
/><br
/> <input
type="hidden" name="no_shipping" value="2" /> <input
type="hidden" name="no_note" value="1" /> <input
type="hidden" name="mrb" value="3FWGC6LFTMTUG" /> <input
type="hidden" name="bn" value="IC_Sample" /> <input
type="hidden" name="return" value="http://matt.eifelle.com" /><input
type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/12/01/vst-plugin-again-reloaded-with-a-qt-gui/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Interactive Raytracer 4: Reflection rays</title><link>http://matt.eifelle.com/2009/09/29/interactive-raytracer-4-reflection-rays/</link> <comments>http://matt.eifelle.com/2009/09/29/interactive-raytracer-4-reflection-rays/#comments</comments> <pubDate>Tue, 29 Sep 2009 08:13:40 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Raytracing]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=763</guid> <description><![CDATA[Now, I will show the implementation of reflection (from the Whitted approach). It is basically using the reflection law and recurse the ray cast.
Reflection rays
Each object can reflect a ray more or less from a different object. A mirror would reflect the light totally, and a matte object would reflect nothing. Each new reflection is [...]]]></description> <content:encoded><![CDATA[<p>Now, I will show the implementation of reflection (from the Whitted approach). It is basically using the reflection law and recurse the ray cast.</p><h4>Reflection rays</h4><p>Each object can reflect a ray more or less from a different object. A mirror would reflect the light totally, and a matte object would reflect nothing. Each new reflection is a new ray tracing call, so it can be costly. The number of recursion levels will be fixed, even if an object reflects nothing: this will be implemented through shaders in the future.<br
/> <span
id="more-763"></span><br
/> Knowing the normal to the intersection point, the reflected ray can be computing: it&#8217;s just the symmetric ray of the incident ray.</p><p>Some elements will be stored in a material structure that will be used to compute the reflected color:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=763&amp;download=primitives.h">primitives.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p76326"><td
class="code" id="p763code26"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">/// Caracteristics of the primitive at a given point</span>
<span style="color: #0000ff;">struct</span> MaterialPoint
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">/// Normale at the intersection point</span>
  Normal3df normal<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>In the past, I&#8217;ve used this structure to contain diffuse or reflection colors, but usually, an object has not mirror property in red and matte in green, so this was deleted.</p><p>Now, I need to update the raytracer:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=763&amp;download=raytracer.cpp">raytracer.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p76327"><td
class="code" id="p763code27"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">computeColor</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, Color<span style="color: #000040;">&amp;</span> color, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> level<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">float</span> dist<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">long</span> index <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getFirstCollision<span style="color: #008000;">&#40;</span>ray, dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>index <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
&nbsp;
  Primitive<span style="color: #000040;">*</span> primitive <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getPrimitive<span style="color: #008000;">&#40;</span>index<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  MaterialPoint caracteristics<span style="color: #008080;">;</span>
  primitive<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>computeColorNormal<span style="color: #008000;">&#40;</span>ray, dist, caracteristics<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  color <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>computeColor<span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">origin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> dist <span style="color: #000040;">*</span> ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, caracteristics, primitive<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>level <span style="color: #000080;">&lt;</span> levels<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Ray ray_sec<span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">origin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> dist <span style="color: #000040;">*</span> ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> caracteristics.<span style="color: #007788;">normal</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> caracteristics.<span style="color: #007788;">normal</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ray_sec.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span>.<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>norm2<span style="color: #008000;">&#40;</span>ray_sec.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Color color_sec<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    computeColor<span style="color: #008000;">&#40;</span>ray_sec, color_sec, level<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    color <span style="color: #000040;">+</span><span style="color: #000080;">=</span> mult<span style="color: #008000;">&#40;</span>color_sec, caracteristics.<span style="color: #007788;">reflect</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>I need to update the original collision method. Contrary to <a
href="http://matt.eifelle.com/2009/09/08/interactive-raytracer-3-lights-and-shadows/">the last post</a>, it isn&#8217;t enough to test if one primitive is hit by a ray, but I still need to test if the retrieved intersection isn&#8217;t too close to the original object. Indeed, I could be testing the intersection with the current object because of numerical instabilities. So I&#8217;ve added a minimum distance test:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=763&amp;download=simple_scene.cpp">simple_scene.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p76328"><td
class="code" id="p763code28"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">long</span> SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">getFirstCollision</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span><span style="color: #000040;">&amp;</span> dist<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">float</span> min_dist <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">numeric_limits</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">float</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">long</span> min_primitive <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>Primitive<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it <span style="color: #000080;">=</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> primitives.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">float</span> dist<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">bool</span> test <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>intersect<span style="color: #008000;">&#40;</span>ray, dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>test <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0001f</span> <span style="color: #000080;">&lt;</span> dist<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>dist <span style="color: #000080;">&lt;</span> min_dist<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      min_primitive <span style="color: #000080;">=</span> it <span style="color: #000040;">-</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      min_dist <span style="color: #000080;">=</span> dist<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>min_primitive <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">else</span>
  <span style="color: #008000;">&#123;</span>
    dist <span style="color: #000080;">=</span> min_dist<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> min_primitive<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Now, when a ray hits a primitive, a material structure is used to get the normal and the other parameters at the intersection point.</p><p>The <strong>level</strong> parameter indicates the recursion level we are at. If the level isn&#8217;t high enough, the symmetric ray is cast and the reflected color is used to add the color of the object. I do not test if the contribution is too small, it is possible in some implementations, but not here. Also, adding more recursion is not always costly, as after a while, all rays point outside the scene.</p><h4>Result</h4><p>Now, this is the result with 3 recursion levels and 3 lights:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt_reflections.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt_reflections-300x226.png" alt="IRT with reflection rays" title="IRT with reflection rays" width="300" height="226" class="aligncenter size-medium wp-image-770" /></a></center></p><form
action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input
type="hidden" name="cmd" value="_xclick" /> <input
type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input
type="hidden" name="item_name" value="Buy Me a Coffee!" /><input
type="hidden" name="currency_code" value="USD" /><span
style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br
/><br
/><select
id="amount" name="amount" class=""><option
value="3">Capuccino - 3$</option><option
value="6">Frappuccino - 6$</option><option
value="10">Hot Chocolate - 10$</option><option
value="20">Expensive Coffee - 20$</option><option
value="50">Alien Coffee - 50$</option></select><br
/><br
/><strong>Other Amount:</strong><br
/><br
/><input
type="text" name="amount" size="10" title="Other donate" value="" /><br
/><br
/><strong> Your Email Address :</strong><input
type="hidden" name="on0" value="Reference" /><br
/><br
/><input
type="text" name="os0" maxlength="60" /> <br
/><br
/> <input
type="hidden" name="no_shipping" value="2" /> <input
type="hidden" name="no_note" value="1" /> <input
type="hidden" name="mrb" value="3FWGC6LFTMTUG" /> <input
type="hidden" name="bn" value="IC_Sample" /> <input
type="hidden" name="return" value="http://matt.eifelle.com" /><input
type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/09/29/interactive-raytracer-4-reflection-rays/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Parallel Studio: Using Advisor Lite</title><link>http://matt.eifelle.com/2009/09/22/parallel-studio-using-advisor-lite/</link> <comments>http://matt.eifelle.com/2009/09/22/parallel-studio-using-advisor-lite/#comments</comments> <pubDate>Tue, 22 Sep 2009 08:02:28 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Distributed Computing]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Profiler]]></category> <category><![CDATA[Tools]]></category> <category><![CDATA[Advisor]]></category> <category><![CDATA[Intel]]></category> <category><![CDATA[Parallel Studio]]></category> <category><![CDATA[Raytracing]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=647</guid> <description><![CDATA[After reviewing Parallel Studio, I&#8217;ve decided to look after Advisor Lite. Intel offers it for free, before the actual Advisor is released with a future Parallel Studio version. It aims at steering multithreaded development with Parallel Studio.I&#8217;ve started with the Starting Guide, and in fact, it is the best way to know how to use [...]]]></description> <content:encoded><![CDATA[<p>After reviewing <a
href="http://matt.eifelle.com/2009/07/07/review-of-intel-parallel-studio/">Parallel Studio</a>, I&#8217;ve decided to look after Advisor Lite. Intel offers it for free, before the actual Advisor is released with a future Parallel Studio version. It aims at steering multithreaded development with Parallel Studio.<br
/> <span
id="more-647"></span><br
/> I&#8217;ve started with the Starting Guide, and in fact, it is the best way to know how to use this plugin. Advisor offers four steps, two of them being short-cuts to the online help, and the two others link to some Parallel Studio actions (namely hotspot in Amplifier and the threaded memory check with Inspector).<br
/> The online help is interesting, but once you know how you can parallelize an application and what to look for, the two Parallel Studio actions with the help of some macros presented in the Starting Guide are the only thing you need.</p><h4>Test on parallelizing a custom library</h4><p>I&#8217;ve decided to test Advisor Lite on my <a
href="http://matt.eifelle.com/category/cpp/interactive-raytracer/">Interactive Raytracer</a>. This is a test to verify if Advisor Lite finds the adequate parallelization and the memory sharing issues. It is a simple raytracer, so it can be parallelized for each pixel in the image. The only memory sharing issue that I know of is in the kd-tree ray traversal.</p><h4>Profiling the library</h4><p>First, I will profile the library. For the complete Advisor Lite workflow, I have to use Intel Compiler, and as it is faster than Microsoft&#8217;s compiler, I will use the <strong>timeit_image.py</strong> script instead of the <strong>measure_image.py</strong> I&#8217;ve used when profiling with <a
href="http://matt.eifelle.com/2009/04/07/profiling-with-valgrind/">Valgrind</a> or <a
href="http://matt.eifelle.com/2009/08/18/profiling-with-visual-studio-performance-tool/">Visual Studio</a>.</p><p>Amplifier can show the results in a bottom-up or in a top-down manner. Unfortunately, you only have the exclusive timing that is displayed. In my case, when displaying bottom-up results, the method <strong>getEntryExitDistances()</strong> is the most costly one. In the top-down view, unfortunately, I can&#8217;t have a simple tree, as it can be seen in the following view:</p><p><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-profile-advisor.png"><img
class="aligncenter size-medium wp-image-727" title="IRT: Amplifier profile (call-tree view)" src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-profile-advisor-300x187.png" alt="IRT: Amplifier profile (call-tree view)" width="300" height="187" /></a></p><p>In Visual Studio, I have the same results &#8211; more or less -, but with a correct top-down call-tree:</p><p><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-profile-msvc.png"><img
class="aligncenter size-medium wp-image-695" title="Profile returned by Visual Studio Performance Tool (call-tree)" src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-profile-msvc-300x187.png" alt="Profile returned by Visual Studio Performance Tool (call-tree)" width="300" height="187" /></a></p><p>The method <strong>getEntryExitDistances()</strong> cannot be parallelized: it is recursively called, several times per pixel, which would lead to a lot of memory contention. The simpler task is thus to parallelized the pixel rendering, a perfect data-parallel problem.</p><h4>Annotation of the code</h4><p>OK, now I can annotate my code. I had to dig inside the help for this, as it was not mentionned in the Starting Guide that Intel provides a header, <strong>annotate.h</strong>, which mimics the issues you may encounter in a multithreaded application.</p><p>So you need to read at least once the online help so that you know the available annotation macros, how you can get them and how they will retrieve what you need. Once the code is annotated, it must be recompiled and then the sharing issues can be detected.</p><h4>Detection of sharing issues</h4><p>As expected, Inspector detected errors in the kd-tree traversal:</p><p><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-advisor-annotate-correctness.png"><img
class="aligncenter size-medium wp-image-696" title="Memory sharing issues detected by Inspector" src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-advisor-annotate-correctness-300x187.png" alt="Memory sharing issues detected by Inspector" width="300" height="187" /></a><br
/> The solution in this case is to have a ray-traversal stack per thread, which will have to be implemented in whichever parallel library will be chosen, or simply to put the stack in the actual traversal algorithm and not in the instance.</p><h4>Using TBB</h4><p>I&#8217;ve decided to go for Thread Building Blocks, as it was already used for game development. This seemed to me a good idea, as it is a Open Source solution. So now, I will split the screen in 2D pieces, and add a thread-specific storage in the kd-tree class. Of course, I will have to add a flag to disable this paralellization if TBB is not available.</p><p>The actual parallelization will be in a future post in the Interactive Raytracer category. It is pretty straightforward once I had the different elements Parallel Studio gave me.</p><h4>Conclusion</h4><p>In fact Advisor is mainly the <strong>annotate.h</strong> header, as you have to know your program to put the macros at correct locations. The parallelization must be done by hand, as well as correcting the memory sharing issues.</p><p>The only problem I had is that <strong>annotate.h</strong> includes <strong>window.h</strong>. This header is not C++ compliant and declares some macros as <strong>max()</strong> (in fact I got the same issue with TBB headers!). As I use a <strong>max()</strong> function declared in <strong>std::numerical_limits</strong>,  I had to explicitely undefine this macro.</p><p>Safe from this, Advisor Lite is a good plugin, and I&#8217;m looking forward to seeing Advisor in a next Parallel Studio release.</p>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/09/22/parallel-studio-using-advisor-lite/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Book review: Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library</title><link>http://matt.eifelle.com/2009/09/10/book-review-effective-stl-50-specific-ways-to-improve-your-use-of-the-standard-template-library/</link> <comments>http://matt.eifelle.com/2009/09/10/book-review-effective-stl-50-specific-ways-to-improve-your-use-of-the-standard-template-library/#comments</comments> <pubDate>Thu, 10 Sep 2009 08:05:34 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[Addison-Wesley]]></category> <category><![CDATA[Book review]]></category> <category><![CDATA[C++]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=411</guid> <description><![CDATA[After Effective C++, I&#8217;d like to speak about Effective STL.Content and opinions
Everything on the STL can be found in this book. Every algorithm is explained with details, almost every function of every container, with their features, qualities and defaults. In fact, there are so many things in the STL that one cannot know every detail [...]]]></description> <content:encoded><![CDATA[<p>After <a
href="http://matt.eifelle.com/2009/09/01/book-review-effective-c-55-specific-ways-to-improve-your-programs-and-designs/">Effective C++</a>, I&#8217;d like to speak about <span
style="text-decoration: underline;">Effective STL</span>.<br
/> <span
id="more-411"></span></p><h4>Content and opinions</h4><p>Everything on the STL can be found in this book. Every algorithm is explained with details, almost every function of every container, with their features, qualities and defaults. In fact, there are so many things in the STL that one cannot know every detail without studying it in depth. And this is where Meyers shines: the book helps learning this fastly!</p><p>Besides the STL, the old non standard containers (hashmap for instance) are also covered, although C++0x changed them, so these items can be bypassed.</p><h4>Conclusion</h4><p>Even more useful than <span
style="text-decoration: underline;">Effective C++</span>, the tips provided in this book can be forgot easilly, so you need to come back to it regularly. If you use the STL (which you must if you&#8217;re a C++ developer), you must have a copy of <span
style="text-decoration: underline;">Effective STL</span>.</p><div
style="border: 1px solid #000; padding: 5px; margin-bottom: 15px; background: url(http://matt.eifelle.com/wp-content/uploads/2009/12/BN_Logo_3tier.jpg) right bottom no-repeat #ffffff;"> <a
rel="nofollow" href="http://r.popshops.com/pp/69394/effective-stl-50-specific-ways-to-improve-your-use-of-the-standard-template-libr"><img
style="width: 150px;" src="http://images.barnesandnoble.com/images/14560000/14569363.JPG" border="0" alt="Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library (Addison-Wesley Professional Computing Series)" /></a><br
/> <a
rel="nofollow" href="http://r.popshops.com/pp/69394/effective-stl-50-specific-ways-to-improve-your-use-of-the-standard-template-libr">Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library (Addison-Wesley Professional Computing Series)</a><br
/> Price: $32.25</div><div
class="subcolumns"><div
style="border: 1px solid #000; padding: 5px; margin-bottom: 15px; background: url(http://matt.eifelle.com/wp-content/plugins/amazonsimpleadmin/img/amazon_US_small.gif) right bottom no-repeat #ffffff;"><div
style="width: 60px; float: left; margin-right: 5px;"> <a
href="http://www.amazon.com/exec/obidos/ASIN/0201749629/masbl03-20" target="_blank"><img
src="http://ecx.images-amazon.com/images/I/41W3B0YFG8L._SL75_.jpg" width="60" height="75" border="0" /></a></div><div><p><a
href="http://www.amazon.com/exec/obidos/ASIN/0201749629/masbl03-20" target="_blank">Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library</a> (Paperback)<br
/> <span
style="font-size: 0.8em;">by <strong>Scott Meyers</strong></span><br
/> ISBN: 0201749629</p><p><strong>Price:</strong> <span
style="color: #990000; font-weight: bold;">USD 40.55</span><br
/> <strong>46 used &#038; new</strong> available from <span
style="color: #990000; font-weight: bold;">USD 27.50</span></p><p> <img
src="http://matt.eifelle.com/wp-content/plugins/amazonsimpleadmin/img/stars-4.5.gif" class="asa_rating_stars" /> | 4.5 | 38</div><div
style="clear: both;"></div></div></div>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/09/10/book-review-effective-stl-50-specific-ways-to-improve-your-use-of-the-standard-template-library/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Served from: matt.eifelle.com @ 2010-07-30 07:56:48 by W3 Total Cache -->