writeDAFFFile.m 4.6 KB
Newer Older
1
function writeDAFFFile( this, file_path, metadata_user )
2 3
% Exports itaHRTF to a DAFF file
%
4
% Input:    file_path (string) [optional]
5 6 7 8 9 10 11
%           user metadata (struct created with daff_add_metadata) [optional]
%
% Required: OpenDAFF matlab scripts, http://www.opendaff.org
%           (but included in ITA-Toolbox)
%
% Output: none

12
metadata = this.mMetadata;
13 14 15 16 17
if nargin >= 3
    metadata = metadata_user;
end

hrtf_variable_name = inputname( 1 );
18
file_name = [ hrtf_variable_name '_' int2str( this.nSamples ) 'samples_' int2str( this.resAzimuth ) 'x' int2str( this.resElevation ) '.daff'];
19
if nargin >= 2
20
    file_name = file_path;    
21 22 23 24 25 26 27 28 29 30 31 32 33 34
end

if nargin == 0
    error( 'Not enough input arguments' );
end


%% Inject content type indicator 'ir' or 'dft' into file name

ct_indicator = 'ir';
if strcmp( this.domain, 'freq' )
    ct_indicator = 'dft';
end

35 36 37
[ file_path, file_base_name, file_suffix ] = fileparts( file_name );
if ~strcmp( file_suffix, '.daff' )
    file_path = fullfile( file_path, strjoin( {file_base_name file_suffix 'v17' ct_indicator 'daff' }, '.' ) );
38
else
39
    file_path = fullfile( file_path, strjoin( {file_base_name 'v17' ct_indicator 'daff'}, '.' ) );
40 41 42 43 44 45 46
end


%% Prepare angle ranges and resolution

theta_start_deg = rad2deg( min( this.channelCoordinates.theta ) );
theta_end_deg = rad2deg( max( this.channelCoordinates.theta ) );
47
theta_num_elements = size( uniquetol( this.channelCoordinates.theta ), 1 );
48

49 50
phi_start_deg = rad2deg( min( mod( this.channelCoordinates.phi, 2 * pi ) ) );
phi_end_deg = rad2deg( max( mod( this.channelCoordinates.phi, 2 * pi ) ) );
51
phi_num_elements = size( uniquetol( this.channelCoordinates.phi ), 1 );
52 53 54 55 56

assert( phi_num_elements ~= 0 );
alphares = ( phi_end_deg - phi_start_deg ) / phi_num_elements; % phi end does not cover entire circle in this case
alphares_full_circle = ( phi_end_deg - phi_start_deg ) / ( phi_num_elements - 1 ); % phi end does not cover entire circle in this case
if phi_end_deg + alphares_full_circle >= 360.0
57
    alpharange = [ phi_start_deg 360 ]; % Account for full circle and force end of range to 360 deg
58 59 60 61 62
    alphares = alphares_full_circle;
else
    alpharange = [ phi_start_deg phi_end_deg ];
end

63 64 65
assert( alpharange( 1 ) >= 0.0 )
assert( alpharange( 2 ) <= 360.0 )

66 67 68 69
assert( theta_num_elements ~= 0 );
betares = ( theta_end_deg - theta_start_deg ) / ( theta_num_elements - 1 ); % phi end does not cover entire circle
betarange = 180 - [ theta_start_deg theta_end_deg ]; % Flip poles (DAFF starts at south pole)

70 71 72 73
assert( betarange( 2 ) >= 0.0 )
assert( betarange( 1 ) <= 180.0 )


74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
%% Assemble metadata (if not already present)

keyname = 'Generation script';
if ~any( strcmpi( { metadata(:).name }, keyname ) )
    metadata = daffv17_add_metadata( metadata, keyname, 'String', 'writeDAFFFile.m' );
end

keyname = 'Generation toolkit';
if ~any( strcmpi( { metadata(:).name }, keyname ) )
    metadata = daffv17_add_metadata( metadata, keyname, 'String', 'ITA-Toolkit' );
end

keyname = 'Generation date';
if ~any( strcmpi( { metadata(:).name }, keyname ) )
    metadata = daffv17_add_metadata( metadata, keyname, 'String', date );
end

keyname = 'Web resource';
if ~any( strcmpi( { metadata(:).name }, keyname ) )
    metadata = daffv17_add_metadata( metadata, keyname, 'String', 'http://www.ita-toolkit.org' );
end

96

97 98 99 100 101
channels=this.nChannels/this.nDirections;
if(channels<1)
    warning('Number of channels per record was not detected correctly, assuming 2 channel records');
    channels = 2;
end
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

% Content type switcher between time domain (ir) and frequency domain (dft)
% (requires different data functions)
if strcmp( this.domain, 'time' )

    daffv17_write('filename', file_path, ...
               'content', 'ir', ...
               'datafunc', @dfitaHRIRDAFFDataFunc, ...
               'channels', channels, ...
               'alphares', alphares, ...
               'alpharange', alpharange, ...
               'betares', betares, ...
               'betarange', betarange, ...
               'orient', [ 0 0 0 ], ...
               'metadata', metadata, ...
               'userdata', this, ...
               'quantization', 'float32' );
           
elseif strcmp( this.domain, 'freq' )
    
    daffv17_write('filename', file_path, ...
               'content', 'dft', ...
               'datafunc', @dfitaHRTFDAFFDataFunc, ...
               'channels', channels, ...
               'alphares', alphares, ...
               'alpharange', alpharange, ...
               'betares', betares, ...
               'betarange', betarange, ...
               'orient', [ 0 0 0 ], ...
               'metadata', metadata, ...
               'userdata', this, ...
               'quantization', 'float32' );
           
end