% Platform-independent reading of raw 16-bit sample files
%
% [data, size] = rawread(filename)
% [data, size] = rawread(filename, startpoint)
%     startpoint zero points to the beginning of the file
%     startpoint > 0 typically used to skip header
%
% by Guillaume LATHOUD at IDIAP (lathoud@idiap.ch)

function [data, count] = rawread(filename, startpoint)

    if nargin < 2
        startpoint = 0;
    end

    fd = fopen(filename, 'r');
    
    if fseek(fd, startpoint, 'bof') ~= 0
        error('Could not move to startpoint ' + startpoint + ' in file ' + filename)
    end
    
    [orig_data, count] = fread(fd, 'uint8');
    fclose(fd);
    n = floor(count/2);
    odd = 1 + (0:n-1) * 2;
    even = 1 + odd;
    data = bit2cmp(bitshift(orig_data(odd), 8)+ orig_data(even), 16);
    count = n;
    
