<phyphox xmlns="http://phyphox.org/xml" xmlns:editor="http://phyphox.org/editor/xml" version="1.7" editor:version="1.0" locale="en">
    <title>Acoustic Resonance</title>
    <category>Other</category>
    <description>Creates différent frequencies, and measures the audio response amplitude (in arbitrary unit). Can be used to look for an audio resonance (helmotz resonnator or a tube).</description>
    <icon>AR1</icon>
    <data-containers>
			<!-- data buffers *********************************************-->
		<container size="0" static="false">all_freq</container>
		<container size="0" static="false">all_amp</container>
			<!-- analysis gestion buffers *********************************-->	
        <container size="1" init="0" static="false">index</container>	
        <container size="1" init="0" static="false">temp</container>
        <container size="1" init="0" static="false">now</container>		
        <container size="1" init="0" static="false">first_run</container>	
        <container size="1" init="0" static="false">temporary</container>			
			<!-- tone generator buffers ***********************************-->		
		<container size="1" static="false">freq</container>
		<container size="1" static="false">start_f</container>				
		<container size="1" static="false">add_f</container>	
		<container size="1" static="false">mult_f</container>				
			<!-- audio buffers ********************************************-->		
        <container size="1" static="false">length</container>	
        <container size="48000" static="true">tfull</container>				
        <container size="48000" static="false">t</container>
        <container size="48000" static="false">phase</container>
		<container size="48000" static="false">audio_in</container>
	    <container size="48000" static="false">audio_out</container>	
		<container size="48000" static="false">audio_squared</container>
		<container size="1" init="0" static="false">sum</container>	
			
		
    </data-containers>
	
    <input>
        <audio rate="48000">
            <output>audio_in</output>
        </audio>
    </input>
	
    <output>
        <audio loop="true" rate="48000">
            <input>audio_out</input>
        </audio>
    </output>
	
    <views>
        <view label="Acoustic resonance">
			<separator height="1" /> 
			
			<value label="Freq:" unit="Hz">
				<input>freq</input>
			</value>
			<value label="Audio Amplitude:" >
				<input>sum</input>
			</value>
			<separator height="1" /> 
				
            <graph  label="acoustic response" aspectRatio="2" style="dots" lineWidth="1" color="ff7e22" partialUpdate="false" history="1" labelX="freq" labelY="amp" labelZ="" unitX="Hz" unitY="a.u." unitZ="" logX="false" logY="false" logZ="false" xPrecision="3" yPrecision="3" zPrecision="3" scaleMinX="auto" scaleMaxX="auto" scaleMinY="auto" scaleMaxY="auto" scaleMinZ="auto" scaleMaxZ="auto" minX="0" maxX="0" minY="0" maxY="0" minZ="0" maxZ="0"  mapWidth="0"       >
                <input axis="x">all_freq</input>
                <input axis="y">all_amp</input>
            </graph>	
			<separator height="1" /> 
			<info label="New F = (Old F + Step)*factor"  /> 	
			<separator height="1" /> 			
            <edit label="Start frequency" signed="false" decimal="true" min="20" max="20000" unit="Hz" factor="1" default="200">
                <output>start_f</output>
            </edit>
            <edit label="Frequency step" signed="true" decimal="true"  max="20000" unit="Hz" factor="1" default="50">
                <output>add_f</output>
			</edit>	
            <edit label="Frequency factor" signed="false" decimal="true"  max="1000"  factor="1" default="1">
                <output>mult_f</output>				
            </edit>
 
        </view>
		
		<view label="Debug">
			<separator height="1" /> 
			
			<value label="Freq:" unit="Hz">
				<input>freq</input>
			</value>
			<value label="Audio Amplitude:" >
				<input>sum</input>
			</value>
			<value label="time:" unit="s">
				<input>now</input>
			</value>
			<value label="index:" >
				<input>index</input>
			</value>		
			<value label="first run?" >
				<input>first_run</input>
			</value>	
		</view>
    </views>	
	
    <analysis sleep="1"  onUserInput="false">
	
<!-- now will be smaller than 1s during the first run in Android (but larger than 1 in iOS ?) -->	
<!-- index will be equal to 1 during the first run in iOS (but equal to 2 in Android) -->
<!-- index+now  will be smaler than 3 during the first run in iOS and Android... -->
		<timer>
			<output>now</output>			
		</timer>
		<add>
            <input clear="false">index</input>
			<input type="value">1</input>
            <output>index</output>		
		</add>	
		<add>
            <input clear="false">index</input>
			<input clear="false">now</input>
            <output>temporary</output>		
		</add>	
        <if less="true">
            <input clear="false">temporary</input>
            <input type="value">3</input>
            <input type="value">1</input>
            <input type="value">0</input>			
            <output>first_run</output>
        </if>			

<!-- save the freq data if not first run (first_run=0) -->	
<!-- needs to be done before the calculation of the new freq  -->	
<!-- since the audio data comes from the previous analysis run freq -->			
        <if equal="true">
            <input clear="false">first_run</input>
            <input type="value">0</input>
            <input clear="false">freq</input>
            <output clear="false">all_freq</output>
        </if>		
		
<!-- next run frequency calculation : freq = (freq+add_f)*mult_f -->	
		<add>
            <input clear="false">freq</input>
			<input clear="false">add_f</input>
            <output>freq</output>		
		</add>	
		<multiply>
            <input clear="false">freq</input>
			<input clear="false">mult_f</input>
            <output>freq</output>		
		</multiply>
	    <if less="true">
            <input clear="false">freq</input>
            <input type="value">0</input>
            <input clear="false">start_f</input>
            <output>freq</output>
        </if>
	    <if greater="true">
            <input clear="false">freq</input>
            <input type="value">20000</input>
            <input type="value">20000</input>
            <output>freq</output>
        </if>		
	    <if less="true">
            <input clear="false">freq</input>
            <input type="value">20</input>
            <input type="value">20</input>
            <output>freq</output>
        </if>	
		
		
<!-- if  first run (first_run=1) , freq should be start_f -->				
		<if equal="true">
            <input clear="false">first_run</input>
            <input type="value">1</input>
            <input clear="false">start_f</input>
            <output clear="false">freq</output>
        </if>	
		
						
<!-- generation of the output audio at frequency freq -->			
        <ramp>
            <input as="start" type="value">0.0000208333</input>
            <input as="stop" type="value">1</input>
            <output as="out">tfull</output>
        </ramp>		
        <divide>
            <input as="dividend" type="value">1000</input>
            <input as="divisor" clear="false">freq</input>
            <output as="quotient">length</output>
        </divide>	
        <rangefilter>
            <input as="in">tfull</input>
            <input as="max">length</input>
            <output as="out">t</output>
        </rangefilter>		
        <multiply>
            <input as="factor" clear="false">freq</input>
            <input as="factor" clear="false">t</input>
            <input as="factor" type="value">6.283185307</input>
            <output as="product">phase</output>
        </multiply>		
        <sin>
            <input as="value">phase</input>
            <output as="sin">audio_out</output>
        </sin>

<!-- calculation of the audio input amplitude -->	
<!-- the audio data comes from the previous analysis run freq -->
<!-- having calculated the new audio_out first creates a delay that ensures proper audio_in (maybe) -->			
		<power>
		  <input as="base" clear="false">audio_in</input>
		  <input as="exponent" type="value">2</input>
		  <output>audio_squared</output>
		</power>
		<integrate>
		  <input>audio_squared</input>
		  <output>sum</output>
		</integrate>

<!-- save the audio data if not first run (first_run=0) -->					
        <if equal="true">
            <input clear="false">first_run</input>
            <input type="value">0</input>
            <input clear="false">sum</input>
            <output clear="false">all_amp</output>
        </if>		
		

		
    </analysis>
	

	
    <export>
        <set name="Acoustic Resonance">
            <data name="frequency (Hz)">all_freq</data>
            <data name="audio amplitude (a.u.)">all_amp</data>
		</set>	
    </export>
</phyphox>
