function [data, sampleRate, nrOfSamples, nrOfChannels, microphonePositions] = readRecordingGroup(storageLocation, duration, start)
    % Reads data from a Sorama Portal Measurement stored on disk as a
    % RecordingGroup into matlab. Inputs duration and start can be used to
    % read in a portion of the measurement.
    %
    % Input:
    % storageLocation:  The full path to the RecordingGroup folder that
    %                   contains the information about the Sorama Portal 
    %                   Measurement. Get the Storage Location from the
    %                   Sorama Portal via the "Manage" tab by clicking on
    %                   the (i) icon next to a measurement.
    %
    % duration: Number of (fractional) seconds to read, from the start 
    %           of the measurement. Defaults to entire measurement.
    %
    % start:    If duration is specified, start time in seconds, from the
    %           beginning of the measurement, to start reading from.
    %           Defaults to 0.
    %
    % Output:
    % data:     Array of size nrOfChannels * nrOfSamples containing the raw
    %           data as output directly by the microphones. This unit of
    %           this data is "Full Scale" or "Digital Amplitude". 
    %           This means that it is an integer
    %           value, where the lowest number is the least amount of
    %           pressure the microphone can measure, and the highest number
    %           is the most amount of pressure the microphone can measure.
    %           The actual Pascal value depends on the microphone's
    %           sensitivity value obtained from its datasheet.
    %           The conversion to Pascal can be performed with the script
    %           'convertRawToPascal'.
    %           data(1,:) is the data of the 1st microphone, data(2,:) is
    %           the data of the 2nd microphone and so on. For the Nth 
    %           microphone, data(N,1) is the first sample, data(N,2) is the
    %           2nd sample and so on.
    %
    % sampleRate:   The samplerate at which the measurement was done in [Hz].
    %
    % nrOfSamples:  The amount of individual samples measured per
    %               microphone.
    %
    % nrOfChannels: The amount of microphones of the device that performed
    %               the measurement.
    %
    % microphonePositions:  An array containing the positions of the
    %                       microphones of the measurement device in [m]
    %                       relative to the center (0,0,0) of the device.
    %                       microphonePositions(1,:) is the location of the
    %                       1st microphone, microphonePositions(2,:) of the
    %                       2nd and so on. microphonePositions(N,1) is the
    %                       x coordinate, microphonePositions(N,2) the y
    %                       coordinate and microphonePositions(N,3) the z
    %                       coordinate of Nth microphone.

    if ~exist('duration','var')
      duration = inf;
    end
    
    if ~exist('start','var')
      start = 0;
    end
    
    recordingGroupContainerPath = fullfile(storageLocation, "container.json");
    recordingGroupContainer = readFileAsJson(recordingGroupContainerPath);
    
    nrOfSteps = length(recordingGroupContainer.metadata.steps);
    
    if(nrOfSteps > 1)
        error("The measurement contains more then 1 step, this is currntly not supported by this script.");
    end
    
    recordingId = recordingGroupContainer.metadata.steps(1).recordingId;
    
    recordingPath = fullfile(storageLocation, "..", "..", "Recording", recordingId);
    recordingContainerPath = fullfile(recordingPath, "container.json");
    recordingContainer = readFileAsJson(recordingContainerPath);
    
    nrOfChannels = recordingContainer.metadata.files.sound.frontEnd.microphoneCount;
    
    if(isfield(recordingContainer.metadata, 'sampleRate'))
        sampleRate = recordingContainer.metadata.sampleRate;
    else
        % Old format with samplerate, fill in default
        sampleRate = 1500000 / 32;
    end
    
    % Read channel ordering (+1 because of matlab 1 indexing)
    channelOrdering = recordingContainer.metadata.files.sound.channelOrdering + 1;
    
    microphonePositions = recordingContainer.metadata.files.sound.frontEnd.microphonePositions;    
    microphonePositions = [vertcat(microphonePositions.X) vertcat(microphonePositions.Y) vertcat(microphonePositions.Z)];
    microphonePositions = microphonePositions(channelOrdering,:);
   
    soundFileName = recordingContainer.files.sound;
    soundFilePath = fullfile(recordingPath, soundFileName);
    
    nrOfSamples = duration * sampleRate;
    startSample = start * sampleRate;
    data = readRawSoundFile(soundFilePath, nrOfChannels, nrOfSamples, startSample);
    
    nrOfSamples = size(data,2);
end

function json = readFileAsJson(path)

    fid = fopen(path);
    raw = fread(fid, inf);
    fclose(fid);
    
    json = jsondecode(char(raw'));
    clear raw;
end