daffv15_write.m 42.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
%
%  OpenDAFF - A free, open-source software package for directional audio data,
%  OpenDAFF is distributed under the terms of the GNU Lesser Public License (LGPL)
%
%  Copyright (C) Institute of Technical Acoustics, RWTH Aachen University
%
%  Visit the OpenDAFF homepage: http://www.opendaff.org
%
%  -------------------------------------------------------------------------------
%
%  File:    daff_write.m
%  Purpose: Writer DAFF files
%  Author:  Frank Wefers (Frank.Wefers@akustik.rwth-aachen.de)
%
%  $Id: daff_write.m,v 1.7 2010/03/08 14:32:41 stienen Exp $
%

% <ITA-Toolbox>
% This file is part of the application openDAFF for the ITA-Toolbox. All rights reserved.
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>



% Jan 2014 - Pascal Dietrich - changed strcmp() to strcmpi() - BUGFIX

function [] = daffv15_write( varargin )
%DAFF_WRITE Writes DAFF files
%   ...
%
%  --= General parameters =--
%
%  pretend      none        Do not write a DAFF file, just analyze
%  quiet        none        Suppress information and warning messages
%  verbose      none        Extra informative output
%
%  --= Required parameters =--
%
%  filename     char        Output filename (*.daff)
%  content      char        Content type
%                           ('IR' => Impulse responses,
%                            'MS' => Magnitude spectra,
%                            'PS' => Phase spectra,
%                            'MPS' => Magnitude phase spectra,
%                            'DFT' => discrete Fourier spectra)
%  datafunc     function    Data function (delivers the data for a direction)
%  dataset      function    Dataset containing all records
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
48
%  orient       vector-3    Orientation [yaw pitch roll] angles [�]
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
%  channels     int         Number of channels
%
%  alphares     float       Resolution of alpha-angles
%  betares      float       Resolution of beta-angles
%  alphapoints  int         Number of points in alpha direction
%  betapoints   int         Number of points in beta direction
%
%  You must either specify a dataset or a data function, but not both.
%  You must either specify resolutions or points, but not both.
%
%  --= Optional parameters =--
%
%  basepath     char        Base path for all input files
%  metadata     struct      Global metadata
%
%  alpharange   vector-2    Range of alpha-angles
%  betarange    vector-2    Range of beta-angles
%
%  --= IR content parameters =--
%
%  samplerate       float   Sampling rate [Hertz]
%  quantization     char    Quantization (int16|int24|float32)
%  zthreshold       float   Detection threshold for zero-coefficients (default: -inf)
%
%  --= DFT content parameters =--
%
%  transformsize    int     Discrete Fourier transform size
%  symmetric        logical Complex-conjugate symmetric spectra
%                           Write only first ceil(N+1/2) DFT coefficients
%                           Used for real-valued input data
%
%  --= MS|PS|MPS content parameters =--
%
%  freqs            vector  Support frequencies [Hertz]
%

% --= Option definitions =--

% Options with logical arguments (true|false)
boolarg = {};

% Options with integer number > 0 arguments
ingzarg = {'alphapoints', 'betapoints', 'channels', 'transformsize'};

% Options with floating point number arguments
floatarg = {'zthreshold'};

% Options with floating point number >= 0 arguments
pfloatarg = {'alphares', 'betares', 'samplerate'};

floatvecarg = {'alpharange', 'betarange', 'freqs', 'orient'};

% Options with string parameters
strarg = {'filename', 'basepath', 'content', 'quantization'};

% Options without an argument
nonarg = {'pretend', 'quiet', 'verbose', 'symmetric'};

% Options with one argument
onearg = [boolarg ingzarg floatarg pfloatarg floatvecarg strarg 'datafunc' 'dataset', 'metadata'];

% Required options
reqarg = {'filename', 'content'};


% +------------------------------------------------+
% |                                                |
% |   Parsing and validation of input parameters   |
% |                                                |
% +------------------------------------------------+

% Parse the arguments
args = struct();
for i=1:length(nonarg), args.(nonarg{i}) = false; end

