Adding more data functions to read, manipulate and write daff files

parent 39635de2
%% Read HRIR and export
daff_input_file_path = '2015_ITA-Kunstkopf_HRIR_2ch_D186_5x5_256_v17.daff';
daff_output_file_path_44kHz = 'ITA-Kunstkopf_5x5_256_44kHz.v17.ir.daff';
daff_output_file_path_48kHz = 'ITA-Kunstkopf_5x5_256_44kHz.v17.ir.daff';
h = DAFFv17( 'open', daff_input_file_path );
props = DAFFv17( 'getProperties', h );
md = DAFFv17( 'getMetadata', h );
%% Add new metadata
metadata_v17 = [];
metadata_v17 = daffv17_add_metadata( metadata_v17, 'License', 'String', 'CC BY-SA-NC 4.0' );
%% Copy metadata
for j = 1:numel( md )
switch md( j ).datatype
case 0
md_type = 'BOOL';
case 1
md_type = 'INT';
case 2
md_type = 'FLOAT';
otherwise
md_type = 'STRING';
end
metadata_v17 = daffv17_add_metadata( metadata_v17, md( j ).name, md_type, md( j ).value );
end
%% Export 44 kHz 256 samples
daffv17_write( 'filename', daff_output_file_path_44khz, ...
'metadata', metadata_v17, ...
'datafunc', @dfCopyIR, ... % @dfCopyIR @dfShortenIR @dfPostProcessIR
'userdata', h, ...
'content', props.contentType, ...
'quantization', props.quantization, ...
'channels', props.numChannels, ...
'alphares', props.alphaResolution, ...
'alpharange', props.alphaRange, ...
'betares', props.betaResolution, ...
'betarange', props.betaRange, ...
'orient', props.orientation, ...
'zthreshold', -120 );
%% Export 48 kHz 256 samples
daffv17_write( 'filename', daff_output_file_path_48kHz, ...
'metadata', metadata_v17, ...
'datafunc', @dfResample48kHzIR, ...
'userdata', h, ...
'content', props.contentType, ...
'quantization', props.quantization, ...
'channels', props.numChannels, ...
'alphares', props.alphaResolution, ...
'alpharange', props.alphaRange, ...
'betares', props.betaResolution, ...
'betarange', props.betaRange, ...
'orient', props.orientation, ...
'zthreshold', -120 );
function [] = daffv17_copy_append_metadata( daff_input_file_path, daff_output_file_path, additional_metadata )
% Reads a DAFF file, appends new metadata and exports the DAFF file again
%
% Will overwrite an existing key with new metadata entry.
%
% daff_input_file_path Input file path
% daff_output_file_path Output file path
% additional_metadata Extend metadata of target file
%
%
h = DAFFv17( 'open', daff_input_file_path );
props = DAFFv17( 'getProperties', h );
md_old = DAFFv17( 'getMetadata', h );
metadata_v17 = [];
for j = 1:numel( additional_metadata )
% key also present in new metadata, use new.
switch additional_metadata( j ).datatype
case 0
md_type = 'BOOL';
case 1
md_type = 'INT';
case 2
md_type = 'FLOAT';
otherwise
md_type = 'STRING';
end
metadata_v17 = daffv17_add_metadata( metadata_v17, additional_metadata( j ).name, md_type, additional_metadata( j ).value );
end
for i = 1:numel( md_old )
present = false;
for j = 1:numel( metadata_v17 )
if strcmpi( md_old( i ).name, metadata_v17( j ).name )
present = true;
end
end
if ~present
switch md_old( i ).datatype
case 0
md_type = 'BOOL';
case 1
md_type = 'INT';
case 2
md_type = 'FLOAT';
otherwise
md_type = 'STRING';
end
metadata_v17 = daffv17_add_metadata( metadata_v17, md_old( i ).name, md_type, md_old( i ).value );
end
end
switch( props.contentType )
case { 'ms', 'mps', 'ps' }
daffv17_write( 'filename', daff_output_file_path, ...
'metadata', metadata_v17, ...
'datafunc', @dfCopyMS, ...
'userdata', h, ...
'content', props.contentType, ...
'quantization', props.quantization, ...
'channels', props.numChannels, ...
'alphares', props.alphaResolution, ...
'alpharange', props.alphaRange, ...
'betares', props.betaResolution, ...
'betarange', props.betaRange, ...
'orient', props.orientation );
case 'ir'
daffv17_write( 'filename', daff_output_file_path, ...
'metadata', metadata_v17, ...
'datafunc', @dfCopyIR, ...
'userdata', h, ...
'content', props.contentType, ...
'quantization', props.quantization, ...
'channels', props.numChannels, ...
'alphares', props.alphaResolution, ...
'alpharange', props.alphaRange, ...
'betares', props.betaResolution, ...
'betarange', props.betaRange, ...
'orient', props.orientation );
case 'dft'
daffv17_write( 'filename', daff_output_file_path, ...
'metadata', metadata_v17, ...
'datafunc', @dfCopyDFT, ...
'userdata', h, ...
'content', props.contentType, ...
'quantization', props.quantization, ...
'channels', props.numChannels, ...
'alphares', props.alphaResolution, ...
'alpharange', props.alphaRange, ...
'betares', props.betaResolution, ...
'betarange', props.betaRange, ...
'orient', props.orientation );
end
DAFFv17( 'close', h );
end
\ No newline at end of file
function [ data, samplerate, metadata ] = dfCopyIR( alpha, beta, h )
[ record_index, out_of_bounds ] = DAFFv17( 'getNearestNeighbourIndex', h, 'data', alpha, beta );
assert( not ( out_of_bounds == 1 ) );
data = DAFFv17( 'getRecordByIndex', h, record_index );
props = DAFFv17( 'getProperties', h );
samplerate = props.samplerate;
[ metadata, empty ] = DAFFv17( 'getRecordMetadata', h, record_index );
if empty
metadata = [];
end
end
function [ freqs, mags, metadata ] = dfCopyMS( alpha, beta, h )
[ record_index, out_of_bounds ] = DAFFv17( 'getNearestNeighbourIndex', h, 'data', alpha, beta );
assert( not ( out_of_bounds == 1 ) );
mags = DAFFv17( 'getRecordByIndex', h, record_index );
props = DAFFv17( 'getProperties', h );
freqs = props.freqs;
[ metadata, empty ] = DAFFv17( 'getRecordMetadata', h, record_index );
if empty
metadata = [];
end
end
function [ data, samplerate, metadata ] = dfPostProcessIR( alpha, beta, h )
[ record_index, out_of_bounds ] = DAFFv17( 'getNearestNeighbourIndex', h, 'data', alpha, beta );
props = DAFFv17( 'getProperties', h );
samplerate = props.samplerate;
metadata = [];
assert( not ( out_of_bounds == 1 ) );
data_in = DAFFv17( 'getRecordByIndex', h, record_index );
ir = itaAudio();
ir.timeData = data_in';
% to something ...
data = ir.timeData';
end
function [ data, samplerate, metadata ] = dfResample48kHzIR( alpha, beta, h )
[ record_index, out_of_bounds ] = DAFFv17( 'getNearestNeighbourIndex', h, 'data', alpha, beta );
metadata = [];
assert( not ( out_of_bounds == 1 ) );
data_in = DAFFv17( 'getRecordByIndex', h, record_index );
props = DAFFv17( 'getProperties', h );
ir = itaAudio();
ir.samplingRate = props.samplerate;
ir.timeData = data_in';
new_samplerate = 48000;
ir_r = ita_resample( ir, new_samplerate );
ir_c = ita_time_crop( ir_r, [ 1 256 ], 'samples' );
data = ir_c.timeData';
samplerate = new_samplerate;
end
function [ data, samplerate, metadata ] = dfShortenIR( alpha, beta, h )
[ record_index, out_of_bounds ] = DAFFv17( 'getNearestNeighbourIndex', h, 'data', alpha, beta );
assert( not ( out_of_bounds == 1 ) );
data_in = DAFFv17( 'getRecordByIndex', h, record_index );
data = data_in( :, 1:128 );
props = DAFFv17( 'getProperties', h );
samplerate = props.samplerate;
metadata = [];
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment