Why am I unable to add these elements to an array?
Show older comments
I'm currently working on a project where I am creating music from the intensities of an image, and I'm able to produce the overall sounds through a for loop, but I am running into a new error -- I am trying to take all of these frequencies and write them using audiowrite, but I am getting an error that says "Unable to perform assignment because the size of the left side is 1-by-4096 and the size of the right side is 1-by-8192." I am using code from a previous project where I did something similar in creating an array of sounds, so I don't understand why I am getting this error now.
%% SNIPPET OF CODE FROM MAIN FUNCTION
for currentImage = 1:length(allImgs)
filename = allImgs(currentImage) + ".wav";
img = imread(allImgs(currentImage));
[imGray, histMtx, binEdge, notes, songLength] = mus306GenerateNotes(img);
fs = 44100;
time = 1;
values = 0:1/fs:time;
rate = 32768;
for i = 1:songLength
currentNote = notes(i);
singleNote = mus306NoteFinder(currentNote);
[currentOct, ~] = mus306NoteFinder2(currentNote);
octaves(i) = currentOct;
finalNotes(i) = singleNote;
end
secondImg = img;
secondImg(:) = ones(size(secondImg)) * 255;
[r,c,~] = size(img);
for i = 1:songLength
noteLength = durations(randi(durationOption));
octToHear = octaves(i);
note = @(t,freq,oct) sin(linspace(0,2*pi*t*freq*2^oct,round(t*rate)));
% noteSound = sin(linspace(0,2*pi*noteLength*finalNotes(i),...
% round(noteLength*rate)));
noteSound = note(noteLength, finalNotes(i), octToHear);
% ERROR IN LINE BELOW: Unable to perform assignment because the
% size of the left side is 1-by-4096 and the size of the right side
% is 1-by-8192. (i can't figure out how to change the color of this
% to red, apologies)
allNotes(i,:) = noteSound;
sound(noteSound, fs)
pause(0.1)
for row = 1:r
for col = 1:c
if (imGray(row, col) == i)
secondImg(row, col,:) = img(row, col, :);
end
end
end
audiowrite(filename, allNotes, fs);
end
%% CUSTOM FUNCTION MUS306GENERATENOTES.M
function [imGray, histMtx, binEdge, notes, songLength] = mus306GenerateNotes(imgToSee)
audibleMin = 200;
audibleMax = 1000;
imGray = rgb2gray(imgToSee);
[histCount, binEdge] = histcounts(imGray(:), 0:255);
histMtx = [binEdge(1:end-1)', histCount'];
figure(1)
notes = histMtx(:, 2);
songLength = length(notes);
for i = 1:songLength
audibleNote = notes(i);
if ((audibleNote / audibleMin) < 1)
audibleNote = audibleNote * 10;
if ((audibleNote / audibleMax) > 1)
audibleNote = audibleNote / 10;
end
elseif (((audibleNote / audibleMax) > 1))
audibleNote = audibleNote / 10;
if ((audibleNote / audibleMin) < 1)
audibleNote = audibleNote * 10;
end
end
notes(i) = audibleNote;
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!