Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %ALPHA 0.6 FOR RGB PWM REACTIVE LIGHTING : AS IF ACTUALLY SAMPLING
- %Joe Swelland 5/11/2018
- %==========================================================================
- %ALPHA 0.5 : Introduction of Peak Detection
- %Import Audio
- [clip, Fs] = audioread('hose5.wav'); %Import test song
- clipt = clip(:,1);
- %=======================================================================
- %FILTERING
- %-----------------------------------------------------------------------
- %controls
- lowfc = 150;
- midfc = 600;
- highfc = midfc;
- intensity = 1; %background intensity
- %COLOR ASSIGNMENTS (RANDOMLY CHOSEN)
- %LOW SPECTRUM - BLUE
- [lowA, lowB] = butter(4,lowfc/(Fs/2));
- %MID SPECTRUM - GREEN
- [midA, midB] = butter(4,[lowfc/(Fs/2) midfc/(Fs/2)]);
- %HIGH SPECTRUM - RED
- [highA, highB] = butter(4,highfc/(Fs/2), 'high');
- %SIGNAL FILTERING
- %LOW
- lowFl = intensity * filter(lowA,lowB,clipt);
- %MID
- midFl = intensity * filter(midA,midB,clipt);
- %HIGH
- highFl = intensity * filter(highA,highB,clipt);
- %======================================================================
- %UPDATE FRAMES : to hold value of color for X duration
- %----------------------------------------------------------------------
- %controls
- cnt = 0; %iterator
- interval = 0.05; %time in seconds for color duration
- win = interval * Fs;
- %LOW
- blueFrame = zeros(length(clipt),1);
- maxVolume = 0;
- for i=1:length(clipt)
- volume = abs(lowFl(i));
- if (cnt > win)
- maxVolume = volume;
- cnt = 0;
- else
- cnt = cnt + 1; %increment window counter
- if (volume > maxVolume)
- maxVolume = volume;
- cnt = 0; %reset window counter
- end
- end
- blueFrame(i) = maxVolume;
- end
- %MID
- greenFrame = zeros(length(clipt),1);
- maxVolume = 0;
- for i=1:length(clipt)
- volume = abs(midFl(i));
- if (cnt > win)
- maxVolume = volume;
- cnt = 0;
- else
- cnt = cnt + 1; %increment window counter
- if (volume > maxVolume)
- maxVolume = volume;
- cnt = 0; %reset window counter
- end
- end
- greenFrame(i) = maxVolume;
- end
- %HIGH
- redFrame = zeros(length(clipt),1);
- maxVolume = 0;
- for i=1:length(clipt)
- volume = abs(highFl(i));
- if (cnt > win)
- maxVolume = volume;
- cnt = 0;
- else
- cnt = cnt + 1; %increment window counter
- if (volume > maxVolume)
- maxVolume = volume;
- cnt = 0; %reset window counter
- end
- end
- redFrame(i) = maxVolume;
- end
- %==========================================================================
- %BUMP DETECTOR
- %--------------------------------------------------------------------------
- %controls
- volume = 0;
- avgTot = 0;
- avgLog = zeros((floor(0.07*Fs)),1);
- currAvg = zeros(length(clipt),1);
- logSlope = zeros(length(clipt),1);
- a = 1; %iterator
- lastAvg = 0;
- up = 1;
- peak = 0;
- n = 0; %iterator
- %lowAvgLog=zeros(length(clipt),1);
- for i=1:length(clipt)
- volume = abs(clipt(i)); %grab current sample
- if (a > (floor(0.07 * Fs))) %average array rollover
- a = 1;
- avgTot = avgTot - avgLog(a);
- avgLog(a) = volume;
- avgTot = avgTot + avgLog(a);
- else %non-rollover
- avgTot = avgTot - avgLog(a);
- avgLog(a) = volume;
- avgTot = avgTot + avgLog(a);
- a = a + 1;
- end
- %Update average
- currAvg(i) = avgTot/(0.07*Fs);
- %Slope Detection of Average
- if (up) %Upwards mode
- if (currAvg(i) > lastAvg)
- lastAvg = currAvg(i);
- n = 0;
- elseif (currAvg(i) < lastAvg)
- if (n >(0.5*Fs)) %will only switch to down if no new max in entire window length n
- %peak detected, follow down next
- if (abs(currAvg(i) - lastAvg) > lastAvg/4) %if the change is tiny, probably not a big enough slope change
- up = 0;
- peak = 0; %sound peak detected toggle
- end
- n = 0; %reset window counter
- else
- n = n + 1; %increment window counter
- end
- end
- elseif (up==0) %Downwards mode, works mostly same as upwards.
- if (currAvg(i) < lastAvg)
- lastAvg = currAvg(i);
- n = 0;
- elseif (currAvg(i) > lastAvg)
- if (n > (0.5*Fs))
- %trough detected, follow up next
- if (abs(currAvg(i) - lastAvg) > currAvg(i)/4)
- up = 1;
- peak = 1;
- end
- n = 0;
- else
- n = n + 1;
- end
- end
- end
- %if currently in a sound bump
- if (peak)
- logSlope(i) = 1 - fade; %decay intensity of bump
- fade = fade + logSlope(i)/10000; %decay rate, made it up.
- else
- logSlope(i) = 0; %if not bumping, bump effect = 0
- fade = 0;
- end
- end
- %==========================================================================
- %ADD BUMP TO FRAMES : still under develeopment. not sure how I want to have
- %a bump effect color.
- %--------------------------------------------------------------------------
- for i=1:length(clipt)
- if (blueFrame(i) > redFrame(i) && blueFrame(i) > greenFrame(i) && logSlope(i))
- greenFrame(i) = redFrame(i) + logSlope(i);
- %redFrame(i) = greenFrame(i) + logSlope(i);
- %blueFrame(i) = blueFrame(i);
- elseif (redFrame(i) > blueFrame(i) && redFrame(i) > greenFrame(i) && logSlope(i))
- %greenFrame(i) = greenFrame(i) + logSlope(i);
- blueFrame(i) = blueFrame(i) + logSlope(i);
- %redFrame(i) = redFrame(i);
- elseif (greenFrame(i) > blueFrame(i) && greenFrame(i) > redFrame(i) && logSlope(i))
- redFrame(i) = redFrame(i) + logSlope(i);
- %blueFrame(i) = blueFrame(i) + logSlope(i);
- %greenFrame(i) = greenFrame(i);
- end
- end
- %==========================================================================
- %SIMULATION OF LIGHTS : draws a "square" frame of color representing the
- %RGB output to the LED
- %--------------------------------------------------------------------------
- %controls
- spacing = 5;%width in pixels of each color frame
- draw_start = 0; %seconds
- draw_time = 120; %seconds
- current = 1; %image array index for colors
- total_samples = draw_time * Fs; %total samples expected to be drawn.
- squares = total_samples/(interval*Fs); %amount of squares of color to draw. Squares SHOULD be length = interval seconds
- next = current + spacing; %jump pixels to next color
- move = 0; %reseting just incase
- drw_smple_start = draw_start*Fs; %image sample start
- stream=zeros(500, floor(squares), 3); %create image stream
- for i=1:floor(squares)
- move = move + (interval*Fs);
- %SQUARE CREATION
- stream(:,current:next,1)= redFrame(drw_smple_start+move); %RED
- stream(:,current:next,2)= greenFrame(drw_smple_start+move); %GREEN
- stream(:,current:next,3)= blueFrame(drw_smple_start+move); %BLUE
- %Move to next box
- current = current + spacing;
- next = next + spacing;
- end
- figure(1)
- imshow(stream)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement