Herma Applet Code

function varargout = Hermaui(varargin)
% HERMAUI MATLAB code for Hermaui.fig
%      HERMAUI, by itself, creates a new HERMAUI or raises the existing
%      singleton*.
%
%      H = HERMAUI returns the handle to a new HERMAUI or the handle to
%      the existing singleton*.
%
%      HERMAUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in HERMAUI.M with the given input arguments.
%
%      HERMAUI('Property','Value',...) creates a new HERMAUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Hermaui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Hermaui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Hermaui

% Last Modified by GUIDE v2.5 30-Oct-2020 10:13:23

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Hermaui_OpeningFcn, ...
                   'gui_OutputFcn',  @Hermaui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Hermaui is made visible.
function Hermaui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Hermaui (see VARARGIN)

% Choose default command line output for Hermaui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

global fields
global tab
global nm
fields = hermafields(88);
nm = [];
clc
tab = [ 1	1	40	14	0	1.73;...
        1	1	50	9	14	2.8;...
        1	1	60	5.5	23	4.53;...
        1	1	70	3.5	28.5	7.32;...
        1	1	80	2.5	32	11.8;...
        1	1	90	2.5	34.5	19;...
        1	1	127	0.5	36	31;...
        2	2	100	46	36.5	0.8;...
        2	2	20	16	45	3.3;...
        2	2	20	12.5	70	5;...
        2	3	100	26	100	10;...
        2	4	80	14	126	1.76;...
        2	4	20	14	128	3.3;...
        2	4	20	1	134	5;...
        2	4	20	24	138	5;...
        2	4	80	19	150	1.76;...
        2	5	100	22	182	10;...
        2	6	20	18	208	2.5;...
        2	6	100	10	212	5;...
        2	7	100	34	216	9;...
        3	8	20	6	256	0.8;...
        3	9	80	4	270	0.8;...
        3	8	20	4	256	10;...
        3	10	20	12	278	20;...
        3	8	80	2	285	5;...
        3	11	120	4	287	6;...
        3	10	20	4	294	20;...
        3	12	120	6	296	6;...
        3	12	100	8	298	12;...
        3	13	80	2	300	6;...
        4	14	80	8	302	10;...
        4	15	20	12	306	1;...
        4	16	120	2	313	3;...
        4	17	20	17	318	3;...
        4	12	100	2	322	6;...
        4	16	120	2	338	3;...
        4	14	80	10	340	10;...
        4	17	20	2	344	5;...
        4	16	120	2	346	1;...
        4	17	20	8	350	1;...
        4	12	100	2	354	10;...
        4	14	80	16	362	5;...
        4	16	120	2	366	5;...
        4	18	120	2	376	20;...
        4	17	20	12	382	1;...
        4	12	100	2	386	3;...
        4	16	120	2	390	1;...
        4	18	120	2	394	3;...
        4	17	100	5	398	6;...
        4	19	120	11	414	20];
for f = 1:size(tab, 1)
    linha = tab(f,:);
    nm = [nm; modresult(linha)];
end
set(handles.hermadata, 'Data', tab,...
    'ColumnEditable', true)
set(handles.nm, 'Data', nm,...
    'ColumnEditable', true)

% UIWAIT makes Hermaui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Hermaui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in new.
function new_Callback(hObject, eventdata, handles)
% hObject    handle to new (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA
global fields
global tab
global nm
fields = hermafields(88);
nm = [];
clc
tab = [ 1	1	40	14	0	1.73;...
        1	1	50	9	14	2.8;...
        1	1	60	5.5	23	4.53;...
        1	1	70	3.5	28.5	7.32;...
        1	1	80	2.5	32	11.8;...
        1	1	90	2.5	34.5	19;...
        1	1	127	0.5	36	31;...
        2	2	100	46	36.5	0.8;...
        2	2	20	16	45	3.3;...
        2	2	20	12.5	70	5;...
        2	3	100	26	100	10;...
        2	4	80	14	126	1.76;...
        2	4	20	14	128	3.3;...
        2	4	20	1	134	5;...
        2	4	20	24	138	5;...
        2	4	80	19	150	1.76;...
        2	5	100	22	182	10;...
        2	6	20	18	208	2.5;...
        2	6	100	10	212	5;...
        2	7	100	34	216	9;...
        3	8	20	6	256	0.8;...
        3	9	80	4	270	0.8;...
        3	8	20	4	256	10;...
        3	10	20	12	278	20;...
        3	8	80	2	285	5;...
        3	11	120	4	287	6;...
        3	10	20	4	294	20;...
        3	12	120	6	296	6;...
        3	12	100	8	298	12;...
        3	13	80	2	300	6;...
        4	14	80	8	302	10;...
        4	15	20	12	306	1;...
        4	16	120	2	313	3;...
        4	17	20	17	318	3;...
        4	12	100	2	322	6;...
        4	16	120	2	338	3;...
        4	14	80	10	340	10;...
        4	17	20	2	344	5;...
        4	16	120	2	346	1;...
        4	17	20	8	350	1;...
        4	12	100	2	354	10;...
        4	14	80	16	362	5;...
        4	16	120	2	366	5;...
        4	18	120	2	376	20;...
        4	17	20	12	382	1;...
        4	12	100	2	386	3;...
        4	16	120	2	390	1;...
        4	18	120	2	394	3;...
        4	17	100	5	398	6;...
        4	19	120	11	414	20];
for f = 1:size(tab, 1)
    linha = tab(f,:);
    nm = [nm; modresult(linha)];
end
set(handles.hermadata, 'Data', tab)
set(handles.nm, 'Data', nm)

function nm = modresult(linha)
ptc = hermacomps(linha(2));
dyn = linha(3);
seg = linha(4)+linha(5);
tp = linha(5);
durprop = linha(6);
osc = .5;
nm = [];
indp = 1;
dur = ((rand(1)/2+osc)/durprop);

while tp <= seg
    if indp >= size(ptc,2)
        ptc = ptc(randperm(length(ptc)));
        indp = 1;
    end
    line = [tp dur 1 ptc(indp) dyn tp dur];
    nm = [nm; line];
    dur = ((rand(1)/2+osc)/durprop);
    tp = tp + dur;
    indp = indp+1;
    if indp >= size(ptc,2)
        ptc = ptc(randperm(length(ptc)));
        indp = 1;
    end
end

function fields = hermafields(range)
%Hermafields produz os campos distintos a partir dos quais as opera��es
%booleanas ser�o aplicadas.
% Como Herma � produzida por opera��es booleanas aplicadas a tr�s
% conjuntos, A, B e C, dentro de um superconjunto R, e h� instersec��es
% espec�ficas entre eles, � necess�rio gerar previamente 8 campos, que ir�o
% se combinar para gerar os diversos resultados
% A extens�o do piano vai das notas MIDI 21 a 108.
% As alturas s�o embaralhadas para garantir a aleatoriedade
alturas = randperm(range)+20;

% O vetor de alturas recebe sete pontos de segmenta��o, definindo assim
% oito campos.
cutpoints = [1 sort(randperm(88,7)) 88];

% A vari�vel fields recebe os diversos valores.
fields = {};

for f = 1:8
    fields{f} = alturas(cutpoints(f):(cutpoints(f+1)-1));
end

function alturas = hermacomps (modnumber)
% HERMACOMPS retorna o resultado das opera��es de uni�o entre os
% hermafields
% As alturas de cada m�dulo de Herma s�o resultado de opera��es de uni�o
% entre hermafields, que s�o os campos definidos no diagrama de Venn de
% Herma.
global fields
global hermamods
hermamods = {   [1 2 3 4 5 6 7 8]...
                [2 5 7 8]...
                [1 3 4 6]...
                [3 5 6 8]...
                [1 2 4 7]...
                [4 6 7 8]...
                [1 2 3 5]...
                [5 8]...
                [6 8]...
                [1 4 5 8]...
                [8]...
                [4]...
                [2 3 6 7]...
                [1 2]...
                [2]...
                [2 3]...
                [1 3]...
                [3]...
                [2 3 4 8]};
union = [];
hermamodline = hermamods{modnumber};
for f = 1:size(hermamodline, 2)
    tempfield = hermamodline(f);
    union = [union fields{tempfield}];
end
alturas = union;


% --- Executes on button press in openherma.
function openherma_Callback(hObject, eventdata, handles)
% hObject    handle to openherma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global nm
newnm = nm;
nmat = mt2ks(newnm);
matrix = matrix2midi(nmat);
arquivo = ['tinyhgu.mid'];
midi = writemidi (matrix, arquivo);
winopen(arquivo);
% system(['open tinyhgu.mid']);



% --- Executes on button press in saveherma.
function saveherma_Callback(hObject, eventdata, handles)
% hObject    handle to saveherma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tabsave = get(handles.hermadata, 'Data');
uisave('tabsave', 'tab');


% --- Executes on button press in loadherma.
function loadherma_Callback(hObject, eventdata, handles)
% hObject    handle to loadherma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global tab
[arquivo, endereco] = uigetfile({'*.mat', 'Choose MAT'});
caminhocompleto = [endereco arquivo];
if max(caminhocompleto)==0
else
    tabload=load(caminhocompleto);
    msgbox('table loaded');
    tab = tabload.tabsave;
    set(handles.hermadata, 'Data', tab);
end


% --- Executes when entered data in editable cell(s) in hermadata.
function hermadata_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to hermadata (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)
global tab
tab = get(handles.hermadata, 'Data');


% --- Executes when entered data in editable cell(s) in nm.
function nm_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to nm (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)
global nm
nm = get(handles.hermadata, 'Data');