<phyphox version="1.10" locale="en">
    <title>Acoustic Resonance v.5.3</title>
    <category>TP</category>
    <description>Sweep the frequency and measures the audio response amplitude (in arbitrary unit). Can be used to look for an audio resonance (Helmholtz resonnator or a tube). Determines the resonance frequency simply by the signal maximum (will not work for multiple resonances). </description>
    <icon>Res</icon>
    <color>yellow</color>
    <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="1" static="false">first_run</container>	
			
			<!-- tone generator buffers ***********************************-->		
	<container size="1" init="230" static="false">freq</container>
	<container size="1" static="false">start_f</container>				
	<container size="1" static="false">add_f</container>	
				
			<!-- audio buffers *********************************************-->		
	<container size="48000" static="false">audio_in</container>
	<container size="1" init="0.9" static="false">audio_out_amp</container>	
	<container size="1" init="230" static="false">audio_out_freq</container>	
	<container size="1" init="1.0" static="false">audio_out_duration</container>	
	<container size="48000" static="false">audio_squared</container>
	<container size="1" init="0" static="false">sum</container>

			<!-- resonance buffers ******************************************-->
	<container size="1" init="0" static="false">max_ind</container>
	<container size="1" init="0" static="false">res_freq</container>	
					
    </data-containers>
	
	
	<!-- use the NEW TONE GENERATOR ! phyphox 1.1.6 file format 1.10 -->	
	
	<output>
	    <audio rate="48000" loop="true" normalize="false"> <!-- loop="true" -->
		<tone>
		    <input parameter="amplitude" type="buffer">audio_out_amp</input>
		    <input parameter="frequency" type="buffer">freq</input>
		    <input parameter="duration" type="value">0.1</input>
		    <!-- was amplitude=0.9 and duration=0.1 (restored) -->
		</tone>
	    </audio>
	</output>

    <input>
        <audio rate="48000">
            <output>audio_in</output>
        </audio>
    </input>
	
    <views>
        <view label="Acoustic resonance">
	    <separator height="1" /> 
			
	    <value label="Current frequency:" unit="Hz">
		<input>freq</input>
	    </value>
	    <value label="Audio Amplitude:" >
		<input>sum</input>
	    </value>
	    <value label="Resonance frequency:" unit="Hz">
		<input>res_freq</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"  /> 	
	    <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="1">
                <output>add_f</output>
	    </edit>
 
        </view>	
<!-- remove debug	
	<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="index:" >
		<input>index</input>
	    </value>		
	    <value label="first run?" >
		<input>first_run</input>
	    </value>	
	</view>
-->
    </views>	

<!-- Tried sleep time set to 0.5 sec (instead of 1) TOO FAST makes irregular points -->	
    <analysis sleep="1.0"  onUserInput="false">
	
    <add>
        <input clear="false">index</input>
	<input type="value">1</input>
        <output>index</output>		
    </add>		
        <if equal="true">
            <input as="a" clear="false">index</input>
            <input as="b" type="value">3</input>
            <input as="true" type="value">0</input>			
            <output>first_run</output>
        </if>
        <if greater="true">
            <input as="a" clear="false">index</input>
            <input as="b" type="value">2503</input>
            <input as="true" type="value">1</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 -->	
		<add>
            		<input clear="false">freq</input>
			<input clear="false">add_f</input>
            		<output>freq</output>		
		</add>	
<!-- if still first_run=1 freq = start_f (back from + add_f )-->
	<if equal="true">
            <input clear="false">first_run</input>
            <input type="value">1</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>	
		

						
<!-- generation of the output audio at frequency freq (TO REMOVE?) 		
		<add>
			<input clear="false">freq</input>
			<output>audio_out_freq</output>
		</add>
desactivated (mIv) -->

<!-- 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>
		<power>
		  <input as="base" clear="false">sum</input>
		  <input as="exponent" type="value">0.5</input>
		  <output>sum</output>
		</power>

<!-- 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>		
		
<!-- for indication of the resonance frequency (frequency of max responce) -->
	<max>
		<input clear="false" as="y">all_amp</input>
		<output as="position">max_ind</output>
	</max>
	<subrange>
		<input as="from">max_ind</input>
		<input as="length" type="value">1</input>
		<input clear="false">all_freq</input>
		<output>res_freq</output>
	</subrange>
		
    </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>
