MATLAB Answers

Audio to Image Conversion

1 view (last 30 days)
Hello Friends, I keep getting errors with this code...please help me out...
%% Audio to Image
infilename = 'audio1.wav';
outfile = 'drums.jpg';
fid = fopen(infilename, 'r');
bytes = fread(fid, [1 inf], '*uint8');
fclose(fid);
numbytes = length(bytes);
f = factor(numbytes);
n = f(end);
m = numbytes/n;
img = reshape(bytes, n, m);
imwrite(img, outfile, 'mode', 'lossless', 'bitdepth', 12);
imshow(outfile)
The error I get is this
Error using matlab.internal.imagesci.wjpg12c
JPEG library error (12 bit), "Maximum supported image dimension is 65500 pixels".
Error in writejpg (line 127)
matlab.internal.imagesci.wjpg12c(data, filename, props);
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in learningAudio (line 31)
imwrite(img, outfile, 'mode', 'lossless', 'bitdepth', 12);
I honestly don't know what to do next

Accepted Answer

DGM
DGM on 9 Sep 2021
What are n and m? Is either greater than 65500? Should it be?
I don't know if using factor() this way is really doing what you want it to. If you're trying to reshape the vector into a roughly square array, this isn't going to do that. Factor() returns the prime factors, all of which are likely very small compared to the square root of numbytes. If your goal is to make the image reasonably square so that writing/viewing it is more practical, then consider something like this:
numbytes = 600; % for example
f = 1:ceil(numbytes/2);
f = f(rem(numbytes,f)==0);
f = sort([f; numbytes./f],1,'ascend'); % factor pairs
ar = f(1,:)./f(2,:); % normalized aspect ratio
[~,idx] = max(ar); % find AR closest to 1
imagegeometry = f(:,idx) % this is the target image geometry
imagegeometry = 2×1
24 25
If the particular value of numbytes doesn't factor well enough to produce an image with an aspect ratio reasonably close to 1 (i.e. maximizing available area within the geometry constraints of the filetype), then consider padding the vector such that it factors more favorably.
The geometry limitations of other filetypes may differ. PNG seems to write images up to 1E6px wide. TIF supports at least 500E6px, though you'll probably run into filesize limits before then if your aspect ratio is sensible.
  1 Comment
DARLINGTON ETAJE
DARLINGTON ETAJE on 9 Sep 2021
Thanks DGM...you gave me the insight to solve it...I changed to png...and changed the number in the last line to 8

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!