- /
-
Never Gonna Give You Up
on 2 Nov 2024
- 52
- 400
- 0
- 4
- 502
Cite your audio source here (if applicable):
% Audio Source
% Rick Astley - Never Gonna Give You Up
drawframe(1);
Write your drawframe function below
% The audio file can be used to store data as shown in
% Tomoaki Takagi / Mini Hack (Audio to Image)
% Extending his ideo for video to store 96 frames each 40-by-40 pixels,
% and 5-bit color can be stored in 12 seconds of 2-channel audio sampled at
% 8000 Hz, with enough room to also fit another 4 second audio to recreate
% the background audio
function drawframe(f)
% Read and store the audio data
persistent linear
if isempty(linear)
[d,fs] = audioread("audio.wav");
linear = reshape(d, [fs*24, 1]);
end
% Get the data for the given frame
linearCropped = linear(f*1600-1599:f*1600);
linearCropped = 1 + reshape(linearCropped, [40,40]) * 32768;
% Assign RGB values for each pixel in 40*40 px image
% Each element of linearCropped has RGB value for 1 pixel
% It is a 16-bit double so I used 5 bits to store each of R,G and B
c = zeros(40,40,3);
c(:,:,1) = fix(linearCropped / (32*32));
c(:,:,2) = mod(fix(linearCropped / 32), 32);
c(:,:,3) = mod(linearCropped, 32);
c = 8*c/255; % To get the value between 0 and 1
% Display the image
set(gca, 'position', [0 0 1 1]);
imagesc(c, Interpolation="bilinear");
axis off;
% Recreate the audio file
if f==96
audiowrite('audio.wav', linear(97*1600-1599:97*1600-1599+8000*4), 8000);
end
end