%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % main script for smart segmentation and quantization routines: % % Jeff Lieberman 2oo5 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; format compact; tic % measure compute time % first segment the sound, etc: file = '~/Desktop/9'; [ttemp x soundDataLeft soundDataRight] = nasty(strcat(file,'.wav'),0.05); %(file,noise) timeIndeces = ttemp; % change ttemp to scaled times, but keep indeces for sound splitting % remap times in between 0 and 4 b/c it's a bar ttemp = 16*(ttemp./max(ttemp))+1; % and remove the final, now that we've scaled with it: t = ttemp(1:end-1); % and normalize weights: x = x/max(x); templateTimes = 1:0.25:17; % for now, four beats, given loop length % set up normal 16th note grid tq = [1 0.2 0.5 0.5]; templateWeights = [tq tq tq tq tq tq tq tq tq tq tq tq tq tq tq tq 1]; % align samples, generate template indeces of where samples should go: % a b g d = paramters signify -> deprecated, moving cost, weight cost, acceleration cost index = align6(t,x,templateTimes,templateWeights,0,100,100,1000); % 10,50 % show normal quantized times also, for comparison quantizedTimes = round(4*t)/4; % nearest 16th note % quant is the indeces, for timing: quant = quantizedTimes*4-3; % make output sound: % soundDataLeft/Right are tracks. timeIndeces are the start sample times % so use timeIndeces(i+1)-1 as the end point toc; %%%%%%%%%%%%%%%% % output: %%%%%%%%%%%%%%%% % place at template points with some spacing: space = round(length(soundDataLeft)/(64*4))*4; % this is 1/16th note: so, a beat is 8000 = about 0.2 seconds.. padSamples = 44100; % pad one second for longer samples to wrap around outputLoop = zeros(1,space*64+padSamples); % time buffer scales with samples, ten extra sec at end quantLoop = zeros(1,space*64+padSamples); for i=1:length(timeIndeces)-1, % for each sample: sampleLength = timeIndeces(i+1)-timeIndeces(i); outputLoop(space*index(i):space*index(i)+sampleLength) = ... outputLoop(space*index(i):space*index(i)+sampleLength) + ... soundDataLeft(timeIndeces(i):timeIndeces(i)+sampleLength)'; % add some fun: 1/16th notes if (rand > 1), % add some copies of this one at space/4 or space/2 interval: outputLoop(space*index(i)+space:space*index(i)+sampleLength+space) = ... outputLoop(space*index(i)+space:space*index(i)+sampleLength+space) + ... 0.8*soundDataLeft(timeIndeces(i):timeIndeces(i)+sampleLength)'; elseif (rand > 1), % 1/32 notes % add some copies of this one at space/4 or space/2 interval: for j=1:2, outputLoop(space*index(i)+j*space/2:space*index(i)+sampleLength+j*space/2) = ... outputLoop(space*index(i)+j*space/2:space*index(i)+sampleLength+j*space/2) + ... 0.5*soundDataLeft(timeIndeces(i):timeIndeces(i)+sampleLength)'; end elseif (rand > 1 ), % 1/64 notes % add some copies of this one at space/4 or space/2 interval: for j=1:4, outputLoop(space*index(i)+j*space/4:space*index(i)+sampleLength+j*space/4) = ... outputLoop(space*index(i)+j*space/4:space*index(i)+sampleLength+j*space/4) + ... 0.4*soundDataLeft(timeIndeces(i):timeIndeces(i)+sampleLength)'; end end % also output quantized version: quantLoop(space*quant(i):space*quant(i)+sampleLength) = ... quantLoop(space*quant(i):space*quant(i)+sampleLength) + ... soundDataLeft(timeIndeces(i):timeIndeces(i)+sampleLength)'; end % add metronome: for i=1:16, outputLoop(space*(4*i-3)) = outputLoop(space*(4*i-3))+1; quantLoop(space*(4*i-3)) = quantLoop(space*(4*i-3))+1; end % % trim extra zeros: % i=length(outputLoop); % while(outputLoop(i)==0), % i = i-1; % track back % end % trimmedOutputLoop = outputLoop(1:i); % i=length(quantLoop); % while(quantLoop(i)==0), % i = i-1; % track back % end % trimmedQuantLoop = quantLoop(1:i); trimmedOutputLoop = outputLoop(1:space*64); trimmedQuantLoop = quantLoop(1:space*64); sound(trimmedOutputLoop,44100) % listen! % store output file: wavwrite(trimmedOutputLoop,44100,16,strcat(file,'Fixed.wav')); wavwrite(trimmedQuantLoop,44100,16,strcat(file,'NormalQuant.wav')); figure; subplot(2,1,1); % first plot the times, then the accelerations below % plot(t,t,'--'); % hold on; % plot(t,t,'x'); plot(t,quantizedTimes-t,'rx-'); hold on; plot(t,templateTimes(index)-t,'x-'); plot(t,t-t,'--'); %plot(t,templateTimes(index),'x'); % quantized version: %plot(t,quantizedTimes,'kx'); title('Remapped Timing Deviation Information') xlabel('Original Time'); ylabel('Mapped Time [Deviation]'); legend('Regular Quantization','Intelligent Quantization'); subplot(2,1,2); % velocities: v = diff(t); templateV = diff(templateTimes(index)); quantizedVelocities = diff(quantizedTimes); % acceleration calcs: a = templateV ./ v; % change in slope from normal each time quantA = quantizedVelocities ./ v; % quant version for comparison plot(diff(quantA).^2,'k'); hold on; plot(diff(a).^2); xlabel('Samples'); ylabel('Local Accelerations'); title('Time Acceleration Minimization'); legend('Regular Quantization','Intelligent Quantization'); %axis([0 10 0 3])