i=1;
while i<=nargin
    if ~ischar(varargin{i}), error(['Parameter ' num2str(i) ': String expected']); end
    key = lower(varargin{i});
    i = i+1;
    r = nargin-i+1; % Number of remaining arguments
    
    switch key
        % Flag options without argument
        case nonarg
            args.(key) = true;
            
            % Options with one argument
        case onearg
            if (r < 1), error(['Option ''' key ''' requires an argument']); end
            args.(key) = varargin{i};
            i = i+1;
            
        otherwise
            error(['Invalid option (''' key ''')']);
    end
end

% Validate the arguments
for i=1:length(reqarg)
    key = reqarg{i};
    if ~isfield(args, key), error(['Option ''' key ''' must be specified']); end
end

for i=1:length(boolarg)
    key = boolarg{i};
    if isfield(args, key)
        if ~islogical(args.(key))
            error(['Argument for option ''' key ''' must be logical']);
        else
            % Type cast
            args.(key) = logical( args.(key) );
        end
    end
end

for i=1:length(ingzarg)
    key = ingzarg{i};
    if isfield(args, key)
        if (~isscalar(args.(key)) || ~any(isfinite(args.(key))) || ~isreal(args.(key)) || (ceil(args.(key)) ~= args.(key)) || (args.(key) <= 0))
            error(['Argument for option ''' key ''' must be an integer > 0']);
        else
            % Type cast
            args.(key) = int32( args.(key) );
        end
    end
end

for i=1:length(floatarg)
    key = floatarg{i};
    if isfield(args, key)
        if (~isscalar(args.(key)) || ~any(isfinite(args.(key))) || ~isreal(args.(key)))
            error(['Argument for option ''' key ''' must be a real number']);
        else
            % Type cast
            args.(key) = double( args.(key) );
        end
    end
end

for i=1:length(pfloatarg)
    key = pfloatarg{i};
    if isfield(args, key)
        if (~isscalar(args.(key)) || ~any(isfinite(args.(key))) || ~isreal(args.(key)) || (args.(key) < 0))
            error(['Argument for option ''' key ''' must be a non-negative real number']);
        else
            % Type cast
            args.(key) = double( args.(key) );
        end
    end
end

for i=1:length(floatvecarg)
    key = floatvecarg{i};
    if isfield(args, key)
        if (~isvector(args.(key)) || ~any(isfinite(args.(key))) || ~isreal(args.(key)))
            error(['Argument for option ''' key ''' must be a vector of real numbers']);
        else
            % Type cast
            args.(key) = double( args.(key) );
        end
    end
end

for i=1:length(strarg)
    key = strarg{i};
    if isfield(args, key)
        if (~ischar(args.(key)) || (isempty(args.(key))))
            error(['Argument for option ''' key ''' must be a non-empty string']);
        end
    end
end

% More validation ;-)

% Note: We validate fields in 'args' and compile a seperate 'props' structure
% from all the necessary arguments.

props.filename = args.filename;
props.quiet = args.quiet;
props.verbose = args.verbose;

% Verbose overrides quiet
if (props.verbose)
    props.quiet = false;
end

% Content
props.content = upper(args.content);
switch props.content
    case 'IR'
        props.contentStr = 'Impulse responses';
        props.contentType = 0; % DAFF_IMPULSE_RESPONSE
    case 'MS'
        props.contentStr = 'Magnitude spectra';
        props.contentType = 1; % DAFF_MAGNITUDE_SPECTRUM
    case 'PS'
        props.contentStr = 'Phase spectra';
        props.contentType = 2; % DAFF_PHASE_SPECTRUM
    case 'MPS'
        props.contentStr = 'Magnitude phase spectra';
        props.contentType = 3; % DAFF_MAGNITUDE_PHASE_SPECTRUM
    case 'DFT'
        props.contentStr = 'Discrete Fourier spectra';
        props.contentType = 4; % DAFF_DFT_SPECTRUM
        
    otherwise
        error(['Invalid content type (' args.content ')']);
end

% Data function or dataset
props.hasDatafunc = isfield(args, 'datafunc');
props.hasDataset = isfield(args, 'dataset');

if (~xor(props.hasDatafunc, props.hasDataset))
    error('You must specify either ''datafunc'' or ''dataset'', but not both');
end

if (props.hasDataset)
    % Case: Dataset supplied
    
    % Note: Some arguments are not applicable when using datasets
    
    if (isfield(args, 'channels'))
        error('When using a dataset you cannot not specify ''channels''');
    end
    
    if (isfield(args, 'alphapoints'))
        error('When using a dataset you cannot not specify ''alphapoints''');
    end
    
    if (isfield(args, 'alphares'))
        error('When using a dataset you cannot not specify ''alphares''');
    end
    
    if (isfield(args, 'alpharange'))
        error('When using a dataset you cannot not specify ''alpharange''');
    end
    
    if (isfield(args, 'betapoints'))
        error('When using a dataset you cannot not specify ''betapoints''');
    end
    
    if (isfield(args, 'betares'))
        error('When using a dataset you cannot not specify ''betares''');
    end
    
    if (isfield(args, 'betarange'))
        error('When using a dataset you cannot not specify ''betarange''');
    end
    
    % Copy values from the dataset
    props.dataset = args.dataset;
    
else
    
    % Case: Data function supplied
    
    props.datafunc = args.datafunc;
    
    if (~isfield(args, 'channels'))
        error('When using a data function you must specify ''channels''');
    end
    props.channels = args.channels;
    
    % TODO: Check the number of arguments
    % Momentarily deactivated
    %     if nargin(args.datafunc) ~= 1
    %         error('Argument for option ''datafunc'' must be a function which takes exactly one input argument');
    %     end
    %
    %     if nargout(args.datafunc) ~= 1
    %         error('Argument for option ''datafunc'' must be a function which returns exactly one output argument');
    %     end
    
    % Angular ranges default values
    if isfield(args, 'alpharange')
        props.alpharange = args.alpharange;
    else
        % Default range
        props.alpharange = [0 360];
    end
    
    if isfield(args, 'betarange')
        props.betarange = args.betarange;
    else
        % Default range
        props.betarange = [0 180];
    end
    
    % Check range specifications
    if (length(props.alpharange) ~= 2)
        error('Argument for ''alpharange'' must be a two element vector');
    end
    
    if (length(props.betarange) ~= 2)
        error('Argument for ''betarange'' must be a two element vector');
    end
    
    % Correct angular range ordering
    props.alphastart = props.alpharange(1);
    props.alphaend = props.alpharange(2);
    props.betastart = min(props.betarange);
    props.betaend = max(props.betarange);
    
    if ((props.alphastart < 0) || (props.alphastart > 360))
        error('Alpha range values must lie within the interval [0, 360]');
    end
    
    if ((props.betastart < 0) || (props.betastart > 180))
        error('Beta range values must lie within the interval [0, 180]');
    end
    
    if (props.alphastart>  props.alphaend)
        props.alphaspan = 360 - props.alphastart + props.alphaend;
    else
        props.alphaspan = props.alphaend - props.alphastart;
    end
    props.betaspan = props.betaend - props.betastart;
    
    % Alpha points and resolution
    hasAlphaPoints = isfield(args, 'alphapoints');
    hasAlphaRes = isfield(args, 'alphares');
    if (~xor(hasAlphaPoints, hasAlphaRes))
        error('You must specify either ''alphapoints'' or ''alphares'', but not both');
    end
    
    if isfield(args, 'alphares')
        % Alpha resolution speficied
        props.alphares = args.alphares;
        
        % [fwe] Bugfix 2011-07-05
        % If the azimuth span does not wrap around the whole sphere
        % we need to add another point. Otherwise there will not be
        % a point at alphaend. Moreover we need to cast to double
        % explicitly, otherwise the division is evaluated in integers.
        
        if (props.alphaspan == 360)
            % Full alpha coverage
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
388
            % Last point of the interval (360�) coincides with the first (0�)
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
            props.alphapoints = props.alphaspan / double( props.alphares );
        else
            % Partial alpha coverage
            % First and last point do not coincide.
            % Therefore the last point is within the span
            props.alphapoints = props.alphaspan / double( props.alphares ) + 1;
        end
        
        if (ceil(props.alphapoints) ~= props.alphapoints)
            error('Alpha range and alpha resolution are not an integer multiple')
        end
        
    else
        % Alpha points speficied
        props.alphapoints = args.alphapoints;
        
        % [fwe] Bugfix 2011-07-05 (see above)
        if (props.alphaspan == 360)
            props.alphares = props.alphaspan / double( props.alphapoints );
        else
            props.alphares = props.alphaspan / double( props.alphapoints - 1 );
        end
    end
    
    % Beta points and resolution
    hasBetaPoints = isfield(args, 'betapoints');
    hasBetaRes = isfield(args, 'betares');
    if (~xor(hasBetaPoints, hasBetaRes))
        error('You can specify either ''betapoints'' or ''betares'', but not both');
    end
    
    if isfield(args, 'betares')
        % Beta resolution specified
        props.betares = args.betares;
        
        % [fwe] Bugfix 2011-07-05
        % We need to cast to double explicitly, otherwise
        % the division is evaluated in integers.
        
        props.betapoints = (props.betaspan / double( props.betares )) + 1;
        if (ceil(props.betares) ~= props.betares)
            error('Beta range and beta resolution are not an integer multiple')
        end
    else
        % Beta points specified
        props.betapoints = args.betapoints;
        
        props.betares = props.betaspan / double( props.betapoints-1 );
    end
    
    % Create an empty helper dataset, which contains all directions
    % (angular pairs). Then we can just iterate over all records,
    % making iteration over directions a lot less complicated
    props.dataset = daff_create_dataset('channels', props.channels, ...
        'alpharange', props.alpharange, ...
        'alphapoints', props.alphapoints, ...
        'betarange', props.betarange, ...
        'betapoints', props.betapoints, ...
        'quiet');
end % -- Endif: Case: Dataset | data function --

% Sampling rate
if isfield(args, 'samplerate')
    props.dataset.samplerate = args.samplerate;
end

% Frequency support
if isfield(args, 'freqs')
    props.dataset.freqs = args.freqs;
end

% Tranform size support
if isfield(args, 'transformsize')
    props.dataset.transformsize = args.transformsize;
end

% Symmetric spectra
if isfield(args, 'symmetric')
    props.symmetric = args.symmetric;
end

% Metadata
if isfield(args, 'metadata')
    props.dataset.metadata = args.metadata;
end

% Orientation
if isfield(args, 'orient')
    if (length(args.orient) ~= 3)
        error('Argument for ''orient'' must be a three element vector [yaw pitch roll]');
    end
    props.orient = args.orient;
else
    % Default orientation
    props.orient = [0 0 0];
end

% Default quantization
if ~isfield(args, 'quantization')
    if (props.contentType == 0)
        % For time-domain data => 16-Bit signed integer
        props.quantization = 'int16';
    else
        % For all other data => 32-Bit floating points
        props.quantization = 'float32';
    end
else
    props.quantization = lower(args.quantization);
end

% Validate quantization
switch props.quantization
    case 'int16'
        props.quantizationStr = '16-bit signed integer';
        props.quantizationType = 0; % DAFF_INT16
        
    case 'int24'
        props.quantizationStr = '24-bit signed integer';
        props.quantizationType = 1; % DAFF_INT24
        
    case 'float32'
        props.quantizationStr = '32-bit floating point';
        props.quantizationType = 2; % DAFF_FLOAT32
        
    otherwise
        error(['Invalid quantization (' args.quantization ')']);
end

% --= Content specific validations =--

if strcmpi(props.content, 'IR')
    % Validation for IR content
    
    % Sampling rate must be provided
    if (~isfield(props.dataset, 'samplerate'))
        error('When writing impulse response content, you must specify ''samplerate''');
    end
    
    % Note: All quantizations are allowed for IR content
    
    % Zero-threshold
    if isfield(args, 'zthreshold')
        props.zthreshold = args.zthreshold;
        props.zthreshold_value = 10^(props.zthreshold/20);
    else
        % Default value = 0 (disabled)
        props.zthreshold = -inf;
        props.zthreshold_value = 0;
    end;
end

if strcmpi(props.content, 'MS')
    % Validation for MS content
    
    % Frequencies must be provided
    if ~isfield(props.dataset, 'freqs')
        error('When writing magnitude spectrum content, you must specify ''freqs''');
    end
    props.numfreqs = length(props.dataset.freqs);
    
    % Allowed quantizations for MS content: Only float32
    if ~strcmpi(props.quantization, 'float32')
        error('MS content may only be quantized with 32-bit floating points (float32)');
    end
end

if strcmpi(props.content, 'PS')
    % Validation for PS content
    
    % Frequencies must be provided
    if ~isfield(props.dataset, 'freqs')
        error('When writing phase spectrum content, you must specify ''freqs''');
    end
    props.numfreqs = length(props.dataset.freqs);
    
    % Allowed quantizations for PS content: Only float32
    if ~strcmpi(props.quantization, 'float32')
        error('PS content may only be quantized with 32-bit floating points (float32)');
    end
end

if strcmpi(props.content, 'MPS')
    % Validation for MPS content
    
    % Frequencies must be provided
    if ~isfield(props.dataset, 'freqs')
        error('When writing magnitude-phase spectrum content, you must specify ''freqs''');
    end
    props.numfreqs = length(props.dataset.freqs);
    
    % Allowed quantizations for MPS content: Only float32
    if ~strcmpi(props.quantization, 'float32')
        error('MPS content may only be quantized with 32-bit floating points (float32)');
    end
end

if strcmpi(props.content, 'DFT')
    % Validation for DFT content
    
    % Sampling rate must be provided
    if (~isfield(props.dataset, 'samplerate'))
        error('When writing impulse response content, you must specify ''samplerate''');
    end
    
    % Transform size must be provided
    if ~isfield(props.dataset, 'transformsize')
        error('When writing DFT spectrum content, you must specify ''transformsize''');
    end
    
    if isfield(props, 'symmetric') && props.symmetric
        % Compute the number of complex-conjugate symmetric DFT coefficients
        props.numDFTCoeffs = ceil( (double(props.dataset.transformsize) + 1) / 2 );
    else
        props.numDFTCoeffs = props.dataset.transformsize;
    end
    
    % Allowed quantizations for DFT content: Only float32
    if ~strcmpi(props.quantization, 'float32')
        error('DFT content may only be quantized with 32-bit floating points (float32)');
    end
end

% Default value for base path
if isfield(args, 'basepath')
    props.basepath = args.basepath;
else
    props.basepath = '';
end

% Now all parameters are parsed
% Clear all arguments. Everything we need is now stored in 'props'.
clear args;

%% --= End of parameter parsing and validation =--


% +------------------------------------------------+
% |                                                |
% |   Writing of the output file                   |
% |                                                |
% +------------------------------------------------+

% File format version of this daff_write
% Current version = 0.105
fileFormatVersion = 0105;

% Very important! 'l' -> little endian
% (DAFF files are always little endian)
fid = fopen(props.filename, 'wb', 'l');

% Structure for remembering offsets in the file
% for inserting values later
fpos = struct;

% Structure for file block data
fblocks = struct;

%
%  1st step: Write the file header
%

fwrite(fid, 'FW', 'char');
fwrite(fid, fileFormatVersion, 'int32');
fpos.numFileBlocksOffset = ftell(fid);
fwrite(fid, 0, 'int32'); % Placeholder

% File block entries
% Note: We write placeholders for yet unknown block sizes
%       and update the offsets and sizes later.

% Main header entry (FILEBLOCK_DAFF1_MAIN_HEADER = 0x0001)
fwrite(fid, hex2dec('0001'), 'int32');
fpos.mainHeaderOffset = ftell(fid);
fwrite(fid, 0, 'uint64');
fpos.mainHeaderSize = ftell(fid);
fwrite(fid, 0, 'uint64');

% Content header entry (FILEBLOCK_DAFF1_CONTENT_HEADER = 0x0002)
fwrite(fid, hex2dec('0002'), 'int32');
fpos.contentHeaderOffset = ftell(fid);
fwrite(fid, 0, 'uint64');
fpos.contentHeaderSize = ftell(fid);
fwrite(fid, 0, 'uint64');

% Record descriptor block entry (FILEBLOCK_DAFF1_RECORD_DESC = 0x0003)
fwrite(fid, hex2dec('0003'), 'int32');
fpos.recordDescOffset = ftell(fid);
fwrite(fid, 0, 'uint64');
fpos.recordDescSize = ftell(fid);
fwrite(fid, 0, 'uint64');

% Data block entry (FILEBLOCK_DAFF1_DATA  = 0x0004)
fwrite(fid, hex2dec('0004'), 'int32');
fpos.dataOffset = ftell(fid);
fwrite(fid, 0, 'uint64');
fpos.dataSize = ftell(fid);
fwrite(fid, 0, 'uint64');

% Metadata block entry (FILEBLOCK_DAFF1_METADATA = 0x0005)
fwrite(fid, hex2dec('0005'), 'int32');
fpos.metadataOffset = ftell(fid);
fwrite(fid, 0, 'uint64');
fpos.metadataSize = ftell(fid);
fwrite(fid, 0, 'uint64');

%
%  2nd step: Write a placeholder for the main header
%

fblocks.mainHeaderOffset = ftell(fid);

% Main header: 15*4 = 60 Bytes
fwrite(fid, zeros(1, 60, 'uint8'), 'uint8');

fblocks.mainHeaderSize = ftell(fid) - fblocks.mainHeaderOffset;

%
%  3rd step: Write a placeholder for the content specific header
%

fblocks.contentHeaderOffset = ftell(fid);

if strcmpi(props.content, 'IR')
    % Impulse response content header: 4+4+4 = 12 Bytes
    fwrite(fid, zeros(1, 12, 'uint8'), 'uint8');
end

if strcmpi(props.content, 'MS')
    % Magnitude spectra content header: 4+4+(numfreqs*4) Bytes
    fwrite(fid, zeros(1, 8+props.numfreqs*4, 'uint8'), 'uint8');
end

if strcmpi(props.content, 'PS')
    % Phase spectra content header: 4+(numfreqs*4) Bytes
    fwrite(fid, zeros(1, 4+props.numfreqs*4, 'uint8'), 'uint8');
end

if strcmpi(props.content, 'MPS')
    % Magnitude-phase spectra content header: 4+4+(nfreqs*4) Bytes
    fwrite(fid, zeros(1, 8+props.numfreqs*4, 'uint8'), 'uint8');
end

if strcmpi(props.content, 'DFT')
    % DFT spectra content header: 4+4+4+4 = 16 Bytes
    fwrite(fid, zeros(1, 16, 'uint8'), 'uint8');
end

fblocks.contentHeaderSize = ftell(fid) - fblocks.contentHeaderOffset;

%
%  4th step: Write a placeholder for the record descriptor table
%

% Start at a 16-Byte boundary
daffv15_fpad16(fid);
fblocks.recordDescOffset = ftell(fid);

% Note: Each record has a channel desc for each channel

if strcmpi(props.content, 'IR')
    % A single IR record channel desc is 4+4+4+8 Byte = 20 Bytes
    fwrite(fid, zeros(1, 20*props.dataset.numrecords*props.dataset.channels, 'uint8'), 'uint8');
else
    % All other content use a default record channel desc (MS/PS/MPS/DFT)
    % which is 8 Bytes
    fwrite(fid, zeros(1, 8*props.dataset.numrecords*props.dataset.channels, 'uint8'), 'uint8');
end

% Finally we write a placeholder for the list of record metadata indices
% Each entry is 4 Bytes
fpos.recordMetadataList = ftell(fid);
fwrite(fid, zeros(1, 4*props.dataset.numrecords, 'uint8'), 'uint8');

% Now the record descriptor block ends
fblocks.recordDescSize = ftell(fid) - fblocks.recordDescOffset;

%
%  5th step: Write the data itself
%

% Start at a 16-Byte boundary
daffv15_fpad16(fid);
fblocks.dataOffset = ftell(fid);

% Structure for record information
recordDesc = cell(1, props.dataset.numrecords);

props.globalPeak = 0;
props.minEffFilterOffset = -1;
props.maxEffFilterLength = -1;

for i=1:props.dataset.numrecords
    record = props.dataset.records{i};
    
    if (props.hasDatafunc)
        % Obtain the record data using the data function
        
        if strcmpi(props.content, 'IR')
            [data, samplerate, metadata] = props.datafunc(record.alpha, record.beta, props.basepath);
            record.data = data;
            record.metadata = metadata;
            
            % Recheck sampling rate
            if (samplerate ~= props.dataset.samplerate)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
793
                error('For record %d (A%0.1f�, B%0.1f�): Data function delivered different samplerate then expected', i, record.alpha, record.beta);
794 795 796 797 798 799 800 801 802 803
            end
        end
        
        if strcmpi(props.content, 'MS')
            [freqs, mags, metadata] = props.datafunc(record.alpha, record.beta, props.basepath);
            record.data = mags;
            record.metadata = metadata;
            
            % Recheck sampling rate
            if (isequal(freqs, props.dataset.freqs))
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
804
                error('For record %d (A%0.1f�, B%0.1f�): Data function delivered different frequencies then expected', i, record.alpha, record.beta);
805 806 807 808 809 810 811 812 813 814 815
            end
        end
        
        % TODO: Implement other datafunctions
    end
    
    
    % --= Validate the data =--
    
    % Empty data is now allowed
    if isempty(record.data)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
816
        error('For record %d (A%0.1f�, B%0.1f�): No data provided', i, record.alpha, record.beta);
817 818 819
    end
    
    if ~isnumeric(record.data)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
820
        error('For record %d (A%0.1f�, B%0.1f�): Data must be numerical', i, record.alpha, record.beta);
821 822 823 824 825 826 827 828 829 830 831 832
    end
    
    % Implicit datatype conversion to double
    if ~strcmpi(class(record.data), 'double')
        data = double( record.data );
    else
        data = record.data;
    end
    
    % Check metadata types
    if isfield(record, 'metadata')
        if ~isstruct(record.metadata)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
833
            error('For record %d (A%0.1f�, B%0.1f�): Metadata must be at least an empty structure', i, record.alpha, record.beta);
834 835 836 837 838 839 840 841 842 843
        end
    end
    
    
    % Note: From here we use local data variable: data
    
    [channels, numelements] = size(data);
    
    % Test for correct numnber of channels
    if (channels ~= props.dataset.channels)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
844
        error('For record (A%0.1f�, B%0.1f�): Wrong number of channels', i, record.alpha, record.beta);
845 846 847 848 849
    end
    
    % Elements per records
    if isfield(props, 'elementsPerRecord')
        if (numelements ~= props.elementsPerRecord)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
850
            error('For record (A%0.1f�, B%0.1f�): Wrong data size', i, record.alpha, record.beta);
851 852 853 854 855 856 857 858 859 860
        end
    else
        % The first record defines the number of elements/record
        props.elementsPerRecord = numelements;
    end
    
    
    % Test for real-valued data (IR, MS)
    if (strcmpi(props.content, 'IR') || strcmpi(props.content, 'MS'))
        if ~isreal(data)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
861
            error('For record (A%0.1f�, B%0.1f�): Data must be real-valued', i, record.alpha, record.beta);
862 863 864 865 866 867 868 869
        end
    end
    
    % Peak detection
    recordDesc{i}.peak = max(max(abs(data)));
    props.globalPeak = max([props.globalPeak  recordDesc{i}.peak]);
    
    if props.verbose && (recordDesc{i}.peak > 1)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
870
        %         fprintf('For record (A%0.1f�, B%0.1f�): Peak value %0.3f greater then 1', i, record.alpha, record.beta, recordDesc{i}.peak);
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
        fprintf('For record %d: Peak value %0.3f greater then 1\n', i, recordDesc{i}.peak);
    end
    
    % --= Write the data =--
    
    % Vectors storing the data offsets for each channel
    recordDesc{i}.dataOffset = zeros(1, props.dataset.channels);
    
    if strcmpi(props.content, 'IR')
        
        % Vectors for offsets, effective lengths, etc.
        recordDesc{i}.effOffset = zeros(1, props.dataset.channels);
        recordDesc{i}.effLength = zeros(1, props.dataset.channels);
        recordDesc{i}.scaling = zeros(1, props.dataset.channels);
        
        for c=1:props.dataset.channels
            % Remember the offset of this channel data
            % Important: Relative to the beginning of the data block
            recordDesc{i}.dataOffset(c) = ftell(fid) - fblocks.dataOffset;
            
            % Scan the effective boundaries (Matlab indices!)
            [lwr, upr] = daffv15_effective_filter_bounds(data(c,:), props.zthreshold_value);
            
            % Keep the offset and length a modulo of 4 (16-byte alignment)
            % (Note: lwr-1 => switch from Matlab indexing to C-indexing)
            elen = upr-lwr+1;
            recordDesc{i}.effOffset(c) = daffv15_lwrmul(lwr-1, 4);
            recordDesc{i}.effLength(c) = daffv15_uprmul(elen, 4);
            
            props.minEffFilterOffset = min([props.minEffFilterOffset recordDesc{i}.effOffset(c)]);
            props.maxEffFilterLength = max([props.maxEffFilterLength recordDesc{i}.effLength(c)]);
            
            % Back to Matlab indices (+1)
            i1 = recordDesc{i}.effOffset(c) + 1;
            i2 = i1 + recordDesc{i}.effLength(c) - 1;
            
            % Write down the filter coefficients
            switch props.quantization
                case 'int16'
                    % Peak detection within the effective filter coefficients
                    % which defines the scaling for integer quantizations
                    peak = max(abs( data(c,i1:i2) ));
                    
                    % Division by zero protection
                    if (peak == 0)
                        peak = 1;
                    end
                    
                    recordDesc{i}.scaling(c) = peak;
                    
                    % Note: We normalize the data so that -1, +1 maps
                    % to -32767, +32767. The when reading the content
                    % later, we transform -32767, +32767 back to -1, +1
                    % and THEN apply the scaling factor, which is
                    % nothing but the detected peak value in the effective
                    % filter coefficients ...
                    
                    % int16 dynamic range: 2^15-1 = 32767
                    idata = int16( data(c,i1:i2) ./ peak * 32767 );
                    fwrite(fid, idata, 'bit16');
                    clear idata;
                    
                case 'int24'
                    % Peak detection within the effective filter coefficients
                    % which defines the scaling for integer quantizations
                    peak = max(abs( data(c,i1:i2) ));
                    
                    % Division by zero protection
                    if (peak == 0)
                        peak = 1;
                    end
                    
                    recordDesc{i}.scaling(c) = peak;
                    
                    % Note: We normalize the data so that -1, +1 maps
                    % to -8388607, +8388607. The when reading the content
                    % later, we transform -8388607, +8388607 back to -1, +1
                    % and THEN apply the scaling factor, which is
                    % nothing but the detected peak value in the effective
                    % filter coefficients ...
                    
                    % int24 dynamic range: 2^23-1 = 8388607
                    idata = int32( data(c,i1:i2) ./ peak * 8388607 );
                    fwrite(fid, idata, 'bit24');
                    clear idata;
                    
                case 'float32'
                    % Note: Scaling factors are unused for floating points.
                    recordDesc{i}.scaling(c) = 1;
                    fwrite(fid, data(c,i1:i2), 'float32');
            end
        end
    end
    
    if strcmpi(props.content, 'MS') || strcmpi(props.content, 'PS')
        
        for c=1:props.dataset.channels
            % Remember the offset of this channel data
            % Important: Relative to the beginning of the data block
            recordDesc{i}.dataOffset(c) = ftell(fid) - fblocks.dataOffset;
            
            if (props.elementsPerRecord ~= props.numfreqs)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
973
                error('For record (A%0.1f�, B%0.1f�): Data size does not match the number of frequencies', i, record.alpha, record.beta);
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
            end
            
            % Write down the magnitudes
            switch props.quantization
                case 'float32'
                    fwrite(fid, data(c,:), 'float32');
            end
        end
        
        clear data;
    end % End-if: Case MS|PS content
    
    if strcmpi(props.content, 'MPS')
        
        for c=1:props.dataset.channels
            % Remember the offset of this channel data
            % Important: Relative to the beginning of the data block
            recordDesc{i}.dataOffset(c) = ftell(fid) - fblocks.dataOffset;
            
            if (props.elementsPerRecord ~= props.numfreqs)
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
994
                error('For record (A%0.1f�, B%0.1f�): Data size does not match the number of frequencies', i, record.alpha, record.beta);
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
            end
            
            % Write down the magnitudes
            switch props.quantization
                case 'float32'
                    % Write down complex numbers in interleaved format:
                    % Re(1), Im(1), Re(2), Im(2), ...
                    cdata = zeros(1, props.numfreqs*2);
                    for k=1:props.numfreqs
                        cdata(2*(k-1)+1) = real( data(c,k) );
                        cdata(2*(k-1)+2) = imag( data(c,k) );
                    end
                    
                    % Write the real and imaginary parts
                    fwrite(fid, cdata, 'float32');
            end
        end
        
        clear data;
    end % End-if: Case MPS content
    
    if strcmpi(props.content, 'DFT')
        
        for c=1:props.dataset.channels
            % Remember the offset of this channel data
            % Important: Relative to the beginning of the data block
            recordDesc{i}.dataOffset(c) = ftell(fid) - fblocks.dataOffset;
            
            if (~((props.elementsPerRecord == props.dataset.transformsize) || ...
                    (props.elementsPerRecord == props.numDFTCoeffs)) )
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
1025
                error('For record (A%0.1f�, B%0.1f�): Data size does not match transform size or number of symmetric coefficients', i, record.alpha, record.beta);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
            end
            
            % Write down the magnitudes
            switch props.quantization
                case 'float32'
                    % Write down complex numbers in interleaved format:
                    % Re(1), Im(1), Re(2), Im(2), ...
                    cdata = zeros(1, props.numDFTCoeffs*2);
                    for k=1:props.numDFTCoeffs
                        cdata(2*(k-1)+1) = real( data(c,k) );
                        cdata(2*(k-1)+2) = imag( data(c,k) );
                    end
                    
                    % Write the real and imaginary parts
                    fwrite(fid, cdata, 'float32');
            end
        end
        
        clear data;
    end % End-if: Case DFT content
    
end % End-for: Writing record data

if (props.minEffFilterOffset == -1)
    props.minEffFilterOffset = 0;
end

if (props.maxEffFilterLength == -1)
    props.maxEffFilterLength = props.elementsPerRecord;
end

fblocks.dataSize = ftell(fid) - fblocks.dataOffset;


%
%  6th step: Write the metadata
%

fblocks.metadataOffset = ftell(fid);

props.globalMetadataIndex = -1;
hasMetadata = false;
metadataIndex = 0;

% Write global metadata
if isfield(props.dataset, 'metadata')
    % Test for empty metadata
    if ~isempty(fieldnames(props.dataset.metadata))
        daffv15_write_metadata(fid, props.dataset.metadata);
        props.globalMetadataIndex = metadataIndex;
        metadataIndex = metadataIndex + 1;
        hasMetadata = true;
    end
end

% Write record metadata

for i=1:props.dataset.numrecords
    record = props.dataset.records{i};
    
    recordDesc{i}.metadataIndex = -1;
    
    if isfield(record, 'metadata')
        if ~isempty(fieldnames(record.metadata))
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
1090
            daffv15_write_metadata(fid, record.metadata);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
            recordDesc{i}.metadataIndex = metadataIndex;
            metadataIndex = metadataIndex + 1;
            hasMetadata = true;
        end
    end
end

fblocks.metadataSize = ftell(fid) - fblocks.metadataOffset;
props.filesize = ftell(fid);

%
%  Xth step: Update offsets and sizes in the file header
%

fseek(fid, fpos.mainHeaderOffset, 'bof');
fwrite(fid, fblocks.mainHeaderOffset, 'uint64');

fseek(fid, fpos.mainHeaderSize, 'bof');
fwrite(fid, fblocks.mainHeaderSize, 'uint64');

fseek(fid, fpos.contentHeaderOffset, 'bof');
fwrite(fid, fblocks.contentHeaderOffset, 'uint64');

fseek(fid, fpos.contentHeaderSize, 'bof');
fwrite(fid, fblocks.contentHeaderSize, 'uint64');

fseek(fid, fpos.recordDescOffset, 'bof');
fwrite(fid, fblocks.recordDescOffset, 'uint64');

fseek(fid, fpos.recordDescSize, 'bof');
fwrite(fid, fblocks.recordDescSize, 'uint64');

fseek(fid, fpos.dataOffset, 'bof');
fwrite(fid, fblocks.dataOffset, 'uint64');

fseek(fid, fpos.dataSize, 'bof');
fwrite(fid, fblocks.dataSize, 'uint64');

if hasMetadata
    % 5 file blocks, when there is metadata
    fseek(fid, fpos.numFileBlocksOffset, 'bof');
    fwrite(fid, 5, 'int32');
    
    fseek(fid, fpos.metadataOffset, 'bof');
    fwrite(fid, fblocks.metadataOffset, 'uint64');
    
    fseek(fid, fpos.metadataSize, 'bof');
    fwrite(fid, fblocks.metadataSize, 'uint64');
else
    % Just 4 file blocks, when there is no metadata
    fseek(fid, fpos.numFileBlocksOffset, 'bof');
    fwrite(fid, 4, 'int32');
end


%
%  7th step: Update the main header
%

fseek(fid, fblocks.mainHeaderOffset, 'bof');
fwrite(fid, props.contentType, 'int32');
fwrite(fid, props.quantizationType, 'int32');
fwrite(fid, props.dataset.channels, 'int32');
fwrite(fid, props.dataset.numrecords, 'int32');
fwrite(fid, props.elementsPerRecord, 'int32');
fwrite(fid, props.globalMetadataIndex, 'int32');
fwrite(fid, props.dataset.alphapoints, 'int32');
fwrite(fid, props.dataset.alpharange(1), 'float32');
fwrite(fid, props.dataset.alpharange(2), 'float32');
fwrite(fid, props.dataset.betapoints, 'int32');
fwrite(fid, props.dataset.betarange(1), 'float32');
fwrite(fid, props.dataset.betarange(2), 'float32');
fwrite(fid, props.orient(1), 'float32');
fwrite(fid, props.orient(2), 'float32');
fwrite(fid, props.orient(3), 'float32');

%
%  8th step: Update the content header
%

fseek(fid, fblocks.contentHeaderOffset, 'bof');

if strcmpi(props.content, 'IR')
    % Impulse response content header
    fwrite(fid, props.dataset.samplerate, 'float32');
    fwrite(fid, props.minEffFilterOffset, 'int32');
    fwrite(fid, props.maxEffFilterLength, 'int32');
end

if strcmpi(props.content, 'MS')
    % Magnitude spectra  content header
    fwrite(fid, props.globalPeak, 'float32');
    fwrite(fid, props.numfreqs, 'int32');
    fwrite(fid, props.dataset.freqs, 'float32');
end

if strcmpi(props.content, 'PS')
    % Phase spectra content header
    fwrite(fid, props.numfreqs, 'int32');
    fwrite(fid, props.dataset.freqs, 'float32');
end

if strcmpi(props.content, 'MPS')
    % Magnitude-phase spectra content header
    fwrite(fid, props.globalPeak, 'float32');
    fwrite(fid, props.numfreqs, 'int32');
    fwrite(fid, props.dataset.freqs, 'float32');
end

if strcmpi(props.content, 'DFT')
    % DFT spectra content header
    fwrite(fid, props.numDFTCoeffs, 'int32');
    fwrite(fid, props.dataset.transformsize, 'int32');
    fwrite(fid, props.dataset.samplerate, 'int32'); % stienen: really? Why not float/double?
    fwrite(fid, props.globalPeak, 'float32');
end


%
%  9th step: Update the record descriptors
%

fseek(fid, fblocks.recordDescOffset, 'bof');

for i=1:props.dataset.numrecords
    if strcmpi(props.content, 'IR')
        % Impulse response content
        
        % Important: For IR content we write an individual descriptor
        % for each channel. This is necessary when we perform zero-compression.
        
        for c=1:props.dataset.channels
            fwrite(fid, recordDesc{i}.effOffset(c), 'int32');
            fwrite(fid, recordDesc{i}.effLength(c), 'int32');
            fwrite(fid, recordDesc{i}.scaling(c), 'float32');
            fwrite(fid, recordDesc{i}.dataOffset(c), 'uint64');
        end
    else
        % All other content (MS,PS,MPS,DFT) uses the default desc
        
        % Also here we write an individual descriptor for each channel
        for c=1:props.dataset.channels
            fwrite(fid, recordDesc{i}.dataOffset(c), 'uint64');
        end
    end
    
    % Afterwards we write the index of the metadata
    fwrite(fid, recordDesc{i}.metadataIndex, 'int32');
end

% Writing is finished
fclose(fid);

if ~props.quiet
    fprintf('\n--= DAFF write summary =----------------------------------------------\n\n');
    
    % Print a intermediate summary of the information
    
    fprintf('  Filename:               \t%s\n', props.filename);
    fprintf('  Filesize:               \t%d Bytes\n', props.filesize);
    fprintf('  Content type:           \t%s\n', props.contentStr);
    fprintf('  Num channels:           \t%d\n', props.dataset.channels);
    fprintf('  Num records:            \t%d\n', props.dataset.numrecords);
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
1254 1255
    fprintf('  Alpha range:            \t[%0.1f�, %0.1f�]\n', props.dataset.alpharange(1), props.dataset.alpharange(2));
    fprintf('  Alpha resolution:       \t%0.1f�\n', props.dataset.alphares);
1256
    fprintf('  Num alpha points:       \t%d\n', props.dataset.alphapoints);
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
1257 1258
    fprintf('  Beta range:             \t[%0.1f�, %0.1f�]\n', props.dataset.betarange(1), props.dataset.betarange(2));
    fprintf('  Beta resolution:        \t%0.1f�\n', props.dataset.betares);
1259
    fprintf('  Num beta points:        \t%d\n', props.dataset.betapoints);
Jan-Gerrit Richter's avatar
Jan-Gerrit Richter committed
1260
    fprintf('  Orientation:            \t(Y%+0.1f�, P%+0.1f�, R%+0.1f�)\n\n', ...
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
        props.orient(1), props.orient(2), props.orient(3));
    
    if strcmpi(props.content, 'IR')
        fprintf('  Sampling rate:          \t%0.1f Hz\n', props.dataset.samplerate);
        fprintf('  Quantization:           \t%s\n', props.quantizationStr);
        fprintf('  Zero threshold:         \t%+0.1f dB (%0.6f)\n', props.zthreshold, props.zthreshold_value);
    end
    
    if strcmpi(props.content, 'MS')
        fprintf('  Frequencies:            \t%s Hz\n', mat2str(props.dataset.freqs));
    end
    
    if strcmpi(props.content, 'PS')
        fprintf('  Frequencies:            \t%s Hz\n', mat2str(props.dataset.freqs));
    end
    
    if strcmpi(props.content, 'MPS')
        fprintf('  Frequencies:            \t%s Hz\n', mat2str(props.dataset.freqs));
    end
    
    if strcmpi(props.content, 'DFT')
        fprintf('  Sampling rate:          \t%0.1f Hz\n', props.dataset.samplerate);
        fprintf('  Transform size:         \t%d\n', props.dataset.transformsize);
        fprintf('  DFT coefficients stored:\t%d\n', props.numDFTCoeffs);
    end
    
    fprintf('  Global peak:            \t%+0.1f dB (%0.6f)\n\n', 20*log10(props.globalPeak), props.globalPeak);
    
    if props.verbose
        fprintf('  Content header size:    \t%d Bytes\n', fblocks.contentHeaderSize);
        fprintf('  Record descriptor size: \t%d Bytes\n', fblocks.recordDescSize);
        fprintf('  Data size:              \t%d Bytes\n', fblocks.dataSize);
        fprintf('  Metadata size:          \t%d Bytes\n', fblocks.metadataSize);
        fprintf('  Metadata blocks:        \t%d\n\n', metadataIndex);
    end
    
    fprintf('----------------------------------------------------------------------\n\n');
    
    % What we all been waiting for...
    fprintf('DAFF file ''%s'' successfully written\n\n', props.filename);
end
end