<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>al_uninstall_audio - what exactly happens?</title>
		<link>http://www.allegro.cc/forums/view/616932</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 06 Jun 2017 00:06:46 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Do samples get freed?  What would happen if I uninstalled the audio system while a game was running and then re-installed it?  Is that standard practice, in order to cut audio when say for instance the window loses focus? (To save CPU - I noticed that it seems to take up a certain percentage of CPU even when no sounds are being played)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Sun, 04 Jun 2017 10:55:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>How do you know that it&#39;s the audio system that is consuming the CPU?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (amarillion)</author>
		<pubDate>Sun, 04 Jun 2017 14:25:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If I don&#39;t install it to begin with the CPU usage is much lower when the window doesn&#39;t have focus.  I pause the game loop when it loses focus - it&#39;s 6-8% with audio, 1-2% without.  (This might sound like a small difference but keep in mind I have 8 cores so I think &quot;100%&quot; is 12.5%.)</p><p>edit: also, since my language includes a REPL I can just manually install it to watch the change in realtime, which I just did to confirm and it&#39;s still doing it.</p><p>edit 2: i looked at my audio init function and i found the source of the CPU spike.  i was reserving thousands of samples with al_reserve_sample!  don&#39;t ask me how or why...  has to do with fixed-point numbers <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>edit 3: still, there is still a 1-2% bump (out of 12.5%!) in CPU even with nothing playing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Sun, 04 Jun 2017 17:52:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Allegro&#39;s mixer thread and timer thread are both still running consuming CPU. On Android, I do this on ALLEGRO_EVENT_DISPLAY_HALT_DRAWING:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_MIXER"><span class="a">ALLEGRO_MIXER</span></a> <span class="k3">*</span>mixer <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_default_mixer"><span class="a">al_get_default_mixer</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">if</span> <span class="k2">(</span>mixer<span class="k2">)</span> <a href="http://www.allegro.cc/manual/al_set_mixer_playing"><span class="a">al_set_mixer_playing</span></a><span class="k2">(</span>mixer, <span class="k1">false</span><span class="k2">)</span><span class="k2">;</span>
al_set_default_voice<span class="k2">(</span>NULL<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_stop_timer"><span class="a">al_stop_timer</span></a><span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

This gets my CPU usage to 0.0%. The timer is my single Allegro timer I use for timing.</p><p>And I do this on ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_restore_default_mixer"><span class="a">al_restore_default_mixer</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_MIXER"><span class="a">ALLEGRO_MIXER</span></a> <span class="k3">*</span>mixer <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_default_mixer"><span class="a">al_get_default_mixer</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">if</span> <span class="k2">(</span>mixer<span class="k2">)</span> <a href="http://www.allegro.cc/manual/al_set_mixer_playing"><span class="a">al_set_mixer_playing</span></a><span class="k2">(</span>mixer, <span class="k1">true</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_start_timer"><span class="a">al_start_timer</span></a><span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sun, 04 Jun 2017 22:01:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Elias Thanks.  You saved me some time.  I&#39;ll implement this.  </p><p>EDIT: What does the al_set_default_voice call do?  Did you mean al_set_default_mixer?</p><p>EDIT 2: Nvm, checked the 5.2.2 dox.  Turns out something else seems to be eating up the CPU even when the game is suspended :/
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Mon, 05 Jun 2017 21:16:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Can you post your event loop? Are any events still being generated after you stop all timers?</p><p>Also, if this is Windows, try using OpenGL instead of DirectX, just to see if it makes a difference. I don&#39;t know anything about the Windows port myself but I saw some posts about DirectX causing some strange CPU usage in some cases.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Mon, 05 Jun 2017 23:30:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m using OpenGL.  I&#39;ll post the loop, but it&#39;s in Forth, so, assuming you don&#39;t know it ... sorry.  Maybe you&#39;ll be able to figure it out still.  Just read the code from left to right, each word separated by spaces is a function call.  With the exception of: <tt>:
 if then begin while repeat again do loop case endcase of endof </tt> which are flow control keywords.  The one weirdness is <tt>then</tt> which means &quot;and then&quot;, think of it as &quot;endif&quot;.</p><pre>
\ Universal main loop
\  Skips rendering frames if logic takes too long (up to 4 frames are skipped)
\  You can disable the frame timer to save CPU when making editors etc
\  When the window is switched away from the timer will be disabled, and re-enabled when
\    it regains focus.
\  The loop has some common controls:
\    F12 - break the loop
\    F4 - quit the process
\    ALT-ENTER - toggle fullscreen
\    TILDE - toggles a flag called INFO, doesn&#39;t do anything on its own.

\ Values
0 value #frames \ frame counter.
0 value renderr
0 value steperr
0 value alt?  \ part of fix for alt-enter bug when game doesn&#39;t have focus
0 value breaking?
0 value lag  \ completed ticks
0 value &#39;go
0 value &#39;step
0 value &#39;render

\ Flags
variable info  \ enables debugging mode display
variable allowwin  allowwin on
variable fs    \ is fullscreen enabled?

create fse  /ALLEGRO_ANY_EVENT /allot  \ fullscreen event
#999 constant EVENT_FULLSCREEN 

: poll  pollKB  pollJoys  [defined] dev [if] pause [then] ;
: break  true to breaking? ;
: -break  false to breaking? ;
: unmount  ( -- )   1-1   0 0 displayw displayh al_set_clipping_rectangle ;

[defined] dev [if]
    variable (sp)
    : try  dup -exit  sp@ &gt;r  [&#39;] call catch (sp) !  r&gt; sp!  (sp) @ ;
[else]
    : try  dup -exit call 0 ;
[then]
    : try  dup -exit call 0 ;

private:
    \ : alt?  e ALLEGRO_KEYBOARD_EVENT-modifiers @ ALLEGRO_KEYMOD_ALT and ;
    : std
      etype ALLEGRO_EVENT_DISPLAY_RESIZE = if
        -timer  display al_acknowledge_resize  +timer  \ we have to turn off the timer to avoid a race condition
                                                       \ where bitmaps aren&#39;t recreated before trying to draw to them
      then
      etype ALLEGRO_EVENT_DISPLAY_SWITCH_OUT = if  -timer  -audio  then
      etype ALLEGRO_EVENT_DISPLAY_SWITCH_IN = if  clearkb  +timer   +audio  false to alt?  then
      etype ALLEGRO_EVENT_DISPLAY_CLOSE = if  0 ExitProcess  then
      etype ALLEGRO_EVENT_KEY_DOWN = if
        e ALLEGRO_KEYBOARD_EVENT-keycode @ case
          &lt;alt&gt;    of  true to alt?  endof
          &lt;altgr&gt;  of  true to alt?  endof
          &lt;enter&gt;  of  alt? -exit  fs toggle  endof
          &lt;f4&gt;     of  alt? -exit  0 ExitProcess endof
          &lt;f5&gt;     of  refresh  endof
          &lt;f12&gt;    of  break  endof
          &lt;tilde&gt;  of  alt? -exit  info toggle  endof
        endcase
      then
      etype ALLEGRO_EVENT_KEY_UP = if
        e ALLEGRO_KEYBOARD_EVENT-keycode @ case
          &lt;alt&gt;    of  false to alt?  endof
          &lt;altgr&gt;  of  false to alt?  endof
        endcase
      then ;
public:


: fsflag  fs @ allowwin @ not or ;
: ?fserr  0= if fs off  &quot; Fullscreen is not supported by your driver.&quot; alert  then ;
variable winx  variable winy
: ?poswin   \ save/restore window position when toggling in and out of fullscreen
    display al_get_display_flags ALLEGRO_FULLSCREEN_WINDOW and if
        fs @ 0= if  r&gt; call  display winx @ winy @ al_set_window_position  then
    else
        fs @ if     display winx winy al_get_window_position  then
    then ;

variable newfs
: ?fs
    ?poswin  display ALLEGRO_FULLSCREEN_WINDOW fsflag al_toggle_display_flag ?fserr
    fs @  newfs @  &lt;&gt; if
        fse EVENT_FULLSCREEN emit-user-event
    then
    fs @ newfs ! ;

defer ?overlay  &#39; noop is ?overlay  \ render ide
defer ?system   &#39; noop is ?system   \ system events

private:
    : update?  timer? if  lag dup -exit drop  then  eventq al_is_event_queue_empty  lag 4 &gt;= or ;
    : wait  eventq e al_wait_for_event ;
    : render  ?fs  unmount  &#39;render try to renderr  unmount  ?overlay  al_flip_display  0 to lag ;
    : ?render  update? -exit  1 +to #frames  render ;
    : ?step  etype ALLEGRO_EVENT_TIMER = if  poll  1 +to lag   &#39;step try to steperr  then ;
    : /ok  resetkb  -break  &gt;display  +timer  render ;
    : ok/  eventq al_flush_event_queue -timer  &gt;ide  -break ;
public:

: render&gt;  r&gt;  to &#39;render ;  ( -- &lt;code&gt; )  ( -- )
: step&gt;  r&gt;  to &#39;step ;  ( -- &lt;code&gt; )  ( -- )
: go&gt;  r&gt; to &#39;go   0 to &#39;step ;  ( -- &lt;code&gt; )  ( -- )

: ok
    /ok
    begin
        wait
        begin
            std  ?system  &#39;go try drop  ?step  ?render
            eventq e al_get_next_event 0=  breaking? or
        until  ?render  \ again for sans timer
    breaking? until
    ok/ ;

</pre><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Mon, 05 Jun 2017 23:48:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I wrote several Forth implementations so I know about reading Forth <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><p>Can you add a debug print to see if you are still getting timer events (or any other events) while your program is suspended?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Tue, 06 Jun 2017 00:01:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s awesome!  Same here.  I feel like Forth has been getting gradually more well-known after being treated like an anathema for years, so to me this was the definitive confirmation that it&#39;s turning around, haha.  Been in love since 2000.  (I post frequently on the Forth Programming Facebook group if you want to be up-to-date on my doings... I&#39;m finalizing a framework for game-and-engine development... also check out rogerlevy/bubble-internal on Github...)</p><p>I&#39;ll try your suggestion... will post an update later today.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Tue, 06 Jun 2017 00:06:46 +0000</pubDate>
	</item>
</rss>
