- /
-
MATLAB, you are my sunshine
on 14 Oct 2024
- 23
- 259
- 0
- 2
- 1761
Cite your audio source here (if applicable):
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent fig ax keyObjs txtObj
if isempty(fig)
[fig, ax, keyObjs, txtObj] = initFig();
end
numWhiteKeys = 15;
keySeq = [5,8,9,10,10,10,9,10,8,8,8,9,10,11];
keyTimes = [0,7,14,21,28,35,42,49,56,63,70,77,84,91];
keyNames = {'C','D','E','F','G','A','B','C','D','E','F','G','A','B','C'};
segFrame = mod(f-1, 96);
pressIdx = find(segFrame >= keyTimes, 1, 'last');
if isempty(pressIdx)
pressKey = 0;
else
pressKey = keySeq(pressIdx);
end
for i = 1:numWhiteKeys
if i == pressKey
if (i == 10 && pressIdx >= 5) || (i == 8 && pressIdx >= 9)
color = [0.5, 0.5, 0.8];
else
color = [0.7, 0.7, 0.9];
end
set(keyObjs(i), 'FaceColor', color, 'Position', [i-3.45, 0.2, 0.9, 4.8]);
else
set(keyObjs(i), 'FaceColor', [0.95,0.95,0.95], 'Position', [i-3.45, 0, 0.9, 5]);
end
end
if pressKey > 0 && pressKey <= numWhiteKeys
set(txtObj, 'String', keyNames{pressKey}, 'Position', [pressKey-3,5.2,0], 'Visible', 'on');
else
set(txtObj, 'Visible', 'off');
end
end
function [fig,ax,keyObjs,txtObj] = initFig()
fig = figure('Visible', 'off');
ax = axes('Parent', fig);
hold(ax, 'on');
xlim([-3.5,15.5]);
ylim([-1,6]);
axis equal off
rectangle('Position', [-3,-0.5,18,6], 'FaceColor', [0.2,0.2,0.2], 'EdgeColor', 'none');
keyObjs = gobjects(15,1);
for i = 1:15
keyObjs(i) = rectangle('Position', [i-3.45,0,0.9,5], 'FaceColor', [0.95,0.95,0.95], 'EdgeColor', 'k');
line([i-3.45,i-3.45], [0,5], 'Color', [1,1,1], 'LineWidth', 2);
end
blackPos = [-1.5,-0.5,1.5,2.5,3.5,5.5,6.5,8.5,9.5,10.5];
for x = blackPos
rectangle('Position', [x-0.25,2.5,0.5,2.3], 'FaceColor', [0.1,0.1,0.1], 'EdgeColor', 'k');
line([x-0.25,x-0.25], [2.5,4.8], 'Color', [0.3,0.3,0.3], 'LineWidth', 1);
end
txtObj = text(0,5.2,'', 'HorizontalAlignment', 'center', 'Color', 'w', 'FontSize', 14, 'FontWeight', 'bold', 'Visible', 'off');
light('Position', [5,10,10], 'Style', 'infinite');
lighting gouraud
end